Skip to content

Instantly share code, notes, and snippets.

@schaunwheeler
Created September 21, 2016 18:59
Show Gist options
  • Save schaunwheeler/30f205f405c859995bebb23a2d87c36f to your computer and use it in GitHub Desktop.
Save schaunwheeler/30f205f405c859995bebb23a2d87c36f to your computer and use it in GitHub Desktop.
Reproduces problem with adjusting plot size
from pandas import DataFrame
from bokeh.models.axes import CategoricalAxis
from bokeh.models import Plot, ColumnDataSource
from bokeh.models.ranges import FactorRange
from bokeh.models.glyphs import Circle
from bokeh.models.widgets.markups import PreText
from bokeh.layouts import Column
from bokeh.models.widgets.inputs import Select
from bokeh.plotting import curdoc
class TestResize(object):
def __init__(self):
self.plot_source = ColumnDataSource()
self.plot = Plot()
self.number_elements = Select(options=['5', '10', '15', '20'], value='20')
self.number_elements.on_change('value', self.resize_plot)
self.monitor_box = PreText(width=500, height=50)
self.update_plot()
self.make_plot()
self.update_monitor()
self.layout = Column()
self.layout.children = [self.number_elements, self.monitor_box, self.plot]
def make_plot(self):
plot = Plot(
x_range=FactorRange(factors=[str(x) for x in range(int(self.number_elements.value) + 1)]),
y_range=FactorRange(factors=[str(x) for x in range(int(self.number_elements.value) + 1)]),
min_border_bottom=10,
min_border_top=10,
min_border_right=10,
min_border_left=10,
plot_width=int(self.number_elements.value) * 25,
plot_height=int(self.number_elements.value) * 25,
outline_line_alpha=0.0,
toolbar_location=None,
logo=None)
circle = Circle(x='x_position', y='y_position', radius=0.5)
plot.add_glyph(self.plot_source, circle)
plot.add_layout(CategoricalAxis(), 'left')
plot.add_layout(CategoricalAxis(), 'above')
self.plot = plot
def update_plot(self):
df = DataFrame()
df['x_position'] = [x for x in range(int(self.number_elements.value) + 1)]
df['y_position'] = [x for x in range(int(self.number_elements.value) + 1)]
self.plot_source.data = ColumnDataSource(df).data
self.plot_source.column_names = df.columns.tolist()
def resize_plot(self, attr, old, new):
self.update_plot()
self.make_plot()
self.update_monitor()
# The below, uncommented, triggers 'Uncaught Error: unknown edit variable' (bokeh.min.js?v=02fddad…:237)
# self.layout.children = [self.number_elements, self.monitor_box, self.plot]
def update_monitor(self):
self.monitor_box.text = ''
self.monitor_box.text += 'self.plot.plot_width == ' + str(self.plot.plot_width) + '\n'
self.monitor_box.text += "self.plot.ref['id']: " + self.plot.ref['id']
app = TestResize()
curdoc().add_root(app.layout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment