无法从烧瓶下拉菜单中获取价值

时间:2019-03-31 11:53:58

标签: python flask dynamic graph dashboard

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

有人可以帮我提些建议吗?

1 个答案:

答案 0 :(得分:0)

我正在跳过与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>

输出:

带有月份下拉列表的表格:

form with month selection dropdown

根据用户选择显示结果:

showing result based on user selection