Skip to content

Commit 972209d

Browse files
committed
Update the first example in the README to show many features
Other tweaks to the readme
1 parent 629dcc2 commit 972209d

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

README.md

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,65 @@ helpers included for the following frameworks:
2121
* [Sanic](https://sanic.dev/en/)
2222
* [Starlette](https://www.starlette.io/)
2323

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.
2526

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.
2829

2930
```python
3031
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
3532
from datetime import datetime
3633

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+
3738
app = Quart(__name__)
3839

3940
# Import frontend library via Content Distribution Network, create targets for Server Sent Events
4041
@app.route("/")
4142
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+
"""
4654

4755

4856
@app.route("/updates")
57+
@datastar_response
4958
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)
6269

6370

6471
app.run()
6572
```
6673

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.
6883

6984
## Response Helpers
7085

@@ -77,7 +92,7 @@ e.g. `from datastar_py.quart import DatastarResponse` The containing functions
7792
are not shown here, as they will differ per framework.
7893

7994
```python
80-
# per framework Response import, eg:
95+
# per framework Response import. (Replace 'fastapi' with your framework.) e.g.:
8196
# from datastar_py.fastapi import DatastarResponse
8297
from datastar_py import ServerSentEventGenerator as SSE
8398

@@ -98,7 +113,6 @@ def two_event():
98113
])
99114

100115
# N events, a long lived stream (for all frameworks but sanic)
101-
102116
@app.get("/updates")
103117
async def updates():
104118
async def _():
@@ -128,11 +142,12 @@ works the same for any of the supported frameworks, and should be used under
128142
any routing decorator from the framework.
129143

130144
```python
145+
# Import the decorator from the package specific to your framework
131146
from datastar_py.sanic import datastar_response, ServerSentEventGenerator as SSE
132147

133148
@app.get('/my_route')
134149
@datastar_response
135-
def my_route(request):
150+
async def my_route(request):
136151
while True:
137152
yield SSE.patch_elements("<div id='mydiv'></div>")
138153
await asyncio.sleep(1)
@@ -141,7 +156,8 @@ def my_route(request):
141156
## Signal Helpers
142157
The current state of the datastar signals is included by default in every
143158
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.
145161

146162
```python
147163
from datastar_py.quart import read_signals

0 commit comments

Comments
 (0)