-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import asyncio | ||
from collections.abc import AsyncIterator | ||
|
||
from starlette.applications import Starlette | ||
from starlette.requests import Request | ||
from starlette.responses import HTMLResponse, StreamingResponse | ||
|
||
import htpy as h | ||
|
||
app = Starlette(debug=True) | ||
|
||
|
||
@app.route("/") | ||
async def homepage(request: Request) -> HTMLResponse: | ||
return HTMLResponse( | ||
h.div[ | ||
h.h1["Hello, World!"], | ||
h.p["This is a simple example of using htpy with Starlette."], | ||
h.a(href=str(request.url_for("stream")))["Go to the async stream example"], | ||
], | ||
) | ||
|
||
|
||
async def slow_numbers(minimum: int, maximum: int) -> AsyncIterator[int]: | ||
for number in range(minimum, maximum + 1): | ||
yield number | ||
await asyncio.sleep(0.5) | ||
|
||
|
||
@app.route("/stream", name="stream") | ||
async def async_stream(request: Request) -> StreamingResponse: | ||
return StreamingResponse( | ||
h.div[ | ||
h.h1["Async Stream"], | ||
h.p["This page is generated asyncronously."], | ||
h.ul[(h.li[str(num)] async for num in slow_numbers(1, 10))], | ||
], | ||
media_type="text/html", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters