[Answered ]-General way of iterating over a dictionary containing multiple mpld3 graph images in Django?

1👍

Try this:

def histogram_plot(request):
    context={}
    df = pd.read_csv(test_file) #Read using pandas
    hist_list = []
    for column in df.columns:
                fig=plt.figure()
                plt.hist(df[column])
                histogram=mpld3.fig_to_html(fig)
                hist_list.append([histogram])
    context["hist_list"] = hist_list
    
    return render(request,"base.html",context)
{% for histogram in hist_list %}
   {% for elem in histogram %}
       {{  elem|safe  }}
   {% endfor %}
{% endfor %}

This simply appends all your histograms to a list and then loops over them in your template.

Leave a comment