0Pricing
Flask Academy · レッスン

グローバルなappオブジェクトの問題

モジュールレベルの単一appが制約になる理由を学びます

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

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

The Tempting Shortcut

Most tutorials create app at the top of a module and use it everywhere. It feels simple, but that single global object quietly limits how your project can grow. 🌱

One App, Forever

A module-level app is built the moment the file is imported. You get exactly one instance, with one fixed configuration, baked in for the whole process.

from flask import Flask
app = Flask(__name__)  # created on import

Hard to Test

Testing wants a fresh app per run, ideally with test settings. A global app is already configured, so tests inherit your dev config and bleed state between them.

No Per-Environment Config

Dev, test, and prod each need different settings. With one global app, swapping config means editing globals or relying on import-time tricks that are easy to get wrong.

Circular Import Traps

Other modules import app to register routes, and app imports them back. This two-way dependency creates circular imports that crash on startup. 😣

Extensions Bound Too Early

Calling things like db = SQLAlchemy(app) at import time locks the extension to one app, before you even know which config to use.

db = SQLAlchemy(app)  # bound too soon

Import Order Becomes Fragile

Because everything hangs off one global, the import order starts to matter. Move one line and a route silently fails to register.

Multiple Apps Are Impossible

Sometimes you want two app instances, like one for an admin panel. A single global app makes running more than one in the same process awkward at best.

Config Set Before You Know It

At import time you often do not yet know if this is a test or prod run. A global app forces config decisions before that answer exists.

The Pattern That Fixes It

The cure is the application factory: a function that builds and returns a fresh, fully configured app on demand instead of one frozen global.

A Tiny Preview

Instead of a global, you call a function. Each call hands back a clean app you can configure however the situation needs.

def create_app():
    app = Flask(__name__)
    return app

Quick Check

What is the core problem with a single module-level app object?

Recap

A global app is one frozen instance: hard to test, tough to configure per environment, and prone to circular imports. The factory pattern is the fix you will build next. 🚀

よくある質問

「グローバルなappオブジェクトの問題」レッスンは無料ですか?

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

「グローバルなappオブジェクトの問題」で何を学びますか?

モジュールレベルの単一appが制約になる理由を学びます ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「グローバルなappオブジェクトの問題」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. グローバルなappオブジェクトの問題
  2. create_app関数を書く
  3. init_appで拡張機能を初期化する
  4. ファクトリでBlueprintを登録する
← Flask Academyに戻る