3
votes

In working on a flask website that will allow users to plot certain data, I decided to use bokeh instead of matplotlib as it seems to be built for embedding, with the ability to use dynamic data. I've scoured online examples, and the bokeh documentation. In the examples I see the command 'create_html_snippet', which is supposed to return a snippet of html that can be inserted into a template:

from bokeh.plotting import *

import numpy as np


# Define a function that will return an HTML snippet.

def build_plot():

# Set the output for our plot.

output_file('plot.html', title='Plot')

# Create some data for our plot.

x_data = np.arange(1, 101)
y_data = np.random.randint(0, 101, 100)

# Create a line plot from our data.

line(x_data, y_data)

# Create an HTML snippet of our plot.

snippet = curplot().create_html_snippet(embed_base_url='../static/js/',
                                        embed_save_loc='./static/js')

# Return the snippet we want to place in our page.

return snippet

I'm running this code in conjunction with the main flask code below:

from flask import Flask, render_template
from plots import build_plot

app = Flask(__name__)

@app.route('/') # The base URL for the home page.
def render_plot():
plot_snippet = build_plot()

return plot_snippet

if __name__ == "__main__":
app.run(debug=True)

The "create_html_snippet" command is not found in the documentation, and my anaconda version of python (created by the same people that are developing bokeh) gives the following error:

AttributeError: 'Plot' object has no attribute 'create_html_snippet'

It seems like bokeh is going through rapid development, and I'm wondering if it is deprecated. Does anyone know the current best way to get the html snippet I'm looking for?

1

1 Answers

7
votes

create_html_snippet is indeed deprecated. We will be releasing Bokeh 0.5 on July 7, there is a much improved, simplified, and documented bokeh.embed module now that supersedes that function. If you'd like to try it out sooner, there are dev builds available now, the instructions are on the mailing list:

https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/NVxeqdYy2eQ

You can see the new embed module (with complete docstrings) here:

https://github.com/ContinuumIO/bokeh/blob/master/bokeh/embed.py

as well as a very nice Flask embed example here:

https://github.com/ContinuumIO/bokeh/tree/master/examples/embed

We don't yet have the capability to publish Sphinx docs for dev builds but you can check out the markdown files for the new docs here:

https://github.com/ContinuumIO/bokeh/blob/master/sphinx/source/docs/user_guide.rst#embedding

These will be getting expanded even more as well but they give a good overview now.

Edit: That said, create_html_snippet should still be there, and functional, for the time being. If you'd like to file an issue on GH we can discuss it or investigate more.