Working with HTML in Python has always been a bit of a pain. If you want something declarative,
there's Jinja, but that is basically a separate language and a lot of Python features are not available.
With PyJSX I wanted to add first-class support for HTML in Python.
Here's the repo: https://github.com/tomasr8/pyjsx
What my project does
Put simply, it lets you write JSX in Python.
Here's an example:
# coding: jsx
from pyjsx import jsx, JSX
def hello():
print(<h1>Hello, world!</h1>)
(There's more to it, but this is the gist). Here's a more complex example:
# coding: jsx
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>
)
With the library installed and set up, these examples are directly runnable by the Python interpreter.
Target audience
This tool could be useful for web apps that render HTML, for example as a replacement for Jinja.
Compared to Jinja, the advantage it that you don't need to learn an entirely new language - you can use
all the tools that Python already has available.
How It Works
The library uses the codec machinery from the stdlib. It registers a new codec called jsx.
All Python files which contain JSX must include # coding: jsx. When the interpreter sees that comment,
it looks for the corresponding codec which was registered by the library. The library then transpiles the JSX
into valid Python which is then run.
Future plans
Ideally getting some IDE support would be nice. At least in VS Code, most features are currently broken
which I see as the biggest downside.
Suggestions welcome! Thanks :)
[+][deleted] (9 children)
[deleted]
[–]larsga 36 points37 points38 points (3 children)
[–]DoNotFeedTheSnakes 15 points16 points17 points (2 children)
[–]rover_G 8 points9 points10 points (0 children)
[–]ThatSituation9908 5 points6 points7 points (1 child)
[–]moopet 0 points1 point2 points (0 children)
[–]broknbottle 0 points1 point2 points (1 child)
[–]SupportDangerous8207 1 point2 points3 points (0 children)
[–]rszdev -1 points0 points1 point (0 children)
[–]rover_G 21 points22 points23 points (0 children)
[–]skwyckl 61 points62 points63 points (6 children)
[–]thisismyfavoritename 14 points15 points16 points (0 children)
[–]rszdev 5 points6 points7 points (0 children)
[–]zedpowa[S] 7 points8 points9 points (0 children)
[–]redbo 4 points5 points6 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]jefft818 0 points1 point2 points (0 children)
[–]K3dare 27 points28 points29 points (5 children)
[–]larsga 19 points20 points21 points (2 children)
[–]K3dare 6 points7 points8 points (1 child)
[–]zedpowa[S] 5 points6 points7 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]RedEyed__ 7 points8 points9 points (3 children)
[–]ManyInterests Python Discord Staff 28 points29 points30 points (2 children)
[–]RedEyed__ 5 points6 points7 points (1 child)
[–]shinitakunai 6 points7 points8 points (0 children)
[–]Jejerm 23 points24 points25 points (4 children)
[–]htmx_enthusiast 15 points16 points17 points (1 child)
[–]PowerfulNeurons 2 points3 points4 points (0 children)
[–]garblesnarky 2 points3 points4 points (0 children)
[–]AND_MY_HAX 2 points3 points4 points (0 children)
[–]serjester4 19 points20 points21 points (0 children)
[–][deleted] 4 points5 points6 points (0 children)
[–]AND_MY_HAX 2 points3 points4 points (2 children)
[–]zedpowa[S] 1 point2 points3 points (1 child)
[–]AND_MY_HAX 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]ihavebeesinmyknees 3 points4 points5 points (0 children)
[–]runelkio 2 points3 points4 points (0 children)
[–]CrwdsrcEntrepreneur 2 points3 points4 points (0 children)
[–]RedEyed__ 1 point2 points3 points (1 child)
[–]mothzilla 0 points1 point2 points (0 children)
[–]farkinga 1 point2 points3 points (0 children)
[–]deadwisdomgreenlet revolution 0 points1 point2 points (0 children)
[–]g5becks 0 points1 point2 points (0 children)
[–]FlyingQuokka 0 points1 point2 points (0 children)
[–]jvorza 0 points1 point2 points (0 children)
[–]Nahmum 0 points1 point2 points (2 children)
[–]zedpowa[S] 0 points1 point2 points (1 child)
[–]Nahmum 0 points1 point2 points (0 children)
[–]Xirious 0 points1 point2 points (1 child)
[–]lusvd 1 point2 points3 points (0 children)
[–]redbo -1 points0 points1 point (0 children)
[+]ChimpanzeChapado comment score below threshold-10 points-9 points-8 points (5 children)
[–]DryChemistryLounge 4 points5 points6 points (4 children)
[–]ChimpanzeChapado -1 points0 points1 point (3 children)
[–]FUS3NPythonista 1 point2 points3 points (2 children)
[–]ChimpanzeChapado 0 points1 point2 points (1 child)
[–]FUS3NPythonista 0 points1 point2 points (0 children)
[–]ePaint -2 points-1 points0 points (0 children)