bokeh.embed

Provide functions to embed Bokeh models (e.g., plots, widget, layouts) in various different ways.

There are a number of different combinations of options when embedding Bokeh plots. The data for the plot can be contained in the document, or on a Bokeh server, or in a sidecar JavaScript file. Likewise, BokehJS may be inlined in the document, or loaded from CDN or a Bokeh server.

The functions in bokeh.embed provide functionality to embed in all these different cases.

autoload_server(model=None, app_path=None, session_id=None, url='default', relative_urls=False, arguments=None)[source]

Return a script tag that embeds content from a Bokeh server and loads all the necessary JS/CSS resource files. Also accepts an optional dictionary of arguments to be passed to Bokeh.

autoload_static(model, resources, script_path)[source]

Return JavaScript code and a script tag that can be used to embed Bokeh Plots.

The data for the plot is stored directly in the returned JavaScript code.

Parameters:
Returns:

JavaScript code to be saved at script_path and a <script> tag to load it

Return type:

(js, tag)

Raises:

ValueError

components(models, wrap_script=True, wrap_plot_info=True, theme=<class 'bokeh.embed.FromCurdoc'>)[source]

Return HTML components to embed a Bokeh plot. The data for the plot is stored directly in the returned HTML.

An example can be found in examples/embed/embed_multiple.py

Note

The returned components assume that BokehJS resources are already loaded.

Parameters:
  • models (Model|list|dict|tuple) – A single Model, a list/tuple of Models, or a dictionary of keys and Models.
  • wrap_script (boolean, optional) – If True, the returned javascript is wrapped in a script tag. (default: True)
  • wrap_plot_info (boolean, optional) –

    If True, returns <div> strings. Otherwise, return dicts that can be used to build your own divs. (default: True)

    If False, the returned dictionary contains the following information:

    {
        'modelid':  'The model ID, used with Document.get_model_by_id',
        'elementid': 'The css identifier the BokehJS will look for to target the plot',
        'docid': 'Used by Bokeh to find the doc embedded in the returned script',
    }
    
  • theme (Theme, optional) – Defaults to the Theme instance in the current document. Setting this to None uses the default theme or the theme already specified in the document. Any other value must be an instance of the Theme class.
Returns:

UTF-8 encoded (script, div[s]) or (raw_script, plot_info[s])

Examples

With default wrapping parameter values:

components(plot)
# => (script, plot_div)

components((plot1, plot2))
# => (script, (plot1_div, plot2_div))

components({"Plot 1": plot1, "Plot 2": plot2})
# => (script, {"Plot 1": plot1_div, "Plot 2": plot2_div})

Examples

With wrapping parameters set to False:

components(plot, wrap_script=False, wrap_plot_info=False)
# => (javascript, plot_dict)

components((plot1, plot2), wrap_script=False, wrap_plot_info=False)
# => (javascript, (plot1_dict, plot2_dict))

components({"Plot 1": plot1, "Plot 2": plot2}, wrap_script=False, wrap_plot_info=False)
# => (javascript, {"Plot 1": plot1_dict, "Plot 2": plot2_dict})
file_html(models, resources, title=None, template=<Template 'file.html'>, template_variables={}, theme=<class 'bokeh.embed.FromCurdoc'>)[source]

Return an HTML document that embeds Bokeh Model or Document objects.

The data for the plot is stored directly in the returned HTML, with support for customizing the JS/CSS resources independently and customizing the jinja2 template.

Parameters:
  • models (Model or Document or list) – Bokeh object or objects to render typically a Model or Document
  • resources (Resources or tuple(JSResources or None, CSSResources or None)) – a resource configuration for Bokeh JS & CSS assets.
  • title (str, optional) – a title for the HTML document <title> tags or None. (default: None) If None, attempt to automatically find the Document title from the given plot objects.
  • template (Template, optional) – HTML document template (default: FILE) A Jinja2 Template, see bokeh.core.templates.FILE for the required template parameters
  • template_variables (dict, optional) – variables to be used in the Jinja2 template. If used, the following variable names will be overwritten: title, bokeh_js, bokeh_css, plot_script, plot_div
  • theme (Theme, optional) – Defaults to the Theme instance in the current document. Setting this to None uses the default theme or the theme already specified in the document. Any other value must be an instance of the Theme class.
Returns:

UTF-8 encoded HTML

notebook_div(model, notebook_comms_target=None, theme=<class 'bokeh.embed.FromCurdoc'>)[source]

Return HTML for a div that will display a Bokeh plot in a Jupyter/Zeppelin Notebook. notebook_comms_target is only supported in Jupyter for now.

The data for the plot is stored directly in the returned HTML.

Parameters:
  • model (Model) – Bokeh object to render
  • notebook_comms_target (str, optional) – A target name for a Jupyter Comms object that can update the document that is rendered to this notebook div
  • theme (Theme, optional) – Defaults to the Theme instance in the current document. Setting this to None uses the default theme or the theme already specified in the document. Any other value must be an instance of the Theme class.
Returns:

UTF-8 encoded HTML text for a <div>

Note

Assumes load_notebook() or the equivalent has already been executed.

server_document(url='default', relative_urls=False, resources='default', arguments=None)[source]

Works similarly to autoload_server except a new session will be systematically generated and an entire document will be returned. Also accepts an optional dictionary of arguments to be passed to Bokeh, and resources files may optionally be omitted from loading by passing resources=”none”.

server_session(model, session_id, url='default', relative_urls=False, resources='default', arguments=None)[source]

Works similarly to autoload_server except an existing session id and model must be provided. Also accepts an optional dictionary of arguments to be passed to Bokeh, and resources files may optionally be omitted from loading by passing resources=”none”.

standalone_html_page_for_models(models, resources, title)[source]

Return an HTML document that renders zero or more Bokeh documents or models.

The document for each model will be embedded directly in the HTML, so the resulting HTML file is standalone (does not require a server). Depending on the provided resources, the HTML file may be completely self-contained or may have to load JS and CSS from different files.

Parameters:
  • models (Model or Document) – Bokeh object to render typically a Model or a Document
  • resources (Resources) – a resource configuration for BokehJS assets
  • title (str) – a title for the HTML document <title> tags or None to use the document title
Returns:

UTF-8 encoded HTML