Skip to main content

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)

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

Inspired by packed and pyxl4.

Metadata

Release files for python-jsx 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-jsx 0.4.0
File Size Uploaded
python_jsx-0.4.0.tar.gz 111.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-jsx 0.4.0
File Interpreter ABI Platform
python_jsx-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 129.2 kB

Release files / python_jsx-0.4.0.tar.gz

Download URL python_jsx-0.4.0.tar.gz
Size 111.1 kB
Tags Source
SHA-256 checksum
How to use checksums
9c9effa81437b7414dee206f6b59b79bcdfaa87da4163cc558eb15b75335a43a
BLAKE2b-256 checksum
How to use checksums
452e9201b6dd0b136c92ad4e121c6520680ff12fd948e68cd2ee3f1c10a9ebad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 13, 2025.

Transparency log

Release files / python_jsx-0.4.0-py3-none-any.whl

Download URL python_jsx-0.4.0-py3-none-any.whl
Size 18.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4ba8da3b86e01a48cbda773e835938a521b82b760a1ebc099b6275258fabdcc5
BLAKE2b-256 checksum
How to use checksums
f12dc03a1102d8473bfcf40af0d201ddc34c61d4eedb5cc5916e81b4f4dfdec5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 13, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page