Scrivere una funzione create_app
Costruisca e configuri l'app all'interno di una factory.
Scrivere una funzione create_app è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 🎯
Domande Frequenti
La lezione «Scrivere una funzione create_app» è gratuita?
Sì — il testo completo di «Scrivere una funzione create_app» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.
Cosa imparerò in «Scrivere una funzione create_app»?
Costruisca e configuri l'app all'interno di una factory. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Flask Academy?
Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Scrivere una funzione create_app»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Flask Academy?
Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Problemi con un oggetto app globale
- Scrivere una funzione create_app
- Inizializzare le estensioni con init_app
- Registrare i Blueprint nella factory