Reflex Logo
Blog
Builder
Squares Vertical DocsSquares Vertical Docs

How to Build a Python Web App: Complete Tutorial

Set up your environment, build UI in Python, manage state, connect a database, and deploy with Reflex—full-stack web apps without writing JavaScript.

Tom Gotsman

Image for blog post: How to Build a Python Web App: Complete Tutorial

You know Python, but every web framework tutorial still makes you learn a frontend framework in JavaScript to build anything interactive. This python web application framework tutorial focuses on Reflex, which under the hood compiles your Python code into React components so you can build complete web apps without context-switching between languages. You'll set up your environment, build components, manage state, connect a database, and deploy to production while writing nothing but Python code.

TLDR:

  • Build full-stack Python web apps without JavaScript using Reflex's 60+ components and state management
  • Deploy production apps with one command (reflex deploy) to multi-region infrastructure in minutes
  • Connect databases, handle authentication, and manage routing entirely in readable Python code
  • Reflex powers 1M+ apps and is used by 40% of Fortune 500 companies for internal tools
  • On-premises deployment options meet enterprise compliance for compliance-focused industries

Before writing any code, verify Python 3.10 or higher is installed by running python --version in your terminal. If needed, download the latest version from python.org.

Next, create a virtual environment to isolate project dependencies and avoid version conflicts. In your project folder, run:

Your terminal prompt will change when active. Activate this environment each time you work on the project to keep dependencies contained and prevent debugging headaches.

Python's popularity in web development stems from its readability and extensive library ecosystem. 51% of developers globally use Python, making it the most widely adopted language for building web applications.

When choosing a Python web framework, you'll encounter three main categories:

  • Full-stack frameworks like Django provide everything from authentication to database management in one package, but still require JavaScript for interactive frontends.
  • Micro-frameworks like Flask offer flexibility and simplicity for backend APIs, yet again need separate frontend tech.
  • Traditional frameworks handle the backend while leaving you to build the frontend in React, Vue, or other JavaScript libraries.

Reflex breaks this pattern by letting you write both frontend and backend in pure Python. You don't context-switch between languages or manage separate codebases.

Reflex (Full-Stack Python)

  • Frontend language: Pure Python (compiles to React)
  • Backend language: Pure Python
  • Built-in UI components: 60+ components including buttons, forms, tables, charts, and data displays
  • State management: Python class-based state with automatic UI updates when state changes
  • Best use case: Full-stack apps where Python teams want to avoid JavaScript and maintain a single codebase

Django

  • Frontend language: Requires a separate JavaScript framework (React, Vue, etc.)
  • Backend language: Python
  • Built-in UI components: Admin interface only; frontend components require separate JavaScript libraries
  • State management: Backend session management; frontend state requires a JavaScript framework
  • Best use case: Content-heavy websites and traditional web apps where the interactive frontend is minimal or managed separately

Flask

  • Frontend language: Requires a separate JavaScript framework (React, Vue, etc.)
  • Backend language: Python
  • Built-in UI components: None; all UI components must be added through JavaScript libraries
  • State management: Backend session management; frontend state requires a JavaScript framework
  • Best use case: Lightweight APIs and microservices where the frontend is built as a separate application

With your virtual environment activated, install Reflex using pip:

Create a new directory for your project and move into it:

Run the initialization command:

Reflex generates a project structure with a single Python file containing your entire application. You'll see my_app.py with state management and UI components already defined, plus rxconfig.py for project settings and an assets folder for static files.

Start the development server:

Your browser opens to localhost:3000 showing your running app. The server includes fast refresh, so code changes appear instantly without manual reloading.

Abstract illustration of Python code structure becoming modular UI components in a blue and purple gradient.

Reflex provides 60+ built-in components that handle everything from simple text and buttons to complex data tables and charts. Each component is a Python function you import and call with keyword arguments. No HTML templates, no JSX syntax, just Python functions that return UI elements.

Here's how you create a basic button:

The component accepts text as the first argument and event handlers as keyword arguments. Behind the scenes, Reflex compiles Python to React components, but you never write or see the JavaScript.

Build complex layouts by nesting components inside container functions:

Style components using keyword arguments that map to CSS properties. The box component acts as a container with visual properties controlling appearance.

Abstract illustration of state management with nodes and bidirectional flow in blue and purple tones.

State in Reflex uses Python classes that inherit from rx.State. Variables are class attributes, and methods modify these variables. When a method changes a variable, Reflex updates every UI component displaying that value.

Counter example:

Connect state to UI components:

Event handlers run when users interact with components. The on_click, on_change, and on_submit arguments connect actions to state methods.

Reflex includes built-in database support through SQLAlchemy, letting you define models as Python classes. Each class attribute becomes a database column with automatic type mapping. Reflex handles table creation, migrations, and query generation without writing SQL.

Access database records inside your state class using the with rx.session() context manager:

This works with SQLite by default for local development. Switch to PostgreSQL or MySQL in production by updating the database URL in your config file.

Reflex handles routing through a function-based system where each page is a Python function decorated with @rx.page. Define routes by specifying the URL path:

Create flexible routes using square brackets for parameters. Access them through AppState.router.page.params:

Build navigation with rx.link components that connect pages without full reloads.

Reflex includes a theming system that controls colors, fonts, and spacing across your entire application. Set a theme once and every component inherits those styles automatically.

Switch between dark and light modes with a single line of code:

Users can toggle between modes at runtime by binding the appearance property to a state variable. The theme handles all color inversions and contrast adjustments.

Customize your theme by passing configuration options:

Apply CSS directly to components using keyword arguments that match CSS property names:

Snake case replaces hyphens in property names, making styles readable as Python code without separate CSS files.

Once your app is ready, deployment takes one command. The web development market reached $10.5 billion in 2026, making fast deployment critical for staying competitive.

Test locally with reflex run to catch errors. Verify all dependencies appear in requirements.txt and database connections work in production mode.

Deploy by running:

This packages your application, provisions infrastructure, and launches your app across a multi-region network. You'll receive a live URL within minutes, with deployment status, metrics, and logs available through the dashboard.

For organizations requiring on-premises deployment or VPC infrastructure, custom deployment configurations are available that meet enterprise compliance requirements while maintaining the same Python codebase you developed locally.

Reflex has powered over 1 million applications because it keeps everything in Python code your team can debug and extend. 40% of Fortune 500 companies use Reflex for internal tools and data applications. On-premises deployment options meet compliance requirements for healthcare, finance, and government sectors, while role-based access control lets you define granular permissions in Python code that security teams can audit. When systems behave unexpectedly, engineers read the Python source to diagnose issues without specialized frontend debugging tools.

This Python web app tutorial shows you can skip the JavaScript learning curve and build modern web apps entirely in Python. You control your UI, manage state, connect databases, and deploy to production without leaving your favorite language. The framework handles the complexity while you focus on building features. Give it a try and see what you can create.

The Unified Platform to Build and Scale Enterprise AppsDescribe your idea, and let AI transform it into a complete, production-ready Python web application.
Book a Demo
Try for free
CTA Card

Built with Reflex