Bokeh view opening in new browser tab

Hi,

When I create a Bokeh figure in WebView it doesn’t open it in the app, but it opens a new tab in my browser with the figures. Below my code.

Regards,

Bart

@WebView(‘Selected Failures’, duration_guess=1)
def get_web_view(self, params, **kwargs):

    dict_updated_dist = params.updated_dist 
    dict_failure_dist = params.failure_dist
    dict_original_dist = params.original_dist

    figure_kwargs = dict(height=500, width=500, output_backend="webgl")
    ax_original = bkp.figure(**figure_kwargs)
    ax_failures = bkp.figure(**figure_kwargs)
    ax_update = bkp.figure(**figure_kwargs)

    az.plot_dist(dict_original_dist, color="black", label="Original MTTF", ax=ax_original, backend="bokeh", show=False)
    az.plot_dist(dict_failure_dist, color="blue", label="Failure (per hour)", ax=ax_failures, backend="bokeh", show=False)
    az.plot_dist(dict_updated_dist, color="red", label="Updated MTTF", ax=ax_update, backend="bokeh", show=False)

    ax = row(ax_original, ax_failures, ax_update)

    if az.rcParams["plot.bokeh.show"]:
        bkp.show(ax)

    html_str = az.output_file(filename='test',mode='cdn')

    return WebResult(html=StringIO(html_str))

Hi @BartG,

Not too familiar with bokeh myself but I’d venture a guess: it’s the bkp.show(ax) that causes bokeh to show the plot “prematurely” and in a new window. You do not need to show the image at all, you just need to build it up, and then give the html version of it to the webresult.

A (very) small example of this is:

    @WebView("Test", duration_guess=1)
    def test_bokeh(self, params, entity_id, **kwargs):
        from bokeh.plotting import figure
        from bokeh.resources import CDN
        from bokeh.embed import file_html
        from io import StringIO

        plot = figure()
        plot.circle([1,2], [3,4])

        return WebResult(html=StringIO(file_html(plot, CDN, "my plot")))

See also this link in the bokeh docs

Roeland

1 Like

Perfect solution, Roeland! Thank you very much!

1 Like