簡體   English   中英

無法從燒瓶下拉菜單中獲取價值

[英]Unable to get value from flask dropdown menu

我正在制作一個儀表板來顯示不同月份的不同統計數據。 我需要從下拉列表中選擇一個月,因此與該月相關的文件將在我的 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>

有人可以幫助我並提出建議嗎?

我正在跳過與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>

輸出:

帶有月份下拉列表的表單:

帶有月份選擇下拉列表的表單

根據用戶選擇顯示結果:

顯示基於用戶選擇的結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM