@@ -58,34 +58,54 @@ e.g. `from datastar_py.quart import DatastarResponse` The containing functions
5858are not shown here, as they will differ per framework.
5959
6060``` python
61+ from datastar_py import ServerSentEventGenerator as SSE
62+
6163# 0 events, a 204
6264return DatastarResponse()
6365# 1 event
64- return DatastarResponse(ServerSentEventGenerator .patch_elements(" <div id='mydiv'></div>" ))
66+ return DatastarResponse(SSE .patch_elements(" <div id='mydiv'></div>" ))
6567# 2 events
6668return DatastarResponse([
67- ServerSentEventGenerator .patch_elements(" <div id='mydiv'></div>" ),
68- ServerSentEventGenerator .patch_signals({" mysignal" : " myval" }),
69+ SSE .patch_elements(" <div id='mydiv'></div>" ),
70+ SSE .patch_signals({" mysignal" : " myval" }),
6971])
7072
71-
7273# N events, a long lived stream (for all frameworks but sanic)
7374async def updates ():
7475 while True :
75- yield ServerSentEventGenerator .patch_elements(" <div id='mydiv'></div>" )
76+ yield SSE .patch_elements(" <div id='mydiv'></div>" )
7677 await asyncio.sleep(1 )
77-
78-
7978return DatastarResponse(updates())
79+
8080# A long lived stream for sanic
8181response = await datastar_respond(request)
8282# which is just a helper for the following
8383# response = await request.respond(DatastarResponse())
8484while True :
85- await response.send(ServerSentEventGenerator .patch_elements(" <div id='mydiv'></div>" ))
85+ await response.send(SSE .patch_elements(" <div id='mydiv'></div>" ))
8686 await asyncio.sleep(1 )
8787```
8888
89+ ### Response Decorator
90+ To make returning a ` DatastarResponse ` simpler, there is a decorator
91+ ` datastar_response ` available that automatically wraps a function result in
92+ ` DatastarResponse ` . It works on async and regular functions and generator
93+ functions. The main use case is when using a generator function, as you can
94+ avoid a second generator function inside your response function. The decorator
95+ works the same for any of the supported frameworks, and should be used under
96+ any routing decorator from the framework.
97+
98+ ``` python
99+ from datastar_py.sanic import datastar_response, ServerSentEventGenerator as SSE
100+
101+ @app.get (' /my_route' )
102+ @datastar_response
103+ def my_route (request ):
104+ while True :
105+ yield SSE .patch_elements(" <div id='mydiv'></div>" )
106+ await asyncio.sleep(1 )
107+ ```
108+
89109## Signal Helpers
90110The current state of the datastar signals is included by default in every
91111datastar request. A helper is included to load those signals for each
@@ -117,4 +137,16 @@ Button("My Button", data.on("click", "console.log('clicked')").debounce(1000).st
117137f " <button { data.on(" click" , " console.log('clicked')" ).debounce(1000 ).stop} >My Button</button> "
118138# Jinja, but no editor completion :(
119139< button {{data.on(" click" , " console.log('clicked')" ).debounce(1000 ).stop}}> My Button< / button>
140+ ```
141+
142+ When using datastar with a different alias, you can instantiate the class yourself.
143+
144+ ``` python
145+ from datastar_py.attributes import AttributeGenerator
146+
147+ data = AttributeGenerator(alias = " data-star-" )
148+
149+ # htmy (htmy will transform _ into - unless the attribute starts with _, which will be stripped)
150+ data = AttributeGenerator(alias = " _data-" )
151+ html.button(" My Button" , ** data.on(" click" , " console.log('clicked')" ).debounce(" 1s" ).stop)
120152```
0 commit comments