Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasr8 committed Jan 26, 2025
1 parent 29fab0d commit 450fd3b
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ Get it via pip:
pip install python-jsx
```

## Minimal example
## Minimal example (using the `coding` directive)

> [!TIP]
> There are more examples available in the [examples folder](examples).
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](#minimal-example-using-an-import-hook).

```python
# hello.py
Expand All @@ -58,9 +64,6 @@ $ 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
Expand All @@ -72,6 +75,42 @@ 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.

## Minimal example (using an import hook)

> [!TIP]
> There are more examples available in the [examples folder](examples).
```python
# hello.px
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>
```

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 `from pyjsx import auto_setup` (same as for the codec version). This must occur before importing any other file containing JSX.

## Supported grammar

The full [JSX grammar](https://facebook.github.io/jsx/) is supported.
Expand Down

0 comments on commit 450fd3b

Please sign in to comment.