JSX transpiler for Python
Project description
PyJSX - Write JSX directly in Python
👉 We now have a VSCode plugin!
from pyjsx import jsx, JSX
def Header(style, children) -> JSX:
return <h1 style={style}>{children}</h1>
def Main(children) -> 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>
)
Installation
Get it via pip:
pip install python-jsx
Minimal example (using the coding directive)
[!TIP] There are more examples available in the examples folder.
There are two supported ways to seamlessly integrate JSX into your codebase. One is by registering a custom codec shown here and the other by using a custom import hook shown below.
# hello.py
# coding: jsx
from pyjsx import jsx
def hello():
print(<h1>Hello, world!</h1>)
# main.py
import pyjsx.auto_setup
from hello import hello
hello()
$ python main.py
<h1>Hello, word!</h1>
Each file containing JSX must contain two things:
# coding: jsxdirective - This tells Python to let our library parse the file first.from pyjsx import jsximport. PyJSX transpiles JSX intojsx(...)calls so it must be in scope.
To run a file containing JSX, the jsx codec must be registered first which can
be done with import pyjsx.auto_setup. This must occur before importing
any other file containing JSX.
Minimal example (using an import hook)
[!TIP] There are more examples available in the examples folder.
# hello.px
from pyjsx import jsx
def hello():
print(<h1>Hello, world!</h1>)
# main.py
import pyjsx.auto_setup
from hello import hello
hello()
$ python main.py
<h1>Hello, word!</h1>
Each file containing JSX must contain two things:
- The file extension must be
.px from pyjsx import jsximport. PyJSX transpiles JSX intojsx(...)calls so it must be in scope.
To be able to import .px, the import hook must be registered first which can
be done with import pyjsx.auto_setup (same as for the codec version). This must occur before importing any other file containing JSX.
Supported grammar
The full JSX grammar is supported. Here are a few examples:
Normal and self-closing tags
x = <div></div>
y = <img />
Props
<a href="example.com">Click me!</a>
<div style={{"color": "red"}}>This is red</div>
<span {...props}>Spread operator</span>
Nested expressions
<div>
{[<p>Row: {i}</p> for i in range(10)]}
</div>
Fragments
fragment = (
<>
<p>1st paragraph</p>
<p>2nd paragraph</p>
</>
)
Custom components
A custom component can be any function that takes **kwargs and
returns JSX or a plain string. The special prop children is a list
containing the element's children.
def Header(children, **rest):
return <h1>{children}</h1>
header = <Header>Title</Header>
print(header)
Preventing HTML escaping
By default, PyJSX escapes all string content to prevent XSS attacks. To render raw HTML, wrap strings with HTMLDontEscape:
from pyjsx import jsx, HTMLDontEscape
safe_html = HTMLDontEscape("<strong>Bold text</strong>")
element = <div>{safe_html}</div>
print(element) # <div><strong>Bold text</strong></div>
VS Code support
PyJSX comes with a VS Code plugin that provides syntax highlighting.
Type checking
PyJSX includes a plugin that allows mypy to parse files with JSX in them. To
use it, add pyjsx.mypy to the plugins list in your mypy configuration
file. For example, in mypy.ini:
[mypy]
plugins = pyjsx.mypy
Or in pyproject.toml:
[tool.mypy]
plugins = ["pyjsx.mypy"]
Prior art
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_jsx-0.4.0.tar.gz.
File metadata
- Download URL: python_jsx-0.4.0.tar.gz
- Upload date:
- Size: 111.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c9effa81437b7414dee206f6b59b79bcdfaa87da4163cc558eb15b75335a43a
|
|
| MD5 |
f862a38e6b3eb2ca14eea56692c99665
|
|
| BLAKE2b-256 |
452e9201b6dd0b136c92ad4e121c6520680ff12fd948e68cd2ee3f1c10a9ebad
|
Provenance
The following attestation bundles were made for python_jsx-0.4.0.tar.gz:
Publisher:
release.yml on tomasr8/pyjsx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_jsx-0.4.0.tar.gz -
Subject digest:
9c9effa81437b7414dee206f6b59b79bcdfaa87da4163cc558eb15b75335a43a - Sigstore transparency entry: 763573845
- Sigstore integration time:
-
Permalink:
tomasr8/pyjsx@5c5cf3bb17053f685967b07f34ac1d3431e826b2 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/tomasr8
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@5c5cf3bb17053f685967b07f34ac1d3431e826b2 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file python_jsx-0.4.0-py3-none-any.whl.
File metadata
- Download URL: python_jsx-0.4.0-py3-none-any.whl
- Upload date:
- Size: 18.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ba8da3b86e01a48cbda773e835938a521b82b760a1ebc099b6275258fabdcc5
|
|
| MD5 |
c3af4060f704c0d2e80d7ce380ee830f
|
|
| BLAKE2b-256 |
f12dc03a1102d8473bfcf40af0d201ddc34c61d4eedb5cc5916e81b4f4dfdec5
|
Provenance
The following attestation bundles were made for python_jsx-0.4.0-py3-none-any.whl:
Publisher:
release.yml on tomasr8/pyjsx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_jsx-0.4.0-py3-none-any.whl -
Subject digest:
4ba8da3b86e01a48cbda773e835938a521b82b760a1ebc099b6275258fabdcc5 - Sigstore transparency entry: 763573849
- Sigstore integration time:
-
Permalink:
tomasr8/pyjsx@5c5cf3bb17053f685967b07f34ac1d3431e826b2 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/tomasr8
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@5c5cf3bb17053f685967b07f34ac1d3431e826b2 -
Trigger Event:
workflow_dispatch
-
Statement type: