Skip to content

Commit a3fb25a

Browse files
authored
Merge branch 'main' into alek/stats
2 parents 41fd6ea + 20e7ee5 commit a3fb25a

File tree

5 files changed

+82
-51
lines changed

5 files changed

+82
-51
lines changed

docs/events/background_events.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,34 @@ def background_task_example():
104104
)
105105
```
106106

107+
## Terminating Background Tasks on Page Close or Navigation
108+
109+
Sometimes, background tasks may continue running even after the user navigates away from the page or closes the browser tab. To handle such cases, you can check if the websocket associated with the state is disconnected and terminate the background task when necessary.
110+
111+
The solution involves checking if the client_token is still valid in the app.event_namespace.token_to_sid mapping. If the session is lost (e.g., the user navigates away or closes the page), the background task will stop.
112+
113+
```python
114+
import asyncio
115+
import reflex as rx
116+
117+
class State(rx.State):
118+
@rx.event(background=True)
119+
async def loop_function(self):
120+
while True:
121+
if self.router.session.client_token not in app.event_namespace.token_to_sid:
122+
print("WebSocket connection closed or user navigated away. Stopping background task.")
123+
break
124+
125+
print("Running background task...")
126+
await asyncio.sleep(2)
127+
128+
129+
@rx.page(on_load=State.loop_function)
130+
def index():
131+
return rx.text("Hello, this page will manage background tasks and stop them when the page is closed or navigated away.")
132+
133+
```
134+
107135
## Task Lifecycle
108136

109137
When a background task is triggered, it starts immediately, saving a reference to
@@ -118,6 +146,9 @@ the circumstances that are undesirable. In the example above, the `_n_tasks`
118146
backend var is used to control whether `my_task` will enter the increment loop,
119147
or exit early.
120148

149+
150+
151+
121152
## Background Task Limitations
122153

123154
Background tasks mostly work like normal `EventHandler` methods, with certain exceptions:

pcweb/pages/pricing/calculator.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,17 @@ def compute_table() -> rx.Component:
106106
return rx.box(
107107
tabs.root(
108108
rx.box(
109-
rx.vstack(
109+
rx.box(
110110
rx.el.h3(
111111
"Compute Pricing",
112112
class_name="text-slate-12 text-3xl font-semibold",
113113
),
114114
rx.el.p(
115115
"Only pay when your app is being used, nothing more.",
116-
class_name="text-slate-9 text-lg font-semibold",
116+
class_name="text-slate-9 text-lg font-semibold text-center lg:text-start text-balance",
117117
),
118-
align_items="left",
119-
justify_content="center`",
120-
class_name="flex flex-col gap-1",
118+
class_name="flex flex-col gap-1 justify-center items-center lg:items-start lg:justify-start",
121119
),
122-
rx.spacer(),
123120
tabs.list(
124121
tabs.tab(
125122
"Per min",
@@ -131,7 +128,7 @@ def compute_table() -> rx.Component:
131128
),
132129
tabs.indicator(),
133130
),
134-
class_name="flex flex-row gap-2 items-center justify-end p-6 border-b border-slate-4",
131+
class_name="flex flex-col lg:flex-row gap-4 items-center justify-center lg:justify-between p-6 border-b border-slate-4",
135132
),
136133
tabs.panel(
137134
min_table("Cost / min", description=True),
@@ -190,5 +187,5 @@ def compute_table_base() -> rx.Component:
190187
def calculator_section() -> rx.Component:
191188
return rx.el.section(
192189
compute_table(),
193-
class_name="flex flex-col w-full max-w-[64.19rem] 2xl:border-x border-slate-4 2xl:border-b pb-[6rem] justify-center items-center",
190+
class_name="flex flex-col w-full max-w-[64.19rem] 2xl:border-x border-slate-4 2xl:border-t 2xl:border-b pb-[6rem] justify-center items-center",
194191
)

pcweb/pages/pricing/faq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def header() -> rx.Component:
2323
"Check FAQ",
2424
class_name="text-slate-9 text-3xl font-semibold",
2525
),
26-
class_name="flex items-center justify-between text-slate-11 flex-col pt-[5rem] pb-82xl:border-x border-slate-4",
26+
class_name="flex items-center justify-between text-slate-11 flex-col pt-[5rem] xl:border-x border-slate-4",
2727
)
2828

2929

@@ -130,5 +130,5 @@ def faq() -> rx.Component:
130130
class_name="max-w-[40rem] flex justify-center items-center flex-col mx-auto w-full gap-2",
131131
),
132132
sales_button(),
133-
class_name="flex flex-col gap-8 w-full max-w-[64.19rem] 2xl:border-x border-slate-4 2xl:border-b pb-[6rem] py-32",
133+
class_name="flex flex-col gap-8 w-full max-w-[64.19rem] 2xl:border-x border-slate-4 2xl:border-b pb-[6rem]",
134134
)

pcweb/pages/pricing/header.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,12 @@ def header() -> rx.Component:
66
return rx.box(
77
rx.el.h1(
88
"Pricing that scales with you",
9-
class_name="gradient-heading font-semibold text-4xl xl:text-5xl text-center",
9+
class_name="gradient-heading font-semibold text-3xl lg:text-5xl text-center",
1010
),
11-
rx.hstack(
12-
rx.el.a(
13-
rx.badge(
14-
"Build with AI",
15-
class_name="text-lg text-center text-wrap rounded-md bg-[--violet-9] flex items-center justify-center text-[#FCFCFD]",
16-
),
17-
href="https://reflex.build",
18-
),
19-
rx.el.a(
20-
rx.badge(
21-
"Deploy with Cloud",
22-
class_name="text-lg text-center text-wrap rounded-md bg-[--violet-9] flex items-center justify-center text-[#FCFCFD]",
23-
),
24-
href="https://cloud.reflex.dev",
25-
),
26-
rx.badge(
27-
"Scale with Enterprise",
28-
class_name="text-lg text-center text-wrap rounded-md bg-[--violet-9] flex items-center justify-center text-[#FCFCFD]",
29-
),
30-
gap="2rem",
11+
rx.el.p(
12+
"Build with AI. Deploy with Cloud. Scale with Enterprise",
13+
class_name="text-slate-9 text-lg lg:text-2xl font-semibold text-center",
3114
),
32-
class_name="flex flex-col gap-4 justify-center items-center max-w-[64.19rem] 2xl:border-x border-slate-4 w-full pb-16 "
33-
+ rx.cond(HostingBannerState.show_banner, "pt-[13rem]", "pt-[10rem]"),
15+
class_name="flex flex-col gap-2 justify-center items-center max-w-[64.19rem] 2xl:border-x border-slate-4 w-full pb-16 "
16+
+ rx.cond(HostingBannerState.show_banner, "pt-[11rem]", "pt-[8rem]"),
3417
)

pcweb/pages/pricing/plan_cards.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,17 @@ def card(
148148
return rx.box(
149149
rx.el.div(
150150
rx.el.h3(title, class_name="font-semibold text-slate-12 text-2xl"),
151-
rx.badge(price, color_scheme="gray", size="3") if price else rx.fragment(),
152-
class_name="flex items-center mb-2 gap-2",
151+
(
152+
rx.badge(
153+
price,
154+
color_scheme="gray",
155+
size="3",
156+
class_name="font-medium text-lg",
157+
)
158+
if price
159+
else rx.fragment()
160+
),
161+
class_name="flex items-center mb-2 gap-4",
153162
),
154163
rx.el.p(
155164
description, class_name="text-sm font-medium text-slate-9 mb-8 text-pretty"
@@ -159,12 +168,14 @@ def card(
159168
rx.el.li(
160169
rx.icon(feature[0], class_name="!text-slate-9", size=16),
161170
feature[1],
162-
rx.tooltip(
163-
rx.icon("info", class_name="!text-slate-9", size=12),
164-
content=feature[2],
165-
)
166-
if len(feature) == 3
167-
else "",
171+
(
172+
rx.tooltip(
173+
rx.icon("info", class_name="!text-slate-9", size=12),
174+
content=feature[2],
175+
)
176+
if len(feature) == 3
177+
else ""
178+
),
168179
class_name="text-sm font-medium text-slate-11 flex items-center gap-3",
169180
)
170181
for feature in features
@@ -204,23 +215,32 @@ def popular_card(
204215
grid(),
205216
rx.hstack(
206217
rx.el.h3(title, class_name="font-semibold text-slate-12 text-2xl"),
207-
rx.badge(price, color_scheme="violet", size="3")
208-
if price
209-
else rx.fragment(),
210-
class_name="flex items-center mb-2 gap-2",
218+
(
219+
rx.badge(
220+
price,
221+
color_scheme="violet",
222+
size="3",
223+
class_name="font-medium text-lg",
224+
)
225+
if price
226+
else rx.fragment()
227+
),
228+
class_name="flex items-center mb-2 gap-4",
211229
),
212230
rx.el.p(description, class_name="text-sm font-medium text-slate-9 mb-8"),
213231
rx.el.ul(
214232
*[
215233
rx.el.li(
216234
rx.icon(feature[0], class_name="!text-violet-9", size=16),
217235
feature[1],
218-
rx.tooltip(
219-
rx.icon("info", class_name="!text-slate-9", size=12),
220-
content=feature[2],
221-
)
222-
if len(feature) == 3
223-
else "",
236+
(
237+
rx.tooltip(
238+
rx.icon("info", class_name="!text-slate-9", size=12),
239+
content=feature[2],
240+
)
241+
if len(feature) == 3
242+
else ""
243+
),
224244
class_name="text-sm font-medium text-slate-11 flex items-center gap-3",
225245
)
226246
for feature in features

0 commit comments

Comments
 (0)