1

Here (https://bokeh.github.io/blog/2018/6/13/release-0-13-0/) it is mentioned that now we can use curdoc() instead of components.

I tried to move the following code

view.py:

script, div = components(PlotView().make_plot(), CDN)
context['plot_script'] = script
context['plot_div'] = div

template.html:

{% if plot_script %}
{{ plot_script|safe }}
{% endif %}

{% if plot_div %}
{{ plot_div|safe }}
{% endif %}

to the curdoc example provided in the link. But this line in the template file {{ embed(roots.region) }} doesn't make sense for django.

Claudiu Creanga
  • 8,031
  • 10
  • 71
  • 110
  • No, tjat is Jinja, a superset of Django templates. – Willem Van Onsem Jul 30 '18 at 09:35
  • @WillemVanOnsem so it only works with Jinja, not default django templates? – Claudiu Creanga Jul 30 '18 at 09:37
  • 1
    correct. Although you can of course either do the processing in the view, or write a custom template tag. But nevertheless, Django templates have been restricted deliberately to avoid such terrible anti-patterns like performing business logic in templates :) The restriction was by design, and circumventing it, is not really a good idea. – Willem Van Onsem Jul 30 '18 at 09:38

1 Answers1

1

But this line in the template file {{ embed(roots.region) }} doesn't make sense for Django.

That's correct. Like the documentation specifies, this is Jinja. Before the codeblock, it says:

Then, those roots can be referred to in your own Jinja template blocks like this:

(...)

You have basically three options to let this work:

  1. you can install Jinja as the template engine, like explained in this answer;
  2. you can write a custom Django template filter (and then call it like {{ roots.region|embed }}; or
  3. you can do - which is probably the most "Djangonic" way, do the processing in the view.
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555