-
Notifications
You must be signed in to change notification settings - Fork 1
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
10 changed files
with
220 additions
and
27 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from pyjsx.codecs import register_jsx | ||
from pyjsx.jsx import JSX, jsx | ||
from pyjsx.jsx import JSX, JSXComponent, jsx | ||
from pyjsx.transpiler import transpile | ||
|
||
|
||
__version__ = "0.1.0" | ||
__all__ = ["register_jsx", "transpile", "jsx", "JSX"] | ||
__all__ = ["register_jsx", "transpile", "jsx", "JSX", "JSXComponent"] |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[project] | ||
name = "jsx-python" | ||
name = "python-jsx" | ||
authors = [{ name = "Tomas Roun", email = "[email protected]" }] | ||
description = "JSX transpiler for Python" | ||
readme = "README.md" | ||
|
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,10 @@ | ||
<div> | ||
<h1 style="color: red"> | ||
Hello, world! | ||
</h1> | ||
<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,28 @@ | ||
<div> | ||
<div style="border-radius: 5px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1)"> | ||
<img src="dog.jpg" alt="A picture of a dog" /> | ||
<h1> | ||
Card title | ||
</h1> | ||
<p> | ||
Card content | ||
</p> | ||
</div> | ||
<div style="border-radius: 5px; box-shadow: none"> | ||
<img src="cat.jpg" alt="A picture of a cat" /> | ||
<h1> | ||
Card title | ||
</h1> | ||
<p> | ||
Card content | ||
</p> | ||
</div> | ||
<div style="border-radius: 5px; box-shadow: none"> | ||
<h1> | ||
Card title | ||
</h1> | ||
<p> | ||
Card content | ||
</p> | ||
</div> | ||
</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,30 @@ | ||
<table> | ||
<thead> | ||
<tr> | ||
<th> | ||
Name | ||
</th> | ||
<th> | ||
Age | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td> | ||
Alice | ||
</td> | ||
<td> | ||
34 | ||
</td> | ||
</tr> | ||
<tr> | ||
<td> | ||
Bob | ||
</td> | ||
<td> | ||
56 | ||
</td> | ||
</tr> | ||
</tbody> | ||
</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,30 @@ | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
|
||
def run_example(name: str): | ||
path = Path(__file__).parents[1] / "examples" / name / "main.py" | ||
# print(path) | ||
return subprocess.run( # noqa: S603 | ||
[sys.executable, str(path)], text=True, check=True, capture_output=True | ||
).stdout | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"example", | ||
[ | ||
"table", | ||
"props", | ||
"custom", | ||
], | ||
) | ||
def test_example(request, snapshot, example): | ||
snapshot.snapshot_dir = Path(__file__).parent / "data" | ||
# o = run_example(example) | ||
# print(type(o)) | ||
snapshot.assert_match( | ||
run_example(example), f"examples-{request.node.callspec.id}.txt" | ||
) |
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,60 @@ | ||
import pytest | ||
|
||
from pyjsx import JSX, JSXComponent, jsx, transpile | ||
|
||
|
||
def run_example(source: str, _locals=None): | ||
py_code = transpile(source) | ||
return eval(py_code, {"jsx": jsx}, _locals) # noqa: S307 | ||
|
||
|
||
def test_passing_jsx_as_props(): | ||
def CardWithImageComponent(image: JSX | None = None, **_): | ||
return jsx("div", {}, [image]) | ||
|
||
def CardWithImageCallable(image: JSXComponent, **_): | ||
return jsx("div", {}, [jsx(image, {}, [])]) | ||
|
||
def Image(src="example.jpg", alt=None, **_): | ||
return jsx("img", {"src": src, "alt": alt}, []) | ||
|
||
html = run_example( | ||
"str(<Card />)", {"Card": CardWithImageComponent, "Image": Image} | ||
) | ||
assert html == "<div></div>" | ||
|
||
with pytest.raises(TypeError): | ||
run_example("str(<Card />)", {"Card": CardWithImageCallable, "Image": Image}) | ||
|
||
html = run_example( | ||
"str(<Card image={<Image src='example.jpg' />} />)", | ||
{"Card": CardWithImageComponent, "Image": Image}, | ||
) | ||
assert ( | ||
html | ||
== """\ | ||
<div> | ||
<img src="example.jpg" /> | ||
</div>""" | ||
) | ||
|
||
html = run_example( | ||
"str(<Card image={Image} />)", {"Card": CardWithImageCallable, "Image": Image} | ||
) | ||
assert ( | ||
html | ||
== """\ | ||
<div> | ||
<img src="example.jpg" /> | ||
</div>""" | ||
) | ||
|
||
with pytest.raises(TypeError) as excinfo: | ||
run_example( | ||
"str(<Card image={<Image />} />)", | ||
{"Card": CardWithImageCallable, "Image": Image}, | ||
) | ||
assert ( | ||
str(excinfo.value) | ||
== "Element type is invalid. Expected a string or a function but got: <Image />" | ||
) |
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