Python Flask 多个函数无法在 HTML 中加载



我正在处理一个数据帧,我需要对它进行一些操作,并在网页上显示一个表。第一个在工作,其余的不工作。我该怎么办?更新更改路线也不起作用

@app.route('/')
def about():
return render_template("about.html", result=df.to_html(header='true'))

@app.route('/')
def problem():
data = df[['attempts', 'activity']]
data = df.groupby('activity').sum().sort_values('attempts', ascending=False)
return render_template("about.html", data=data.to_html(header='true'))

这是的html部分

<section id="table">
<h2>table</h2>
<p class="lead" > {{result | safe}}</p>
</section>
<section id="problems">
<h2>problems</h2>
<p class="lead" >{{data | safe}}</p>
</section>

我认为您应该在数据帧上循环,然后尝试显示它。

<section id="problems">
<h2>problems</h2>
{% for d in data%}
<p class="lead" >{{d | safe}}</p>
{% endfor %}
</section>

看看另一个类似问题的答案。希望这个能帮助你。如何在现有的flask html表中显示pandas数据帧?

您已经定义了具有相同路由的两个端点,因此当您转到"你只装载一只熊猫。如果你想同时显示这两个数据帧,你应该尝试一下:

@app.route('/')
def about_and_problem():
data = df[['attempts', 'activity']]
data = df.groupby('activity').sum().sort_values('attempts', ascending=False)
return render_template("about.html",about_dataframe=df.to_html(header='true'), problem_dataframe=data.to_html(header='true'))

然后在html:中

<section id="table">
<h2>table</h2>
<p class="lead" > {{about_dataframe | safe}}</p>
</section>
<section id="problems">
<h2>problems</h2>
<p class="lead" >{{problem_dataframe | safe}}</p>
</section>

最新更新