JSX transpiler for Python
Project description
PyJSX - Write JSX directly in Python
# 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>
)
Installation
Get it via pip:
pip install python-jsx
Minimal example
# hello.py
# coding: jsx
from pyjsx import jsx
def hello():
print(<h1>Hello, world!</h1>)
# main.py
from pyjsx import auto_setup
from hello import hello
hello()
$ python main.py
<h1>Hello, word!</h1>
[!TIP] There are more examples available in the examples folder.
Each file containing JSX must contain two things:
# coding: jsx
directive - This tells Python to let our library parse the file first.from pyjsx import jsx
import. 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 from pyjsx import auto_setup
. 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)
Prior art
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file python_jsx-0.1.0.tar.gz
.
File metadata
- Download URL: python_jsx-0.1.0.tar.gz
- Upload date:
- Size: 21.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4d4e2be80a420ba7dded7ffbb1bf37b777ba82329bd9b074bea369c8343a5e7 |
|
MD5 | ec3928a22ee21ea0291115c7de08f933 |
|
BLAKE2b-256 | e40de98ce8d49a74e347093a6f4f5bf85daa6d67d6640480490a4593e8bdc046 |
File details
Details for the file python_jsx-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: python_jsx-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ca8d4c3e9376441642d58dc7da613b76c36bf9c687768aadf4b03134ef8a77a |
|
MD5 | 6663564a101592269cb0248b70cd2b54 |
|
BLAKE2b-256 | 88e3eef4fde6538e6f581d7aed530ff0b732919fbe79725f6e00c2383b06e771 |