Is there a way to customize the output html when using output_file() or save()?

What are you trying to do?

At a high level, I am trying to embed a bokeh visualization (p) in a markdown page hosted with Github pages/Jekyll. I am trying to do with with {% include_relative p.html%}. I know it’s a little funky to place a complete HTML doc inside of another one, but it almost works perfectly and would be easy for me to implement over and over again. The only problem is the <!DOCTYPE html> is being rendered on the page as plain text since it’s the second occurrence and is inside the body of the actual page. It seems like there should be a way to customize the output of the .HTML so it’s exactly the same except without that tag (maybe with a Jinja template that is barely different from the default?), but I haven’t been able to do it. I can’t find a good example of how to actually use those templates and the appropriate syntax.

What have you tried that did NOT work as expected?

The other obvious options would seem to be autoload_static() or the components() method, but the problem with both of those is that I recreate the viz every day to include updated data it changes the id, so I’d have to manually replace the script in the MD page every day as well, which kind of defeats the purpose. Not to mention getting the js to work across github pages is tricky for me, I’m just trying to learn python/pandas/bokeh and not a ton of js.

I’ve kind of got it working with an iframe but I really don’t want to settle on that option if I can avoid it.

json_item seems like a plausible alternative, i.e. just include the JSON blob in your template. Otherwise, save ultimately just calls file_html and that does accept an optional Jinja template.

Thanks, I’ll look into the json_item option. After 2 days of researching I had a feeling you would be the one to respond.

At the risk of being dense - is there any documentation or examples that are a little more dumbed down on how to actually use Jinja templates in Bokeh? All the documentation I’ve seen seems to be really abstract and goes straight into the weeds. Does it work something like this?

file_html(p, template=my_template.html)

and can that template file be anywhere in the same directory or does it need to be in a specific folder like it does for Bokeh servers?

For questions about argument and property types, the reference guide is a good resource:

Specifically:

  • template (Template , optional) – HTML document template (default: FILE) A Jinja2 Template, see bokeh.core.templates.FILE for the required template parameters

So, the argument should be an actual Jinja2 Template object (that you can load/create however is convenient for you)

The contents of the full default FILE template is in a collapsible block here. As a start you could probably try just deleting the doctype and html tags from it.

is there any documentation or examples that are a little more dumbed down on how to actually use Jinja templates in Bokeh?

Not much for file_html, because it’s just not ever commonly asked about. All the other APIs are used much, much more often for templating. Squeaky wheel gets the grease, etc.

I meant to respond earlier but I wanted to thank you - this got me on the right track. I had read (stared at) the documentation you referenced probably 10 times, but since I was trying to simultaneously solve the same problem with 3 potential solutions at the same time I wasn’t absorbing any of it or getting anywhere. Your answer let me know I was on the right track, so I actually sat down and focused on reading the Jinja docs and the file_html documentation so I was able to work it out and now I’m in business. I’m just a wannabe data scientist so I don’t know enough about web dev or FE to know if this is the ideal solution, but it’s a solution I can use and is working.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.