using flask-migrate with flask-script, flask-socketio and application factory

时光怂恿深爱的人放手 提交于 2019-12-07 08:20:49

问题


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

回答1:


Based on the comment by Miguel I found a way which works.

For some reason the following code does not work

manager.add_command("run", socketio.run(
   flask.current_app,
   host='127.0.0.1',
   port=5000,
   use_reloader=False)
)

But this actually works.

@manager.command
def run():
   socketio.run(flask.current_app,
                host='127.0.0.1',
                port=5000,
                use_reloader=False)


来源:https://stackoverflow.com/questions/30454704/using-flask-migrate-with-flask-script-flask-socketio-and-application-factory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!