[英]HTML dropdown list not returning Python module in Flask route
我有兩個腳本以數據幀結尾加載到我的app.py
。 我的第一個HTML頁面中的下拉列表是兩個數據幀的列名稱。 單擊提交時,我正在嘗試將選擇路由到regrplot
中的第三個模塊app.py
。 此模塊將使用選擇來確定數據框,在年份列上連接兩個,並運行回歸圖圖像生成器。
我得到的錯誤: UnboundLocalError: local variable 'y1' referenced before assignment
到達dropdown.html
頁面時。 我能夠看到填充的下拉列表以及提交按鈕。
名稱中帶有df
變量是從其他模塊導入的數據幀。
dropdown.html
<body>
<form name="var1" action="/dropdown_x">
<fieldset>
<legend>Table Variable 1</legend>
<p>
<label>Select</label>
<select name="df_variable1">
{% for each in dropdown_fields %}
<option value="{{each}}">{{each}}</option>
{% endfor %}
</select>
</p>
</fieldset>
</form>
<form name = "var2" action="/dropdown_y">
<fieldset>
<legend>Table Variable 2</legend>
<p>
<label>Select</label>
<select name="df_variable2">
{% for each in dropdown_fields %}
<option value="{{each}}">{{each}}</option>
{% endfor %}
</select>
</p>
</fieldset>
<button><input name="regrplt" method="GET" action="/regrplot" type="submit" class="btn btn-default" value="Submit"></button>
</form>
</body>
app.py
import pandas as pd
app = Flask(__name__)
book_fields= list(book_data.year_book_data)
census1_fields = list(censusLoad.df_full1)
census2_fields = list(censusLoad.df_full2)
dropdown_fields = book_fields + census1_fields + census2_fields
@app.route("/dropdown")
def dropdownList():
return render_template('dropdown.html', dropdown_fields=dropdown_fields)
@app.route("/dropdown_x", methods=['GET', 'POST'])
def testt1():
if request.method == 'POST':
df_variable1 = request.form['df_variable1']
if df_variable1 in book_fields:
x1 = book_data.df_yearbook
if df_variable1 in census1_fields:
x1 = censusLoad.df_full1
if df_variable1 in census2_fields:
x1 = censusLoad.df_full2
return x1
@app.route("/dropdown_y", methods=['GET', 'POST'])
def testt2():
if request.method == 'POST':
df_variable2 = request.form['df_variable2']
if df_variable2 in book_fields:
y1 = book_data.df_year_book
if df_variable2 in census1_fields:
y1 = censusLoad.df_full1
if df_variable2 in census2_fields:
y1 = censusLoad.df_full2
return y1
@app.route("/regrplot", methods=['GET','POST'])
def regrplot():
if request.method == 'POST':
# Have tried with and without this block
df_variable1 = request.form['df_variable1']
df_variable2 = request.form['df_variable2']
if df_variable1 in book_fields:
x1 = book_data.df_yearbook
if df_variable1 in census1_fields:
x1 = censusLoad.df_full1
if df_variable1 in census2_fields:
x1 = censusLoad.df_full2
if df_variable2 in book_fields:
y1 = book_data.df_yearbook
if df_variable2 in census1_fields:
y1 = censusLoad.df_full1
if df_variable2 in census2_fields:
y1 = censusLoad.df_full2
#
Reg_df = x1.merge(y1, how = "inner", left_on ='year', right_on = 'year')
plot = sns.regplot(x=f'{df_variable1}', y=f'{df_variable2}', data = Reg_df)
plot.savefig('regrplot', format='png')
return render_template("regression.html")
if __name__ == "__main__":
app.run(debug=True)
regression.html
<!DOCTYPE HTML>
<html lang = "en">
<head>
<title>formDemo.html</title>
<meta charset = "UTF-8" />
</head>
<body>
<image src="regrplot.png"></image>
</body>
</html>
我沒有測試過,但嘗試這些修復:
選項1:表單需要設置為POST才能使用“request.form”方法,否則請使用“request.args”方法(GET)。 這就是為什么我認為你有錯誤,因為“request.method =='POST'”條件為False,因此沒有定義“y1”,因此從未聲明“y1”。 對於“x1”應該是相同的。
<form name="var1" action="/dropdown_x" method="post">
選項2:如果您正在使用GET,請嘗試檢查“GET”而不是“POST”:
if request.method == 'GET':
df_variable1 = request.args['df_variable1']
確保你使用“request.args.get('df_variable1')或”request.args ['df_variable1']“因為”request.form []“僅適用於”POST“。
對於最佳實踐,如果條件為假,則應聲明“x1”和“y1”,在這種情況下,“x1”和“y1”將是未定義的。
希望這有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.