什么?你能直接在HTML中插入Python代码吗?
时间:2023-03-20 21:01:20
科技观察
导入异步导入面板作为pnimportnumpy作为npimportpandas作为pdfrombokeh.modelsimportColumnDataSourcefrombokeh.plottingimportfigurefrompanel.io.pyodideimportshowdf=pd.DataFrame(np.random.randn(10,4),columns=list('ABCD')).cumsum()rollover=pn.widgets.IntInput(name='Rollover',value=15)follow=pn.widgets.Checkbox(name='Follow',value=True,align='end')tabulator=pn.widgets.Tabulator(df,height=450,width=400)defcolor_negative_red(val):"""接受一个标量并返回一个带有css属性`'color:red'`的字符串用于负数字符串,否则为黑色。"""color='red'ifval<0else'green'return'color:%s'%colortabulator.style.applymap(color_negative_red)p=figure(height=450,width=600)cds=ColumnDataSource(data=ColumnDataSource.from_df(df))p.line('index','A',source=cds,line_color='red')p.line('index','B',source=cds,line_color='green')p.line('index','C',source=cds,line_color='blue')p.line('index','D',source=cds,line_color='purple')defstream():data=df.iloc[-1]+np.random.randn(4)制表符。stream(data,rollover=rollover.value,follow=follow.value)value={k:[v]fork,vintabulator.value.iloc[-1].to_dict().items()}值['索引']=[tabulator.value.index[-1]]cds.stream(值)cb=pn.state.add_periodic_callback(stream,200)controls=pn.Row(cb.param.period,rollover,follow,width=400)awaitshow(controls,'controls')awaitshow(tabulator,'table')awaitshow(p,'plot')