Helper functions and classes for the Datastar library (https://data-star.dev/)
Project description
datastar-py
The datastar-py package provides backend helpers for the Datastar JS library.
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 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:
Event Generation Helpers
To use datastar-py, import the SSE generator in your app and then use
it in your route handler:
from datastar_py import ServerSentEventGenerator as SSE
# ... 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():
async def time_updates():
while True:
yield SSE.merge_fragments(
[f"""<span id="currentTime">{datetime.now().isoformat()}"""]
)
await asyncio.sleep(1)
yield SSE.merge_signals({"currentTime": f"{datetime.now().isoformat()}"})
await asyncio.sleep(1)
response = await make_response(time_updates(), SSE_HEADERS)
response.timeout = None
return response
Response Helpers
A datastar response consists of 0..N datastar events. There are response classes included to make this easy in all of the supported frameworks.
The following examples will work across all supported frameworks when the
response class is imported from the appropriate framework package.
e.g. from datastar_py.quart import DatastarResponse The containing functions
are not shown here, as they will differ per framework.
# 0 events, a 204
return DatastarResponse()
# 1 event
return DatastarResponse(ServerSentEventGenerator.merge_fragments("<div id='mydiv'></div>"))
# 2 events
return DatastarResponse([
ServerSentEventGenerator.merge_fragments("<div id='mydiv'></div>"),
ServerSentEventGenerator.merge_signals({"mysignal": "myval"}),
])
# N events, a long lived stream (for all frameworks but sanic)
async def updates():
while True:
yield ServerSentEventGenerator.merge_fragments("<div id='mydiv'></div>")
await asyncio.sleep(1)
return DatastarResponse(updates())
# A long lived stream for sanic
response = await datastar_respond(request)
# which is just a helper for the following
# response = await request.respond(DatastarResponse())
while True:
await response.send(ServerSentEventGenerator.merge_fragments("<div id='mydiv'></div>"))
await asyncio.sleep(1)
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
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.
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>
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file datastar_py-0.5.0.tar.gz.
File metadata
- Download URL: datastar_py-0.5.0.tar.gz
- Upload date:
- Size: 72.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59c01f5bc39b3d2434824d752b48bdf4414fb0a38804151b88e868f43348728e
|
|
| MD5 |
48f70f01419a41bcddfdeecd97328e7a
|
|
| BLAKE2b-256 |
e237827dd25f03589b1833a5ba3762f4164eb7dda710756710d13d2e9af1e81c
|
File details
Details for the file datastar_py-0.5.0-py3-none-any.whl.
File metadata
- Download URL: datastar_py-0.5.0-py3-none-any.whl
- Upload date:
- Size: 16.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b133c49c977c878e828bfb99c69fcab8996a127d4621ccc67fb5460fcd704d4
|
|
| MD5 |
7047eb6e600399376b6fad9dcecf9bf3
|
|
| BLAKE2b-256 |
faea90ba56b06fa462fe891574581eab8ced32227759d98d90d99efbb7c1f1ee
|