Eine create_app-Funktion schreiben
Erstellen und konfigurieren Sie die App innerhalb einer Factory.
Eine create_app-Funktion schreiben ist eine kostenlose Flask Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flask Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „Eine create_app-Funktion schreiben“ kostenlos?
Ja — der vollständige Text von „Eine create_app-Funktion schreiben“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flask Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Eine create_app-Funktion schreiben“?
Erstellen und konfigurieren Sie die App innerhalb einer Factory. Du übst Flask Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Flask Academy zu starten?
Keine Vorkenntnisse erforderlich. Flask Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Eine create_app-Funktion schreiben“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Flask Academy-Lektion Code schreiben und ausführen?
Ja. Jede Flask Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Probleme mit einem globalen app-Objekt
- Eine create_app-Funktion schreiben
- Extensions mit init_app initialisieren
- Blueprints in der Factory registrieren