I run Connexion/Flask app like this:
import connexion
from flask_cors import CORS
from flask import g
app = connexion.App(__name__, specification_dir='swagger_server/swagger/')
app.add_api('swagger.yaml')
CORS(app.app)
with app.app.app_context():
g.foo = 'bar'
q = g.get('foo') # THIS WORKS
print('variable', q)
app.run(port=8088, use_reloader=False)
somewhere else in code:
from flask import abort, g, current_app
def batch(machine=None): # noqa: E501
try:
app = current_app._get_current_object()
with app.app_context:
bq = g.get('foo', None) # DOES NOT WORK HERE
print('variable:', bq)
res = MYHandler(bq).batch(machine)
except:
abort(404)
return res
This does not work - I am not able to pass variable ('bla') to second code example.
Any idea how to pass context variable correctly? Or how to pass a variable and use it globally for all Flask handlers?
I've tried this solution (which works): In first code section I'd add:
app.app.config['foo'] = 'bar'
and in second code section there would be:
bq = current_app.config.get('foo')
This solution does not use application context and I am not sure if it's a correct way.