Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
95dc48b
Add a helper for constructing data-* attributes
gazpachoking May 20, 2025
098bbc9
Automatically add appropriate case mods for data-computed and data-on
gazpachoking May 20, 2025
0c1bbe0
Fix usage as a dict
gazpachoking May 20, 2025
30091d5
Change attribute generator name and import location
gazpachoking May 20, 2025
ffd0ea2
Allow keyword args to several attributes.
gazpachoking May 21, 2025
69711b9
Add docstrings for all the modifiers
gazpachoking May 21, 2025
f17fbb3
Simplify some of the constructors
gazpachoking May 21, 2025
24ddc94
Make sure user input is escaped if rendering to text directly
gazpachoking May 21, 2025
26c678f
Clean up the code for generating js objects
gazpachoking May 21, 2025
ff139c2
Clean up docstrings
gazpachoking May 21, 2025
ff80b1b
Make data.computed have the same interface as data.attr and data.class_
gazpachoking May 21, 2025
fd77743
Remove the string form of data.signals in favor of an 'expressions_' …
gazpachoking May 21, 2025
83953aa
Formatting
gazpachoking May 21, 2025
58823db
Accomodate aliased builds with the attribute generator
gazpachoking May 25, 2025
d0c7b86
Only allow certain arguments as keywords
gazpachoking May 25, 2025
87e60a6
Don't require alias as a keyword argument to instantiate AttributeGen…
gazpachoking May 27, 2025
3a717bf
Fix automatic pascal casing for namespaced signals
gazpachoking May 31, 2025
270b980
Update for the 1.0 RC
gazpachoking May 31, 2025
89dfcd1
Improve typing for data-signals
gazpachoking Jun 2, 2025
996cc9b
Merge branch 'develop' into attribute-helper
gazpachoking Jun 2, 2025
f1d2471
Update the readme to document new python SDK features
gazpachoking Jun 5, 2025
f47a8cc
More readme tweaks
gazpachoking Jun 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 57 additions & 13 deletions sdk/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@

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

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

`datastar-py` helps with the formatting of these responses, while also
providing helper functions for the different supported responses.
`datastar-py` has helpers for creating those responses, formatting the events,
reading signals from the frontend, and generating the data-* HTML attributes.

The event generator can be used with any framework. There are also custom
helpers included for the following frameworks:

* [Django](https://www.djangoproject.com/)
* [FastAPI](https://fastapi.tiangolo.com/)
* [FastHTML](https://fastht.ml/)
* [Litestar](https://litestar.dev/)
* [Quart](https://quart.palletsprojects.com/en/stable/)
* [Sanic](https://sanic.dev/en/)
* [Starlette](https://www.starlette.io/)

## Event Generation Helpers

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

```python
from datastar_py import ServerSentEventGenerator as SSE

# ... various app setup. The example below is for the Quart framework
# ... various app setup.
# The example below is for the Quart framework, and is only using the event generation helpers.

@app.route("/updates")
async def updates():
Expand All @@ -32,12 +46,42 @@ async def updates():
return response
```

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

* [Django](https://www.djangoproject.com/)
* [FastAPI](https://fastapi.tiangolo.com/)
* [FastHTML](https://fastht.ml/)
* [Litestar](https://litestar.dev/)
* [Quart](https://quart.palletsprojects.com/en/stable/)
* [Sanic](https://sanic.dev/en/)
* [Starlette](https://www.starlette.io/)
The response for the quart example above could be rewritten using the helpers:
```python
return await make_datastar_response(time_updates())
```

## Signal Helpers
The current state of the datastar signals is included by default in every
datastar request. A helper is included to load those signals for each
framework. `read_signals`

```python
from datastar_py.quart import read_signals

@app.route("/updates")
async def updates():
signals = await read_signals()
```

## Attribute Generation Helper
Datastar allows HTML generation to be done on the backend. datastar-py includes
a helper to generate data-* attributes in your HTML with IDE completion and
type checking. It can be used with many different HTML generation libraries.

```python
from datastar_py import attribute_generator as data

# htpy
button(data.on("click", "console.log('clicked')").debounce(1000).stop)["My Button"]
# FastHTML
Button("My Button", **data.on("click", "console.log('clicked')").debounce(1000).stop)
# After next release of FastHTML you don't have to unpack the datastar helpers e.g.
Button("My Button", data.on("click", "console.log('clicked')").debounce(1000).stop)
# f-strings
f"<button {data.on("click", "console.log('clicked')").debounce(1000).stop}>My Button</button>"
# Jinja, but no editor completion :(
<button {{data.on("click", "console.log('clicked')").debounce(1000).stop}}>My Button</button>
```
3 changes: 2 additions & 1 deletion sdk/python/src/datastar_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from typing import Any

from .sse import SSE_HEADERS, ServerSentEventGenerator
from .attributes import attribute_generator

__all__ = ["ServerSentEventGenerator", "SSE_HEADERS"]
__all__ = ["attribute_generator", "ServerSentEventGenerator", "SSE_HEADERS"]


def _read_signals(
Expand Down
Loading