I have two scripts that end in dataframes loaded into my app.py
. The dropdown lists in my first HTML page are the column names of both dataframes. When clicking submit, I'm trying to route the selections to a third module regrplot
in app.py
. This module would use the selection to determine the dataframes, join the two on the year column, and run a regression plot image generator.
Error I'm getting: UnboundLocalError: local variable 'y1' referenced before assignment
When getting to the dropdown.html
page. I am able to see the dropdown lists populated, as well as the submit button.
Variables with df
in their name, are dataframes imported from other modules.
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>
I haven't tested, but try these fixes:
Option 1: The forms need to be set to POST in order to use the "request.form" method, otherwise use the "request.args" method instead (GET). This is why I believe you have the error where "y1" isn't defined because the "request.method == 'POST'" condition is False, thus the "y1" was never declared. Should be the same for "x1".
<form name="var1" action="/dropdown_x" method="post">
Option 2: If you're using GET, then try checking for "GET" instead of "POST":
if request.method == 'GET':
df_variable1 = request.args['df_variable1']
Make sure you're using "request.args.get('df_variable1') or "request.args['df_variable1']" because "request.form[]" only works for "POST".
For best practice, you should declare "x1" and "y1" in case the condition is false, in which case "x1" and "y1" will be undefined.
Hope this works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.