開発サーバ Development Server

Flaskはアプリケーションを開発サーバを使って走らせるrunコマンドを提供します。developmentモードでは、このサーバはインタラクティブなデバッガを提供し、コードが変更されたときは再読み込み(reload)します。 Flask provides a ``run`` command to run the application with a development server. In development mode, this server provides an interactive debugger and will reload when code is changed.

警告

本番環境へ展開(deploy)するときは、開発サーバは使わないでください。それはローカルでの開発の間だけ使うために意図されたものです。それは効率性、安定性、セキュリティを特別意識して設計されてはいません。 Do not use the development server when deploying to production. It is intended for use only during local development. It is not designed to be particularly efficient, stable, or secure.

展開(deploy)のオプションについては展開の選択肢(Deployment Options)を見てください。 See :doc:`/deploying/index` for deployment options.

コマンドライン Command Line

コマンドラインのスクリプトであるflask runは、開発サーバを走らせる推奨される方法です。flask runが全ての開発モードを有効にするには、FLASK_APP環境変数は自分のアプリケーションを指し、FLASK_ENV=developmentと環境変数が設定されることが必要です。 The ``flask run`` command line script is the recommended way to run the development server. It requires setting the ``FLASK_APP`` environment variable to point to your application, and ``FLASK_ENV=development`` to fully enable development mode.

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

この例ではインタラクティブなデバッガと再読み込み機能(reloader)を含む、開発環境を有効にし、それからhttp://localhost:5000でサーバを開始します。利用可能なオプションを見るにはflask run --helpを使用し、CLIの使用や設定についての指示(instructions)の詳細はコマンドライン・インタフェースを見てください。 This enables the development environment, including the interactive debugger and reloader, and then starts the server on http://localhost:5000/. Use ``flask run --help`` to see the available options, and :doc:`/cli` for detailed instructions about configuring and using the CLI.

注釈

Flask 1.0より前は、FLASK_ENV環境変数はサポートされておらず、デバッグモードを有効にするにはFLASK_DEBUG=1をexportする必要がありました。これは今でもデバッグモードの制御に使用可能ですが、上述した開発環境の設定の方が好ましいものです。 Prior to Flask 1.0 the ``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.

Address already in use

もし他のプログラムが既に5000番ポートを使用中であるときは、サーバを開始しようと試みたときにOSErrorを見ることになるでしょう。それは以下のメッセージの中の1つでしょう。 If another program is already using port 5000, you'll see an ``OSError`` when the server tries to start. It may have one of the following messages:

  • OSError: [Errno 98] Address already in use

  • OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions

他のプログラムを特定して停止するか、flask run --port 5001を使って違うポートを選んでください。 Either identify and stop the other program, or use ``flask run --port 5001`` to pick a different port.

ポートを使っているのがどのプロセスIDなのか特定するためにnetstatまたはlsofを使うことができ、それからOSの他のツールを使ってそのプロセスを止めてください。以下の例は、プロセスIDが6847のプロセスが5000番ポートを使っていることを示しています。 You can use ``netstat`` or ``lsof`` to identify what process id is using a port, then use other operating system tools stop that process. The following example shows that process id 6847 is using port 5000.

$ netstat -nlp | grep 5000
tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 6847/python

macOS Monterey以降は、自動的に5000番ポートを使うサービスを開始します。そのサービスを無効にするには、System Preferences、Sharingへ行き、「AirPlay Receiver」を無効にします。 macOS Monterey and later automatically starts a service that uses port 5000. To disable the service, go to System Preferences, Sharing, and disable "AirPlay Receiver".

遅延または先読みローディング(Lazy or Eager Loading) Lazy or Eager Loading

再読み込み機能(reloader)と一緒にflask runコマンドを使うとき、もし文法エラーまたはその他の初期化エラーをコードに導入してしまった場合でも、そのサーバーは走り続けようとします。サイトへアクセスすると、サーバがクラッシュするのではなく、エラーに対するインタラクティブなデバッガを見られるでしょう。この機能は「遅延読み込み(lazy loading)」と呼ばれています。 When using the ``flask run`` command with the reloader, the server will continue to run even if you introduce syntax errors or other initialization errors into the code. Accessing the site will show the interactive debugger for the error, rather than crashing the server. This feature is called "lazy loading".

もしflask runを呼んだとき文法エラーが既に存在している場合、サイトにアクセスされるまで待つのではなく、flask runコマンドは即座に失敗してトレースバックを表示します。サーバーがreload時はエラーを処理できるようにしながら、最初はエラーをより可視化させるために、これは意図されたものです。 If a syntax error is already present when calling ``flask run``, it will fail immediately and show the traceback rather than waiting until the site is accessed. This is intended to make errors more visible initially while still allowing the server to handle errors on reload.

この振る舞いを上書きして、reload時であっても、(エラーがあったときは)常に即座に失敗させるには、--eager-loadingオプションを渡します。最初に呼ばれたときであっても、常にサーバが走り続けるようにするには、--lazy-loadingを渡します。 To override this behavior and always fail immediately, even on reload, pass the ``--eager-loading`` option. To always keep the server running, even on the initial call, pass ``--lazy-loading``.

コードの中 In Code

flask runコマンドの代替手段として、開発サーバはPythonからFlask.run()を使っても開始できます。このメソッドはサーバーを制御するためにCLIのオプションと似た引数を取ります。CLIコマンドからの主な違いは、reload時にエラーがあればクラッシュすることです。 As an alternative to the ``flask run`` command, the development server can also be started from Python with the :meth:`Flask.run` method. This method takes arguments similar to the CLI options to control the server. The main difference from the CLI command is that the server will crash if there are errors when reloading.

デバッガとreloaderを有効にするために(Flask.runに)debug=Trueを渡すことができますが、まだFLASK_ENV=development環境変数は全ての開発モードを有効にするために必要です。 ``debug=True`` can be passed to enable the debugger and reloader, but the ``FLASK_ENV=development`` environment variable is still required to fully enable development mode.

mainのブロック内にFlask.runの呼び出しを置いてください。そうしないと、後で本番環境のサーバー内でアプリケーションをimportして走らせようとするときに、その呼び出しが邪魔をするでしょう。 Place the call in a main block, otherwise it will interfere when trying to import and run the application with a production server later.

if __name__ == "__main__":
    app.run(debug=True)
$ python hello.py