Skip to content

Instantly share code, notes, and snippets.

@jnettels
Created December 14, 2017 12:12
Show Gist options
  • Save jnettels/c4573f531fbc52cf5d0811d81d30a3fa to your computer and use it in GitHub Desktop.
Save jnettels/c4573f531fbc52cf5d0811d81d30a3fa to your computer and use it in GitHub Desktop.
Bokeh: After the layout has been added to the root, exporting the png of a fig is not possible anymore
# -*- coding: utf-8 -*-
'''
After the layout has been added to the root, exporting the png of a fig is
not possible anymore
With "bokeh serve export_test.py", the console output for me is the following:
doc_layout works
fig works
Layout is added to root
doc_layout works
fig throws error
'''
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.layouts import layout, widgetbox
from bokeh.io import export_png
from bokeh.models.widgets import Button
def download():
try:
export_png(doc_layout, filename="export doc_layout.png")
print('doc_layout works')
except Exception as ex:
print('doc_layout throws error:', ex)
try:
export_png(fig, filename="export fig.png")
print('fig works')
except Exception as ex:
print('fig throws error:', ex)
source = ColumnDataSource({'x': [2, 3, 4], 'y': [0.5, 1.5, 2.5]})
fig = figure(plot_width=1000, plot_height=300)
fig.hbar(y='y', right='x', height=0.4, source=source)
button = Button(label="Download")
button.on_click(download)
doc_layout = layout([[widgetbox(button)], [fig]], sizing_mode='fixed')
download() # Test 1
print('Layout is added to root')
curdoc().add_root(doc_layout)
curdoc().title = "Export PNG test"
download() # Test 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment