【问题标题】:Unable to get value from flask dropdown menu无法从烧瓶下拉菜单中获取价值
【发布时间】:2019-08-21 18:20:31
【问题描述】:

我正在制作一个仪表板来显示不同月份的不同统计数据。我需要从下拉列表中选择一个月,因此与该月相关的文件将在我的 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>

有人可以帮助我并提出一些建议吗?

【问题讨论】:

  • 你能发布一个完整的例子吗?您的 home.html 中的 userinput 是什么?你如何渲染这个模板?
  • return render_template('home.html', userinput = current_userinput )
  • @AndreySemakin 你有什么解决办法吗?
  • 请添加app.py的完整代码。否则很难发现问题。
  • @arsho 下面是 app.py 代码

标签: python flask dynamic graph dashboard


【解决方案1】:

我正在跳过任何与 matplotlibgraphing 包相关的代码。

相反,我展示了一个在 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>

输出:

带有月份下拉菜单的表单:

根据用户选择显示结果:

【讨论】:

  • 在 def home() 下:当用户从下拉菜单更改月份时,我需要从我的目录中获取文件名。例如,当使用选择一月时,@application.route('/', methods=['GET', 'POST']) def home(): if request.method == "POST": month = request.form.get('month') ios_path= os.path.join(ios_ratings, month) #here I graph from ios_path which is a csv file return render_template('home.html', graph1 = graph_ios, data = get_monthly_data(month)) return render_template('home.html') 它给了我错误“在本地变量“月”之前引用?为什么?
  • Python 是关于缩进的。您在评论中粘贴的代码不可读。您是否运行了我在答案中包含的代码?
  • 是的,我确实有。它自己工作得很好。我的问题是,当用户从下拉列表中选择月份时,所选月份的路径应附加到我的文件阅读器功能并相应地显示图表。但是,它给我一个错误,说“本地变量“月份”之前引用的月份?
  • 根据错误信息,我猜你在初始化之前更新了month 变量的值。使用更新的代码、预期行为和错误描述更新您的问题。
  • 谢谢@arsho,我设法找出了错误。它的工作原理!非常感谢:)
猜你喜欢
  • 2019-12-02
  • 1970-01-01
  • 2020-12-19
  • 2015-11-08
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 2019-10-19
相关资源
最近更新 更多