How to embed matplotlib graph in Django webpage?

前端 未结 4 1866
情歌与酒
情歌与酒 2021-02-04 12:00

So, bear with me as I\'m VERY new to Django, Python, and web-development in general. What I want to do is display a graph that I\'ve made using matplotlib. I had it working to w

4条回答
  •  清酒与你
    2021-02-04 12:42

    I know this question is tagged as matplotlib, but I needed to do the same thing and I have found plotly to be easier to use and visually appealing as well. You can simply plot a graph and then in the view get the html code of the graph as :

    # fig is plotly figure object and graph_div the html code for displaying the graph
    graph_div = plotly.offline.plot(fig, auto_open = False, output_type="div")
    # pass the div to the template
    

    In the template do:

    {{ graph_div|safe }}

提交回复
热议问题