Bokeh run time error, models must be owned by a single document

646 Views Asked by At

when trying to enter two datasource this error popped up. RuntimeError: Models must be owned by only a single document, StringFormatter(id='1266', ...) is already in a doc

df_2 = pd.DataFrame({
    'Fields': x ,
    'C_Info': values
})

src_1 = ColumnDataSource(df_2)

cols = [
    TableColumn(field='Fields', title='Portfolio'),
    TableColumn(field='C_Info', title='CapInfo')
]

myTable = DataTable(source=src_1, columns=cols)

src_3 = ColumnDataSource(df2)

cols_1 = [
    TableColumn(field='variable', title='Earnings Component'),
    TableColumn(field='values', title='Amount'),
    TableColumn(field='PercentageTotalEarning', title='Percentage Total Earning'),
]

myTable2 = DataTable(source=src_3, columns=cols_1)

show(column(myTable,myTable2))
1

There are 1 best solutions below

0
On

This error occurs when using bokeh.plotting.save and bokeh.plotting.show in the same cell of a python notebook.

Solution

import bokeh
import bokeh.io
bokeh.io.output_notebook()
import bokeh.plotting
from bokeh.models import HoverTool
from IPython.display import IFrame

Now create your figure as usual, calling bokeh.plotting.figure():

p = bokeh.plotting.figure(your figure as usual with plot_width and plot_height)

Now save your figure but don't show it using bokeh:

bokeh.plotting.save(p, filename="your path/name.html")

Now show your figure using IFrame:

IFrame(src='your path/name.html', width=1000, height=500)

Please note, width and height in IFrame should be set slightly larger than your figure dimension that you have set in plot_width and plot_height

Now you have both the saving and the displaying in the same cell.