-
Notifications
You must be signed in to change notification settings - Fork 231
How to build python app blog #1786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+287
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,286 @@ | ||
| --- | ||
| author: Tom Gotsman | ||
| date: 2026-03-20 | ||
| title: "How to Build a Python Web App: Complete Tutorial" | ||
| description: "Set up your environment, build UI in Python, manage state, connect a database, and deploy with Reflex—full-stack web apps without writing JavaScript." | ||
| image: /blog/how-to-build-python-web-app-1.webp | ||
| tag: Builder | ||
| meta: [ | ||
| {"name": "keywords", "content": "Python web app tutorial, Reflex Python, full-stack Python, build web app Python, Python web framework 2026, reflex deploy, Python without JavaScript, SQLAlchemy Reflex"} | ||
| ] | ||
| faq: [ | ||
| {"question": "How long does it take to build and deploy a Python web app with Reflex?", "answer": "You can create a working app in minutes using reflex init and have it deployed to production with a single reflex deploy command. The entire process from installation to live URL typically takes under 15 minutes for simple applications."}, | ||
| {"question": "What's the main difference between Reflex and traditional frameworks like Django or Flask?", "answer": "Django and Flask handle only the backend while requiring separate JavaScript frameworks for interactive frontends, meaning you manage two codebases in different languages. Reflex lets you write both frontend and backend in pure Python within a single codebase, eliminating the need for JavaScript knowledge entirely."}, | ||
| {"question": "Can I connect my Reflex app to an existing database?", "answer": "Yes, Reflex includes built-in SQLAlchemy support that works with SQLite by default and can connect to PostgreSQL, MySQL, or other databases by updating the database URL in your config file. No SQL writing required as models are defined as Python classes."}, | ||
| {"question": "When should I use Reflex Build versus other AI code generation tools like Lovable or Replit?", "answer": "Choose Reflex Build when you need maintainable code your Python team can debug and modify. Unlike Lovable or Replit that generate standalone JavaScript applications, Reflex Build outputs readable Python code using the framework's 60+ components. When something breaks, your team reads Python instead of reverse-engineering generated JavaScript. You get AI speed with Python maintainability, plus on-premises deployment and role-based access control for enterprise needs."}, | ||
| {"question": "Do I need to know React or JavaScript to build interactive web interfaces with Reflex?", "answer": "No, Reflex compiles Python code to React components behind the scenes, so you never write or see JavaScript. You build UI using 60+ Python functions for components like buttons, forms, and data tables, styling them with keyword arguments that match CSS properties."} | ||
| ] | ||
| --- | ||
|
|
||
| ```python exec | ||
| import reflex as rx | ||
| from reflex_image_zoom import image_zoom | ||
| from pcweb.constants import REFLEX_ASSETS_CDN | ||
| ``` | ||
|
|
||
| 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](https://reflex.dev/) 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 | ||
|
|
||
| ## Setting Up Your Python Development Environment | ||
|
|
||
| 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](https://www.python.org/). | ||
|
|
||
| Next, create a virtual environment to isolate project dependencies and avoid version conflicts. In your project folder, run: | ||
|
|
||
| ```bash | ||
| python -m venv venv | ||
| source venv/bin/activate # On Windows: venv\Scripts\activate | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| ## Understanding Python Web Frameworks in 2026 | ||
|
|
||
| Python's popularity in web development stems from its readability and extensive library ecosystem. [51% of developers globally use Python](https://www.integrate.io/blog/python-etl-framework-usage-trends/), making it the most widely adopted language for building web applications. | ||
|
|
||
| When [choosing a Python web framework](https://reflex.dev/blog/top-python-web-frameworks-2026/), you'll encounter three main categories: | ||
|
|
||
| - [Full-stack frameworks like Django](https://reflex.dev/blog/python-comparison/) 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 | ||
|
|
||
| ## Installing and Initializing Your First Reflex Project | ||
|
|
||
| With your virtual environment activated, install Reflex using pip: | ||
|
|
||
| ```bash | ||
| pip install reflex | ||
| ``` | ||
|
|
||
| Create a new directory for your project and move into it: | ||
|
|
||
| ```bash | ||
| mkdir my_app | ||
| cd my_app | ||
| ``` | ||
|
|
||
| Run the initialization command: | ||
|
|
||
| ```bash | ||
| reflex init | ||
| ``` | ||
|
|
||
| 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: | ||
|
|
||
| ```bash | ||
| reflex run | ||
| ``` | ||
|
|
||
| Your browser opens to `localhost:3000` showing your running app. The server includes fast refresh, so code changes appear instantly without manual reloading. | ||
|
|
||
| ## Building UI Components with Pure Python | ||
|
|
||
| ```python eval | ||
| rx.el.div(image_zoom(rx.image(src=f"{REFLEX_ASSETS_CDN}blog/how-to-build-python-web-app-2.webp", border_radius="10px", alt="Abstract illustration of Python code structure becoming modular UI components in a blue and purple gradient.")), class_name="mb-4") | ||
| ``` | ||
|
|
||
| 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: | ||
|
|
||
| ```python | ||
| import reflex as rx | ||
|
|
||
| rx.button("Click me", on_click=handle_click) | ||
| ``` | ||
|
|
||
| The component accepts text as the first argument and event handlers as keyword arguments. Behind the scenes, [Reflex compiles Python to React components](https://reflex.dev/blog/reflex-architecture/), but you never write or see the JavaScript. | ||
|
|
||
| Build complex layouts by nesting components inside container functions: | ||
|
|
||
| ```python | ||
| def user_card(): | ||
| return rx.box( | ||
| rx.heading("Welcome Back"), | ||
| rx.text("Your dashboard is ready"), | ||
| rx.button("Get Started"), | ||
| padding="20px", | ||
| border_radius="8px", | ||
| box_shadow="lg" | ||
| ) | ||
| ``` | ||
|
|
||
| Style components using keyword arguments that map to CSS properties. The `box` component acts as a container with visual properties controlling appearance. | ||
|
|
||
| ## Managing Application State and Event Handlers | ||
|
|
||
| ```python eval | ||
| rx.el.div(image_zoom(rx.image(src=f"{REFLEX_ASSETS_CDN}blog/how-to-build-python-web-app-3.webp", border_radius="10px", alt="Abstract illustration of state management with nodes and bidirectional flow in blue and purple tones.")), class_name="mb-4") | ||
| ``` | ||
|
|
||
| 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: | ||
|
|
||
| ```python | ||
| class CounterState(rx.State): | ||
| count: int = 0 | ||
|
|
||
| def increment(self): | ||
| self.count += 1 | ||
|
|
||
| def decrement(self): | ||
| self.count -= 1 | ||
| ``` | ||
|
|
||
| Connect state to UI components: | ||
|
|
||
| ```python | ||
| def counter(): | ||
| return rx.box( | ||
| rx.text(f"Count: {CounterState.count}"), | ||
| rx.button("Add", on_click=CounterState.increment), | ||
| rx.button("Subtract", on_click=CounterState.decrement) | ||
| ) | ||
| ``` | ||
|
|
||
| Event handlers run when users interact with components. The `on_click`, `on_change`, and `on_submit` arguments connect actions to state methods. | ||
|
|
||
| ## Connecting Your Web App to a Database | ||
|
|
||
| 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. | ||
|
|
||
| ```python | ||
| class User(rx.Model, table=True): | ||
| email: str | ||
| username: str | ||
| created_at: str | ||
| ``` | ||
|
|
||
| Access database records inside your state class using the `with rx.session()` context manager: | ||
|
|
||
| ```python | ||
| class AppState(rx.State): | ||
| users: list[User] = [] | ||
|
|
||
| def load_users(self): | ||
| with rx.session() as session: | ||
| self.users = session.exec(User).all() | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| ## Building Multi-Page Applications with Routing | ||
|
|
||
| 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: | ||
|
|
||
| ```python | ||
| @rx.page(route="/") | ||
| def home(): | ||
| return rx.box(rx.heading("Home Page")) | ||
|
|
||
| @rx.page(route="/about") | ||
| def about(): | ||
| return rx.box(rx.heading("About Us")) | ||
| ``` | ||
|
|
||
| Create flexible routes using square brackets for parameters. Access them through `AppState.router.page.params`: | ||
|
|
||
| ```python | ||
| @rx.page(route="/user/[username]") | ||
| def user_profile(): | ||
| return rx.box(rx.text(f"Profile: {AppState.router.page.params.username}")) | ||
carlosabadia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| Build navigation with `rx.link` components that connect pages without full reloads. | ||
|
|
||
| ## Styling and Theming Your Web App | ||
|
|
||
| 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: | ||
|
|
||
| ```python | ||
| rx.theme(appearance="dark") | ||
| ``` | ||
|
|
||
| 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: | ||
|
|
||
| ```python | ||
| rx.theme( | ||
| accent_color="blue", | ||
| gray_color="slate", | ||
| radius="large" | ||
| ) | ||
| ``` | ||
|
|
||
| Apply CSS directly to components using keyword arguments that match CSS property names: | ||
|
|
||
| ```python | ||
| rx.box( | ||
| rx.text("Styled Text"), | ||
| background_color="#f0f0f0", | ||
| padding="20px", | ||
| border="1px solid #ddd" | ||
| ) | ||
| ``` | ||
|
|
||
| Snake case replaces hyphens in property names, making styles readable as Python code without separate CSS files. | ||
|
|
||
| ## Deploying Your Python Web App to Production | ||
|
|
||
| Once your app is ready, [deployment takes one command](https://reflex.dev/hosting/). The [web development market reached $10.5 billion](https://www.esparkinfo.com/web-development/statistics) 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: | ||
|
|
||
| ```bash | ||
| reflex deploy | ||
| ``` | ||
|
|
||
| This packages your application, provisions infrastructure, and [launches your app across a multi-region network](https://reflex.dev/blog/reflex-cloud/). You'll receive a live URL within minutes, with deployment status, metrics, and logs available through the dashboard. | ||
|
|
||
| For organizations requiring [on-premises deployment](https://reflex.dev/blog/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. | ||
|
|
||
| ## Building Production Apps with Reflex | ||
|
|
||
| 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. | ||
|
|
||
| ## Final Thoughts on Python Web Application Development | ||
|
|
||
| This [Python web app tutorial](https://reflex.dev/) 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.