#手順
stack overflowのDebugging flask with pdbを参考にしています。
-
screenなどを使って2つ端末を立ち上げておきます。以降端末1、端末2と書きます。
-
flaskアプリ上のデバッグを開始したい箇所に、次のコードを埋め込みます。
import pdb; pdb.set_trace()
-
端末1で、flaskのdevelopment用サーバをflask runにて起動します。下のようにFLASK_ENVを設定するとデバッグ出力モードになります。(参考:flask Development Server)
$ export FLASK_ENV=development $ flask run
-
この状態のまま、端末2で、curlコマンドで該当のアプリにアクセスします。
なお、curlの使い方は「curlコマンドの使い方 ~GET、POST、REST(json)の確認と、windowsでの利用手順」が分かりやすく参考になりました。 -
そうすると、端末2(curl側)では待機状態になっているはずです。このまま端末1(flask側)を見るとpdbに入っていますので、後は端末1側でpdbにてデバッグすることができます。
#環境
次でできました。難しい仕組みを使っていないので、広く活用できると思います。
flask: 1.0.2
python: 3.7.3
サーバ側のOS: linux(CentOS7)
#最後に
pdbはシンプルで便利なので、flaskアプリでもpdbでデバッグができて幸せでした。