Skip to content

Commit c49c841

Browse files
20 - Counting with Asycnio and Reflex
1 parent f65704e commit c49c841

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

full_stack_python/pages/contact.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
class ContactState(rx.State):
99
form_data: dict = {}
1010
did_submit: bool = False
11+
timeleft: int = 5
12+
13+
@rx.var
14+
def timeleft_label(self):
15+
if self.timeleft < 1:
16+
return "No time left"
17+
return f"{self.timeleft} seconds"
1118

1219
@rx.var
1320
def thank_you(self):
@@ -24,8 +31,17 @@ async def handle_submit(self, form_data: dict):
2431
await asyncio.sleep(2)
2532
self.did_submit = False
2633
yield
34+
35+
async def start_timer(self):
36+
while self.timeleft > 0:
37+
await asyncio.sleep(1)
38+
self.timeleft -= 1
39+
yield
2740

28-
@rx.page(route=navigation.routes.CONTACT_US_ROUTE)
41+
@rx.page(
42+
on_load=ContactState.start_timer,
43+
route=navigation.routes.CONTACT_US_ROUTE
44+
)
2945
def contact_page() -> rx.Component:
3046
my_form = rx.form(
3147
rx.vstack(
@@ -64,6 +80,7 @@ def contact_page() -> rx.Component:
6480
)
6581
my_child = rx.vstack(
6682
rx.heading("Contact Us", size="9"),
83+
rx.text(ContactState.timeleft_label),
6784
rx.cond(ContactState.did_submit, ContactState.thank_you, ""),
6885
rx.desktop_only(
6986
rx.box(

0 commit comments

Comments
 (0)