Skip to content

Instantly share code, notes, and snippets.

@jrobichaud
Created December 6, 2024 22:20
Show Gist options
  • Save jrobichaud/c967236fa9dc36f2739461e94cb6bf35 to your computer and use it in GitHub Desktop.
Save jrobichaud/c967236fa9dc36f2739461e94cb6bf35 to your computer and use it in GitHub Desktop.
Saglac IO of december 2024: Suspense all the things
import asyncio
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
app = FastAPI()
async def hello_io():
yield """
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<div id="suspense-demo-da4d8e7d-cbd1-4aa9-9805-4de6b8cda2c1">
Loading...
</div>
</body>
</html>
"""
#yield f'<div style="width: 0; height: 0">{200 * "\u200b" }</div>' # hack to force the browser to render the page in Safari
await asyncio.sleep(3)
yield """
<script>
document.getElementById('suspense-demo-da4d8e7d-cbd1-4aa9-9805-4de6b8cda2c1').outerHTML = `
<p>Hello Saglac IO</p>
`;
</script>
"""
@app.get("/")
async def main():
return StreamingResponse(hello_io(), media_type="text/html;charset=UTF-8")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment