-
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
7 changed files
with
80 additions
and
0 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
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,21 @@ | ||
# Usage with Starlette | ||
|
||
htpy can be used with Starlette to generate HTML. Since FastAPI is built upon Starlette, htpy can also be used with FastAPI. | ||
|
||
To return HTML contents, pass a htpy element to Starlette's `HTMLResponse`: | ||
|
||
```py | ||
from starlette.applications import Starlette | ||
from starlette.requests import Request | ||
from starlette.responses import HTMLResponse | ||
from starlette.routing import Route | ||
|
||
from htpy import h1 | ||
|
||
|
||
async def index(request: Request) -> HTMLResponse: | ||
return HTMLResponse(h1["Hi Starlette!"]) | ||
|
||
|
||
app = Starlette(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from starlette.applications import Starlette | ||
from starlette.requests import Request | ||
from starlette.responses import HTMLResponse | ||
from starlette.routing import Route | ||
|
||
from htpy import h1 | ||
|
||
|
||
async def index(request: Request) -> HTMLResponse: | ||
return HTMLResponse(h1["Hi Starlette!"]) | ||
|
||
|
||
app = Starlette( | ||
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
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,32 @@ | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
|
||
from starlette.applications import Starlette | ||
from starlette.responses import HTMLResponse | ||
from starlette.routing import Route | ||
from starlette.testclient import TestClient | ||
|
||
from htpy import h1 | ||
|
||
if t.TYPE_CHECKING: | ||
from starlette.requests import Request | ||
|
||
|
||
async def html_response(request: Request) -> HTMLResponse: | ||
return HTMLResponse(h1["Hello, HTMLResponse!"]) | ||
|
||
|
||
client = TestClient( | ||
Starlette( | ||
debug=True, | ||
routes=[ | ||
Route("/html-response", html_response), | ||
], | ||
) | ||
) | ||
|
||
|
||
def test_html_response() -> None: | ||
response = client.get("/html-response") | ||
assert response.content == b"<h1>Hello, HTMLResponse!</h1>" |