You see the whole point of Flask-Script is that it gives you scripted access not to the application, but to the environment in which the app runs. You don't actually need to be running the app, or importantly, to interfere with the app to do some task or other.
For example, lets say you want to load some data into the database model.
There are three ways you could do this:
- Write a script that understands your app's data model and get it to insert the data. You may be able to import the model module straight from your app, but you may not, due to dependencies etc.
- Run the code straight from your app, possibly using an admin screen somewhere. This means designing and implementing the screens - including the obvious security requirements.
- Use Flask-Script like this:
from application.model import Articles import csv @manager.command def ingest_csv(): '''read data from a csv file and ingest it into the database''' reader = csv.reader(open('articles.csv')) for row in reader: a = Article() a.init_from_csv_row(row)
python manager.py ingest_csv
Simpler I think you'll agree?
There is one other huge advantage to using Flask-Script; you can run them from crontabs.
*/10 * * * * /some/path/bin/python /some/path/manager.py ingest_csv >>/tmp/ingest.log 2>&1
Here are some other examples of things you might want to dovia script:
- Purge old data from your database.
- Backup data from your database.
- Export data from your database.
- Do anything else you want from your database!
- Flush your caches.
- Mail out reports.
- Other stuff I haven't thought of. Let me know!