@@ -33,7 +33,13 @@ Get it via pip:
33
33
pip install python-jsx
34
34
```
35
35
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 ) .
37
43
38
44
``` python
39
45
# hello.py
@@ -58,9 +64,6 @@ $ python main.py
58
64
< h1> Hello, word! < /h1>
59
65
```
60
66
61
- > [ !TIP]
62
- > There are more examples available in the [ examples folder] ( examples ) .
63
-
64
67
Each file containing JSX must contain two things:
65
68
66
69
- ` # 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
72
75
be done with ` from pyjsx import auto_setup ` . This must occur before importing
73
76
any other file containing JSX.
74
77
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
+
75
114
## Supported grammar
76
115
77
116
The full [ JSX grammar] ( https://facebook.github.io/jsx/ ) is supported.
0 commit comments