无法从烧瓶下拉菜单中获取价值
Unable to get value from flask dropdown menu
我正在制作一个仪表板来显示不同月份的不同统计数据。我需要从下拉列表中 select 一个月,因此与该月相关的文件将在我的 home.html 页面上显示其图表。
但是我的下拉列表无法读取月份,我能知道我做错了什么吗?
这是我的 app.py
代码:
from flask import Flask, render_template, url_for, request, redirect
from graph_itunes import graphing
import matplotlib
application = Flask(__name__)
def get_itune_installs(ios_path):
with open (ios_path, 'r') as f:
install_itunes = json.load(f)
results = install_itunes['results'][0]['data']
df_itunes = pd.DataFrame.from_dict(results,orient = 'columns')
return df_itunes
@application.route('/', methods=['GET', 'POST'])
def home():
current_userinput = request.form.get('userinput')
path_ios = 'iTunesConnect/Installs/'
ios_path = os.path.join(path_ios, current_userinput)
itunes_installs = get_itune_installs(ios_path)
graph_itunes_installs = graphing(itunes_installs)
return render_template('home.html',
graph1 = graph_itunes_installs,
userinput = current_userinput)
if __name__ == '__main__':
application.run(debug = True)
这是我的 home.html
:
<form name = 'userinput' action="/" method = 'post'>
<select name = "userinput" id = 'userinput'>
<option value="January">January</option>
<option value="February">February</option>
<option value="March" selected >March</option>
<option value="April">April</option>
<option value="May">May</option>
{% for input in userinput %}
<option selected value= "{{input}}">{{input}}</option>
{% endfor %}
</select>
<p><a class ="btn btn-default" type = "submit" value = "Select" >Submit</a></p>
</form>
有人可以帮助我并提出建议吗?
我正在跳过与 matplotlib
和 graphing
包相关的任何代码。
相反,我展示了一个在 Flask 中处理下拉值的示例。
以下示例将根据用户选择的月份显示值。
app.py
:
from flask import Flask, render_template, url_for, request, redirect
application = Flask(__name__)
def get_monthly_data(month):
data = {
"January": "First month of the year",
"February": "Second month of the year",
"March": "Third month of the year",
"April": "Fourth month of the year",
"May": "Fifth month of the year"
}
return data.get(month, "Data is not found").strip()
@application.route('/', methods=['GET', 'POST'])
def home():
if request.method == "POST":
month = request.form.get('month')
return render_template('home.html', data = get_monthly_data(month))
return render_template('home.html')
if __name__ == '__main__':
application.run(debug = True)
home.html
:
<html>
<head>
<title>Dropdown Example</title>
</head>
<body>
{% if data %}
<div>
<h3>Monthly Data</h3>
<p>{{ data }}</p>
</div>
{% endif %}
<form action="/" method="post">
<select name="month">
<option value="January">January</option>
<option value="February">February</option>
<option value="March" selected >March</option>
<option value="April">April</option>
<option value="May">May</option>
</select>
<input type="submit" value="Select Month">
</form>
</body>
</html>
输出:
带有月份下拉列表的表单:
显示基于用户选择的结果:
我正在制作一个仪表板来显示不同月份的不同统计数据。我需要从下拉列表中 select 一个月,因此与该月相关的文件将在我的 home.html 页面上显示其图表。
但是我的下拉列表无法读取月份,我能知道我做错了什么吗?
这是我的 app.py
代码:
from flask import Flask, render_template, url_for, request, redirect
from graph_itunes import graphing
import matplotlib
application = Flask(__name__)
def get_itune_installs(ios_path):
with open (ios_path, 'r') as f:
install_itunes = json.load(f)
results = install_itunes['results'][0]['data']
df_itunes = pd.DataFrame.from_dict(results,orient = 'columns')
return df_itunes
@application.route('/', methods=['GET', 'POST'])
def home():
current_userinput = request.form.get('userinput')
path_ios = 'iTunesConnect/Installs/'
ios_path = os.path.join(path_ios, current_userinput)
itunes_installs = get_itune_installs(ios_path)
graph_itunes_installs = graphing(itunes_installs)
return render_template('home.html',
graph1 = graph_itunes_installs,
userinput = current_userinput)
if __name__ == '__main__':
application.run(debug = True)
这是我的 home.html
:
<form name = 'userinput' action="/" method = 'post'>
<select name = "userinput" id = 'userinput'>
<option value="January">January</option>
<option value="February">February</option>
<option value="March" selected >March</option>
<option value="April">April</option>
<option value="May">May</option>
{% for input in userinput %}
<option selected value= "{{input}}">{{input}}</option>
{% endfor %}
</select>
<p><a class ="btn btn-default" type = "submit" value = "Select" >Submit</a></p>
</form>
有人可以帮助我并提出建议吗?
我正在跳过与 matplotlib
和 graphing
包相关的任何代码。
相反,我展示了一个在 Flask 中处理下拉值的示例。 以下示例将根据用户选择的月份显示值。
app.py
:
from flask import Flask, render_template, url_for, request, redirect
application = Flask(__name__)
def get_monthly_data(month):
data = {
"January": "First month of the year",
"February": "Second month of the year",
"March": "Third month of the year",
"April": "Fourth month of the year",
"May": "Fifth month of the year"
}
return data.get(month, "Data is not found").strip()
@application.route('/', methods=['GET', 'POST'])
def home():
if request.method == "POST":
month = request.form.get('month')
return render_template('home.html', data = get_monthly_data(month))
return render_template('home.html')
if __name__ == '__main__':
application.run(debug = True)
home.html
:
<html>
<head>
<title>Dropdown Example</title>
</head>
<body>
{% if data %}
<div>
<h3>Monthly Data</h3>
<p>{{ data }}</p>
</div>
{% endif %}
<form action="/" method="post">
<select name="month">
<option value="January">January</option>
<option value="February">February</option>
<option value="March" selected >March</option>
<option value="April">April</option>
<option value="May">May</option>
</select>
<input type="submit" value="Select Month">
</form>
</body>
</html>
输出:
带有月份下拉列表的表单:
显示基于用户选择的结果: