I'm creating an flask application with the application factory approach, but have an issue when using Flask-Migrate with socketio and flask-script.
The problem is that I'm passing my create_app
function to the Manager
but I need to pass the app
to my socketio.run()
as well. And right now I can't seem to see a solution. Is there any way I can combine these two solutions?
manage.py:
#app = create_app(False) <--- Old approach
#manager = flask_script.Manager(app)
manager = flask_script.Manager(create_app)
manager.add_option("-t", "--testing", dest="testing", required=False)
manager.add_command("run", socketio.run(
app,
host='127.0.0.1',
port=5000,
use_reloader=False)
)
# DB Management
manager.add_command("db", flask_migrate.MigrateCommand)
When I used the old approach with socketio, but without flask-migrate everything worked. If I use the new approach, and remove the socketio part, the migrate works.
Note: I would like to be able to call my app with both of the following commands.
python manage.py run
python manage.py -t True db upgrade
Edit:
Trying to use current_app
I'm getting RuntimeError: working outside of application context
manager.add_command("run", socketio.run(
flask.current_app,
host='127.0.0.1',
port=5000,
use_reloader=False)
)
current_app
available inside yourrun
command? It should be, so you can callsocketio.run(current_app, ...)
. – Scotworking outside of application context
– Marrascurrent_app
inside a Flask-Script command, or did you use it outside? You should be able to usecurrent_app
in a command handler function, Flask-Script sets up a test context before invoking these handlers. – Scotadd_command
. But if you look at my answer below you'll see that the original way I had done it (with add_command) did not work, but changing to using the decorator solved the issue. – Marras