Skip to content
Myr

layout.rst

This file

Path

docs/tutorial/layout.rst

110 lines · 2.9 KiB

Last changed by

Not known.

History of this file

Take it

1Project Layout2==============34Create a project directory and enter it:56.. code-block:: none78    $ mkdir flask-tutorial9    $ cd flask-tutorial1011Then follow the :doc:`installation instructions </installation>` to set12up a Python virtual environment and install Flask for your project.1314The tutorial will assume you're working from the ``flask-tutorial``15directory from now on. The file names at the top of each code block are16relative to this directory.1718----1920A Flask application can be as simple as a single file.2122.. code-block:: python23    :caption: ``hello.py``2425    from flask import Flask2627    app = Flask(__name__)282930    @app.route('/')31    def hello():32        return 'Hello, World!'3334However, as a project gets bigger, it becomes overwhelming to keep all35the code in one file. Python projects use *packages* to organize code36into multiple modules that can be imported where needed, and the37tutorial will do this as well.3839The project directory will contain:4041* ``flaskr/``, a Python package containing your application code and42  files.43* ``tests/``, a directory containing test modules.44* ``.venv/``, a Python virtual environment where Flask and other45  dependencies are installed.46* Installation files telling Python how to install your project.47* Version control config, such as `git`_. You should make a habit of48  using some type of version control for all your projects, no matter49  the size.50* Any other project files you might add in the future.5152.. _git: https://git-scm.com/5354By the end, your project layout will look like this:5556.. code-block:: none5758    /home/user/Projects/flask-tutorial59    ├── flaskr/60    │   ├── __init__.py61    │   ├── db.py62    │   ├── schema.sql63    │   ├── auth.py64    │   ├── blog.py65    │   ├── templates/66    │   │   ├── base.html67    │   │   ├── auth/68    │   │   │   ├── login.html69    │   │   │   └── register.html70    │   │   └── blog/71    │   │       ├── create.html72    │   │       ├── index.html73    │   │       └── update.html74    │   └── static/75    │       └── style.css76    ├── tests/77    │   ├── conftest.py78    │   ├── data.sql79    │   ├── test_factory.py80    │   ├── test_db.py81    │   ├── test_auth.py82    │   └── test_blog.py83    ├── .venv/84    ├── pyproject.toml85    └── MANIFEST.in8687If you're using version control, the following files that are generated88while running your project should be ignored. There may be other files89based on the editor you use. In general, ignore files that you didn't90write. For example, with git:9192.. code-block:: none93    :caption: ``.gitignore``9495    .venv/9697    *.pyc98    __pycache__/99100    instance/101102    .pytest_cache/103    .coverage104    htmlcov/105106    dist/107    build/108    *.egg-info/109110Continue to :doc:`factory`.