python-jsx


Namepython-jsx JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryJSX transpiler for Python
upload_time2024-08-31 22:01:17
maintainerNone
docs_urlNone
authorNone
requires_python>=3.12
licenseNone
keywords jsx react transpiler
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align="center">
  <img src="logo_bungee_tint.svg" />
</p>

# PyJSX - Write JSX directly in Python

```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:

```sh
pip install python-jsx
```

## Minimal example

```python
# hello.py
# coding: jsx
from pyjsx import jsx

def hello():
    print(<h1>Hello, world!</h1>)
```

```python
# main.py
from pyjsx import auto_setup

from hello import hello

hello()
```

```sh
$ python main.py
<h1>Hello, word!</h1>
```

> [!TIP]
> There are more examples available in the [examples folder](examples).

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 `from pyjsx import auto_setup`. This must occur before importing
any other file containing JSX.

## Supported grammar

The full [JSX grammar](https://facebook.github.io/jsx/) is supported.
Here are a few examples:

### Normal and self-closing tags

```python
x = <div></div>
y = <img />
```

### Props

```python
<a href="example.com">Click me!</a>
<div style={{"color": "red"}}>This is red</div>
<span {...props}>Spread operator</span>
```

### Nested expressions

```python
<div>
    {[<p>Row: {i}</p> for i in range(10)]}
</div>
```

### Fragments

```python
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.

```python
def Header(children, **rest):
    return <h1>{children}</h1>

header = <Header>Title</Header>
print(header)
```

## Prior art

Inspired by [packed](https://github.com/michaeljones/packed) and
[pyxl4](https://github.com/pyxl4/pyxl4).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "python-jsx",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12",
    "maintainer_email": null,
    "keywords": "JSX, React, transpiler",
    "author": null,
    "author_email": "Tomas Roun <tomas.roun8@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/e4/0d/e98ce8d49a74e347093a6f4f5bf85daa6d67d6640480490a4593e8bdc046/python_jsx-0.1.0.tar.gz",
    "platform": null,
    "description": "<p align=\"center\">\n  <img src=\"logo_bungee_tint.svg\" />\n</p>\n\n# PyJSX - Write JSX directly in Python\n\n```python\n# coding: jsx\nfrom pyjsx import jsx, JSX\n\ndef Header(children, style=None, **rest) -> JSX:\n    return <h1 style={style}>{children}</h1>\n\ndef Main(children, **rest) -> JSX:\n    return <main>{children}</main>\n\ndef App() -> JSX:\n    return (\n        <div>\n            <Header style={{\"color\": \"red\"}}>Hello, world!</Header>\n            <Main>\n                <p>This was rendered with PyJSX!</p>\n            </Main>\n        </div>\n    )\n```\n\n## Installation\n\nGet it via pip:\n\n```sh\npip install python-jsx\n```\n\n## Minimal example\n\n```python\n# hello.py\n# coding: jsx\nfrom pyjsx import jsx\n\ndef hello():\n    print(<h1>Hello, world!</h1>)\n```\n\n```python\n# main.py\nfrom pyjsx import auto_setup\n\nfrom hello import hello\n\nhello()\n```\n\n```sh\n$ python main.py\n<h1>Hello, word!</h1>\n```\n\n> [!TIP]\n> There are more examples available in the [examples folder](examples).\n\nEach file containing JSX must contain two things:\n\n- `# coding: jsx` directive - This tells Python to let our library parse the\n  file first.\n- `from pyjsx import jsx` import. PyJSX transpiles JSX into `jsx(...)` calls so\n  it must be in scope.\n\nTo run a file containing JSX, the `jsx` codec must be registered first which can\nbe done with `from pyjsx import auto_setup`. This must occur before importing\nany other file containing JSX.\n\n## Supported grammar\n\nThe full [JSX grammar](https://facebook.github.io/jsx/) is supported.\nHere are a few examples:\n\n### Normal and self-closing tags\n\n```python\nx = <div></div>\ny = <img />\n```\n\n### Props\n\n```python\n<a href=\"example.com\">Click me!</a>\n<div style={{\"color\": \"red\"}}>This is red</div>\n<span {...props}>Spread operator</span>\n```\n\n### Nested expressions\n\n```python\n<div>\n    {[<p>Row: {i}</p> for i in range(10)]}\n</div>\n```\n\n### Fragments\n\n```python\nfragment = (\n    <>\n        <p>1st paragraph</p>\n        <p>2nd paragraph</p>\n    </>\n)\n```\n\n### Custom components\n\nA custom component can be any function that takes `**kwargs` and\nreturns JSX or a plain string. The special prop `children` is a list\ncontaining the element's children.\n\n```python\ndef Header(children, **rest):\n    return <h1>{children}</h1>\n\nheader = <Header>Title</Header>\nprint(header)\n```\n\n## Prior art\n\nInspired by [packed](https://github.com/michaeljones/packed) and\n[pyxl4](https://github.com/pyxl4/pyxl4).\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "JSX transpiler for Python",
    "version": "0.1.0",
    "project_urls": {
        "Github": "https://github.com/tomasr8/pyjsx",
        "Homepage": "https://github.com/tomasr8/pyjsx"
    },
    "split_keywords": [
        "jsx",
        " react",
        " transpiler"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "88e3eef4fde6538e6f581d7aed530ff0b732919fbe79725f6e00c2383b06e771",
                "md5": "6663564a101592269cb0248b70cd2b54",
                "sha256": "2ca8d4c3e9376441642d58dc7da613b76c36bf9c687768aadf4b03134ef8a77a"
            },
            "downloads": -1,
            "filename": "python_jsx-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6663564a101592269cb0248b70cd2b54",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.12",
            "size": 10980,
            "upload_time": "2024-08-31T22:01:16",
            "upload_time_iso_8601": "2024-08-31T22:01:16.292174Z",
            "url": "https://files.pythonhosted.org/packages/88/e3/eef4fde6538e6f581d7aed530ff0b732919fbe79725f6e00c2383b06e771/python_jsx-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e40de98ce8d49a74e347093a6f4f5bf85daa6d67d6640480490a4593e8bdc046",
                "md5": "ec3928a22ee21ea0291115c7de08f933",
                "sha256": "d4d4e2be80a420ba7dded7ffbb1bf37b777ba82329bd9b074bea369c8343a5e7"
            },
            "downloads": -1,
            "filename": "python_jsx-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "ec3928a22ee21ea0291115c7de08f933",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12",
            "size": 21634,
            "upload_time": "2024-08-31T22:01:17",
            "upload_time_iso_8601": "2024-08-31T22:01:17.791309Z",
            "url": "https://files.pythonhosted.org/packages/e4/0d/e98ce8d49a74e347093a6f4f5bf85daa6d67d6640480490a4593e8bdc046/python_jsx-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-31 22:01:17",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tomasr8",
    "github_project": "pyjsx",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "python-jsx"
}
        
Elapsed time: 0.41877s