我们可以使用具有 POST 方法的另一个路由使用 GET 方法更新路由中的现有变量吗

Can we update an existing variable in a route with GET method with the use of another route with POST method

我在 flask 中有这个代码,路由名为 @app.route("/forecastReport/", methods = ['GET']) 它有一个名为 forecast 的全局变量,我将它返回给我的客户端。我可以使用另一个名为 @app.route("/updateForecastReport/", methods = ['POST']) 的路由更新此变量 forecast 吗?所以这个路由将从客户端接收输入并使用它来更新变量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])

举个例子,也许对你有点帮助

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)