-
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
8 changed files
with
340 additions
and
16 deletions.
There are no files selected for viewing
Binary file not shown.
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
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,24 @@ | ||
import asyncio | ||
import random | ||
|
||
from htpy import Element, b, div, h1 | ||
|
||
|
||
async def magic_number() -> Element: | ||
await asyncio.sleep(2) | ||
return b[f"The Magic Number is: {random.randint(1, 100)}"] | ||
|
||
|
||
async def my_component() -> Element: | ||
return div[ | ||
h1["The Magic Number"], | ||
magic_number(), | ||
] | ||
|
||
|
||
async def main() -> None: | ||
async for chunk in await my_component(): | ||
print(chunk) | ||
|
||
|
||
asyncio.run(main()) |
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,35 @@ | ||
import asyncio | ||
from collections.abc import AsyncIterator | ||
|
||
from starlette.applications import Starlette | ||
from starlette.requests import Request | ||
from starlette.responses import StreamingResponse | ||
from starlette.routing import Route | ||
|
||
from htpy import Element, div, h1, li, p, ul | ||
|
||
|
||
async def index(request: Request) -> StreamingResponse: | ||
return StreamingResponse(await index_page(), media_type="text/html") | ||
|
||
|
||
async def index_page() -> Element: | ||
return div[ | ||
h1["Starlette Async example"], | ||
p["This page is generated asynchronously using Starlette and ASGI."], | ||
ul[(li[str(num)] async for num in slow_numbers(1, 10))], | ||
] | ||
|
||
|
||
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 = Starlette( | ||
debug=True, | ||
routes=[ | ||
Route("/", index), | ||
], | ||
) |
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
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
Oops, something went wrong.