0Pricing
Flask Academy · レッスン

スクリプトとシェルでコンテキストをプッシュする

app_contextを使ってリクエスト外でコードを実行します

「スクリプトとシェルでコンテキストをプッシュする」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Outside the Web

Sometimes you run Flask code with no request, like a script that seeds the database. There is no context, so the usual proxies fail.

The Telltale Error

Run such code and Flask raises RuntimeError: working outside of application context. It is telling you no app context is active.

Push It Yourself

Fix it by pushing one with app.app_context(). Wrap your code in a with block and current_app starts working again.

with app.app_context():
    print(current_app.name)

What the with Block Does

Entering the with block pushes the context, and leaving it pops the context. You never have to clean up by hand.

Now Extensions Work

Inside the block, extensions bound to the app behave normally. You can run db.create_all() or query models from a plain script.

with app.app_context():
    db.create_all()

Need a Fake Request?

To exercise code that reads request, push a test request context with test_request_context instead of an app context.

with app.test_request_context("/hello"):
    print(request.path)

The Flask Shell

The flask shell command opens a Python shell with an app context already pushed, so you can poke at your app interactively.

flask shell

CLI Commands Too

Custom flask commands you register also run inside an app context automatically, so seed and admin scripts just work.

@app.cli.command()
def seed():
    db.create_all()

Match the Right Context

Pick by need: use app_context when you only touch the app or database, and a request context when your code reads request data.

Keep It Scoped

Push contexts in a tight with block, not for the whole script. Smaller scope means clearer code and predictable teardown.

Why This Matters

Pushing contexts lets you reuse app logic in migrations, cron jobs, and tests, not just inside live web requests.

Quick Check

Let us nail the fix for the context error.

Recap

No request? Push one. Use app_context for app and database work, test_request_context for request data, and let flask shell handle it for you. 🎉

よくある質問

「スクリプトとシェルでコンテキストをプッシュする」レッスンは無料ですか?

はい。「スクリプトとシェルでコンテキストをプッシュする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「スクリプトとシェルでコンテキストをプッシュする」で何を学びますか?

app_contextを使ってリクエスト外でコードを実行します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「スクリプトとシェルでコンテキストをプッシュする」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. アプリコンテキストとリクエストコンテキスト
  2. current_appとgオブジェクト
  3. コンテキストローカルとスレッドセーフティ
  4. スクリプトとシェルでコンテキストをプッシュする
← Flask Academyに戻る