Home > Net >  Can we update an existing variable in a route with GET method with the use of another route with POS
Can we update an existing variable in a route with GET method with the use of another route with POS

Time:03-02

I have this code in flask with a route named @app.route("/forecastReport/", methods = ['GET']) that has a global variable named forecast and Im returning it to my client side. Can I update this variable forecast with the use of another route named @app.route("/updateForecastReport/", methods = ['POST'])? so this route will receive an input from the client side and use it to update the variable forecast. Can we do that?

this is a sample code:

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])

CodePudding user response:

Here an example, maybe this helps you a little bit

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)
  • Related