-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve and document the import hook mechanism (#5)
* Improve import hook * Add tests * Update examples * Update readme * Remove coding directive from import hook examples * Make pyright happy
- Loading branch information
Showing
26 changed files
with
224 additions
and
24 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,20 @@ | ||
from pyjsx import jsx, JSX | ||
|
||
|
||
def Header(children, style=None, **rest) -> JSX: | ||
return <h1 style={style}>{children}</h1> | ||
|
||
|
||
def Main(children, **rest) -> JSX: | ||
return <main>{children}</main> | ||
|
||
|
||
def App() -> JSX: | ||
return ( | ||
<div> | ||
<Header style={{"color": "red"}}>Hello, world!</Header> | ||
<Main> | ||
<p>This was rendered with PyJSX!</p> | ||
</Main> | ||
</div> | ||
) |
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,6 @@ | ||
from pyjsx import auto_setup | ||
|
||
from custom import App | ||
|
||
|
||
print(App()) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
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,5 @@ | ||
from pyjsx import auto_setup | ||
|
||
from props import App | ||
|
||
print(App()) |
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,37 @@ | ||
from pyjsx import jsx, JSX | ||
|
||
|
||
def Card(rounded=False, raised=False, image=None, children=None, **rest) -> JSX: | ||
style = { | ||
"border-radius": "5px" if rounded else 0, | ||
"box-shadow": "0 2px 4px rgba(0, 0, 0, 0.1)" if raised else "none", | ||
} | ||
return ( | ||
<div style={style}> | ||
{image} | ||
{children} | ||
</div> | ||
) | ||
|
||
|
||
def Image(src, alt, **rest) -> JSX: | ||
return <img src={src} alt={alt} /> | ||
|
||
|
||
def App() -> JSX: | ||
return ( | ||
<div> | ||
<Card rounded raised image={<Image src="dog.jpg" alt="A picture of a dog" />}> | ||
<h1>Card title</h1> | ||
<p>Card content</p> | ||
</Card> | ||
<Card rounded raised={False} disabled image={<Image src="cat.jpg" alt="A picture of a cat" />}> | ||
<h1>Card title</h1> | ||
<p>Card content</p> | ||
</Card> | ||
<Card rounded raised={False}> | ||
<h1>Card title</h1> | ||
<p>Card content</p> | ||
</Card> | ||
</div> | ||
) |
Empty file.
File renamed without changes.
File renamed without changes.
Empty file.
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,6 @@ | ||
from pyjsx import auto_setup | ||
|
||
from table import make_table | ||
|
||
|
||
print(make_table()) |
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,36 @@ | ||
from pyjsx import jsx, JSX | ||
|
||
|
||
def make_header(names: list[str]) -> JSX: | ||
return ( | ||
<thead> | ||
<tr> | ||
{<th>{name}</th> for name in names} | ||
</tr> | ||
</thead> | ||
) | ||
|
||
|
||
def make_body(rows: list[list[str]]) -> JSX: | ||
return ( | ||
<tbody> | ||
{ | ||
<tr> | ||
{<td>{cell}</td> for cell in row} | ||
</tr> | ||
for row in rows | ||
} | ||
</tbody> | ||
) | ||
|
||
|
||
def make_table() -> JSX: | ||
columns = ["Name", "Age"] | ||
rows = [["Alice", "34"], ["Bob", "56"]] | ||
|
||
return ( | ||
<table> | ||
{make_header(columns)} | ||
{make_body(rows)} | ||
</table> | ||
) |
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
File renamed without changes.
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,30 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from pyjsx.import_hook import PyJSXFinder, register_import_hook, unregister_import_hook | ||
|
||
|
||
@pytest.fixture | ||
def import_hook(): | ||
register_import_hook() | ||
yield | ||
unregister_import_hook() | ||
|
||
|
||
def test_finder(): | ||
finder = PyJSXFinder() | ||
path = str(Path(__file__).parent / "test_module") | ||
spec = finder.find_spec("main", [path]) | ||
assert spec is not None | ||
assert spec.name == "main" | ||
|
||
|
||
@pytest.mark.usefixtures("import_hook") | ||
def test_import(): | ||
from .test_module import main # type: ignore[reportAttributeAccessIssue] | ||
|
||
assert str(main.hello()) == """\ | ||
<h1> | ||
Hello, World! | ||
</h1>""" |
Empty file.
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,4 @@ | ||
from pyjsx import jsx | ||
|
||
def hello(): | ||
return <h1>Hello, World!</h1> |