Skip to content

Commit f5ecafd

Browse files
authored
Create README for examples to make them simple to run (#9)
* Create a README for examples to make them easy to run * Fix spelling * Simplify running instructions, refactor dependencies * Exclude examples from annotations rules, sort imports * Apply pyproject-fmt rules --------- Co-authored-by: Lucian Knock <[email protected]>
1 parent a136b7a commit f5ecafd

File tree

11 files changed

+75
-53
lines changed

11 files changed

+75
-53
lines changed

examples/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Datastar Python SDK Examples
2+
3+
## Running Examples with `uv`
4+
5+
All examples (except Django) include Inline Script Metadata (PEP 723), allowing them to be run with [uv](https://docs.astral.sh/uv/) without first installing dependencies.
6+
7+
### General Instructions
8+
9+
Navigate to the specific example directory and run:
10+
11+
```sh
12+
uv run <script-name>
13+
```
14+
15+
For example:
16+
- `cd examples/fastapi && uv run app.py`
17+
- `cd examples/fasthtml && uv run simple.py`
18+
- `cd examples/sanic && uv run app.py`
19+
20+
### Django
21+
22+
The Django example has its own `pyproject.toml` with dependencies. Navigate to the Django directory and run:
23+
24+
```sh
25+
uv run manage.py runserver
26+
```
27+
28+
## Alternative: Running with `pip`
29+
30+
If you prefer using `pip`, you can create a virtual environment and install the dependencies listed in each script's metadata comments manually:
31+
32+
```sh
33+
python -m venv .venv
34+
source .venv/bin/activate # On Windows: .venv\Scripts\activate
35+
pip install datastar-py <other-dependencies-from-script>
36+
python <script-name>
37+
```
38+
39+
Refer to the `# dependencies = [...]` section at the top of each example script to see what packages to install.

examples/django/datastar/urls.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1616
"""
1717

18-
from ds import views
19-
2018
from django.contrib import admin
2119
from django.urls import path
20+
from ds import views
2221

2322
urlpatterns = [
2423
path("admin/", admin.site.urls),

examples/django/ds/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
import time
33
from datetime import datetime
44

5+
from django.http import HttpResponse
6+
57
from datastar_py.django import (
68
DatastarResponse,
79
ServerSentEventGenerator,
810
read_signals,
911
)
1012

11-
from django.http import HttpResponse
12-
1313
# ASGI Example
1414

1515
HTML_ASGI = """\

examples/fastapi/app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
from datetime import datetime
1313

1414
import uvicorn
15+
from fastapi import FastAPI
16+
from fastapi.responses import HTMLResponse
17+
1518
from datastar_py.fastapi import (
1619
DatastarResponse,
1720
ReadSignals,
1821
ServerSentEventGenerator,
1922
)
2023

21-
from fastapi import FastAPI
22-
from fastapi.responses import HTMLResponse
23-
2424
app = FastAPI()
2525

2626

examples/fasthtml/advanced.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
from datetime import datetime
1717

1818
import polars as pl
19-
from datastar_py.fasthtml import DatastarResponse, ServerSentEventGenerator
20-
from great_tables import GT
21-
from great_tables.data import reactions
2219

2320
# ruff: noqa: F403, F405
2421
from fasthtml.common import *
22+
from great_tables import GT
23+
from great_tables.data import reactions
24+
25+
from datastar_py.fasthtml import DatastarResponse, ServerSentEventGenerator
2526

2627
######################################################################################################
2728
# This demo shows how FastHTML can be integrated with Datastar for server-driven interaction with a #

examples/fasthtml/simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import json
1212
from datetime import datetime
1313

14-
from datastar_py.fasthtml import DatastarResponse, ServerSentEventGenerator, read_signals
15-
1614
# ruff: noqa: F403, F405
1715
from fasthtml.common import *
1816

17+
from datastar_py.fasthtml import DatastarResponse, ServerSentEventGenerator, read_signals
18+
1919
app, rt = fast_app(
2020
htmx=False,
2121
surreal=False,

examples/litestar/app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# dependencies = [
33
# "datastar-py",
44
# "litestar",
5+
# "uvicorn",
56
# ]
67
# [tool.uv.sources]
78
# datastar-py = { path = "../../" }
@@ -12,16 +13,16 @@
1213
from datetime import datetime
1314

1415
import uvicorn
16+
from litestar import Litestar, MediaType, get
17+
from litestar.di import Provide
18+
1519
from datastar_py.litestar import (
1620
DatastarResponse,
1721
ServerSentEventGenerator,
1822
read_signals,
1923
)
2024
from datastar_py.sse import DatastarEvent
2125

22-
from litestar import Litestar, MediaType, get
23-
from litestar.di import Provide
24-
2526
HTML = """\
2627
<!DOCTYPE html>
2728
<html lang="en">

examples/pyproject.toml

Lines changed: 0 additions & 20 deletions
This file was deleted.

examples/quart/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
import asyncio
1111
from datetime import datetime
1212

13+
from quart import Quart
14+
1315
from datastar_py.quart import (
1416
DatastarResponse,
1517
ServerSentEventGenerator,
1618
read_signals,
1719
)
1820

19-
from quart import Quart
20-
2121
app = Quart(__name__)
2222

2323
HTML = """\

examples/sanic/app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import asyncio
1111
from datetime import datetime
1212

13+
from sanic import Sanic
14+
from sanic.response import html
15+
1316
from datastar_py.consts import ElementPatchMode
1417
from datastar_py.sanic import (
1518
DatastarResponse,
@@ -18,9 +21,6 @@
1821
read_signals,
1922
)
2023

21-
from sanic import Sanic
22-
from sanic.response import html
23-
2424
app = Sanic("DataStarApp")
2525

2626
HTML = """\

0 commit comments

Comments
 (0)