bokeh.embed (original) (raw)

Provide functions for embedding Bokeh standalone and server content in web pages.

class RenderRoot(elementid: ~bokeh.core.types.ID, id: ~bokeh.core.types.ID, name: str | None = '', tags: list[~typing.Any] = )[source]#

Encapsulate data needed for embedding a Bokeh document root.

Values for name or tags are optional. They may be useful for querying a collection of roots to find a specific one to embed.

elementid_: ID_#

A unique ID to use for the DOM element

id_: ID_#

The Bokeh model ID for this root

name_: str | None_ = ''#

An optional user-supplied name for this root

tags_: list[Any]_#

A list of any user-supplied tag values for this root

autoload_static(model: Model | Document, resources: Resources, script_path: str) → tuple[str, str][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: Model | Sequence[Model] | dict[str, Model], wrap_script: bool = True, wrap_plot_info: bool = True, theme: None | Theme | type[FromCurdoc] = None) → tuple[str, Any][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

The returned components assume that BokehJS resources are already loaded. The HTML document or template in which they will be embedded needs to include scripts tags, either from a local URL or Bokeh’s CDN (replacingx.y.z with the version of Bokeh you are using):

Only the Bokeh core library bokeh-x.y.z.min.js is always required. The other scripts are optional and only need to be included if you want to use corresponding features:

Parameters:

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_root)

components((plot1, plot2), wrap_script=False, wrap_plot_info=False)

=> (javascript, (plot1_root, plot2_root))

components({"Plot 1": plot1, "Plot 2": plot2}, wrap_script=False, wrap_plot_info=False)

=> (javascript, {"Plot 1": plot1_root, "Plot 2": plot2_root})

file_html(models: Model | Document | Sequence[Model], resources: ResourcesLike | None = None, title: str | None = None, *, template: Template | str = <Template 'file.html'>, template_variables: dict[str, Any] = {}, theme: ThemeLike = None, suppress_callback_warning: bool = False, _always_new: bool = False) → str[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:

Returns:

UTF-8 encoded HTML

json_item(model: Model, target: ID | None = None, theme: ThemeLike = None) → StandaloneEmbedJson[source]#

Return a JSON block that can be used to embed standalone Bokeh content.

Parameters:

Returns:

JSON-like

This function returns a JSON block that can be consumed by the BokehJS function Bokeh.embed.embed_item. As an example, a Flask endpoint for/plot might return the following content to embed a Bokeh plot into a div with id “myplot”:

@app.route('/plot') def plot(): p = make_plot('petal_width', 'petal_length') return json.dumps(json_item(p, "myplot"))

Then a web page can retrieve this JSON and embed the plot by callingBokeh.embed.embed_item:

Alternatively, if is more convenient to supply the target div id directly in the page source, that is also possible. If target_id is omitted in the call to this function:

return json.dumps(json_item(p))

Then the value passed to embed_item is used:

Bokeh.embed.embed_item(item, "myplot");

server_document(url: str = 'default', relative_urls: bool = False, resources: Literal['default'] | None = 'default', arguments: dict[str, str] | None = None, headers: dict[str, str] | None = None, with_credentials: bool = False) → str[source]#

Return a script tag that embeds content from a Bokeh server.

Bokeh apps embedded using these methods will NOT set the browser window title.

Parameters:

Returns:

A <script> tag that will embed content from a Bokeh Server.

server_session(model: Model | None = None, session_id: ID | None = None, url: str = 'default', relative_urls: bool = False, resources: Literal['default'] | None = 'default', headers: dict[str, str] = {}, with_credentials: bool = False) → str[source]#

Return a script tag that embeds content from a specific existing session on a Bokeh server.

This function is typically only useful for serving from a a specific session that was previously created using the bokeh.client API.

Bokeh apps embedded using these methods will NOT set the browser window title.

Note

Typically you will not want to save or reuse the output of this function for different or multiple page loads.

Parameters:

Returns:

A <script> tag that will embed content from a Bokeh Server.

Warning

It is typically a bad idea to reuse the same session_id for every page load. This is likely to create scalability and security problems, and will cause “shared Google doc” behavior, which is probably not desired.