У меня есть два сценария, которые заканчиваются на фреймах данных, загруженных в мой 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>