I am facing a problem similar to the one you struggled with @Nathan_Snyder . Therefore, I hope this is the right place to ask my question. However, this is my very first post, so I apologize if I made a mistake.
My goal is to set up an Apache reverse proxy on localhost which utilizes Flask and mod_wsgi to render a jinja template. In the template I make use of the embed(roots.xxx)
syntax as described in Embedding Bokeh to place each Bokeh root element independently.
However, even without the Apache Webserver I encounter problems when establishing a connection between Flask and Bokeh.
My code works fine when serving the entire directory on localhost using
bokeh server flasktest
However, when starting the Bokeh server as
bokeh server flasktest --allow-websocket-origin=localhost:5000
and executing the Python Flask script
python __init__.py
, I receive the error
jinja2.exceptions.UndefinedError: 'base' is undefined
I have already tried the following:
- Copy the Bokeh default Jinja template into the templates folder
(named base.html) and import it as {{% extends "base.html" %}}
.
- My very first attempt included starting the server programmatically as in the
Bokeh howto example flask_embed.
This, however, gives the same error.
My current directory structure is:
flasktest
--- templates
--- index.html
--- __init__.py
--- __main.py
The relevant files for a minimal working example are:
#main.py:
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature
df = sea_surface_temperature.copy()
source = ColumnDataSource(data=df)
plot = figure(x_axis_type='datetime',
y_range=(0, 25),
y_axis_label='Temperature (Celsius)',
title="Sea Surface Temperature at 43.18, -70.43",
name='plot')
plot.line('time', 'temperature', source=source)
curdoc().add_root(plot)
#__init__.py
from flask import Flask, render_template
from bokeh.embed import server_document
app = Flask(__name__)
@app.route('/', methods=['GET'])
def bkapp_page():
script = server_document('http://localhost:5006/main')
return render_template("index.html",
script=script,
template="Flask")
if __name__ == '__main__':
app.run()
{% extends base %}
{% block contents %}
<div> {{ embed(roots.plot) }} </div>
{% endblock %}
I appreciate all the advice you can give me!
Thank you!