How to get embedded Bokeh server doc to report page error to flask server

I have a Flask plus Bokeh server setup where the two servers are started by their own scripts (python and bokeh serve ...) and the Bokeh plots are embedded in pages by templates using server_document. In case there is something wrong with the Bokeh server I would like to capture any page error from the Bokeh server by showing a Flask error template. I have tried to add routes like @app.errorhandler(403) but also @app.teardown_request in order to try to capture any page error but so far no luck. If I understand correctly as long as flask is able to create the embedded template (the html doc) then that would not return any page error.

To show what I mean I have taken the Flask embed example from git ( and used that. In the script I have added some error handling routes but they are not run if Bokeh server reports a eg 403 GET error.

#!/usr/bin/env python

'''This example demonstrates embedding an autoloaded Bokeh server
into a simple Flask application, and passing arguments to Bokeh.

To view the example, run:


in this directory, and navigate to:


import numpy as np

from import curdoc
from bokeh.layouts import column, row
from bokeh.models import ColumnDataSource, Slider, TextInput
from bokeh.plotting import figure

# Retrieving the arguments
args = curdoc().session_context.request.arguments

    batchid = int(args.get('batchid')[0])
except (ValueError, TypeError):
    batchid = 1

func = {
    1 : np.cos,
    2 : np.sin,
    3 : np.tan

# Set up data
N = 200
x = np.linspace(0, 4*np.pi, N)
y = func(x)
source = ColumnDataSource(data=dict(x=x, y=y))

# Set up plot
plot = figure(plot_height=400, plot_width=400, title="my wave",
              x_range=[0, 4*np.pi], y_range=[-2.5, 2.5])

plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

# Set up widgets
text = TextInput(title="title", value="Batch n°{}".format(batchid))
offset = Slider(title="offset", value=0.0, start=-5.0, end=5.0, step=0.1)
amplitude = Slider(title="amplitude", value=1.0, start=-5.0, end=5.0)
phase = Slider(title="phase", value=0.0, start=0.0, end=2*np.pi)
freq = Slider(title="frequency", value=1.0, start=0.1, end=5.1)

# Set up callbacks
def update_title(attrname, old, new):
    plot.title.text = text.value

text.on_change('value', update_title)

def update_data(attrname, old, new):

    # Get the current slider values
    a = amplitude.value
    b = offset.value
    w = phase.value
    k = freq.value

    # Generate the new curve
    x = np.linspace(0, 4*np.pi, N)
    y = a*func(k*x + w) + b = dict(x=x, y=y)

for w in [offset, amplitude, phase, freq]:
    w.on_change('value', update_data)

# Set up layouts and add to document
inputs = column(text, offset, amplitude, phase, freq)

curdoc().add_root(row(inputs, plot, width=800))

Started with

bokeh serve --allow-websocket-origin=

'''This example demonstrates embedding an autoloaded Bokeh server
into a simple Flask application, and passing arguments to Bokeh.

To view the example, run:


in this directory, and navigate to:


import atexit
import subprocess

from flask import Flask, render_template_string

from bokeh.embed import server_document

home_html = """
<!DOCTYPE html>
<html lang="en">
    <div class="bk-root">
      <h1><a href="/batch/1"> Batch 1 (cos)</a></h1>
      <h1><a href="/batch/2"> Batch 2 (sin)</a></h1>
      <h1><a href="/batch/3"> Batch 3 (tan)</a></h1>

app_html = """
<!DOCTYPE html>
<html lang="en">
        <h2><a href="/batch/1">Batch 1 (cos)</a> - <a href="/batch/2">Batch 2 (sin)</a> - <a href="/batch/3">Batch 3 (tan)</a></h2>
    {{ bokeh_script|safe }}

err_html = """
<!DOCTYPE html>
<html lang="en">
        <h2>{{ error }}</h2>

app = Flask(__name__)

bokeh_process = subprocess.Popen(
    ['python', '-m', 'bokeh', 'serve', '--allow-websocket-origin=localhost:5000', ''], stdout=subprocess.PIPE)

def kill_server():

def home():
    return render_template_string(home_html)

def visualization(batchid):
    bokeh_script = server_document(url='http://localhost:5006/bokeh_server', arguments=dict(batchid=batchid))
    return render_template_string(app_html, bokeh_script=bokeh_script)

def page_not_found(e):
  return render_template_string(err_html, error = "404 Not found")

def page_not_found(e):
  return render_template_string(err_html, error = "403 Forbidden")

def teardown_request_func(error=None):
   print("teardown_request is running")
   if error:
      # log error

if __name__ == '__main__':

Flask server started with python

When running this and going to localhost:5000, when one chooses a link I get to the app_html page but no embedded plot is shown due to 403 page error reported by Bokeh server. I would like to capture this error and show to the user that there is a page error - how do I do that?

Firefox console:

Request URL:http://localhost:5006/bokeh_server/ws?bokeh-protocol-version=1.0&bokeh-session-id=MgpZNLFpFlZDDacVqZzjLlWZHmcjCngpWn19QiIZnyXs
Request method:GET
Remote address:
Status code:

Have tested in Bokeh version 1.3.4 (py2.7) and 2.1.1 (py3.6.8), both on Redhat 7.

I don’t think there is any straightforward built-in way at present. The server_document call ultimately provides this snippet of JavaScript to execute an XMLHttpRequest that to get the code from the Bokeh server for actually embedding a session. As you can see, it only defines an onload and doesn’t have any error handling hooks set up. It probably should, and it’s probably not too difficult, but surprisingly no one has ever asked about it before. In the mean time. I’d encourage you to file a GitHub issue to discuss adding it. In the mean time, I think you would have to copy the code for server_document and modify it to supply an updated JS autoload tag that does define and onerror to do whatever you want to happen there.

@Bryan, thanks for taking the time looking into this. I have now opened an issue on GitHub