Reflex was created with the following goals:
Pure Python
Use Python for everything. Don't worry about learning a new language.
Easy to Learn
Build and share your first app in minutes. No webdev experience required.
Full Flexibility
Remain as flexible as traditional web frameworks. Reflex is easy to get started with, but powerful enough for advanced use cases.
Build anything from small data science apps to large, multi-page websites.
This entire site was built and deployed with Reflex!
Batteries Included
No need to reach for a bunch of different tools. Reflex handles the frontend, backend, and deployment of your app.
Let's go over a simple counter app to explore the basics of Reflex.
Here is the complete code to create this.
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.hstack(
rx.button(
"Decrement",
color_scheme="red",
border_radius="1em",
on_click=State.decrement,
),
rx.heading(State.count, font_size="2em"),
rx.button(
"Increment",
color_scheme="green",
border_radius="1em",
on_click=State.increment,
),
)
app = rx.App()
app.add_page(index)
app.compile()
Let's break this counter example down.
import reflex as rx
We begin by importing the library. All Reflex functions and classes begin with the rx.
prefix.
class State(rx.State):
count: int = 0
The state defines all the variables (called vars) in an app that can change, as well as the functions that change them.
Here our state has a single var, count
, which holds the current value of the counter. We initialize it to 0
.
def increment(self):
self.count += 1
def decrement(self):
self.count -= 1
Within the state, we define functions, called event handlers, that change the state vars.
Event handlers are the only way that we can modify the state in Reflex. They can be called in response to user actions, such as clicking a button or typing in a text box. These actions are called events.
Our counter app has two event handlers, increment
and decrement
.
def index():
return rx.hstack(
rx.button(
"Decrement",
color_scheme="red",
border_radius="1em",
on_click=State.decrement,
),
rx.heading(State.count, font_size="2em"),
rx.button(
"Increment",
color_scheme="green",
border_radius="1em",
on_click=State.increment,
),
)
This function defines the frontend of the app.
We use different components such as rx.hstack
, rx.button
, and rx.heading
to build the frontend. Components can be nested to create complex layouts, and can be styled using the full power of CSS.
Reflex comes with 50+ built-in components to help you get started. We are actively adding more components, plus it's easy to wrap your own React components.
rx.heading(State.count, font_size="2em"),
Components can reference the app's state vars. The rx.heading
component displays the current value of the counter by referencing State.count
. All components that reference state will reactively update whenever the state changes.
rx.button(
"Decrement",
color_scheme="red",
border_radius="1em",
on_click=State.decrement,
),
Components interact with the state by binding events triggers to event handlers. For example, on_click
is an event that is triggered when a user clicks a component.
The first button in our app binds its on_click
event to the State.decrement
event handler, and the second button binds its to the State.increment
handler.
app = rx.App()
app.add_page(index)
Next we define our app and add the counter component to the base route.
app.compile()
Finally, we compile our app, and we are ready to run it.
And that's it! We've created an entire frontend and backend in less than 30 lines of code. From here we can continue developing or deploy it to the web in a single command.