create_app関数を書く
ファクトリ内でアプリを構築し、設定します
「create_app関数を書く」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Meet the Factory
An application factory is just a function named create_app that builds a fresh Flask app, configures it, and returns it. No globals required. 🏭
The Smallest Factory
At its core, create_app creates the app inside the function body and returns it. Everything you used to do at module level moves in here.
def create_app():
app = Flask(__name__)
return appLocal, Not Global
Notice the app now lives as a local variable. Each call produces a brand new instance, so nothing leaks between tests or environments.
Configure Inside
Set configuration on the local app before returning it. A common move is app.config.from_object to load a settings class.
def create_app():
app = Flask(__name__)
app.config.from_object("config.Dev")
return appAccept a Config Argument
Make the factory flexible by accepting a config parameter. Pass Dev, Test, or Prod settings depending on how the app is launched.
def create_app(config_class):
app = Flask(__name__)
app.config.from_object(config_class)
return appRegister Routes Inside
Define or import your routes within create_app so they attach to this specific instance, not a shared global.
def create_app():
app = Flask(__name__)
@app.route("/")
def home():
return "Hi"
return appWhere to Put It
Place create_app in your package init, like app/__init__.py. Importers then call the factory instead of grabbing a global.
How Flask Finds It
The dev server looks for a callable named create_app automatically. Setting FLASK_APP to your package is usually enough to launch.
FLASK_APP=app flask runA WSGI Entry Point
For production servers you expose one instance by calling the factory once in a small wsgi.py module.
from app import create_app
app = create_app()Testing Gets Easy
Tests just call create_app with test config and get an isolated app every time. No shared state, no surprises. ✅
app = create_app("config.Test")Keep It Focused
Treat create_app as an assembly line: create, configure, wire up, return. Resist stuffing business logic directly inside it.
Quick Check
What does a create_app factory function return?
Recap
You wrote create_app: a function that builds, configures, and returns a fresh Flask app. It replaces the global and makes config and testing simple. 🎯
よくある質問
「create_app関数を書く」レッスンは無料ですか?
はい。「create_app関数を書く」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「create_app関数を書く」で何を学びますか?
ファクトリ内でアプリを構築し、設定します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「create_app関数を書く」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- グローバルなappオブジェクトの問題
- create_app関数を書く
- init_appで拡張機能を初期化する
- ファクトリでBlueprintを登録する