我在燒瓶中有這個代碼,它的路徑名為@app.route("/forecastReport/", methods = ['GET'])
,具有一個名為的全域變數forecast,我將它回傳到我的客戶端。我可以forecast使用另一個名為 的路由來更新這個變數@app.route("/updateForecastReport/", methods = ['POST'])嗎?所以這條路線將接收來自客戶端的輸入并使用它來更新變數forecast。我們可以這樣做嗎?
這是一個示例代碼:
forecast= []
@app.route("/updateForecastReport/", methods = ['POST'])
def UpdateForecast():
fetchedData = request.data.decode("UTF-8")
fetchedData = (int(i) for i in data.strip("[]").split(","))
# This fetchedData variable must be use to update the forecast variable in the another route
@app.route("/forecastReport/", methods = ['GET'])
def ForecastReport():
global forecast
return jsonify([forecast])
uj5u.com熱心網友回復:
這是一個例子,也許這對你有一點幫助
app = Flask(__name__)
app.forecast = []
@app.route("/updateForecastReport/", methods = ['POST'])
def UpdateForecast():
fetchedData = request.data.decode("UTF-8")
app.forecast = (int(i) for i in data.strip("[]").split(","))
@app.route("/forecastReport/", methods = ['GET'])
def ForecastReport():
return jsonify(app.forecast)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/435983.html
