5

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)
)
Zyber
  • 1,034
  • 8
  • 19
  • Isn't `current_app` available inside your `run` command? It should be, so you can call `socketio.run(current_app, ...)`. – Miguel Grinberg May 26 '15 at 21:18
  • @Miguel I tried your suggestion, but unfortunately I got an `working outside of application context` – Zyber May 27 '15 at 05:52
  • did you use `current_app` inside a Flask-Script command, or did you use it outside? You should be able to use `current_app` in a command handler function, Flask-Script sets up a test context before invoking these handlers. – Miguel Grinberg May 27 '15 at 06:41
  • @Miguel I used it inside of my call to `add_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. – Zyber May 27 '15 at 06:47

1 Answers1

8

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)
Zyber
  • 1,034
  • 8
  • 19