Reflex Web Framework
Reflex is a full-stack web framework that allows developers to build web applications entirely in Python. It automatically generates a React-based frontend and manages backend logic, routing, and state. Currently at version 0.8.28.post1, Reflex maintains a rapid release cadence, frequently pushing new features and bug fixes.
Common errors
-
ModuleNotFoundError: No module named 'reflex'
cause The Reflex library is not installed in your current Python environment.fixRun `pip install reflex` in your active Python environment. -
reflex: command not found
cause The Reflex CLI executable is not in your system's PATH. This often happens if Reflex was installed in a virtual environment that is not activated, or if the installation was incomplete.fixEnsure your virtual environment is activated before running `reflex` commands. If you're not using a virtual environment, verify `pip install reflex` completed successfully and your PATH is correctly configured for Python scripts. -
Error: The backend server failed to start.
cause This generic error can indicate several issues, such as the default port (8000) being in use, a corrupt `reflex` project setup, or conflicts with other applications.fixTry changing the backend port with `reflex run --backend-port <new_port>`. If the issue persists, ensure no other process is using port 8000. For project corruption, consider `reflex init --clear-cache` or regenerating the project. -
Changes in my Python code are not updating in the browser.
cause The Reflex development server might not be correctly detecting file changes, or the browser cache is serving an old version of the frontend.fixFirst, ensure `reflex run` is still active and hasn't crashed. If it is, try hard refreshing your browser (Ctrl+Shift+R or Cmd+Shift+R). If the problem persists, stop the server and restart it with `reflex run --clear-cache`.
Warnings
- breaking Reflex was formerly known as Pynecone. Upgrading from Pynecone (versions < 0.5.0) to Reflex (0.5.0+) involves significant API changes and is not a direct in-place upgrade.
- gotcha Event handlers in Reflex must be references to methods/functions, not function calls. Forgetting this will cause the handler to execute immediately instead of on the event.
- gotcha Sometimes, frontend changes might not reflect immediately due to browser or Reflex's build cache. This can lead to frustration during development.
- deprecated The `app.compile()` method for deploying apps is deprecated. Application deployment is now managed automatically when calling `reflex deploy`.
Install
-
pip install reflex
Imports
- reflex
import reflex as rx
- State
from reflex import State
class State(rx.State):
Quickstart
import reflex as rx
class State(rx.State):
count: int = 0
def increment(self):
self.count += 1
def decrement(self):
self.count -= 1
def index():
return rx.center(
rx.vstack(
rx.heading(State.count, size="9"),
rx.hstack(
rx.button(
"Decrement",
color_scheme="red",
on_click=State.decrement,
),
rx.button(
"Increment",
color_scheme="green",
on_click=State.increment,
),
),
spacing="5",
),
width="100vw",
height="100vh",
)
app = rx.App(state=State)
app.add_page(index, route="/")