Bokeh server embedded in gunicorn/Flask hosted to cloud

For others that have application architectures similar that embed a bokeh server in a gunicorn/Flask framework and want to deploy it to a platform as a service / cloud service, this can be done in Heroku using two dynos. After exhaustively exploring different mechanisms and helpful exchanges with the Heroku support team, getting everything to work in one dyno is not possible.

DETAILS

Flask-app which embeds the bokeh server on one dyno:

Create a python Flask app, e.g. a simple one as follows, and use the Heroku Procfile to run it via gunicorn, which is well documented on Heroku’s site.

from flask import Flask, render_template

from bokeh.client import pull_session
from bokeh.embed import server_session

BOKEH_URL = "https://<bokeh-app-name>.herokuapp.com/<bokeh-server-name>"

app = Flask(__name__)

@app.route('/', methods=['GET'])
def bkapp_page():
    with pull_session(url=PANEL_URL) as session:
        script = server_session(session_id=session.id, url=BOKEH_URL)
        return render_template("embed.html", script=script, template="Flask")

Bokeh server app which runs the bokeh server on second dyno:

Implement this as usual and create a Heroku Procfile that runs the server, e.g.

web: bokeh serve --address="0.0.0.0" --port=$PORT <bokeh-server-name> --allow-websocket-origin=<flask-app-name>.herokuapp.com

NB: The port argument is important b/c Heroku assigns the port that is used; you cannot choose it yourself.

NB: The quantities in <…> above should reflect the app names for your Heroku apps and the name of your bokeh server following the bokeh conventions if it is in single-file or directory format. Caveat, that only the single module bokeh-server-name.py case has been tested in determining the viability of the approach.

Thanks to @p-himik and @Bryan for the valuable help getting this to work!