-
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
14 changed files
with
435 additions
and
133 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
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(1) | ||
return b[f"The Magic Number is: {random.randint(1, 100)}"] | ||
|
||
|
||
async def my_component() -> Element: | ||
return div[ | ||
h1["The Magic Number"], | ||
await magic_number(), | ||
] | ||
|
||
|
||
async def main() -> None: | ||
async for _chunk in my_component(): | ||
print("got: 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,29 @@ | ||
import asyncio | ||
from collections.abc import AsyncIterator | ||
|
||
from starlette.applications import Starlette | ||
from starlette.requests import Request | ||
from starlette.responses import StreamingResponse | ||
|
||
from htpy import Element, div, h1, li, p, ul | ||
|
||
app = Starlette(debug=True) | ||
|
||
|
||
@app.route("/") | ||
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) |
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
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,48 @@ | ||
from collections.abc import AsyncIterator | ||
|
||
import pytest | ||
|
||
from htpy import Element, li, ul | ||
|
||
|
||
async def async_lis() -> AsyncIterator[Element]: | ||
yield li["a"] | ||
yield li["b"] | ||
|
||
|
||
async def hi() -> Element: | ||
return li["hi"] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_iterator() -> None: | ||
result = [chunk async for chunk in ul[async_lis()]] | ||
assert result == ["<ul>", "<li>", "a", "</li>", "<li>", "b", "</li>", "</ul>"] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_cororoutinefunction_children() -> None: | ||
result = [chunk async for chunk in ul[hi]] | ||
assert result == ["<ul>", "<li>", "hi", "</li>", "</ul>"] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_cororoutine_children() -> None: | ||
result = [chunk async for chunk in ul[hi()]] | ||
assert result == ["<ul>", "<li>", "hi", "</li>", "</ul>"] | ||
|
||
|
||
def test_sync_iteration_with_async_children() -> None: | ||
with pytest.raises( | ||
ValueError, | ||
match=( | ||
r"<async_generator object async_lis at .+> is not a valid child element\. " | ||
r"Use async iteration to retrieve element content: https://htpy.dev/async/" | ||
), | ||
): | ||
str(ul[async_lis()]) | ||
|
||
|
||
@pytest.mark.xfail | ||
def test_repr_with_async_children() -> None: | ||
assert repr(ul[async_lis()]) == "<Element '<ul></ul>'>" |
Oops, something went wrong.