Skip to content

Commit 28d6472

Browse files
authored
fasthtml: use attribute generators in examples (#29)
* fasthtml: use attribute generators in examples
1 parent 16d6a06 commit 28d6472

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

examples/fasthtml/advanced.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from great_tables import GT
2323
from great_tables.data import reactions
2424

25+
from datastar_py import attribute_generator as data
2526
from datastar_py.fasthtml import DatastarResponse, ServerSentEventGenerator
2627

2728
######################################################################################################
@@ -42,7 +43,7 @@
4243
app, rt = fast_app(
4344
htmx=False,
4445
surreal=False,
45-
live=True,
46+
live=False,
4647
hdrs=(
4748
Script(
4849
type="module",
@@ -133,14 +134,12 @@ def index():
133134
),
134135
),
135136
# When the below request is in flight, $filtering becomes true, setting the aria-busy attribute
136-
Label({"data-attr:aria-busy": "$filtering"}, fr="filter")("Filter Compound"),
137+
Label(data.attr({"aria-busy": "$filtering"}), fr="filter")("Filter Compound"),
137138
# Bind the 'filter' signal to the value of this input, debouncing using Datastar modifier
138139
Input(
139-
{
140-
"data-on:input__debounce.250ms": f"@post('{table}')",
141-
"data-bind:filter": True,
142-
"data-indicator:filtering": True,
143-
},
140+
data.on("input", f"@post('{table}')").debounce("250ms"),
141+
data.bind("filter"),
142+
data.indicator("filtering"),
144143
id="filter",
145144
name="filter",
146145
),
@@ -177,12 +176,11 @@ async def _():
177176
async def reset():
178177
reset_and_hello = Div(id="myElement")(
179178
Button(
180-
{
181-
"data-on:click": f"@get('{hello}')",
182-
"data-indicator:resetting": True,
183-
"data-attr:aria-busy": "$resetting",
184-
"data-attr:disabled": "$resetting",
185-
},
179+
# Attributes can either be defined using the Datastar SDK's
180+
# attribute_generator or with dicts as in HELLO_BUTTON below
181+
data.on("click", f"@get('{hello}')"),
182+
data.indicator("resetting"),
183+
data.attr({"aria-busy": "$resetting", "disabled": "$resetting"}),
186184
type="reset",
187185
)("Reset"),
188186
Div("Hello!"),
@@ -200,7 +198,7 @@ async def _():
200198
Button(
201199
{
202200
"data-on:click": f"@get('{reset}')",
203-
"data-indicator:loading": True,
201+
"data-indicator": "loading",
204202
"data-attr:aria-busy": "$loading",
205203
"data-attr:disabled": "$loading",
206204
}

examples/fasthtml/simple.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
# datastar-py = { path = "../../" }
99
# ///
1010
import asyncio
11-
import json
1211
from datetime import datetime
1312

1413
# ruff: noqa: F403, F405
1514
from fasthtml.common import *
1615

16+
from datastar_py import attribute_generator as data
1717
from datastar_py.fasthtml import DatastarResponse, ServerSentEventGenerator, read_signals
1818

1919
app, rt = fast_app(
2020
htmx=False,
2121
surreal=False,
22-
live=True,
22+
live=False,
2323
hdrs=(
2424
Script(
2525
type="module",
@@ -39,15 +39,15 @@ async def index():
3939
return Titled(
4040
"Datastar FastHTML example",
4141
example_style,
42-
Body(data_signals=json.dumps({"currentTime": now}))(
42+
Body(data.signals({"currentTime": now}))(
4343
Div(cls="container")(
44-
Div(data_init="@get('/updates')", cls="time")(
44+
Div(data.init("@get('/updates')"), cls="time")(
4545
"Current time from element: ",
4646
Span(id="currentTime")(now),
4747
),
4848
Div(cls="time")(
4949
"Current time from signal: ",
50-
Span(data_text="$currentTime")(now),
50+
Span(data.text("$currentTime"))(now),
5151
),
5252
),
5353
),

0 commit comments

Comments
 (0)