Embedding a plot in a website with Python/bokeh

后端 未结 2 737
情书的邮戳
情书的邮戳 2021-02-01 07:39

I am trying to statically embed a bokeh plot in a personal website, and am encountering some behavior I do not understand. Basically, I am generating a plot using bokeh as foll

2条回答
  •  灰色年华
    2021-02-01 08:06

    UPDATE: the create_html_snippet function mentioned in the original question was deprecated and removed years ago. There are now various newer ways to embed Bokeh content available in the bokeh.embed module. This answer will summarize some of them.

    Standalone Content

    Standalone Bokeh content is pure HTML/JS/CSS that is not backed by a running Bokeh server. However, standalone Bokeh content can still be highly interactive, with plot tools (e.g. pan, zoom, selection), linked brushing, and widgets that trigger CustomJS actions. There are several ways to embed standalone content:

    json_item

    If you would like to create a pure JSON representation of the content that can be loaded by JS functions, you can use the json_item function. As an example, you might server the JSON from a Flask endpoint:

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

    Then the page can load and render the content with JavaScript code like this:

    This assumes you have loaded the BokehJS library on the page, e.g. by templating CDN.render() in the of the page. See a complete minimal example here.

    components

    If you would like to generate a simple

提交回复
热议问题