Write a create_app Function
Construct and configure the app inside a factory.
Write a create_app Function is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 🎯
Frequently asked questions
Is the “Write a create_app Function” lesson free?
Yes — the full text of “Write a create_app Function” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Write a create_app Function”?
Construct and configure the app inside a factory. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Write a create_app Function” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Problems with a Global app Object
- Write a create_app Function
- Init Extensions with init_app
- Register Blueprints in the Factory