Shell script to update DB via flask
Question: I got started with [flask](http://flask.pocoo.org/docs/quickstart/#quickstart)
and I tried out the
[Flaskr](https://github.com/mitsuhiko/flask/tree/master/examples/flaskr/)
example. On the execution of a certain python script, I would like to update
one row of my database.

I am a newbie here and would like to understand: am I going to update the DB
from inside that python script or I am going to wait for a signal from the
flask WSGI script: I have referred to this
[thread](http://stackoverflow.com/questions/11292612/flask-sqlalchemy-how-to-
update-insert-row-in-db) but am not sure how I am going to interact with the
external script. Any help or hints are appreciated.


Answer: WSGI handles HTTP requests/responses. A script won't be issuing those.
Instead, import your Flask app in the script and make an application context:

    
    
    from my_project import my_app
    
    ctx = my_app.app_context()
    ctx.push()
    
    # ... my code
    
    db.session.commit()
    
    ctx.pop()
    

Relevant docs: <http://flask.pocoo.org/docs/appcontext/>,
<http://flask.pocoo.org/docs/shell/>

Or consider using [Flask-Script](http://flask-
script.readthedocs.org/en/latest/) to add command line functions to your
application, if the function doesn't need to be a separate script.

