Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't run example code referenced in Flask docs

Tags:

python

flask

I am reading the Flask docs and want to use the examples they reference that are in the git repo. However, the tutorials don't match the code in the repository, and I can't run them; I get the following error:

  @app.cli.command('initdb')
AttributeError: 'Flask' object has no attribute 'cli'

I used pip install flask to install Flask. Why can't I run the repo code?

like image 402
xian zhang Avatar asked Dec 25 '14 06:12

xian zhang


2 Answers

You are reading the development docs, but using the latest stable release (0.10.1). The current builds include many changes, including a cli. To try out the latest code, use:

pip install https://github.com/mitsuhiko/flask/tarball/master

To get something similar in the latest stable release, you either need to write your own commands or use a third-party extension such as Flask-Script. A new extension, Flask-CLI, backports the new Click interface from master to the stable version.

like image 176
davidism Avatar answered Nov 18 '22 21:11

davidism


Here is how I made it work:

change the function init_db()

def init_db():
    with app.app_context():
        db = get_db()
        with app.open_resource('schema.sql', mode='r') as f:
            db.cursor().executescript(f.read())
        db.commit()

And add this,

if __name__ == '__main__':
    init_db()
    app.run()

To run,

python flaskr.py

like image 3
Shivam Singh Avatar answered Nov 18 '22 20:11

Shivam Singh