How to embed an app/document with a custom extension?

I’m trying to embed a document with a custom extension into a Flask application. The Flask application is taken from the documentation found at https://bokeh.pydata.org/en/latest/docs/user_guide/server.html#connecting-with-bokeh-client

from flask import Flask, render_template

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

app = Flask(name)

@app.route(’/’, methods=[‘GET’])
def bkapp_page():

with pull_session(url="http://localhost:5006/main") as session:
    script = server_session(session_id=session.id, url='http://localhost:5006/main')
    return render_template("embed.html", script=script, framework="Flask")

if name == ‘main’:
app.run(port=8080)

``

The extension is defined is equivalent to the custom extension found in the documentation at https://bokeh.pydata.org/en/latest/docs/user_guide/extensions.html#userguide-extensions. The document is constructed with the following script

from bokeh.io import show, output_file
from bokeh.layouts import column
from bokeh.models import Slider
from bokeh.plotting import figure, curdoc

from custom import Custom

slider = Slider(start=0, end=10, step=0.1, value=0, title=“value”)

custom = Custom(text=“Special Slider Display”, slider=slider)

layout = column(slider, custom)

curdoc().add_root(layout)

``

The document is launched with bokeh serve --allow-websocket-origin=localhost:8080 --show main.py

For a document constructed using standard bokeh components this works without issues. If the document is constructed using the custom extension from the documentation and the document is constructed as seen above I get the following error message in the terminal from the Flask server: KeyError: “View model name ‘Custom’ not found”

It seems to me that the custom extension is not available to the bokeh client in the Flask server. From searching it seems that the issue has been discussed previously https://github.com/bokeh/bokeh/issues/3930 but the api seems to have changed since.

Is it possible to embed a document making use of a custom extension?

I’m relatively new to bokeh and i apologize if have not used the correct terminology in the context of bokeh document, application & server.

Thanks,

Rasmus

The solution is quite simple. The custom extension model needs to be imported where the session is being pulled.

···

On Friday, November 16, 2018 at 5:15:04 PM UTC+1, [email protected] wrote:

I’m trying to embed a document with a custom extension into a Flask application. The Flask application is taken from the documentation found at https://bokeh.pydata.org/en/latest/docs/user_guide/server.html#connecting-with-bokeh-client

from flask import Flask, render_template

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

app = Flask(name)

@app.route(’/’, methods=[‘GET’])
def bkapp_page():

with pull_session(url="[http://localhost:5006/main](http://localhost:5006/main)") as session:
    script = server_session(session_id=session.id, url='[http://localhost:5006/main](http://localhost:5006/main)')
    return render_template("embed.html", script=script, framework="Flask")

if name == ‘main’:
app.run(port=8080)

``

The extension is defined is equivalent to the custom extension found in the documentation at https://bokeh.pydata.org/en/latest/docs/user_guide/extensions.html#userguide-extensions. The document is constructed with the following script

from bokeh.io import show, output_file
from bokeh.layouts import column
from bokeh.models import Slider
from bokeh.plotting import figure, curdoc

from custom import Custom

slider = Slider(start=0, end=10, step=0.1, value=0, title=“value”)

custom = Custom(text=“Special Slider Display”, slider=slider)

layout = column(slider, custom)

curdoc().add_root(layout)

``

The document is launched with bokeh serve --allow-websocket-origin=localhost:8080 --show main.py

For a document constructed using standard bokeh components this works without issues. If the document is constructed using the custom extension from the documentation and the document is constructed as seen above I get the following error message in the terminal from the Flask server: KeyError: “View model name ‘Custom’ not found”

It seems to me that the custom extension is not available to the bokeh client in the Flask server. From searching it seems that the issue has been discussed previously https://github.com/bokeh/bokeh/issues/3930 but the api seems to have changed since.

Is it possible to embed a document making use of a custom extension?

I’m relatively new to bokeh and i apologize if have not used the correct terminology in the context of bokeh document, application & server.

Thanks,

Rasmus

Hi,

FYI this is necessary because, otherwise, the pulled session contains references to extension models that the python runtime doing the pull knows nothing about. This is probably worth a mention in the docs. If you are interested in making a small PR to benefit the community, the source file for the "Extending Bokeh" chapter is here:

  https://raw.githubusercontent.com/bokeh/bokeh/master/sphinx/source/docs/user_guide/extensions.rst

Thanks,

Bryan

···

On Nov 27, 2018, at 10:03, [email protected] wrote:

The solution is quite simple. The custom extension model needs to be imported where the session is being pulled.

On Friday, November 16, 2018 at 5:15:04 PM UTC+1, [email protected] wrote:
I'm trying to embed a document with a custom extension into a Flask application. The Flask application is taken from the documentation found at https://bokeh.pydata.org/en/latest/docs/user_guide/server.html#connecting-with-bokeh-client

from flask import Flask, render_template

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

app = Flask(__name__)

@app.route('/', methods=['GET'])
def bkapp_page():

    with pull_session(url="http://localhost:5006/main") as session:
        script = server_session(session_id=session.id, url='http://localhost:5006/main')
        return render_template("embed.html", script=script, framework="Flask")

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

The extension is defined is equivalent to the custom extension found in the documentation at https://bokeh.pydata.org/en/latest/docs/user_guide/extensions.html#userguide-extensions. The document is constructed with the following script

from bokeh.io import show, output_file
from bokeh.layouts import column
from bokeh.models import Slider
from bokeh.plotting import figure, curdoc

from custom import Custom

slider = Slider(start=0, end=10, step=0.1, value=0, title="value")

custom = Custom(text="Special Slider Display", slider=slider)

layout = column(slider, custom)

curdoc().add_root(layout)

The document is launched with bokeh serve --allow-websocket-origin=localhost:8080 --show main.py

For a document constructed using standard bokeh components this works without issues. If the document is constructed using the custom extension from the documentation and the document is constructed as seen above I get the following error message in the terminal from the Flask server: KeyError: "View model name 'Custom' not found"

It seems to me that the custom extension is not available to the bokeh client in the Flask server. From searching it seems that the issue has been discussed previously https://github.com/bokeh/bokeh/issues/3930 but the api seems to have changed since.

Is it possible to embed a document making use of a custom extension?

I'm relatively new to bokeh and i apologize if have not used the correct terminology in the context of bokeh document, application & server.

Thanks,
Rasmus

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/b0dea7ad-e25c-4cb7-80b0-83ec463bc63f%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.