How does jinja2 embedding work in a Bokeh server?

Hi all,

Question: how exactly does jinja2 embedding work in a Bokeh server? which parts of the code handle the embedding itself?

The reason I ask is that I hope it helps us in finding the root cause or a workaround for the following:
We are running into the issue that in some situations embedded jinja2 code inside a Bokeh app is placed outside of the divs in which the embed block is put in the HTML. This issue is described in this bug report:

Earlier it was just an annoyance as it only happened when one did not focus the browser during initialization, but now that we deployed the app on a virtual machine the issue consistently happens. The VM is a bit slower than our laptops, so it may be related to a timeout somewhere where the embedding happens too slowly and Bokeh server “decides” to put the HTML in before the embedded code is ready, after which the embedded code is just appended to the document. Does this make sense?

Any help or directions are appreciated

One addition. The problem in the VM only occurs when we use num-procs=0 or 2+, so the problem does not happen when num-procs=1. So it seems something special is happening when using multi-threading.

There is a discussion of exactly that in the issue

The VM part yes I agree. The explanation of how jinja embedding works in the Bokeh framework is more a support question which I hope helps us to do some further investigation into the issue. The VM case has two issues, one with the embedding (which also occurs in the repro in the issue), another one with num-procs having different behaviors.