Skip to main content

JSX transpiler for Python

Project description

PyJSX - Write JSX directly in Python

PyPI - Version

👉 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: jsx directive - This tells Python to let our library parse the file first.
  • from pyjsx import jsx import. PyJSX transpiles JSX into jsx(...) 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 jsx import. PyJSX transpiles JSX into jsx(...) 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)

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

Inspired by packed and pyxl4.

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

python_jsx-0.3.0.tar.gz (110.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_jsx-0.3.0-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file python_jsx-0.3.0.tar.gz.

File metadata

  • Download URL: python_jsx-0.3.0.tar.gz
  • Upload date:
  • Size: 110.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for python_jsx-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5d6e568606955e37f4d299f330caf2c1c4a2d5fbfffb1c8a9db54e50bb9ec643
MD5 6b907b4895aece98d6ab04b9ca34a580
BLAKE2b-256 911814a601e6938a72423e77eabcf7614a80bedba585c242a48c8069db031641

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_jsx-0.3.0.tar.gz:

Publisher: release.yml on tomasr8/pyjsx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_jsx-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: python_jsx-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for python_jsx-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98a3c8a054537b3191c4e08d9413b4a248b459c37b345e2ae587da0842cab1c3
MD5 9a24476a29a1e9ab8778ef35f32a50c3
BLAKE2b-256 e9918f2d8f31a9fdac7cd4471c0487af2d07f5f0ae32136afb0bc173ee3ad270

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_jsx-0.3.0-py3-none-any.whl:

Publisher: release.yml on tomasr8/pyjsx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page