Skip to content

Commit 5057c10

Browse files
authored
Python SDK: Add a helper for constructing data-* attributes (#902)
* Add a helper for constructing data-* attributes * Automatically add appropriate case mods for data-computed and data-on * Fix usage as a dict * Change attribute generator name and import location * Allow keyword args to several attributes. Add docstrings for all attributes. * Add docstrings for all the modifiers * Simplify some of the constructors * Make sure user input is escaped if rendering to text directly * Clean up the code for generating js objects * Clean up docstrings * Make data.computed have the same interface as data.attr and data.class_ * Remove the string form of data.signals in favor of an 'expressions_' kwarg * Formatting * Accomodate aliased builds with the attribute generator * Only allow certain arguments as keywords Clean up some formatting * Don't require alias as a keyword argument to instantiate AttributeGenerator * Fix automatic pascal casing for namespaced signals * Update for the 1.0 RC * Improve typing for data-signals Let expressions_ mode work for nested signals * Update the readme to document new python SDK features * More readme tweaks
1 parent 686f603 commit 5057c10

File tree

3 files changed

+686
-14
lines changed

3 files changed

+686
-14
lines changed

sdk/python/README.md

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,33 @@
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+
The event generator can be used with any framework. There are also custom
12+
helpers included for the following frameworks:
13+
14+
* [Django](https://www.djangoproject.com/)
15+
* [FastAPI](https://fastapi.tiangolo.com/)
16+
* [FastHTML](https://fastht.ml/)
17+
* [Litestar](https://litestar.dev/)
18+
* [Quart](https://quart.palletsprojects.com/en/stable/)
19+
* [Sanic](https://sanic.dev/en/)
20+
* [Starlette](https://www.starlette.io/)
21+
22+
## Event Generation Helpers
1023

1124
To use `datastar-py`, import the SSE generator in your app and then use
1225
it in your route handler:
1326

1427
```python
1528
from datastar_py import ServerSentEventGenerator as SSE
1629

17-
# ... various app setup. The example below is for the Quart framework
30+
# ... various app setup.
31+
# The example below is for the Quart framework, and is only using the event generation helpers.
1832

1933
@app.route("/updates")
2034
async def updates():
@@ -32,12 +46,42 @@ async def updates():
3246
return response
3347
```
3448

35-
There are also a number of custom responses/helpers for various frameworks. Currently the following frameworks are supported:
49+
## Response Helpers
3650

37-
* [Django](https://www.djangoproject.com/)
38-
* [FastAPI](https://fastapi.tiangolo.com/)
39-
* [FastHTML](https://fastht.ml/)
40-
* [Litestar](https://litestar.dev/)
41-
* [Quart](https://quart.palletsprojects.com/en/stable/)
42-
* [Sanic](https://sanic.dev/en/)
43-
* [Starlette](https://www.starlette.io/)
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 Helpers
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+
```

sdk/python/src/datastar_py/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
from typing import Any
66

77
from .sse import SSE_HEADERS, ServerSentEventGenerator
8+
from .attributes import attribute_generator
89

9-
__all__ = ["ServerSentEventGenerator", "SSE_HEADERS"]
10+
__all__ = ["attribute_generator", "ServerSentEventGenerator", "SSE_HEADERS"]
1011

1112

1213
def _read_signals(

0 commit comments

Comments
 (0)