-
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
77 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 is easy to integrate with Starlette. Since FastAPI is built upon Starlette, htpy can also be used with FastAPI to generate HTML. | ||
|
||
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 htpy import h1 | ||
|
||
app = Starlette() | ||
|
||
|
||
@app.route("/") | ||
def index(request: Request) -> HTMLResponse: | ||
return HTMLResponse(h1["Hi Starlette!"]) | ||
|
||
``` |
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,12 @@ | ||
from starlette.applications import Starlette | ||
from starlette.requests import Request | ||
from starlette.responses import HTMLResponse | ||
|
||
from htpy import h1 | ||
|
||
app = Starlette() | ||
|
||
|
||
@app.route("/") | ||
def index(request: Request) -> HTMLResponse: | ||
return HTMLResponse(h1["Hi Starlette!"]) |
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 | ||
|
||
|
||
def sync(request: Request) -> HTMLResponse: | ||
return HTMLResponse(h1["Hello, sync world!"]) | ||
|
||
|
||
client = TestClient( | ||
Starlette( | ||
debug=True, | ||
routes=[ | ||
Route("/sync", sync), | ||
], | ||
) | ||
) | ||
|
||
|
||
def test_html_response() -> None: | ||
response = client.get("/sync") | ||
assert response.content == b"<h1>Hello, sync world!</h1>" |