You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+6-143Lines changed: 6 additions & 143 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -105,153 +105,16 @@ If the `reflex` command is not on your PATH, run it through uv instead: `uv run
105
105
106
106
## 🫧 Example App
107
107
108
-
Let's go over an example: creating an image generation UI around [DALL·E](https://platform.openai.com/docs/guides/images/image-generation?context=node). For simplicity, we just call the [OpenAI API](https://platform.openai.com/docs/api-reference/authentication), but you could replace this with an ML model run locally.
109
-
110
-
108
+
Build an image generation app in Python with Reflex: define the UI, manage state in a class, and call an image model from an event handler.
111
109
112
110
<divalign="center">
113
-
<imgsrc="https://raw.githubusercontent.com/reflex-dev/reflex/main/docs/images/dalle.gif"alt="A frontend wrapper for DALL·E, shown in the process of generating an image."width="550" />
<img src="https://raw.githubusercontent.com/reflex-dev/reflex/main/docs/images/reflex-image-generation-app.png" alt="Preview of an image generation app built with Reflex" width="900">
114
+
</a>
115
+
</video>
114
116
</div>
115
117
116
-
117
-
118
-
Here is the complete code to create this. This is all done in one Python file!
119
-
120
-
```python
121
-
import reflex as rx
122
-
import openai
123
-
124
-
openai_client = openai.OpenAI()
125
-
126
-
127
-
classState(rx.State):
128
-
"""The app state."""
129
-
130
-
prompt =""
131
-
image_url =""
132
-
processing =False
133
-
complete =False
134
-
135
-
defget_image(self):
136
-
"""Get the image from the prompt."""
137
-
ifself.prompt =="":
138
-
return rx.window_alert("Prompt Empty")
139
-
140
-
self.processing, self.complete =True, False
141
-
yield
142
-
response = openai_client.images.generate(
143
-
prompt=self.prompt, n=1, size="1024x1024"
144
-
)
145
-
self.image_url = response.data[0].url
146
-
self.processing, self.complete =False, True
147
-
148
-
149
-
defindex():
150
-
return rx.center(
151
-
rx.vstack(
152
-
rx.heading("DALL-E", font_size="1.5em"),
153
-
rx.input(
154
-
placeholder="Enter a prompt..",
155
-
on_blur=State.set_prompt,
156
-
width="25em",
157
-
),
158
-
rx.button(
159
-
"Generate Image",
160
-
on_click=State.get_image,
161
-
width="25em",
162
-
loading=State.processing,
163
-
),
164
-
rx.cond(
165
-
State.complete,
166
-
rx.image(src=State.image_url, width="20em"),
167
-
),
168
-
align="center",
169
-
),
170
-
width="100%",
171
-
height="100vh",
172
-
)
173
-
174
-
175
-
# Add state and page to the app.
176
-
app = rx.App()
177
-
app.add_page(index, title="Reflex:DALL-E")
178
-
```
179
-
180
-
## Let's break this down.
181
-
182
-
<divalign="center">
183
-
<imgsrc="https://raw.githubusercontent.com/reflex-dev/reflex/main/docs/images/dalle_colored_code_example.png"alt="Explaining the differences between backend and frontend parts of the DALL-E app."width="900" />
184
-
</div>
185
-
186
-
### **Reflex UI**
187
-
188
-
Let's start with the UI.
189
-
190
-
```python
191
-
defindex():
192
-
return rx.center(...)
193
-
```
194
-
195
-
This `index` function defines the frontend of the app.
196
-
197
-
We use different components such as `center`, `vstack`, `input`, and `button` to build the frontend. Components can be nested within each other
198
-
to create complex layouts. And you can use keyword args to style them with the full power of CSS.
199
-
200
-
Reflex comes with [60+ built-in components](https://reflex.dev/docs/library) to help you get started. We are actively adding more components, and it's easy to [create your own components](https://reflex.dev/docs/wrapping-react/overview/).
201
-
202
-
### **State**
203
-
204
-
Reflex represents your UI as a function of your state.
205
-
206
-
```python
207
-
classState(rx.State):
208
-
"""The app state."""
209
-
210
-
prompt =""
211
-
image_url =""
212
-
processing =False
213
-
complete =False
214
-
```
215
-
216
-
The state defines all the variables (called vars) in an app that can change and the functions that change them.
217
-
218
-
Here the state is comprised of a `prompt` and `image_url`. There are also the booleans `processing` and `complete` to indicate when to disable the button (during image generation) and when to show the resulting image.
Within the state, we define functions called event handlers that change the state vars. Event handlers are the 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.
236
-
237
-
Our DALL·E app has an event handler, `get_image` which gets this image from the OpenAI API. Using `yield` in the middle of an event handler will cause the UI to update. Otherwise the UI will update at the end of the event handler.
238
-
239
-
### **Routing**
240
-
241
-
Finally, we define our app.
242
-
243
-
```python
244
-
app = rx.App()
245
-
```
246
-
247
-
We add a page from the root of the app to the index component. We also add a title that will show up in the page preview/browser tab.
248
-
249
-
```python
250
-
app.add_page(index, title="DALL-E")
251
-
```
252
-
253
-
You can create a multi-page app by adding more pages.
0 commit comments