Skip to content

Instantly share code, notes, and snippets.

@schaunwheeler
Created April 28, 2015 18:53
Show Gist options
  • Save schaunwheeler/ac1162db95d13a0a5070 to your computer and use it in GitHub Desktop.
Save schaunwheeler/ac1162db95d13a0a5070 to your computer and use it in GitHub Desktop.
Trying to figure out why app reloads
from bokeh.properties import Instance, Bool, String
from bokeh.models.widgets.buttons import Button, Toggle
from bokeh.models.widgets.markups import Paragraph
from bokeh.models.widgets.layouts import VBox, HBox, VBoxForm
from bokeh.models.widgets.inputs import Select
from bokeh.server.app import bokeh_app
from bokeh.server.utils.plugins import object_page
from bokeh.plotting import curdoc
class MenuChange(VBox):
extra_generated_classes = [["MenuChange", "MenuChange", "VBox"]]
jsmodel = "VBox"
# layout boxes
mainrow = Instance(HBox)
input_box = Instance(VBoxForm)
paragraph = Instance(Paragraph)
# inputs
menu_submit = Instance(Button)
menu_toggle = Instance(Toggle)
type_select = Instance(Select)
toggle_active = Bool(default=False)
types = String(default='1')
def __init__(self, *args, **kwargs):
super(MenuChange, self).__init__(*args, **kwargs)
@classmethod
def create(cls):
obj = cls()
obj.input_box = VBoxForm()
obj.mainrow = HBox()
obj.paragraph = Paragraph(text='Select something.')
obj.set_inputs()
obj.set_children()
return obj
def set_inputs(self):
self.menu_toggle = Toggle(label='Select plot criteria', type='primary')
self.type_select = Select(value='1', options=['1', '2', '3'])
self.menu_submit = Button(label='Submit', type='warning')
def set_children(self):
if self.toggle_active:
self.menu_toggle.label = 'Cancel'
self.mainrow.children = [self.menu_submit, self.menu_toggle, self.type_select]
else:
self.menu_toggle.label = 'Select plot criteria'
self.mainrow.children = [self.menu_toggle]
self.input_box.children = [self.mainrow]
self.children = [self.input_box, self.paragraph]
curdoc().add(self)
def toggle_menu(self, obj, attr, old, new):
self.toggle_active = new
if new:
self.menu_toggle.label = 'Cancel'
self.mainrow.children = [self.menu_submit, self.menu_toggle, self.type_select]
else:
self.menu_toggle.label = 'Select plot criteria'
self.mainrow.children = [self.menu_toggle]
def submit_menu(self, obj, attr, old, new):
self.paragraph.text = self.types
self.toggle_menu(None, None, None, False)
def set_selection(self, obj, attr, old, new):
if obj == self.type_select:
self.types = new
def setup_events(self):
super(MenuChange, self).setup_events()
if self.menu_submit:
self.menu_submit.on_change('clicks', self, 'submit_menu')
if self.menu_toggle:
self.menu_toggle.on_change('active', self, 'toggle_menu')
if self.type_select:
self.type_select.on_change('value', self, 'set_selection')
@bokeh_app.route("/bokeh/menu_change/")
@object_page("menu_change")
def make_menu():
app = MenuChange.create()
return app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment