プロジェクトのレイアウト Project Layout

プロジェクトのディレクトリを作成して、そこに移動します: Create a project directory and enter it:

$ mkdir flask-tutorial
$ cd flask-tutorial

そうしたらインストール手順に従って、Pythonの仮想環境(virtual environment)を用意して、プロジェクト用にFlaskをインストールします。 Then follow the :doc:`installation instructions </installation>` to set up a Python virtual environment and install Flask for your project.

このチュートリアルでは、ここからはflask-tutorialディレクトリで作業していると想定します。コード記述部分の上にあるファイル名は、それぞれflask-tutorialディレクトリから相対的なものになります。 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.


Flaskアプリケーションは単一ファイルにまで単純化できます。 A Flask application can be as simple as a single file.

hello.py
from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello():
    return 'Hello, World!'

しかしながら、プロジェクトが大きくなるにつれて、すべてのコードを一つのファイルに保ち続けることは手に余るようになります。Pythonのプロジェクトでは必要なときにimportできる複数のmoduleへコードを編成するためにpackageを使用し、このチュートリアルでも同様に使用します。 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:

  • flaskr/、アプリケーションのコードとファイルを含んだPythonのpackage。 ``flaskr/``, a Python package containing your application code and files.

  • tests/、テスト用moduleを含んだディレクトリ。 ``tests/``, a directory containing test modules.

  • venv/、Flaskとその他の依存対象がインストールされたPythonの仮想環境。 ``venv/``, a Python virtual environment where Flask and other dependencies are installed.

  • インストール関連のファイル(訳注: ここではsetup.pyとMANIFEST.inのこと)は、projectをどのようにインストールするかをPythonへ伝えます。 Installation files telling Python how to install your project.

  • 例えばgitのような、バージョン管理の設定。サイズに関係なく、全てのプロジェクトである種のバージョン管理を使うような習慣を付けるべきです。 Version control config, such as `git`_. You should make a habit of using some type of version control for all your projects, no matter the 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

もしバージョン管理を使用している場合、プロジェクトを実行している間に生成される以下のファイルは(バージョン管理からは)無視されるべきです。使っているエディタによっては、他のファイルもあるかもしれません。一般的に、自分で書かないファイルは無視します。例えば、gitではこのようにします: 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:

.gitignore
venv/

*.pyc
__pycache__/

instance/

.pytest_cache/
.coverage
htmlcov/

dist/
build/
*.egg-info/

アプリケーションの用意(Application Setup)へ続きます。 Continue to :doc:`factory`.