question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Save Bokeh Plot as Bokeh Plot

See original GitHub issue

I would like to save a snapshot of Bokeh plot created by a Bokeh server application in all of the following ways:

  1. Download the contents of the ColumnDataSource as JSON/msgpack/whatever
  2. Download a static HTML file of the Bokeh plot
  3. Publish that bokeh plot as a gist

Dask’s use case

For example with Dask.distributed, the current best way to have a discussion about performance is by referring to our task-stream diagnostic Bokeh plot. This occurs when I write blogposts discussing algorithms and when users have performance questions. If there was a Bokeh Tool that let users publish a static view of their task-stream plot as a gist from within the Dask Dashboard then it would elevate the level of conversation significantly (and produce a lot of cool looking Bokeh images). As a pleasant side effect, I would start including Bokeh plots way more often when writing blogposts and documentation.

How to do this?

When I briefly mentioned this to @birdsarah she recommended building a custom tool like the current SaveTool for Dask which would dump to one of the above forms rather than to png. However, looking at this a bit more I don’t think that this functionality necessarily needs to be Dask-specific. This may be of general utility and would, I think, encourage people to embed Bokeh plots more often within broadcast publications. For example if they built a Bokeh plot in a notebook but wanted to include it in a blogpost then this might help.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
bryevdvcommented, Aug 9, 2018

FYI I am soon working on making “export to JSON, load from JSON” simpler to do. It is possible to do this now:

from collections import OrderedDict
import json

from bokeh.embed.standalone import _ModelInDocument
from bokeh.embed.util import standalone_docs_json_and_render_items
from bokeh.plotting import figure
from bokeh.sampledata.iris import flowers

from flask import Flask
from jinja2 import Template

app = Flask(__name__)

page = Template("""
<!DOCTYPE html>
<html lang="en">
<head>
 <link href="https://cdn.pydata.org/bokeh/release/bokeh-0.13.0.css" rel="stylesheet" type="text/css">
 <script src="https://cdn.pydata.org/bokeh/release/bokeh-0.13.0.js"></script>
</head>

<body>
 <div class="bk-root" id="myplot" />

 <script>
   fetch('/plot').then(function(response) {
     return response.json();
   })
   .then(function(spec) {
     Bokeh.embed.embed_items(spec.docs_json, spec.render_items);
   })
 </script>
</body>

""")


@app.route('/')
def root():
   return page.render()

colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'}
colors = [colormap[x] for x in flowers['species']]

@app.route('/plot')
def plot():
   p = figure(title = "Iris Morphology")
   p.xaxis.axis_label = 'Petal Length'
   p.yaxis.axis_label = 'Petal Width'

   p.circle(flowers["petal_length"], flowers["petal_width"],
            color=colors, fill_alpha=0.2, size=10)

   # Make this simple
   with _ModelInDocument([p]):
       (docs_json, [render_item]) = standalone_docs_json_and_render_items([p])

   roots = list(list(render_item.roots._roots.items())[0])
   roots[1] = "myplot"
   render_item.roots._roots = OrderedDict([roots])

   response = dict(docs_json=docs_json, render_items=[render_item.to_json()])
   return json.dumps(response)

if __name__ == '__main__':
   app.run()

Now the above only specifically concerned exporting to JSON from python more easily from python but the JS “embed” function needs to be made simpler too, and I will see about rounding things out with an easy way to also export from BokehJS. Combined with the new CustomAction custom toolbar button it would then be trivial to make a toolbar button to export to json and do whatever you liked with it.

I will use this issue for this work.

0reactions
bryevdvcommented, Aug 13, 2018

Noting that as part of this work, BOKEH_SIMPLE_IDS=1 should be made the default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exporting plots — Bokeh 2.4.3 Documentation
With code: Use the export_svg() utility function that lets you save a plot or a layout of plots as a single SVG file....
Read more >
Bokeh: save plot (as HTML) but don't show it
Solution is to replace calls to show by calls to save .
Read more >
Exporting Bokeh Plots
Method 1: Using export_png() function to save plot as PNG · obj: obj can be any plot that we are going to export....
Read more >
Introduction to plotting with Bokeh - BE/Bi 103 a
Saving Bokeh plots ​​ To save as a PNG for quick use, you can click the disk icon in the tool bar. To...
Read more >
Bokeh - Exporting Plots
The export_png() function generates RGBA-format PNG image from layout. This function uses Webkit headless browser to render the layout in memory and then ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found