Project Layout
Create a project directory and enter it:
- $ mkdir flask-tutorial
- $ 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-tutorial
directory 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.
- from flask import Flask
- app = Flask(__name__)
- @app.route('/')
- def hello():
- 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:
- /home/user/Projects/flask-tutorial
- ├── flaskr/
- │ ├── __init__.py
- │ ├── db.py
- │ ├── schema.sql
- │ ├── auth.py
- │ ├── blog.py
- │ ├── templates/
- │ │ ├── base.html
- │ │ ├── auth/
- │ │ │ ├── login.html
- │ │ │ └── register.html
- │ │ └── blog/
- │ │ ├── create.html
- │ │ ├── index.html
- │ │ └── update.html
- │ └── static/
- │ └── style.css
- ├── tests/
- │ ├── conftest.py
- │ ├── data.sql
- │ ├── test_factory.py
- │ ├── test_db.py
- │ ├── test_auth.py
- │ └── test_blog.py
- ├── venv/
- ├── setup.py
- └── 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:
- venv/
- *.pyc
- __pycache__/
- instance/
- .pytest_cache/
- .coverage
- htmlcov/
- dist/
- build/
- *.egg-info/
Continue to Application Setup.