javascript - Display mpld3 chart in HTML with django -
I am using mpld3 to display matplotlib charts within an HTML page via django. I use a mplld3.fig_to_dict method to generate a string from a matplotlib shape and save it in a JSON variable.
I do not know how to call JSON variable in HTML
& lt; Div id = "fig01" & gt; & Lt; / Div & gt; & Lt; Script type = "text / javascript" & gt; Var json01 = {& lt; Snippet JSON code & gt; }; Mpld3.draw_figure ("fig", json01); & Lt; / Script & gt;
The JSN variable has been created in the scenes:
json01 = json.dumps (mpld3.fig_to_dict (fig)) return render_to_response ('plot / main. Html ', json01)
This code returns an empty HTML page. I also tried to call such a JSON variable, but it also does not work
var json01 = "{{json01}}";
I think the problem is coming from the javascript variable, because when I set the chart digit in javascript
The answer is ... in views.py:
js_data = json.dumps (mpld3.fig_to_dict (fig)) return render_to_response ('Plot .html ', {"my_data": js_data})
and in the template:
graph = {{my_data | Safe}}; Mpld3.draw_figure ("fig", graph);
Comments
Post a Comment