Last active
August 29, 2015 14:19
-
-
Save schaunwheeler/c560b634076d745cbba2 to your computer and use it in GitHub Desktop.
An attempt to figure out how to use bokeh-server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pandas import DataFrame | |
from numpy import random | |
from bokeh.browserlib import view | |
from bokeh.document import Document | |
from bokeh.session import Session | |
from bokeh.models.plots import Plot, GridPlot, ColumnDataSource, Range1d | |
from bokeh.models.glyphs import Circle, Rect | |
from bokeh.models.tools import ResetTool, PreviewSaveTool, PanTool, BoxZoomTool | |
from bokeh.models.widgets.inputs import Select | |
from bokeh.models.widgets.buttons import Button | |
from bokeh.models.widgets.layouts import VBox, HBox, VBoxForm | |
from bokeh.models import LinearAxis | |
class TestPlots(object): | |
def __init__(self): | |
self.document = Document() | |
self.session = Session(load_from_config=False) | |
self.session.use_doc('test_plots') | |
self.session.load_document(self.document) | |
self.document.clear() | |
self.selections = { | |
'n_plots': '0', | |
'plot_type': 'Bars' | |
} | |
self.plot_type_select = Select(value='Bars', options=['Bars', 'Circles'], name='plot_type') | |
self.plot_type_select.on_change('value', self.set_selections) | |
self.n_plot_select = Select(value='0', options=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'], name='n_plots') | |
self.n_plot_select.on_change('value', self.set_selections) | |
self.menu_submit = Button(label='Submit') | |
self.menu_submit.on_click(self.update_plot) | |
self.menu_reset = Button(label='Reset') | |
self.menu_reset.on_click(self.reset_menu) | |
self.plot = GridPlot(children=[[None]]) | |
self.plot.toolbar_location = None | |
self.plot.add_layout(LinearAxis(), 'below') | |
self.xrange = Range1d(start=-0.05, end=1.05) | |
self.controls = VBoxForm( | |
children=[ | |
VBox( | |
children=[ | |
HBox(children=[self.menu_reset, self.menu_submit]), | |
self.plot_type_select, | |
self.n_plot_select | |
], width=200 | |
) | |
] | |
) | |
self.layout = HBox(children=[self.controls, VBox(children=[self.plot])]) | |
self.document.add(self.layout) | |
self.session.store_document(self.document) | |
self.update_plot() | |
def reset_menu(self): | |
self.n_plot_select.value = '0' | |
self.plot_type_select.value = 'Bars' | |
self.update_plot() | |
def set_selections(self, obj, attr, old, new): | |
self.selections[obj.name] = new | |
self.session.store_objects(self.controls) | |
def update_plot(self): | |
plots = [] | |
n = int(self.selections['n_plots']) | |
for i in range(n): | |
ref_df = DataFrame( | |
{'xvalues': random.choice(100, 100) / 100.} | |
) | |
ref_df['yvalues'] = 0.5 | |
source = ColumnDataSource(ref_df) | |
plot = Plot( | |
x_range=self.xrange, | |
y_range=Range1d(start=-0.05, end=1.05), | |
min_border_top=0, | |
min_border_bottom=0, | |
min_border_right=0, | |
min_border_left=50, | |
plot_width=500, | |
plot_height=75, | |
title=None, | |
title_text_font_size='0pt', | |
outline_line_alpha=0.0) | |
plot.add_layout(LinearAxis(axis_label=str(i+1)), 'left') | |
if self.selections['plot_type'] == 'Circles': | |
glyphs = Circle(x='xvalues', y='yvalues', size=5) | |
else: | |
glyphs = Rect(x='xvalues', y='yvalues', width=0.005, height=0.25) | |
plot.add_glyph(source, glyphs) | |
zoom_tool = BoxZoomTool(plot=plot, dimensions=['width']) | |
reset_tool = ResetTool(plot=plot) | |
save_tool = PreviewSaveTool(plot=plot) | |
pan_tool = PanTool(plot=plot) | |
plot.tools.extend([zoom_tool, pan_tool, reset_tool, save_tool]) | |
plots.append([plot]) | |
self.plot.children = plots if len(plots) > 0 else [[None]] | |
self.plot.toolbar_location = 'right' if len(plots) > 0 else None | |
self.session.store_objects(self.plot) | |
def run(self, do_view=False, poll_interval=None): | |
link = self.session.object_link(self.document.context) | |
print("Visit %s to see the plots" % link) | |
if do_view: | |
view(link) | |
if poll_interval is not None: | |
print("\npress ctrl-C to exit") | |
self.session.poll_document(self.document, interval=poll_interval) | |
tp = TestPlots() | |
tp.run(True, poll_interval=0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment