【发布时间】:2022-01-03 18:12:58
【问题描述】:
有人问了类似的问题,但我无法让它为我工作,我不确定问题是什么,所以我想我会再问一次,以防有人有一些指示。
我正在尝试在 Flask 中传递一个函数,具体取决于在网页上按下了哪个按钮。这些是 HTML 按钮:
<div class="row">
<h2>Your Wordclouds:</h2>
<input type="submit" name="submit_button" value="Positive" class='btn'>
<input type="submit" name="submit_button" value="Negative" class='btn'>
{{value}}
</div>
我的功能:
@app.route('/insight', methods=['GET', 'POST'])
def wordcloud():
if request.method == 'POST':
if request.form['submit_button'] == "Positive":
mysql.connection.cursor()
query = ("SELECT journal_entry FROM journals WHERE sentiment = 'positive'")
df = pd.read_sql(query, mysql.connection)
journal_entry = df['journal_entry']
long_string = (''.join(str(journal_entry.tolist())))
# Create WordCloud object
wordcloud = WordCloud(background_color="white", max_words=4500, contour_width=6, contour_color='blue')
# Generate the cloud
wordcloud.generate(long_string)
wordcloud.to_file("wordcloud.png")
filename = Image.open("wordcloud.png")
main_themes = filename.show()
elif request.form['submit_button'] == "Negative":
mysql.connection.cursor()
query = ("SELECT journal_entry FROM journals WHERE sentiment = 'negative'")
df = pd.read_sql(query, mysql.connection)
journal_entry = df['journal_entry']
long_string = (''.join(str(journal_entry.tolist())))
# Create WordCloud object
wordcloud = WordCloud(background_color="white", max_words=500, contour_width=6, contour_color='blue')
# Generate the cloud
wordcloud.generate(long_string)
wordcloud.to_file("wordcloud.png")
filename = Image.open("wordcloud.png")
main_themes = filename.show()
return render_template('insight.html', value=main_themes)
else:
return render_template('insight.html')
当我使用 GET 方法时,这些功能单独运行良好,并且在页面加载时自动生成 wordcloud 图像。但由于有一些选项,我只想在点击时触发生成。
非常感谢任何帮助
【问题讨论】:
标签: python html if-statement flask button