hello am trying to create flask api where user input the vaue and I will use that value to process my python code here is the code
`
app = Flask(__name__)
api = Api(app)
class Users(Resource):
@app.route('/users/<string:name>/')
def hello(name):
namesource = request.args.get('name')
return "Hello {}!".format(name)
print(namesource) # here am trying to get the sitring/value in name source but i can't because it no variable defines
api.add_resource(Users, name='users')
# For Running our Api on Localhost
if __name__ == '__main__':
app.run(debug=True)
`
am trying to expect that i get that value/String outside of the function of api flask
You are trying to access the variable namesource
outside of the function hello
. You can't do that. You can access the variable name
outside of the function hello
because it is a parameter of the function. You can fix it by making a global variable.
app = Flask(__name__)
api = Api(app)
namesource = None
class Users(Resource):
@app.route('/users/<string:name>/')
def hello(name):
global namesource
namesource = request.args.get('name')
return "Hello {}!".format(name)
print(namesource)
api.add_resource(Users, name='users')
# For Running our Api on Localhost
if __name__ == '__main__':
app.run(debug=True)
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.