[英]How to return different result under 1 decorator in the same time in flask python
I m currently deploying my LSTM model in flask python, I would like to ask how do I return my new CSV file together with my plot graph under one @app.route
into a page, hence I need some advice and tips regarding this issues.
執行代碼時出現此錯誤>> TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
應用程序.py
@app.route('/transform', methods=["POST"])
def transform_view():
if request.method == 'POST':
f = request.files['data_file']
if not f:
return "No file"
stream = io.StringIO(f.stream.read().decode("UTF8"), newline=None)
csv_input = csv.reader(stream)
stream.seek(0)
result = stream.read()
df = pd.read_csv(StringIO(result), usecols=[1])
#extract month value
df2 = pd.read_csv(StringIO(result))
matrix2 = df2[df2.columns[0]].to_numpy()
list1 = matrix2.tolist()
# load the model from disk
model = load_model('model.h5')
dataset = df.values
dataset = dataset.astype('float32')
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
look_back = 1
dataset_look = create_dataset(dataset, look_back)
dataset_look = np.reshape(dataset_look, (dataset_look.shape[0], 1, dataset_look.shape[1]))
predict = model.predict(dataset_look)
transform = scaler.inverse_transform(predict)
X_FUTURE = 12
transform = np.array([])
last = dataset[-1]
for i in range(X_FUTURE):
curr_prediction = model.predict(np.array([last]).reshape(1, look_back, 1))
last = np.concatenate([last[1:], curr_prediction.reshape(-1)])
transform = np.concatenate([transform, curr_prediction[0]])
transform = scaler.inverse_transform([transform])[0]
dicts = []
curr_date = pd.to_datetime(list1[-1])
for i in range(X_FUTURE):
curr_date = curr_date + relativedelta(months=+1)
dicts.append({'Predictions': transform[i], "Month": curr_date})
new_data = pd.DataFrame(dicts).set_index("Month")
##df_predict = pd.DataFrame(transform, columns=["predicted value"])
def index(chartID = 'chart_ID', chart_type = 'line', chart_height = 550):
chart = {"renderTo": chartID, "type": chart_type, "height": chart_height}
series = [{"name": 'Month', "data": [1,2,3]}]
title = {"text": 'Time Series Sales Predicting'}
xAxis = {"categories": ['xAxis Data1', 'xAxis Data2', 'xAxis Data3']}
yAxis = {"title": {"text": 'Sales'}}
response = make_response(new_data.to_csv(index = True, encoding='utf8'))
response.headers["Content-Disposition"] = "attachment; filename=result.csv"
return response
return render_template('graph.html', chartID=chartID, chart=chart, series=series, title=title, xAxis=xAxis, yAxis=yAxis)
從您的錯誤消息中,您的transform_view()
function 不返回任何內容。 考慮在它的底部添加一個return
語句,就像你對index()
所做的那樣
@app.route('/transform', methods=["POST"])
def transform_view():
if request.method == 'POST':
# ...
return # <your_response>
維護你的index()
function
def index(chartID = 'chart_ID', chart_type = 'line', chart_height = 550):
# ...
return render_template(# ...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.