|safe outputs unicode (using for data visualization: Plotly/Bokeh)

To embed an interactive chart with tools like Plotly or Bokeh in a Django project, procedure:

  • in views.py Plotly: fig=fig.to_html(), Bokeh: script1, div1 = components(p)
  • in template Plotly: {{ fig|safe }}, Bokeh: {{ script1|safe } {{ div1|safe }}

What works:

  • Using both libraries the charts display fine, locally in dev.

What doesn’t work:

  • Non-Latin data (title, labels) are displayed as unicode. In plotly also <br> tag displayed as unicode (which means the issue is not necessarily about the language).
  • MemoryError when deploying to a server (tested Plotly), pointing at the line with fig=fig.to_html(). (I am not using much data, it is a simple chart.)

The fact that the issue (unicode characters) repeats for both libraries probably means that the cause lies with Django.

Question:
How to embed interactive charts directly in Django minding international users with non-Latin data?

Any clues on MemoryError are also welcomed.

displayed as unicode

What does that mean? Do you mean escaped unicode or something?

Means in chart it would display something like “\u031f\u0435…” for a German word “für”. And something like “\u003cbr\u003e” for <br>, i.e. where there should be a new line in hover labels. Then labels appear on the same line with “\u003cbr\u003e” in between.

What does the HTML look like? Does it look correct?

The html, looks like:

  • with fig=fig.to_html() and {{ fig|safe }}
<text class="legendtext" text-anchor="start" style="font-family: ..." 
x="40" y="7.8" data-unformatted="\u0410\...u040b" 
data-math="N">\u0410\...\u040b</text>
  • with fig=fig.to_html() and {{ fig }}
"hovertemplate":"%{label}: %{value}","labels":["\u0442\u043..","\u043c\u0443\...\u0430",...]

Looks like {{ fig|safe }} already receives data in this format “\u..”

By the way, I also generated a static version of the chart with Plotly and it looks fine (non-Latin text appears correct). When generating a static chart what gets omitted in code is fig=fig.to_html() and {{ fig|safe }}.