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)
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 …
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.
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