I am using the flask_embed.py example with the whole directory structure as found in the git repository as a test ground to use a custom Jinja template for document rendering. Tried multiple approached, but none with success.
First I added into the templates directory an ‘index.html’ file as described in documentation.
Next idea was to add the template file as parameter to the Server call
@app.route('/', methods=['GET'])
def bkapp_page():
script = server_document('http://localhost:5006/')
return render_template("embed.html", script=script, template="Flask")
def bk_worker():
server = Server({'/': bkapp}, io_loop=IOLoop(), allow_websocket_origin=["localhost:8000"], index='index.html)
server.start()
server.io_loop.start()
Finally I tried to change the document’s template member variable directly using the following code snippet as bkapp() function:
from jinja2 import Environment
def bkapp(doc):
my_template = Environment().from_string("""
{% extends base %}
{% block contents %}
<div>
<p> Hello {{ user_id }}, AKA '{{ last_name }}, {{ first_name }}'! </p>
</div>
{% endblock %}
""")
doc.template = my_template
doc.template_variables["user_id"] = 23
doc.template_variables.update(first_name="Mary", last_name="Jones")
When debugging, doc.template.filename in bkapp() always shows the default file
‘/home/fugu/bin/loc_py/lib/python3.11/site-packages/bokeh/core/_templates/file.html’ or in the last case nothing is displayed.
What is a working strategy to use a custom template for rendering the document?