@@ -21,50 +21,65 @@ helpers included for the following frameworks:
21
21
* [ Sanic] ( https://sanic.dev/en/ )
22
22
* [ Starlette] ( https://www.starlette.io/ )
23
23
24
- ## Event Generation Helpers
24
+ Framework-specific helpers are kept in their own packages. e.g. ` datastar_py.quart `
25
+ Make sure to use the helpers from the package of the framework you are using.
25
26
26
- To use ` datastar-py ` , import the SSE generator in your app and then use
27
- it in your route handler:
27
+ Here is a full example using the quart framework showing many of the features
28
+ available in this package.
28
29
29
30
``` python
30
31
import asyncio
31
-
32
- from datastar_py import ServerSentEventGenerator as SSE
33
- from datastar_py.sse import SSE_HEADERS
34
- from quart import Quart, make_response
35
32
from datetime import datetime
36
33
34
+ from datastar_py import ServerSentEventGenerator as SSE , attribute_generator as data
35
+ from datastar_py.quart import datastar_response, read_signals
36
+ from quart import Quart
37
+
37
38
app = Quart(__name__ )
38
39
39
40
# Import frontend library via Content Distribution Network, create targets for Server Sent Events
40
41
@app.route (" /" )
41
42
def index ():
42
- return '''
43
- <script type="module" src="https://cdn.jsdelivr.net/gh/starfederation/datastar@main/bundles/datastar.js"></script>
44
- <span data-on-load="@get('/updates')" id="currentTime"></span><br><span data-text="$currentTime"></div>
45
- '''
43
+ return f """
44
+ <html>
45
+ <head>
46
+ <script type="module" src="https://cdn.jsdelivr.net/gh/starfederation/datastar@main/bundles/datastar.js"></script>
47
+ </head>
48
+ <body { data.on_load(" @get('/updates')" )} >
49
+ <span id="currentTime"></span><br>
50
+ <span data-text="$currentTime"></span>
51
+ </body>
52
+ </html>
53
+ """
46
54
47
55
48
56
@app.route (" /updates" )
57
+ @datastar_response
49
58
async def updates ():
50
- async def time_updates ():
51
- while True :
52
- yield SSE .patch_elements(
53
- f """ <span id="currentTime"> { datetime.now().isoformat()} """
54
- )
55
- await asyncio.sleep(1 )
56
- yield SSE .patch_signals({" currentTime" : f " { datetime.now().isoformat()} " })
57
- await asyncio.sleep(1 )
58
-
59
- response = await make_response(time_updates(), SSE_HEADERS )
60
- response.timeout = None
61
- return response
59
+ # Retrieve a dictionary with the current state of the signals from the frontend
60
+ signals = await read_signals()
61
+ # Alternate updating an element from the backend, and updating a signal from the backend
62
+ while True :
63
+ yield SSE .patch_elements(
64
+ f """ <span id="currentTime"> { datetime.now().isoformat()} """
65
+ )
66
+ await asyncio.sleep(1 )
67
+ yield SSE .patch_signals({" currentTime" : f " { datetime.now().isoformat()} " })
68
+ await asyncio.sleep(1 )
62
69
63
70
64
71
app.run()
65
72
```
66
73
67
- The example above is for the Quart framework, and is only using the event generation helpers.
74
+ Starting examples for each framework can be found in the [ examples] ( /examples )
75
+ directory.
76
+
77
+ ## Event Generation Helpers
78
+
79
+ This helper is used to generate the actual events that are sent over SSE. They
80
+ are just text blobs that can be sent using any framework. These can even be
81
+ used by frameworks not directly supported in this library if you set up the
82
+ headers of the SSE response yourself.
68
83
69
84
## Response Helpers
70
85
@@ -77,7 +92,7 @@ e.g. `from datastar_py.quart import DatastarResponse` The containing functions
77
92
are not shown here, as they will differ per framework.
78
93
79
94
``` python
80
- # per framework Response import, eg :
95
+ # per framework Response import. (Replace 'fastapi' with your framework.) e.g. :
81
96
# from datastar_py.fastapi import DatastarResponse
82
97
from datastar_py import ServerSentEventGenerator as SSE
83
98
@@ -98,7 +113,6 @@ def two_event():
98
113
])
99
114
100
115
# N events, a long lived stream (for all frameworks but sanic)
101
-
102
116
@app.get (" /updates" )
103
117
async def updates ():
104
118
async def _ ():
@@ -128,11 +142,12 @@ works the same for any of the supported frameworks, and should be used under
128
142
any routing decorator from the framework.
129
143
130
144
``` python
145
+ # Import the decorator from the package specific to your framework
131
146
from datastar_py.sanic import datastar_response, ServerSentEventGenerator as SSE
132
147
133
148
@app.get (' /my_route' )
134
149
@datastar_response
135
- def my_route (request ):
150
+ async def my_route (request ):
136
151
while True :
137
152
yield SSE .patch_elements(" <div id='mydiv'></div>" )
138
153
await asyncio.sleep(1 )
@@ -141,7 +156,8 @@ def my_route(request):
141
156
## Signal Helpers
142
157
The current state of the datastar signals is included by default in every
143
158
datastar request. A helper is included to load those signals for each
144
- framework. ` read_signals `
159
+ framework. ` read_signals ` . The usage varies per framework so check the
160
+ signature for your framework. You usually need to pass the request in.
145
161
146
162
``` python
147
163
from datastar_py.quart import read_signals
0 commit comments