Project Layout

Create a project directory and enter it:

  1. $ mkdir flask-tutorial
  2. $ cd flask-tutorial

Then follow the installation instructions to setup a Python virtual environment and install Flask for your project.

The tutorial will assume you’re working from the flask-tutorialdirectory from now on. The file names at the top of each code block arerelative to this directory.


A Flask application can be as simple as a single file.

hello.py

  1. from flask import Flask
  2.  
  3. app = Flask(__name__)
  4.  
  5.  
  6. @app.route('/')
  7. def hello():
  8. return 'Hello, World!'

However, as a project gets bigger, it becomes overwhelming to keep allthe code in one file. Python projects use packages to organize codeinto multiple modules that can be imported where needed, and thetutorial will do this as well.

The project directory will contain:

  • flaskr/, a Python package containing your application code andfiles.

  • tests/, a directory containing test modules.

  • venv/, a Python virtual environment where Flask and otherdependencies are installed.

  • Installation files telling Python how to install your project.

  • Version control config, such as git. You should make a habit ofusing some type of version control for all your projects, no matterthe size.

  • Any other project files you might add in the future.

By the end, your project layout will look like this:

  1. /home/user/Projects/flask-tutorial
  2. ├── flaskr/
  3. ├── __init__.py
  4. ├── db.py
  5. ├── schema.sql
  6. ├── auth.py
  7. ├── blog.py
  8. ├── templates/
  9. ├── base.html
  10. ├── auth/
  11. ├── login.html
  12. └── register.html
  13. └── blog/
  14. ├── create.html
  15. ├── index.html
  16. └── update.html
  17. └── static/
  18. └── style.css
  19. ├── tests/
  20. ├── conftest.py
  21. ├── data.sql
  22. ├── test_factory.py
  23. ├── test_db.py
  24. ├── test_auth.py
  25. └── test_blog.py
  26. ├── venv/
  27. ├── setup.py
  28. └── MANIFEST.in

If you’re using version control, the following files that are generatedwhile running your project should be ignored. There may be other filesbased on the editor you use. In general, ignore files that you didn’twrite. For example, with git:

.gitignore

  1. venv/
  2.  
  3. *.pyc
  4. __pycache__/
  5.  
  6. instance/
  7.  
  8. .pytest_cache/
  9. .coverage
  10. htmlcov/
  11.  
  12. dist/
  13. build/
  14. *.egg-info/

Continue to Application Setup.