パッケージにした大きなアプリケーション(Large Applications as Packages) Large Applications as Packages¶
以下のような、flaskアプリケーションのシンプルな構造を想像してください: Imagine a simple flask application structure that looks like this::
/yourapplication
yourapplication.py
/static
style.css
/templates
layout.html
index.html
login.html
...
これは小さなアプリケーションでは良いですが、より大きなアプリケーションではモジュールの代わりにパッケージを使うのが良いアイデアです。チュートリアルはパッケージのパターンを使うような構造にされているので、example codeを確認してください。 While this is fine for small applications, for larger applications it's a good idea to use a package instead of a module. The :doc:`/tutorial/index` is structured to use the package pattern, see the :gh:`example code <examples/tutorial>`.
シンプルなパッケージ Simple Packages¶
これをより大きなものへ変換するために、まずは既にあるフォルダの中に新しくyourapplication
フォルダを作成し、全てをそのフォルダの下へ移動します。それからyourapplication.py
のファイル名を__init__.py
へ変更します。(最初に全ての.pyc
ファイルを確実に削除してください。そうしないと、うまくいかない場合が多いでしょう)
To convert that into a larger one, just create a new folder :file:`yourapplication` inside the existing one and move everything below it. Then rename :file:`yourapplication.py` to :file:`__init__.py`. (Make sure to delete all ``.pyc`` files first, otherwise things would most likely break)
そうすると、以下のようになるはずです: You should then end up with something like that::
/yourapplication
/yourapplication
__init__.py
/static
style.css
/templates
layout.html
index.html
login.html
...
しかし、アプリケーションはどうやって実行するのでしょうか?素朴なpython yourapplication/__init__.py
ではうまくいきません。Pythonはパッケージの中のモジュールを開始時の実行ファイル(startup file)にはしたがらないとだけ言わせてください。しかし、それは大きな問題ではなく、setup.py
と呼ばれる新しいファイルを内側のyourapplication
フォルダ(訳注: 上の例で「yourapplication」フォルダが2つあるうちの内側の方)の隣に、以下の内容でただ追加だけしてください:
But how do you run your application now? The naive ``python yourapplication/__init__.py`` will not work. Let's just say that Python does not want modules in packages to be the startup file. But that is not a big problem, just add a new file called :file:`setup.py` next to the inner :file:`yourapplication` folder with the following contents::
from setuptools import setup
setup(
name='yourapplication',
packages=['yourapplication'],
include_package_data=True,
install_requires=[
'flask',
],
)
importできるように、自分のアプリケーションをインストールします: Install your application so it is importable:
$ pip install -e .
flask
コマンドを使って自分のアプリケーションを実行するためには、アプリケーションのインスタンスをどこで見つけられるかFlaskへ伝える--app
オプションを指定する必要があります:
To use the ``flask`` command and run your application you need to set the ``--app`` option that tells Flask where to find the application instance:
$ flask --app yourapplication run
ここから何が得られたのでしょうか?ここまでで、複数のモジュールへと多少アプリケーションを再構成できます。ただひとつ覚えておく必要があることは、以下の手短なチェックリストです: What did we gain from this? Now we can restructure the application a bit into multiple modules. The only thing you have to remember is the following quick checklist:
Flaskアプリケーションのオブジェクトの作成は、
__init__.py
ファイルの中で行う必要があります。そのようにすると、各モジュールがFlaskアプリケーションのオブジェクトを安全にimportし、__name__変数で適切なパッケージが分かるようにできます。 the `Flask` application object creation has to be in the :file:`__init__.py` file. That way each module can import it safely and the `__name__` variable will resolve to the correct package.すべてのview関数(
route()
デコレータが関数名の上にあるもの)は、__init__.py
ファイルの中でimportされる必要があります。(importするのは)オブジェクト(訳注: おそらくview関数のこと)自身ではなく、それ(訳注: 直前に書いてある「オブジェクト」のこと)があるモジュールです。viewのモジュールはアプリケーションのオブジェクトが作成された後で importします。 all the view functions (the ones with a :meth:`~flask.Flask.route` decorator on top) have to be imported in the :file:`__init__.py` file. Not the object itself, but the module it is in. Import the view module **after the application object is created**.
__init__.py
の例がこちらです:
Here's an example :file:`__init__.py`::
from flask import Flask
app = Flask(__name__)
import yourapplication.views
そしてこれは、views.py
がどのようになるか示しています:
And this is what :file:`views.py` would look like::
from yourapplication import app
@app.route('/')
def index():
return 'Hello World!'
そうすると、以下のようになるはずです: You should then end up with something like that::
/yourapplication
setup.py
/yourapplication
__init__.py
views.py
/static
style.css
/templates
layout.html
index.html
login.html
...
循環import(Circular Imports) Circular Imports
全てのPythonプログラマが嫌っており、我々もいくらかそれに加わるものがこちらです: 循環import(2つのモジュールがお互い依存し合うとき循環importになります。今回の場合では、views.py
が__init__.py
に依存しています)。一般的には循環importは悪いアイデアですが、今回のは実際に大丈夫だとお知らせします。その理由は、__init__.py
の中ではviewを実際には使わないようにし、確実にモジュールはimportされるようにし、そしてそのimportをファイルの最後で行うようにしているためです。
Every Python programmer hates them, and yet we just added some: circular imports (That's when two modules depend on each other. In this case :file:`views.py` depends on :file:`__init__.py`). Be advised that this is a bad idea in general but here it is actually fine. The reason for this is that we are not actually using the views in :file:`__init__.py` and just ensuring the module is imported and we are doing that at the bottom of the file.
Blueprintsを使った対応 Working with Blueprints¶
もしも大きなアプリケーションがあるときは、小さなグループへ分割し、blueprintの助けを使って各グループを実装することが推奨されます。この話題に対するやさしい導入説明についてはBlueprintを使ったアプリケーションのモジュール化の章のドキュメントを参照してください。 If you have larger applications it's recommended to divide them into smaller groups where each group is implemented with the help of a blueprint. For a gentle introduction into this topic refer to the :doc:`/blueprints` chapter of the documentation.