简体   繁体   中英

Flask Python - How to pass variable from one method to another

I have a flask app and I need to pass variables from one function to another. The requirement here is I need to pass the value of 'skill' from index() to get_image(), any idea on how this can be achieved?

@app.route('/', methods=['POST'])
def index():
    data = json.loads(request.get_data())
    skill = data['conversation']['skill']


@app.route("/get-image/<image_name>")
def get_image(image_name):
    if skill == 'getpositions':
        # some code

You can try use flask sessions for example:

from flask import Flask, session

...

@app.route('/', methods=['POST'])
def index():
    data = json.loads(request.get_data())
    skill = data['conversation']['skill']
    session['skill'] = skill

@app.route("/get-image/<image_name>")
def get_image(image_name):
    if session['skill'] == 'getpositions':
    #some code

Check the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM