Skip to content

Commit ca27185

Browse files
[ENG-4001] Doc bash fixes for UI section (#1056)
* Doc bash - ui section * Update conditional rendering docs * Update rendering iterables * Fix todo * Update pages docs * Assets section * address comments in docs/components/props.md * address comments in docs/pages/overview * Address rendering iterables comment and get docs running * fix chatapp link * show lambda function example as well * fix tests --------- Co-authored-by: Elijah <[email protected]>
1 parent 751ab10 commit ca27185

File tree

20 files changed

+635
-458
lines changed

20 files changed

+635
-458
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import reflex as rx
44

55
# Assets
66

7-
Static files such as images and stylesheets can be placed in `"assets/"` folder of the project. These files can be referenced within your app.
7+
Static files such as images and stylesheets can be placed in `assets/` folder of the project. These files can be referenced within your app.
88

99
```md alert
1010
# Assets are copied during the build process.
@@ -15,7 +15,7 @@ when running in production mode. The `assets/` folder should only be used for st
1515

1616
## Referencing Assets
1717

18-
To reference an image in the `"assets/"` simply pass the relative path as a prop.
18+
To reference an image in the `assets/` simply pass the relative path as a prop.
1919

2020
For example, you can store your logo in your assets folder:
2121

@@ -38,4 +38,4 @@ rx.image(src="/Reflex.svg", width="5em")
3838

3939
The favicon is the small icon that appears in the browser tab.
4040

41-
You can add a `"favicon.ico"` file to the `"assets/"` folder to change the favicon.
41+
You can add a `favicon.ico` file to the `assets/` folder to change the favicon.
Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +0,0 @@
1-
```python exec
2-
import reflex as rx
3-
```
4-
5-
# Conditional Props
6-
7-
Sometimes you want to set a prop based on a condition. You can use the `rx.cond` function to do this.
8-
9-
```python demo exec
10-
class PropCondState(rx.State):
11-
12-
value: int
13-
14-
@rx.event
15-
def set_end(self, value: list[int]):
16-
self.value = value[0]
17-
18-
19-
def cond_prop():
20-
return rx.slider(
21-
default_value=[50],
22-
on_value_commit=PropCondState.set_end,
23-
color_scheme=rx.cond(PropCondState.value > 50, "green", "pink"),
24-
width="100%",
25-
)
26-
```

docs/components/conditional_rendering.md

Lines changed: 21 additions & 229 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
```python exec
22
import reflex as rx
33

4-
from pcweb.pages.docs import vars, library
4+
from pcweb.pages.docs import library
5+
from pcweb.pages import docs
56
```
67

78
# Conditional Rendering
89

9-
We use the `cond` component to conditionally render components. The `cond` component acts in a similar way to a conditional (ternary) operator in python, acting in a similar fashion to an `if-else` statement.
10+
Recall from the [basics]({docs.getting_started.basics.path}) that we cannot use Python `if/else` statements when referencing state vars in Reflex. Instead, use the `rx.cond` component to conditionally render components or set props based on the value of a state var.
1011

1112
```md video https://youtube.com/embed/ITOZkzjtjUA?start=6040&end=6463
1213
# Video: Conditional Rendering
@@ -20,13 +21,7 @@ We use the `cond` component to conditionally render components. The `cond` compo
2021
rx.box(height="2em")
2122
```
2223

23-
Here is a simple example to show how by checking the value of the state var `show` we can render either `blue` text or `red` text.
24-
25-
The first argument to the `cond` component is the condition we are checking. Here the condition is the value of the state var boolean `show`.
26-
27-
If `show` is `True` then the 2nd argument to the `cond` component is rendered, in this case that is `rx.text("Text 1", color="blue")`.
28-
29-
If `show` is `False` then the 3rd argument to the `cond` component is rendered, in this case that is `rx.text("Text 2", color="red")`.
24+
Below is a simple example showing how to toggle between two text components by checking the value of the state var `show`.
3025

3126
```python demo exec
3227
class CondSimpleState(rx.State):
@@ -48,243 +43,40 @@ def cond_simple_example():
4843
)
4944
```
5045

51-
## Var Operations (negation)
52-
53-
You can use var operations with the `cond` component. To learn more generally about var operators check out [these docs]({vars.var_operations.path}). The logical operator `~` can be used to negate a condition. In this example we show that by negating the condition `~CondNegativeState.show` within the cond, we then render the `rx.text("Text 1", color="blue")` component when the state var `show` is negative.
54-
55-
```python demo exec
56-
class CondNegativeState(rx.State):
57-
show: bool = True
58-
59-
@rx.event
60-
def change(self):
61-
self.show = not (self.show)
62-
63-
64-
def cond_negative_example():
65-
return rx.vstack(
66-
rx.text(f"Value of state var show: {CondNegativeState.show}"),
67-
rx.button("Toggle", on_click=CondNegativeState.change),
68-
rx.cond(
69-
CondNegativeState.show,
70-
rx.text("Text 1", color="blue"),
71-
rx.text("Text 2", color="red"),
72-
),
73-
rx.cond(
74-
~CondNegativeState.show,
75-
rx.text("Text 1", color="blue"),
76-
rx.text("Text 2", color="red"),
77-
),
78-
)
79-
```
80-
81-
## Multiple Conditions
82-
83-
It is also possible to make up complex conditions using the `logical or` (|) and `logical and` (&) operators.
84-
85-
Here we have an example using the var operators `>=`, `<=`, `&`. We define a condition that if a person has an age between 18 and 65, including those ages, they are able to work, otherwise they cannot.
86-
87-
We could equally use the operator `|` to represent a `logical or` in one of our conditions.
88-
89-
```python demo exec
90-
import random
91-
92-
class CondComplexState(rx.State):
93-
age: int = 19
94-
95-
@rx.event
96-
def change(self):
97-
self.age = random.randint(0, 100)
98-
99-
100-
def cond_complex_example():
101-
return rx.vstack(
102-
rx.button("Toggle", on_click=CondComplexState.change),
103-
rx.text(f"Age: {CondComplexState.age}"),
104-
rx.cond(
105-
(CondComplexState.age >= 18) & (CondComplexState.age <=65),
106-
rx.text("You can work!", color="green"),
107-
rx.text("You cannot work!", color="red"),
108-
),
109-
)
110-
111-
```
112-
113-
## Reusing Cond
114-
115-
We can also reuse a `cond` component several times by defining it within a function that returns a `cond`.
116-
117-
In this example we define the function `render_item`. This function takes in an `item`, uses the `cond` to check if the item `is_packed`. If it is packed it returns the `item_name` with a `` next to it, and if not then it just returns the `item_name`.
118-
119-
```python demo exec
120-
import dataclasses
121-
122-
@dataclasses.dataclass
123-
class ToDoListItem:
124-
item_name: str
125-
is_packed: bool
126-
127-
class CondRepeatState(rx.State):
128-
to_do_list: list[ToDoListItem] = [
129-
ToDoListItem(item_name="Space suit", is_packed=True),
130-
ToDoListItem(item_name="Helmet", is_packed=True),
131-
ToDoListItem(item_name="Back Pack", is_packed=False),
132-
]
133-
134-
135-
def render_item(item: [str, bool]):
136-
return rx.cond(
137-
item.is_packed,
138-
rx.list_item(item.item_name + ''),
139-
rx.list_item(item.item_name),
140-
)
141-
142-
def packing_list():
143-
return rx.vstack(
144-
rx.text("Sammy's Packing List"),
145-
rx.list(rx.foreach(CondRepeatState.to_do_list, render_item)),
146-
)
147-
148-
```
149-
150-
## Nested Conditional
151-
152-
We can also nest `cond` components within each other to create more complex logic. In python we can have an `if` statement that then has several `elif` statements before finishing with an `else`. This is also possible in reflex using nested `cond` components. In this example we check whether a number is positive, negative or zero.
153-
154-
Here is the python logic using `if` statements:
155-
156-
```python
157-
number = 0
158-
159-
if number > 0:
160-
print("Positive number")
161-
162-
elif number == 0:
163-
print('Zero')
164-
else:
165-
print('Negative number')
166-
```
167-
168-
This reflex code that is logically identical:
169-
170-
```python demo exec
171-
import random
172-
173-
174-
class NestedState(rx.State):
175-
176-
num: int = 0
177-
178-
@rx.event
179-
def change(self):
180-
self.num = random.randint(-10, 10)
181-
182-
183-
def cond_nested_example():
184-
return rx.vstack(
185-
rx.button("Toggle", on_click=NestedState.change),
186-
rx.cond(
187-
NestedState.num > 0,
188-
rx.text(f"{NestedState.num} is Positive!", color="orange"),
189-
rx.cond(
190-
NestedState.num == 0,
191-
rx.text(f"{NestedState.num} is Zero!", color="blue"),
192-
rx.text(f"{NestedState.num} is Negative!", color="red"),
193-
)
194-
),
195-
)
196-
197-
```
198-
199-
Here is a more advanced example where we have three numbers and we are checking which of the three is the largest. If any two of them are equal then we return that `Some of the numbers are equal!`.
46+
If `show` is `True` then the first component is rendered (in this case the blue text). Otherwise the second component is rendered (in this case the red text).
20047

201-
The reflex code that follows is logically identical to doing the following in python:
48+
## Conditional Props
20249

203-
```python
204-
a = 8
205-
b = 10
206-
c = 2
207-
208-
if((a>b and a>c) and (a != b and a != c)):
209-
print(a, " is the largest!")
210-
elif((b>a and b>c) and (b != a and b != c)):
211-
print(b, " is the largest!")
212-
elif((c>a and c>b) and (c != a and c != b)):
213-
print(c, " is the largest!")
214-
else:
215-
print("Some of the numbers are equal!")
216-
```
50+
You can also set props conditionally using `rx.cond`. In this example, we set the `color` prop of a text component based on the value of the state var `show`.
21751

21852
```python demo exec
219-
import random
220-
221-
222-
class CNS(rx.State):
223-
# CNS: CondNestedState
224-
a: int = 8
225-
b: int = 10
226-
c: int = 2
227-
53+
class PropCondState(rx.State):
22854

55+
value: int
56+
22957
@rx.event
230-
def change(self):
231-
self.a = random.randint(0, 10)
232-
self.b = random.randint(0, 10)
233-
self.c = random.randint(0, 10)
58+
def set_end(self, value: list[int]):
59+
self.value = value[0]
23460

23561

236-
def cond_nested_example_2():
237-
return rx.vstack(
238-
rx.button("Toggle", on_click=CNS.change),
239-
rx.text(f"a: {CNS.a}, b: {CNS.b}, c: {CNS.c}"),
240-
rx.cond(
241-
((CNS.a > CNS.b) & (CNS.a > CNS.c)) & ((CNS.a != CNS.b) & (CNS.a != CNS.c)),
242-
rx.text(f"{CNS.a} is the largest!", color="green"),
243-
rx.cond(
244-
((CNS.b > CNS.a) & (CNS.b > CNS.c)) & ((CNS.b != CNS.a) & (CNS.b != CNS.c)),
245-
rx.text(f"{CNS.b} is the largest!", color="orange"),
246-
rx.cond(
247-
((CNS.c > CNS.a) & (CNS.c > CNS.b)) & ((CNS.c != CNS.a) & (CNS.c != CNS.b)),
248-
rx.text(f"{CNS.c} is the largest!", color="blue"),
249-
rx.text("Some of the numbers are equal!", color="red"),
250-
),
251-
),
252-
),
62+
def cond_prop():
63+
return rx.slider(
64+
default_value=[50],
65+
on_value_commit=PropCondState.set_end,
66+
color_scheme=rx.cond(PropCondState.value > 50, "green", "pink"),
67+
width="100%",
25368
)
254-
25569
```
25670

257-
## Cond used as a style prop
258-
259-
`Cond` can also be used to show and hide content in your reflex app. In this example, we have no third argument to the `cond` operator which means that nothing is rendered if the condition is false.
260-
261-
```python demo exec
262-
class CondStyleState(rx.State):
263-
show: bool = False
264-
img_url: str = "/preview.png"
26571

266-
@rx.event
267-
def change(self):
268-
self.show = not (self.show)
72+
## Var Operations
26973

74+
You can use [var operations]({docs.vars.var_operations.path}) with the `cond` component for more complex conditions. See the full [cond reference]({library.dynamic_rendering.cond.path}) for more details.
27075

271-
def cond_style_example():
272-
return rx.vstack(
273-
rx.button("Toggle", on_click=CondStyleState.change),
274-
rx.cond(
275-
CondStyleState.show,
276-
rx.image(
277-
src=CondStyleState.img_url,
278-
height="25em",
279-
width="25em",
280-
),
281-
),
282-
)
283-
```
28476

28577
## Multiple Conditional Statements
28678

287-
The `rx.match` component in Reflex provides a powerful alternative to`rx.cond` for handling multiple conditional statements and structural pattern matching. This component allows you to handle multiple conditions and their associated components in a cleaner and more readable way compared to nested `rx.cond` structures.
79+
The [`rx.match`]({library.dynamic_rendering.match.path}) component in Reflex provides a powerful alternative to`rx.cond` for handling multiple conditional statements and structural pattern matching. This component allows you to handle multiple conditions and their associated components in a cleaner and more readable way compared to nested `rx.cond` structures.
28880

28981
```python demo exec
29082
from typing import List

0 commit comments

Comments
 (0)