diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..342ed1a --- /dev/null +++ b/examples/README.md @@ -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 +``` + +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 +python +``` + +Refer to the `# dependencies = [...]` section at the top of each example script to see what packages to install. diff --git a/examples/django/datastar/urls.py b/examples/django/datastar/urls.py index af132a1..0f163cc 100644 --- a/examples/django/datastar/urls.py +++ b/examples/django/datastar/urls.py @@ -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), diff --git a/examples/django/ds/views.py b/examples/django/ds/views.py index 8032039..f0057ff 100644 --- a/examples/django/ds/views.py +++ b/examples/django/ds/views.py @@ -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 = """\ diff --git a/examples/fastapi/app.py b/examples/fastapi/app.py index 88f81b8..fc231be 100644 --- a/examples/fastapi/app.py +++ b/examples/fastapi/app.py @@ -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() diff --git a/examples/fasthtml/advanced.py b/examples/fasthtml/advanced.py index 74cb4ab..6322842 100644 --- a/examples/fasthtml/advanced.py +++ b/examples/fasthtml/advanced.py @@ -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 # diff --git a/examples/fasthtml/simple.py b/examples/fasthtml/simple.py index 95cb28a..9206fd5 100644 --- a/examples/fasthtml/simple.py +++ b/examples/fasthtml/simple.py @@ -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, diff --git a/examples/litestar/app.py b/examples/litestar/app.py index f2a8810..b192732 100644 --- a/examples/litestar/app.py +++ b/examples/litestar/app.py @@ -2,6 +2,7 @@ # dependencies = [ # "datastar-py", # "litestar", +# "uvicorn", # ] # [tool.uv.sources] # datastar-py = { path = "../../" } @@ -12,6 +13,9 @@ from datetime import datetime import uvicorn +from litestar import Litestar, MediaType, get +from litestar.di import Provide + from datastar_py.litestar import ( DatastarResponse, ServerSentEventGenerator, @@ -19,9 +23,6 @@ ) from datastar_py.sse import DatastarEvent -from litestar import Litestar, MediaType, get -from litestar.di import Provide - HTML = """\ diff --git a/examples/pyproject.toml b/examples/pyproject.toml deleted file mode 100644 index 501de88..0000000 --- a/examples/pyproject.toml +++ /dev/null @@ -1,20 +0,0 @@ -[tool.ruff] -line-length = 99 -lint.select = [ - # flake8-bugbear - "B", - # pycodestyle - "E", - # Pyflakes - "F", - # isort - "I", - # flake8-simplify - "SIM", - # pyupgrade - "UP", -] -lint.extend-ignore = [ - "E501", # line too long -] -lint.fixable = [ "ALL" ] diff --git a/examples/quart/app.py b/examples/quart/app.py index d9363db..183d1e3 100644 --- a/examples/quart/app.py +++ b/examples/quart/app.py @@ -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 = """\ diff --git a/examples/sanic/app.py b/examples/sanic/app.py index 1897158..5bd53e7 100644 --- a/examples/sanic/app.py +++ b/examples/sanic/app.py @@ -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, @@ -18,9 +21,6 @@ read_signals, ) -from sanic import Sanic -from sanic.response import html - app = Sanic("DataStarApp") HTML = """\ diff --git a/pyproject.toml b/pyproject.toml index 37300e7..24e3aaf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = [ @@ -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 @@ -72,3 +72,5 @@ lint.select = [ ] lint.ignore = [ "E501" ] lint.fixable = [ "ALL" ] + +per-file-ignores."examples/**/*.py" = [ "ANN" ]