|
| 1 | +# Python editor |
| 2 | + |
| 3 | +The PyEditor is a core plugin. |
| 4 | + |
| 5 | +!!! warning |
| 6 | + |
| 7 | + Work on the Python editor is in its early stages. We have made it available |
| 8 | + in this version of PyScript to give the community an opportunity to play, |
| 9 | + experiment and provide feedback. |
| 10 | + |
| 11 | + Future versions of PyScript will include a more refined, robust and perhaps |
| 12 | + differently behaving version of the Python editor. |
| 13 | + |
| 14 | +If you specify the type of a `<script>` tag as either `py-editor` (for Pyodide) |
| 15 | +or `mpy-editor` (for MicroPython), the plugin creates a visual code editor, |
| 16 | +with code highlighting and a "run" button to execute the editable code |
| 17 | +contained therein in a non-blocking worker. |
| 18 | + |
| 19 | +The interpreter is not loaded onto the page until the run button is clicked. By |
| 20 | +default each editor has its own independent instance of the specified |
| 21 | +interpreter: |
| 22 | + |
| 23 | +```html title="Two editors, one with Pyodide, the other with MicroPython." |
| 24 | +<script type="py-editor"> |
| 25 | + import sys |
| 26 | + print(sys.version) |
| 27 | +</script> |
| 28 | +<script type="mpy-editor"> |
| 29 | + import sys |
| 30 | + print(sys.version) |
| 31 | + a = 42 |
| 32 | + print(a) |
| 33 | +</script> |
| 34 | +``` |
| 35 | + |
| 36 | +However, different editors can share the same interpreter if they share the |
| 37 | +same `env` attribute value. |
| 38 | + |
| 39 | +```html title="Two editors sharing the same MicroPython environment." |
| 40 | +<script type="mpy-editor" env="shared"> |
| 41 | + if not 'a' in globals(): |
| 42 | + a = 1 |
| 43 | + else: |
| 44 | + a += 1 |
| 45 | + print(a) |
| 46 | +</script> |
| 47 | +<script type="mpy-editor" env="shared"> |
| 48 | + # doubled a |
| 49 | + print(a * 2) |
| 50 | +</script> |
| 51 | +``` |
| 52 | + |
| 53 | +The outcome of these code fragments should look something like this: |
| 54 | + |
| 55 | +<img src="../../assets/images/pyeditor1.gif" style="border: 1px solid black; border-radius: 0.2rem; box-shadow: var(--md-shadow-z1);"/> |
| 56 | + |
| 57 | +!!! info |
| 58 | + |
| 59 | + Notice that the interpreter type, and optional environment name is shown |
| 60 | + at the top right above the Python editor. |
| 61 | + |
| 62 | + Hovering over the Python editor reveals the "run" button. |
| 63 | + |
| 64 | +Finally, it is possible to specify a target node into which the code editor is |
| 65 | +created: |
| 66 | + |
| 67 | +```html title="Specify a target for the Python editor." |
| 68 | +<script type="mpy-editor" target="editor"> |
| 69 | + import sys |
| 70 | + print(sys.version) |
| 71 | +</script> |
| 72 | +<div id="editor"></div> <!-- will eventually contain the Python editor --> |
| 73 | +``` |
0 commit comments