I have a Flask app set up so it can run two different sites (the sites share businesslogic and database models, so they use the same back end). One is just the regular website, the other is a site that runs tasks (it's a web interface for shooting long running tasks into celery)
The app currently uses Flask-Script (the manage.py command) to start the regular website, but I'd like to use the same script to start the task-site aswell.
In Flask-Script it seems all commands are processes run on a single app. Is it possible to have manage.py start two different apps?
My code is now as follows, where create_app is the factory function for creating the Flask app for the website, and taskserver is the factory function for creating the taskserver website.
import os
from flask_script import Manager
from app import create_app, database, taskserver
if os.getenv('FLASK_CONFIG'):
app = create_app(os.getenv('FLASK_CONFIG'))
manager = Manager(app)
else:
app = create_app
manager = Manager(app)
manager.add_option('-c', '--config_name', dest='config_name', required=False)
@manager.shell
def make_shell_context():
from app.models.data import base_models
return dict(app=app, db=database, models=base_models)
if __name__ == "__main__":
manager.run()
I hope someone knows how to adapt the manage.py script to be able to start either one.
UPDATE:
Thanks to @Joro Tenev, the problem is solved. The code I used eventually is:
import os
from flask_script import Manager
from app import create_website, create_taskserver
def make_app(app_type, config_name):
if os.getenv('FLASK_CONFIG'):
config_name = os.getenv('FLASK_CONFIG')
if app_type == 'webserver':
return create_website(config_name)
else:
return create_taskserver(config_name) # i don't know how your factory works, so adjust accordingly
manager = Manager(make_app)
manager.add_option('-a', '--app_type', dest='app_type', required=True)
manager.add_option('-c', '--config_name', dest='config_name', required=False)
And to start the different apps, I use:
python manage.py --app_type=webserver --config_name=development runserver
python manage.py --app_type=taskserver --config_name=development runserver
You can pass a function to the constructor of Manager. The function should return a Flask app. You can also pass parameters to the function by using the manager.add_option
manager.add_option('-a','--app_type', dest='app_type',required=True)
def make_app(app_type):
if app_type =='webserver':
return create_app(os.getenv('FLASK_CONFIG'))
elif:
return taskserver(os.getenv('FLASK_CONFIG')) # i don't know how your factory works, so adjust accordingly
manager = Manager(make_app)
And you'll use it with
$ python manage.py --app_type='webserver'
Hope this helps :)