Cannot run sample code mentioned in Flask docs

I am reading Flask docs and want to use the examples they reference that are in the git repository. However, the tutorials do not match the code in the repository, and I cannot 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 repo code?

+6
source share
3 answers

You read development documents, but use the latest stable version (0.10.1). Current builds include many changes, including cli. To try out the latest code, use:

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

To get something like this in the latest stable release, you need to either write your own commands or use a third-party extension such as Flask-Script. The new extension, Flask-CLI, supports the new Click interface from the main to the stable version.

+6
source

Here is how I did it:

change the init_db () function

 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 execute,

python flaskr.py

+3
source

Or you can manipulate the python script example as follows: example for example

  • install pip install click
  • modify minitwit.python by importing FlaskCli from flask_cli module
  • initialize it by adding FlaskCLI(app) under app.config.from_envvar('MINITWIT_SETTINGS', silent=True)
0
source

Source: https://habr.com/ru/post/980141/


All Articles