Skip to content

Create README for examples to make them simple to run #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Datastar Python SDK Examples

## Running Examples with `uv`

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.

### General Instructions

Navigate to the specific example directory and run:

```sh
uv run <script-name>
```

For example:
- `cd examples/fastapi && uv run app.py`
- `cd examples/fasthtml && uv run simple.py`
- `cd examples/sanic && uv run app.py`

### Django

The Django example has its own `pyproject.toml` with dependencies. Navigate to the Django directory and run:

```sh
uv run manage.py runserver
```

## Alternative: Running with `pip`

If you prefer using `pip`, you can create a virtual environment and install the dependencies listed in each script's metadata comments manually:

```sh
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install datastar-py <other-dependencies-from-script>
python <script-name>
```

Refer to the `# dependencies = [...]` section at the top of each example script to see what packages to install.
3 changes: 1 addition & 2 deletions examples/django/datastar/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from ds import views

from django.contrib import admin
from django.urls import path
from ds import views

urlpatterns = [
path("admin/", admin.site.urls),
Expand Down
4 changes: 2 additions & 2 deletions examples/django/ds/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import time
from datetime import datetime

from django.http import HttpResponse

from datastar_py.django import (
DatastarResponse,
ServerSentEventGenerator,
read_signals,
)

from django.http import HttpResponse

# ASGI Example

HTML_ASGI = """\
Expand Down
6 changes: 3 additions & 3 deletions examples/fastapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
from datetime import datetime

import uvicorn
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

from datastar_py.fastapi import (
DatastarResponse,
ReadSignals,
ServerSentEventGenerator,
)

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


Expand Down
7 changes: 4 additions & 3 deletions examples/fasthtml/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
from datetime import datetime

import polars as pl
from datastar_py.fasthtml import DatastarResponse, ServerSentEventGenerator
from great_tables import GT
from great_tables.data import reactions

# ruff: noqa: F403, F405
from fasthtml.common import *
from great_tables import GT
from great_tables.data import reactions

from datastar_py.fasthtml import DatastarResponse, ServerSentEventGenerator

######################################################################################################
# This demo shows how FastHTML can be integrated with Datastar for server-driven interaction with a #
Expand Down
4 changes: 2 additions & 2 deletions examples/fasthtml/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import json
from datetime import datetime

from datastar_py.fasthtml import DatastarResponse, ServerSentEventGenerator, read_signals

# ruff: noqa: F403, F405
from fasthtml.common import *

from datastar_py.fasthtml import DatastarResponse, ServerSentEventGenerator, read_signals

app, rt = fast_app(
htmx=False,
surreal=False,
Expand Down
7 changes: 4 additions & 3 deletions examples/litestar/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# dependencies = [
# "datastar-py",
# "litestar",
# "uvicorn",
# ]
# [tool.uv.sources]
# datastar-py = { path = "../../" }
Expand All @@ -12,16 +13,16 @@
from datetime import datetime

import uvicorn
from litestar import Litestar, MediaType, get
from litestar.di import Provide

from datastar_py.litestar import (
DatastarResponse,
ServerSentEventGenerator,
read_signals,
)
from datastar_py.sse import DatastarEvent

from litestar import Litestar, MediaType, get
from litestar.di import Provide

HTML = """\
<!DOCTYPE html>
<html lang="en">
Expand Down
20 changes: 0 additions & 20 deletions examples/pyproject.toml

This file was deleted.

4 changes: 2 additions & 2 deletions examples/quart/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import asyncio
from datetime import datetime

from quart import Quart

from datastar_py.quart import (
DatastarResponse,
ServerSentEventGenerator,
read_signals,
)

from quart import Quart

app = Quart(__name__)

HTML = """\
Expand Down
6 changes: 3 additions & 3 deletions examples/sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import asyncio
from datetime import datetime

from sanic import Sanic
from sanic.response import html

from datastar_py.consts import ElementPatchMode
from datastar_py.sanic import (
DatastarResponse,
Expand All @@ -18,9 +21,6 @@
read_signals,
)

from sanic import Sanic
from sanic.response import html

app = Sanic("DataStarApp")

HTML = """\
Expand Down
28 changes: 15 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ name = "datastar-py"
version = "0.6.3"
description = "Helper functions and classes for the Datastar library (https://data-star.dev/)"
readme = "README.md"
keywords = [ "datastar", "django", "fastapi", "fasthtml", "flask", "html", "litestar", "quart", "sanic", "starlette" ]
keywords = [
"datastar",
"django",
"fastapi",
"fasthtml",
"flask",
"html",
"litestar",
"quart",
"sanic",
"starlette",
]
license = "MIT"
license-files = [ "LICENSE.md" ]
authors = [
Expand Down Expand Up @@ -37,18 +48,7 @@ urls.Documentation = "https://github.com/starfederation/datastar-python/blob/dev
urls.GitHub = "https://github.com/starfederation/datastar-python"

[dependency-groups]
dev = [
"django>=4.2.17",
"fastapi[standard]>=0.115.4",
"flask[async]>=3.0.3",
"litestar>=2.15.2",
"pre-commit>=4.2",
"python-fasthtml>=0.12; python_version>'3.10'",
"quart>=0.19.9",
"sanic>=24.6",
"starlette>=0.46.1",
"uvicorn>=0.32.1",
]
dev = [ "pre-commit>=4.2" ]

[tool.ruff]
line-length = 99
Expand All @@ -72,3 +72,5 @@ lint.select = [
]
lint.ignore = [ "E501" ]
lint.fixable = [ "ALL" ]

per-file-ignores."examples/**/*.py" = [ "ANN" ]
Loading