在Django中迭代包含多个mpld3图像的字典的一般方法是什么?

huangapple go评论130阅读模式
英文:

General way of iterating over a dictionary containing multiple mpld3 graph images in Django?

问题

我正在进行一个个人项目。其中的一部分是在网页上显示多个直方图。到目前为止,我已经能够使用以下代码使其工作:

views.py:

  1. import pandas as pd
  2. import matplotlib.pyplot as plt, mpld3
  3. import scipy.stats as stats
  4. import numpy as np
  5. def histogram_plot(request):
  6. context = {}
  7. df = pd.read_csv(test_file) # 使用pandas读取
  8. for column in df.columns:
  9. fig = plt.figure()
  10. plt.hist(df[column])
  11. histogram = mpld3.fig_to_html(fig)
  12. context[f"{column}Histogram"] = [histogram]
  13. return render(request, "base.html", context)

base.html:

  1. {% for elem in context %}
  2. {{ context[elem]|safe }}
  3. {% endfor %}

我在网页上看到了所有的直方图,但问题是所有的HTML代码都使用了字典中的硬编码键。

是否有一种通用的方式可以使用for循环来编写我在这个HTML代码中编写的内容,而不是硬编码字典中的键?如果有人能告诉我如何做到这一点,我将不胜感激。

英文:

I am working on a personal project. One part of this is to display multiple histograms on a webpage. So far, I have been able to get this to work using the following code:

views.py:

  1. import pandas as pd
  2. import matplotlib.pyplot as plt,mpld3
  3. import scipy.stats as stats
  4. import numpy as np
  5. def histogram_plot(request):
  6. context={}
  7. df = pd.read_csv(test_file) #Read using pandas
  8. for column in df.columns:
  9. fig=plt.figure()
  10. plt.hist(df[column])
  11. histogram=mpld3.fig_to_html(fig)
  12. context[f"{column}Histogram"]=[histogram]
  13. return render(request,"base.html",context)

base.html:

  1. {% for elem in PregnanciesHistogram %}
  2. {{elem|safe}}
  3. {% endfor %}
  4. {% for elem in GlucoseHistogram %}
  5. {{elem|safe}}
  6. {% endfor %}
  7. {% for elem in BloodPressureHistogram %}
  8. {{elem|safe}}
  9. {% endfor %}
  10. {% for elem in SkinThicknessHistogram %}
  11. {{elem|safe}}
  12. {% endfor %}
  13. {% for elem in InsulinHistogram %}
  14. {{elem|safe}}
  15. {% endfor %}
  16. {% for elem in BMIHistogram %}
  17. {{elem|safe}}
  18. {% endfor %}
  19. {% for elem in DiabetesPedigreeFunctionHistogram %}
  20. {{elem|safe}}
  21. {% endfor %}
  22. {% for elem in AgeHistogram %}
  23. {{elem|safe}}
  24. {% endfor %}

I do see all the histograms on my webpage, but the problem is that all of the HTML code uses hardcoded keys in the dictionary.

Is there a general way of writing what I wrote in this HTML code, using a for loop, without hardcoding the keys from the dictionary? I would appreciate it if someone could tell me how to do that.

答案1

得分: 1

尝试这个:

  1. def histogram_plot(request):
  2. context = {}
  3. df = pd.read_csv(test_file) # 使用pandas读取
  4. hist_list = []
  5. for column in df.columns:
  6. fig = plt.figure()
  7. plt.hist(df[column])
  8. histogram = mpld3.fig_to_html(fig)
  9. hist_list.append([histogram])
  10. context["hist_list"] = hist_list
  11. return render(request, "base.html", context)
  1. {% for histogram in hist_list %}
  2. {% for elem in histogram %}
  3. {{ elem|safe }}
  4. {% endfor %}
  5. {% endfor %}

这只是将所有的直方图附加到一个列表中,然后在您的模板中对它们进行循环。

英文:

Try this:

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

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

huangapple
  • 本文由 发表于 2023年8月10日 17:09:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定