Key takeaway
- Python Flask is a lightweight WSGI framework. Learn routes, templates, sessions, testing with pytest and how to deploy Flask to production safely.
Flask is a lightweight Python web framework that handles requests through the WSGI standard and leaves choices such as the database layer to you. The Flask documentation describes it as designed to make getting started quick and easy, with the ability to scale up to complex applications.
This guide is for developers and engineering leads who want a clear picture of how a Flask application is built, tested and deployed. It covers routing, templates, requests, sessions, databases, testing with pytest and production deployment, and ends with what changes when AI agents sit beside your services.
What is Flask and how does it differ from Django?
Flask depends on the Werkzeug WSGI toolkit, the Jinja template engine and the Click command-line toolkit, according to the Flask documentation. It supplies configuration and conventions with sensible defaults, and the documentation points to community-maintained extensions for further functionality. That small core is why developers call it a micro-framework.
Django sits at the other end of the range: a full-stack framework that ships many components for you. Flask suits services where you want to choose each part yourself, which is common when teams build microservices or small APIs.
| Question | Flask | Django |
|---|---|---|
| Starting point | Small core plus extensions | Full-stack framework with many built-in parts |
| Best fit | APIs, services, small to mid-size apps, custom stacks | Content-heavy sites and apps that want one integrated toolkit |
| Database layer | You choose, for example SQLAlchemy through Flask-SQLAlchemy | Bundled with the framework |
| Trade-off | More decisions for the team | Less flexibility in how parts fit together |
How does a Flask application work?
A Flask application is an instance of the Flask class. The quickstart shows the minimum: create the app with Flask(__name__), then use the route() decorator to bind a function to a URL. The function is a view, and its return value becomes the response. The association between a URL and the function that handles it is a route.
- Dynamic routes. URL parts can be variables, and converters such as
int,float,pathanduuidset the argument type. - HTTP methods. By default a route answers only GET. Pass
methodsto accept others, or use@app.post. If GET is present, Flask adds HEAD automatically, and OPTIONS is handled for you. - Templates. Flask configures Jinja for you and renders files from a
templatesfolder withrender_template. Automatic escaping is on, so HTML in a variable is escaped. - URL building.
url_for()builds URLs from function names, which keeps links correct when routes change. - Sessions. The
sessionobject stores per-user data in a cookie signed with your secret key. Keep that key secret and generate it randomly.
For larger projects, the Flask tutorial layout moves code from one file into a package with separate modules, a templates folder, a static folder and a tests directory. The same page says to use version control and to ignore files you did not write.
How do you run Flask locally?
Use flask --app hello run, and add --debug for reloading and an interactive debugger. The quickstart is blunt about the risk: the debugger allows executing arbitrary Python code from the browser, so do not run the development server or debugger in production. By default the server listens only on your own machine. The --host=0.0.0.0 option listens on all public IPs, which the documentation says is safe only if the debugger is disabled or you trust the users on your network.
How do you connect Flask to a database?
Flask has no required database layer. The Flask page on SQLAlchemy in Flask says many people prefer SQLAlchemy, and that the Flask-SQLAlchemy extension handles the configuration and is recommended for getting started quickly. The same page warns that some of its older examples use SQLAlchemy APIs that were deprecated or removed in version 2.0, so check the current SQLAlchemy documentation before copying code.
How do you test a Flask application?
The Flask testing guide uses pytest. Tests are functions starting with test_ in modules that start with test_, and fixtures in tests/conftest.py build the app and a test client. The test client makes requests to the application without running a live server, so you can call client.get() or client.post() and assert on the response. If you build the app with a factory function, set TESTING to true in the fixture.
Our guides to unit testing and test-driven development and unit testing in Python cover the method in more depth.
How do you deploy Flask to production?
The Flask documentation says not to use the development server in production: it is not designed to be particularly secure, stable or efficient. Flask is a WSGI application, so a WSGI server such as Gunicorn, Waitress, mod_wsgi or uWSGI runs it. A reverse proxy such as nginx or Apache httpd in front of the WSGI server is optional but often advisable, because a dedicated HTTP server may be safer, more efficient or more capable.
Managed platforms take over much of this work. The Flask documentation lists several, including PythonAnywhere, Google Cloud Run, AWS Elastic Beanstalk and Microsoft Azure. On Heroku, the Heroku Python guide uses a Procfile to declare the web process, a requirements.txt that the Python buildpack installs, and a Git push to the heroku remote. That guide covers a Django sample and recommends an Eco dynos plan subscription, so check current plans before you choose a host. Automate the build and test steps in a pipeline: see CI and CD pipeline for Python.
| Stage | What to use | Why |
|---|---|---|
| Local development | flask run --debug | Reloads on change and shows an interactive debugger |
| Tests | pytest with the test client | Requests run without a live server |
| Production process | Gunicorn or another WSGI server | The development server is not for production |
| Edge | nginx or Apache httpd as reverse proxy | Optional, often safer and more capable |
| Hosting | Managed platform or your own servers | Platforms handle servers, networking and domains |
How do AI agents change Flask work?
Flask services often sit between data and the people or systems that consume it. When AI agents call those endpoints, the response shape, status codes and error handling become inputs to automated decisions, so route tests and clear contracts matter more. Agents can also help write and run tests, review pull requests and keep pipelines moving. ElixirClaw is XenonStack's AI agents computing platform for building and running agent workflows around services like these. For model-heavy features, see machine learning model on Kubernetes and Apache Spark architecture.
What should you do first?
- Start with a single-file app and move to a package once routes, models and templates grow.
- Write pytest fixtures with a test client before adding the second route.
- Keep the secret key and database settings out of the code.
- Choose a WSGI server and host before launch, and never ship the debugger.
- Add a pipeline that runs your tests on every change.
To map one decision your agents make, and the data and policy it depends on, book a Context Workshop. See what happens in the workshop.
Frequently asked questions
What is Flask used for?
Flask is used to build web applications and APIs in Python. Its small core and extension model suit services where you want to choose each component yourself.
Is Flask a framework or a library?
Flask is a web framework. It depends on Werkzeug, Jinja and Click, and adds routing, request handling and templating on top.
Can I use the Flask development server in production?
No. The Flask documentation says the development server is intended only for local development and is not designed to be particularly secure, stable or efficient. Use a WSGI server such as Gunicorn.
How do I test a Flask app?
Use pytest with the Flask test client. The client makes requests to the application without running a live server, and fixtures in conftest.py set up the app and client.
Which database should I use with Flask?
Flask does not require one. Many teams use SQLAlchemy through the Flask-SQLAlchemy extension for relational data, and pick a different store when the data is loosely structured.
Follow XenonStack on LinkedIn: XenonStack on LinkedIn for new guides on agentic AI, data and platform engineering.
Ready to put AI agents to work? Explore ElixirClaw, the agentic OS platform to build, orchestrate and govern AI agents.
Explore the XenonStack platform family: ElixirData · ElixirClaw · ElixirHub · NexaStack · MetaSecure · Neural AI · AgentGRC · AgentSRE · AgentAnalyst · AgentInstruct · AgentRAI · AgentScrum · AgentSearch · AgentForce · AI Aviator · SAIF Aviator · CohortIQ · XenonStack AI · XenonStack UAE