Skip to content

Commit 450fd3b

Browse files
committed
Update readme
1 parent 29fab0d commit 450fd3b

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

README.md

+43-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ Get it via pip:
3333
pip install python-jsx
3434
```
3535

36-
## Minimal example
36+
## Minimal example (using the `coding` directive)
37+
38+
> [!TIP]
39+
> There are more examples available in the [examples folder](examples).
40+
41+
There are two supported ways to seamlessly integrate JSX into your codebase.
42+
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).
3743

3844
```python
3945
# hello.py
@@ -58,9 +64,6 @@ $ python main.py
5864
<h1>Hello, word!</h1>
5965
```
6066

61-
> [!TIP]
62-
> There are more examples available in the [examples folder](examples).
63-
6467
Each file containing JSX must contain two things:
6568

6669
- `# coding: jsx` directive - This tells Python to let our library parse the
@@ -72,6 +75,42 @@ To run a file containing JSX, the `jsx` codec must be registered first which can
7275
be done with `from pyjsx import auto_setup`. This must occur before importing
7376
any other file containing JSX.
7477

78+
## Minimal example (using an import hook)
79+
80+
> [!TIP]
81+
> There are more examples available in the [examples folder](examples).
82+
83+
```python
84+
# hello.px
85+
from pyjsx import jsx
86+
87+
def hello():
88+
print(<h1>Hello, world!</h1>)
89+
```
90+
91+
```python
92+
# main.py
93+
from pyjsx import auto_setup
94+
95+
from hello import hello
96+
97+
hello()
98+
```
99+
100+
```sh
101+
$ python main.py
102+
<h1>Hello, word!</h1>
103+
```
104+
105+
Each file containing JSX must contain two things:
106+
107+
- The file extension must be `.px`
108+
- `from pyjsx import jsx` import. PyJSX transpiles JSX into `jsx(...)` calls so
109+
it must be in scope.
110+
111+
To be able to import `.px`, the import hook must be registered first which can
112+
be done with `from pyjsx import auto_setup` (same as for the codec version). This must occur before importing any other file containing JSX.
113+
75114
## Supported grammar
76115

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

0 commit comments

Comments
 (0)