Is it possible to define custom Jinja2 filters when using `bokeh serve`

Hi everyone,

With Jinja2, it’s possible to define custom filters, like so:

def datetimeformat(value, format='%H:%M / %d-%m-%Y'): 
   return value.strftime(format)

And then you can set your environment like so:

environment.filters['datetimeformat'] = datetimeformat

then in your template, you can do something like:
publication date: {{ article.pub_date|datetimeformat('%d-%m-%Y') }}

Reference

Is it possible to do this with apps launched by bokeh serve? In my case, I’ve used the dash dashboard example from the bokeh github repository, and added to my main.py

from jinja2 import Environment, FileSystemLoader
from templating import format_currency

root = os.path.dirname(os.path.abspath(__file__))
env = Environment( loader= FileSystemLoader(root) ) 
env.filters["format_currency"] = format_currency 

However it doesn’t seem like it works. When I start the app, I get ERROR: no filter named 'format_currency'. Am I missing something or is it not possible to do that at this time?

@mbelmadani For “directory format” apps, Bokeh will load the template itself automatically, and that happens before the app code executes, so I would not expect this to work. I think it might work if you bypass the auto-template loading, and load and set curdoc().template explicitly yourself (using your configured loader). I can’t guarantee this will work, though, you will just have to try. It may be the case that some new development would be necessary to support this (which we can discuss on GitHub if that’s necessary).