-
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
3 changed files
with
157 additions
and
8 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,32 @@ | ||
import htpy as h | ||
|
||
# Defaults | ||
theme_ctx = h.Context("plain") | ||
side_menu_open_ctx = h.Context(False) | ||
|
||
|
||
# @app.get("/") | ||
def get_root() -> h.Node: | ||
current_theme = "blue" # get user theme from database/session etc | ||
side_menu_open = True # get the side menu open state from database/session etc | ||
|
||
return theme_ctx.provider( | ||
current_theme, | ||
lambda: side_menu_open_ctx.provider(side_menu_open, lambda: index_page()), | ||
) | ||
|
||
|
||
@theme_ctx.consumer | ||
def index_page(theme: str) -> h.Node: | ||
return h.body(class_=f"theme-{theme}")[ | ||
side_menu(), | ||
h.main(), | ||
] | ||
|
||
|
||
@side_menu_open_ctx.consumer | ||
def side_menu(is_open: bool) -> h.Node: | ||
return h.aside(open=is_open)["The side menu"] | ||
|
||
|
||
print(get_root()) |
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,46 @@ | ||
import typing as t | ||
|
||
import pytest | ||
|
||
from htpy import Context, div | ||
|
||
letter_ctx: Context[t.Literal["a", "b", "c"]] = Context("a") | ||
no_default_ctx = Context[str]() | ||
|
||
|
||
@letter_ctx.consumer | ||
def display_letter(letter: t.Literal["a", "b", "c"], greeting: str) -> str: | ||
return f"{greeting}: {letter}!" | ||
|
||
|
||
@no_default_ctx.consumer | ||
def display_no_default(value: str) -> str: | ||
return f"{value=}" | ||
|
||
|
||
def test_context_default() -> None: | ||
result = div[display_letter("Yo")] | ||
assert str(result) == "<div>Yo: a!</div>" | ||
|
||
|
||
def test_context_provider() -> None: | ||
result = letter_ctx.provider("c", lambda: div[display_letter("Hello")]) | ||
assert str(result) == "<div>Hello: c!</div>" | ||
|
||
|
||
def test_no_default() -> None: | ||
with pytest.raises(ValueError, match="Context has no value"): | ||
str(div[display_no_default()]) | ||
|
||
|
||
def test_nested_override() -> None: | ||
result = div[ | ||
letter_ctx.provider( | ||
"b", | ||
lambda: letter_ctx.provider( | ||
"c", | ||
lambda: display_letter("Nested"), | ||
), | ||
) | ||
] | ||
assert str(result) == "<div>Nested: c!</div>" |