我正在寻找一种将包含交互式散景图和小部件的 Jupyter Notebook 导出到独立 HTML 的方法。当使用位于工具栏中的 Jupyter NB“下载到”HTML 功能时,除了交互式散景图之外的所有内容都可以很好地导出,静态散景图也是如此(静态图也是“交互式”的,但基础数据不会改变)
如何使用独立 HTML 中的小部件获得交互式绘图?
您可以在下面找到安装了 Bokeh 13.0 的 Jupyter Notebook 中的工作示例。
import numpy as np
import pandas as pd
from bokeh.io import save, curdoc,output_file ,show, output_notebook, push_notebook
from bokeh.plotting import figure, gridplot
from bokeh.models import ColumnDataSource, Panel
from bokeh.models.widgets import Slider, Tabs, DataTable, TableColumn
from bokeh.layouts import layout, WidgetBox
from bokeh.application.handlers import FunctionHandler
from bokeh.application import Application
# output_file('tryout.html')
output_notebook()
# Static Bokeh plot:
data = pd.DataFrame(np.random.random([10,2]),columns=['x','y'])
dataMean = pd.DataFrame([],columns=['mean','std'])
dataMean.loc[:,'mean'] =data.mean()
dataMean.loc[:,'std'] =data.std()
src1 = ColumnDataSource(data)
src2 = ColumnDataSource(dataMean)
p = figure(plot_width = 700, plot_height = 400,
title = 'Test case',x_axis_label = 'x', y_axis_label = 'y')
p.line(source=src1,y='y',x='x',color='blue',line_width=2)
p.circle(source=src1,y='y',x='x',color='green')
columnsT = [TableColumn(field="mean", title="mean"),
TableColumn(field="std", title='std')]
data_table = DataTable(source=src2, columns=columnsT, width=400, height=400)
data_table.index_position = None
controls = WidgetBox(data_table)
layO = layout([[p],[controls]])
# Make a tab with the layout
tab1 = Panel(child=layO, title = 'test')
tabs = Tabs(tabs=[tab1])
show(tabs)
# Now the same plot, but fitted with a slider widget
def modify_doc(doc):
def make_dataset(N = 2):
data = pd.DataFrame(np.random.random([N,2]),columns=['x','y'])
dataMean = pd.DataFrame([],columns=['mean','std'])
dataMean.loc[:,'mean'] =data.mean()
dataMean.loc[:,'std'] =data.std()
return ColumnDataSource(data),ColumnDataSource(dataMean)
def make_plot(src):
# Blank plot with correct labels
p = figure(plot_width = 700, plot_height = 400,
title = 'Test case',x_axis_label = 'x', y_axis_label = 'y')
p.line(source=src,y='y',x='x',color='blue',line_width=2)
p.circle(source=src,y='y',x='x',color='green')
return p
def update(attr, old, new):
new_src, new_src2 = make_dataset(N_select.value)
src.data.update(new_src.data)
src2.data.update(new_src2.data)
N_select = Slider(start = 2, end = 20, step = 1, value = 2, title = 'number of points',width=700)
N_select.on_change('value', update)
columnsT = [
TableColumn(field="mean", title="mean"),
TableColumn(field="std", title='std')]
src, src2 = make_dataset(N_select.value)
data_table = DataTable(source=src2, columns=columnsT, width=400, height=400)
data_table.index_position = None
p = make_plot(src)
# Put controls in a single element
controls = WidgetBox(N_select,data_table)
layO = layout([[p],[controls]])
# Make a tab with the layout
tab1 = Panel(child=layO, title = 'test')
tabs = Tabs(tabs=[tab1])
doc.add_root(tabs)
handler= FunctionHandler(modify_doc)
app = Application(handler)
show(app)
如果我将 output_notebook() 更改为 output_file('tryout.html'),它会给我以下错误,我不明白,也可以找到解决方案:
RuntimeError: no display hook installed for notebook type None
希望有人能够帮助我解决这个问题。
提前致谢!