1

Trying to rewrite Bokeh example as standalone document but not sure how I can add widgets to the html file.

With "file_html" I can create an html string but what should I do if I also want to save and add widgets like Sliders, Buttons etc. ?

from bokeh.models.widgets import *
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import file_html
from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature

def bkapp():
    df = sea_surface_temperature.copy()
    source = ColumnDataSource(data=df)

    plot = figure(x_axis_type='datetime', y_range=(0, 25), y_axis_label='Temperature (Celsius)',
                  title="Sea Surface Temperature at 43.18, -70.43")
    plot.line('time', 'temperature', source=source)

    def callback(attr, old, new):
        if new == 0:
            data = df
        else:
            data = df.rolling('{0}D'.format(new)).mean()
        source.data = ColumnDataSource(data=data).data

    slider = Slider(start=0, end=30, value=0, step=1, title="Smoothing by N Days")
    slider.on_change('value', callback)


    html = file_html(plot, CDN, "myplot")
    with open('my-plot.html','w') as file:
        file.write(html)

1 Answers1

0

file_html only accepts a single object, but there is no reason that object can't be a layout:

from bokeh.layouts import column

html = file_html(column(slider, plot), CDN, "myplot")
bigreddot
  • 33,642
  • 5
  • 69
  • 122
  • I looked through some examples and documentation. Unfortunately, it seems like there is no way to add widgets with real Python callbacks to the standalone document without the Bokeh server. I found out I can use CustomJS for the callbacks, but I don't want to rewrite my callbacks so I guess I'll give up on this. – Igor Molchanov Nov 07 '19 at 09:36
  • You never mentioned real Python callbacks in your question at all. Yes, that is impossible (for Bokeh or anything else) because browsers have no capability to run Python code. To have Python callbacks means having a Python process to run them! That process is the Bokeh server, that's the reason it was created, to be the Python process that runs real Python callbacks. – bigreddot Nov 07 '19 at 13:44