Panel Templates vs Bokeh server document

I have a flask app that serves bokeh server documents:

def route...
return render_template(
    script=server_document(url=url, arguments=kwargs))

I adopted panel early on before it had templates, that we knew of, and now I’d like to be able to serve templated panel dashboards too.

I’m able to get the template to display correctly within a jupyter notebook, (we usually serve the bokeh apps with panel serve), but when it’s embedded in the Flask app it doesn’t serve correctly.

I think I traced the problem to {% from macros import embed %}. That line was missing from my base.html. You can see it’s included in the typical templating from panel: Panel Templates User Guide

So I added it but that seemed to lead to errors:

jinja2.exceptions.UndefinedError: 'macros' is undefined


UndefinedError: 'embed' is undefined

I thought it was a jinja2 thing so I made sure I had an updated version, but that didn’t help. Is macros coming from an IPython module of some kind?

Perhaps I’m actually getting ahead of myself. I may not want to embed the components at all, from my understanding that can take a while if you have a lot of data. And we’re already set up to talk directly to a bokeh server.

I guess all I really want is the panel Templating functionality, plus or minus the embed. Perhaps there’s a simpler solution to achieving templating directly in our jupyter notebooks.

Any advice is greatly appreciated!

I believe it is not posible to use the panel templates in flask with server_document or server_session. This approach only use the bokeh layout, but it does not utilize the css or the html of the panel template, but it utilizes the css and the html you define in the flask template.

The solution I found was to embed the panel app in a iframe, as described here Distributed fiber optic sensors: panel multi app with flask

I am not sure if I understand the templating in the jupyter notebooks ?..

1 Like

Actually, that’s the solution I landed on this morning: just got finished testing an iframe solution

1 Like

@lastmeta seems like you found a solution, but FWIW i also encountered the jinja2 'macros' is undefined error when trying to pass Panel Templates to Flask. Looks like macros, along with a lot of the other jinja2 context, is defined at the Bokeh level, here:

I’m going to keep exploring this area, and if I get a Template up and running, I’ll let you know how it worked!


Thank you, I’d appreciate that. I’ve made a followup question on this topic where I am still attempting to avoid the iframe solution: Panel template doesn't work when served from bokeh server document

1 Like

Sure thing. Just posted a bit of context on your new question. Please let me know of any follow-up questions. This is an interesting topic!

1 Like