| | Project Layout |
| | ============== |
| |
|
| | Create a project directory and enter it: |
| |
|
| | .. code-block:: none |
| |
|
| | $ mkdir flask-tutorial |
| | $ cd flask-tutorial |
| |
|
| | Then follow the :doc:`installation instructions </installation>` to set |
| | up 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 are |
| | relative to this directory. |
| |
|
| | ---- |
| |
|
| | A Flask application can be as simple as a single file. |
| |
|
| | .. code-block:: python |
| | :caption: ``hello.py`` |
| |
|
| | 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 all |
| | the code in one file. Python projects use *packages* to organize code |
| | into multiple modules that can be imported where needed, and the |
| | tutorial will do this as well. |
| |
|
| | The project directory will contain: |
| |
|
| | |
| | files. |
| | |
| | |
| | dependencies are installed. |
| | |
| | |
| | using some type of version control for all your projects, no matter |
| | the size. |
| | |
| |
|
| | .. _git: https: |
| |
|
| | By the end, your project layout will look like this: |
| |
|
| | .. code-block:: none |
| |
|
| | /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 generated |
| | while running your project should be ignored. There may be other files |
| | based on the editor you use. In general, ignore files that you didn't |
| | write. For example, with git: |
| |
|
| | .. code-block:: none |
| | :caption: ``.gitignore`` |
| |
|
| | venv/ |
| |
|
| | |
| | __pycache__/ |
| |
|
| | instance/ |
| |
|
| | .pytest_cache/ |
| | .coverage |
| | htmlcov/ |
| |
|
| | dist/ |
| | build/ |
| | |
| |
|
| | Continue to :doc:`factory`. |
| |
|