0

I would like to display multiple plotly graphs in a HTML webpage. I tried many codes, but the simplest one I found was this:

In my views function, I have:

for graphs in res:
            plot_div = plot(res[graphs], output_type='div')

        return render(request, '__', context={'plot_div': plot_div, "result": res})

In my HTML, I have:

<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>test</title>
</head>
<body>
  {% autoescape off %}
  {{ plot_div }}
  {% endautoescape %}
</body>
</html>

However, only the last graph is displayed. That's because the variable plot_div is constantly being updated under the for loop. Thus, I tried to create a dictionary like so:

plot_div = dict()
        for index, graphs in enumerate(res, 1):
            plot_div.update({index: plot(res[graphs], output_type='div')})

        return render(request, 'account/user/IndFin/indfin_resultado.html', context={'plot_div': plot_div, "result": res})

Thus, I needed to change the HTML to:

{% for graphs in plot_div %}
    {{plot_div.graphs}}
{% endfor %}

But, as a result I get, in my webpage, the matrix result and the last graph again. No graphS at all, just the last one. Has anyone faced such problem before?

Thanks

1
  • 1
    I think this might help you out! Commented Dec 14, 2021 at 14:19

2 Answers 2

1

I think you need a list of dicts instead of a dict, something like

plot_div = [{index: plot(res[graphs], output_type='div')} for index, graphs in enumerate(res, 1)]
return render(request, 'account/user/IndFin/indfin_resultado.html', context={'plot_div': plot_div, "result": res})

And in your template

{% for plot in plot_div %}
    {{plot .output_type}}
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

0

It worked for me to create figure multiple times and passing them separately.

def index(request):
        figure1= fig.to_html()
        figure2= fig.to_html()
    
    return render(request, 'more.html', {'figure1':figure1,'figure2':figure2})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.