Skip to content

Commit 3587991

Browse files
committed
Update the readme to document new python SDK features
1 parent 996cc9b commit 3587991

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

sdk/python/README.md

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22

33
The `datastar-py` package provides backend helpers for the [Datastar](https://data-star.dev) JS library.
44

5-
Datastar requires all backend responses to use SSE. This allows the backend to
6-
send any number of responses, from zero to inifinity.
5+
Datastar sends responses back to the browser using SSE. This allows the backend to
6+
send any number of events, from zero to infinity in response to a single request.
77

8-
`datastar-py` helps with the formatting of these responses, while also
9-
providing helper functions for the different supported responses.
8+
`datastar-py` has helpers for creating those responses, formatting the events,
9+
reading signals from the frontend, and generating the data-* HTML attributes.
10+
11+
## Event Generation Helpers
1012

1113
To use `datastar-py`, import the SSE generator in your app and then use
1214
it in your route handler:
1315

1416
```python
1517
from datastar_py import ServerSentEventGenerator as SSE
1618

17-
# ... various app setup. The example below is for the Quart framework
19+
# ... various app setup.
20+
# The example below is for the Quart framework, and is only using the event generation helpers.
1821

1922
@app.route("/updates")
2023
async def updates():
@@ -32,7 +35,10 @@ async def updates():
3235
return response
3336
```
3437

35-
There are also a number of custom responses/helpers for various frameworks. Currently the following frameworks are supported:
38+
## Response Helpers
39+
40+
There are also a number of custom responses/helpers for various frameworks.
41+
Currently, the following frameworks are supported:
3642

3743
* [Django](https://www.djangoproject.com/)
3844
* [FastAPI](https://fastapi.tiangolo.com/)
@@ -41,3 +47,41 @@ There are also a number of custom responses/helpers for various frameworks. Curr
4147
* [Quart](https://quart.palletsprojects.com/en/stable/)
4248
* [Sanic](https://sanic.dev/en/)
4349
* [Starlette](https://www.starlette.io/)
50+
51+
The response for the quart example above could be rewritten using the helpers:
52+
```python
53+
return await make_datastar_response(time_updates())
54+
```
55+
56+
## Signal Helper
57+
The current state of the datastar signals is included by default in every
58+
datastar request. A helper is included to load those signals for each
59+
framework. `read_signals`
60+
61+
```python
62+
from datastar_py.quart import read_signals
63+
64+
@app.route("/updates")
65+
async def updates():
66+
signals = await read_signals()
67+
```
68+
69+
## Attribute Generation Helper
70+
Datastar allows HTML generation to be done on the backend. datastar-py includes
71+
a helper to generate data-* attributes in your HTML with IDE completion and
72+
type checking. It can be used with many different HTML generation libraries.
73+
74+
```python
75+
from datastar_py import attribute_generator as data
76+
77+
# htpy
78+
button(data.on("click", "console.log('clicked')").debounce(1000).stop)["My Button"]
79+
# FastHTML
80+
Button("My Button", **data.on("click", "console.log('clicked')").debounce(1000).stop)
81+
# After next release of FastHTML you don't have to unpack the datastar helpers e.g.
82+
Button("My Button", data.on("click", "console.log('clicked')").debounce(1000).stop)
83+
# f-strings
84+
f"<button {data.on("click", "console.log('clicked')").debounce(1000).stop}>My Button</button>"
85+
# Jinja, but no editor completion :(
86+
<button {{data.on("click", "console.log('clicked')").debounce(1000).stop}}>My Button</button>
87+
```

0 commit comments

Comments
 (0)