Need help for Connecting with bokeh.client

i try to connect to my bokeh.client how is running to show charts in flask app. But does’t work :

from flask import Flask, render_template
from bokeh.client import pull_session
from bokeh.embed import server_session

app = Flask(__name__)

@app.route('/')
def bkapp_page():

with pull_session(url="http://localhost:5006/bokeh_serv") as session:

    # generate a script to load the customized session
    script = server_session(session_id=session.id, url='http://localhost:5006/bokeh_serv')

    # use the script in the rendered page
    return render_template("embed.html", script=script, template="Flask")

if __name__ == '__main__':
    app.run(port=8080)

But when i go to http://localhost:8080/ there is nothing (i have charts with http://localhost:5006/bokeh_serv)

Someone have any idea ?

Julien

is this example works for someone ? I am stuck !

You can check this answer a weeks ago

Thank you. I will try, but both are running on the same computer.

Are you adding an appropriate --allow-websocket-origin value on the command when you run bokeh serve?

1 Like

@Julien1

A few points, some inferred based on how I think you are running things.

Indentation matters. The code that is cut-and-pasted into this topic looks off. I assume that everything within the pull_session() block of code should be indented as part of the bkapp_page() function that defines the default route for Flask.

If so, then your code for the flask app looks fine.

I assume that you are running the bokeh server as a separate process via something along the lines of

bokeh serve bokeh_serv.py

If so, the reason that you most likely see a blank page when navigating to the URL http://localhost:8080 is that the bokeh server is disallowing connections from the host:port combination of the Flask app that is trying to connect to it. You should see such a message in the terminal window where you run the bokeh server.

Try the following when invoking the bokeh server …

bokeh serve bokeh_serv.py --allow-websocket-origin=localhost:8080

2 Likes

Thank you, it works now.

Do you know how to Accept server connections from any origin or multiple origin ?

thx

@Julien1

You can use the wildcard, i.e. --allow-websocket-orign='*' to accept connections from any origin, but this is generally only recommended for testing b/c of security reasons.

If you want to allow more than one origin, you can specify multiple allowed origins in the bokeh serve command.

From the bokeh server documentation in the Network Configuration section.

https://docs.bokeh.org/en/latest/docs/reference/command/subcommands/serve.html

It is possible to specify multiple allowed websocket origins by adding the --allow-websocket-origin option multiple times and to provide a comma separated list of hosts to BOKEH_ALLOW_WS_ORIGIN