Python 无法从烧瓶下拉菜单中获取值
我正在制作一个仪表板,以显示不同月份的不同统计数据。我需要从下拉列表中选择一个月,相应地,与该月相关的文件将在我的home.html页面上显示其图表 然而,我的下拉列表无法阅读这个月,我能知道我做错了什么吗 以下是我的app.py代码: 这是my home.html:Python 无法从烧瓶下拉菜单中获取值,python,flask,dynamic,graph,dashboard,Python,Flask,Dynamic,Graph,Dashboard,我正在制作一个仪表板,以显示不同月份的不同统计数据。我需要从下拉列表中选择一个月,相应地,与该月相关的文件将在我的home.html页面上显示其图表 然而,我的下拉列表无法阅读这个月,我能知道我做错了什么吗 以下是我的app.py代码: 这是my home.html: 有人能帮我提些建议吗?我跳过了任何与matplotlib和graphing软件包相关的代码 相反,我展示了一个处理烧瓶中下拉值的示例。 下面的示例将显示基于用户选择的月份的值 app.py: home.html: 输出: 带有月份
有人能帮我提些建议吗?我跳过了任何与matplotlib和graphing软件包相关的代码 相反,我展示了一个处理烧瓶中下拉值的示例。 下面的示例将显示基于用户选择的月份的值 app.py: home.html: 输出: 带有月份下拉列表的表单: 根据用户选择显示结果:
你能举一个完整的例子吗?home.html中的userinput是什么?如何呈现此模板?返回render_template'home.html',userinput=current_userinput@AndreySemakin是否有任何解决方案?请添加app.py的完整代码。否则很难确定问题。@arsho下面是def home下的app.py代码:我需要从我的目录中获取文件名,因为用户从下拉列表更改为月份。例如,当use选择Jan、@application.route'/'时,methods=['GET','POST']def home:if request.method==POST:month=request.form.GET'month'ios\u path=os.path.joinios\u ratings,month在这里我从ios\u path绘制图形,这是一个csv文件返回渲染模板'home.html',graph1=graph\u ios,data=get\u monthly\u datamonth return render\u模板'home.html'它在本地变量月份之前给我提供了引用错误?为什么?Python完全是关于缩进的。粘贴在注释中的代码不可读。你运行过我在回答中包含的代码吗?是的,我确实运行过。它本身工作得很好。我的问题是,当用户从下拉列表中选择月份时,所选月份的路径应该附加到我的文件读取器函数,并相应地显示图形。但是,它给我带来了一个错误,说month referenced before local var month?根据错误消息,我猜您是在初始化之前更新month变量的值。用更新的代码、预期的行为和错误描述更新您的问题。谢谢@arsho,我设法找出了错误。它的工作!非常感谢:
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)
<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>
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)
<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>