Python Forum

Full Version: Bokeh - dynamic update widget.Select values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using a Bokeh Select widget, from bokeh.models.widgets import Select which is essentially a dropdown on a web page. eg: http://v4-alpha.getbootstrap.com/components/dropdowns/.  I'm trying to figure out how to update the values after bokeh server has been initialized.  for example.  when the server is started, the Select widget has the values, [foo, bar]. I update that list with an myList.append('baz') so that the list now contains ['foo','bar','baz'] but the widget doesn't show the new value. If the process below seems to complicated, I can create a github project for it.

I worked off of this example: http://demo.bokehplots.com/apps/movies
full source code: https://github.com/bokeh/bokeh/tree/mast...app/movies

1) Use the description.html file from the movies app if you try running the code below. It should be placed in the same dir as the script.
https://github.com/bokeh/bokeh/blob/mast...ption.html

2) you also need to create a dir in the same location as the script.  place at least one file in there.  the name or names of the files in the data dir will appear on the drop down.


from os.path import dirname, join
import os
from bokeh.models import Div, Button
from bokeh.io import curdoc
from bokeh.models.widgets import Select


class myClass:

    def __init__(self):
        '''CONSTRUCTOR'''

        html_path = "description.html"
        FILE_DIR = "./data"
       
        # Initialize the refresh button
        self.refresh_button = Button(label="Refresh")
        self.refresh_button.on_click(self._file_selection_setup)
       
        # Initialize the Selection widget
        self._file_selection_setup()
        self.selection.on_change('value', lambda attr, old, new: self._selection_setup())

        # Data Descriptor -- load base HTML file
        html = open(join(dirname(__file__), html_path)).read()
        self.desc = Div(text=html, width=800)

        # setup widgetbox containing the button and Select dropdown
        self.widgetbox = widgetbox(self.refresh_button, self.file_selection, width=300)


    def _my_selection_setup(self):
        fileList = ps.listdir(FILE_DIR)
        print fileList
        self.file_selection = Select(title="FileInput:", value="selectOne", options=fileList)
        self.widgetbox = widgetbox(self.refresh_button, self.file_selection, width=300)


mc = myClass()
curdoc().add_root(mc.widgetbox)
curdoc().title = "my app"