開発サーバ Development Server

Flask 0.11から、開発サーバを実行する複数の組み込まれた(最初からFlaskで用意している)やり方があります。最も良いやり方のひとつはflaskコマンドラインユーティリティですが、Flask.run()メソッドを使い続けることもできます。 Starting with Flask 0.11 there are multiple built-in ways to run a development server. The best one is the :command:`flask` command line utility but you can also continue using the :meth:`Flask.run` method.

コマンドライン Command Line

flaskコマンドラインスクリプト(コマンドライン・インタフェース)は、アプリケーションの読み込み(load)のやり方によって、読み込みに関してより優れた経験を提供してくれるため、強く推奨されています。基本的な使い方は以下のようになります: The :command:`flask` command line script (:ref:`cli`) is strongly recommended for development because it provides a superior reload experience due to how it loads the application. The basic usage is like this::

$ export FLASK_APP=my_application
$ export FLASK_ENV=development
$ flask run

この例ではインタラクティブなデバッガと再読み込み機能(reloader)を含む、開発環境を有効にし、それからhttp://localhost:5000でサーバを開始します。 This enables the development environment, including the interactive debugger and reloader, and then starts the server on *http://localhost:5000/*.

サーバの個々の目玉機能(feature)は、runのオプションをより多く与えることで制御可能です。例えば、(以下のようにして)再読み込み機能を無効にできます: The individual features of the server can be controlled by passing more arguments to the ``run`` option. For instance the reloader can be disabled::

$ flask run --no-reload

注釈

Flask 1.0より前は、FLASK_ENV環境変数はサポートされておらず、デバッグモードを有効にするにはFLASK_DEBUG=1をexportする必要がありました。これは今でもデバッグモードの制御に使用可能ですが、上述した開発環境の設定の方が好ましいものです。 Prior to Flask 1.0 the :envvar:`FLASK_ENV` environment variable was not supported and you needed to enable debug mode by exporting ``FLASK_DEBUG=1``. This can still be used to control debug mode, but you should prefer setting the development environment as shown above.

コードの中 In Code

アプリケーションを開始する別のやり方は、Flask.run()メソッドをとおすものです。これはflaskスクリプトが行うのと全く同じようにローカルサーバを即座に起動します。 The alternative way to start the application is through the :meth:`Flask.run` method. This will immediately launch a local server exactly the same way the :command:`flask` script does.

例: Example::

if __name__ == '__main__':
    app.run()

これは普通のケースでは上手く機能しますが、開発向けには上手く機能せず、それがFlask 0.11以後はflaskが推奨される理由になっています。この理由は、再読み込みの機構を機能させるための仕組みによって、奇怪な副作用が生じるためです(あるコードを2回実行したり、ときにはメッセージなしでクラッシュしたり、またはsyntax errorおよびimport errorが発生したとき死んでしまったりするようなことがあります)。 This works well for the common case but it does not work well for development which is why from Flask 0.11 onwards the :command:`flask` method is recommended. The reason for this is that due to how the reload mechanism works there are some bizarre side-effects (like executing certain code twice, sometimes crashing without message or dying when a syntax or import error happens).

しかしながら、それでも自動再読み込みをしないアプリケーションを起動するときには、(Flask.run()で)コードの中で起動することは完全に適切な手法です。 It is however still a perfectly valid method for invoking a non automatic reloading application.