Skip to content

Conversation

@axelknock
Copy link
Contributor

Also disabled live=True in the FastHTML app creation since it was causing some issue with the reload method (websocket) constantly reloading the page. Not sure what the issue is, but it's not a datastar issue. It might be better to just provide an example of Delaney's hotreload method. Here's a rough draft of it using the python SDK:

For it to work correctly:

uvicorn main:app --reload --timeout-graceful-shutdown 0
import asyncio
from starlette.applications import Starlette
from starlette.responses import HTMLResponse
from starlette.requests import Request
from starlette.routing import Route

from datastar_py import ServerSentEventGenerator as SSE
from datastar_py import attribute_generator as data
from datastar_py.starlette import datastar_response


hot_reload_sent = False
hot_reload_lock = asyncio.Lock()


async def index(request: Request):
    return HTMLResponse(
        f"""
        <html>
            <head>
                <script type="module"
                    src="https://cdn.jsdelivr.net/gh/starfederation/datastar@v1.0.0-RC.7/bundles/datastar.js">
                </script>
            </head>
            <body>
                <div>Hello world!</div>
                <div id="hotreload"
                     {data.init("@get('/hotreload', {retryMaxCount: 1000, retryInterval: 20, retryMaxWaitMs: 200})")}>
                </div>
            </body>
        </html>
        """
    )


@datastar_response
async def hotreload(request: Request):
    global hot_reload_sent

    async with hot_reload_lock:
        if not hot_reload_sent:
            hot_reload_sent = True
            yield SSE.execute_script("window.location.reload()")

    while not await request.is_disconnected():
        await asyncio.sleep(0.5)


app = Starlette(
    routes=[
        Route("/", index),
        Route("/hotreload", hotreload),
    ]
)

@gazpachoking gazpachoking merged commit 28d6472 into starfederation:develop Dec 22, 2025
1 of 2 checks passed
@gazpachoking
Copy link
Collaborator

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants