|
| 1 | +--- |
| 2 | +sidebar_position: 100 |
| 3 | +--- |
| 4 | + |
| 5 | +# Python Code Blocks |
| 6 | + |
| 7 | +Markdown preformatted code blocks with the `python` language identifier are |
| 8 | +automatically converted to runnable and editable code blocks thanks |
| 9 | +to [react-py](https://github.com/elilambnz/react-py). |
| 10 | +See the react-py repo and documentation for more info. |
| 11 | + |
| 12 | +For example, in a Markdown docs file: |
| 13 | + |
| 14 | +```` |
| 15 | +```python |
| 16 | +print("Hello, World!") |
| 17 | +``` |
| 18 | +```` |
| 19 | + |
| 20 | +Becomes: |
| 21 | + |
| 22 | +```python |
| 23 | +print("Hello, World!") |
| 24 | +``` |
| 25 | + |
| 26 | +Play and reset buttons appear when hovering over the code windows. |
| 27 | + |
| 28 | +Non-runnable preformatted python blocks can be included by using `py` as the |
| 29 | +language identifier. |
| 30 | + |
| 31 | +```` |
| 32 | +```py |
| 33 | +print("Hello, World!") |
| 34 | +``` |
| 35 | +```` |
| 36 | + |
| 37 | +Becomes: |
| 38 | + |
| 39 | +```py |
| 40 | +print("Hello, World!") |
| 41 | +``` |
| 42 | + |
| 43 | +## Modifying the Default Code Editor / Runner |
| 44 | + |
| 45 | +The `CodeEditor` component is located in `src/components/CodeEditor.js`. |
| 46 | +This code editor uses font files located in `static/fonts` for the run and |
| 47 | +reset buttons. Modifying this will alter all code editing windows. |
| 48 | + |
| 49 | +Code windows are automatically inserted into documents by the modified |
| 50 | +`CodeBlock` theme component located in `src/theme/CodeBlock/index.js`. This |
| 51 | +is a good place to pass any global/default props to the `CodeEditor` |
| 52 | +component – for example, globally setting the `showButtons` prop to have |
| 53 | +play and reset buttons always visible. |
| 54 | + |
| 55 | +The `PythonProvider` required by `react-py` is located in the `Root` theme |
| 56 | +component in `src/theme/Root.js`. This is where lazy-loading or any other |
| 57 | +global `react-py` settings can be set. |
| 58 | + |
| 59 | +## Notes |
| 60 | + |
| 61 | +If you intend to have several runnable code windows on a single page, it is |
| 62 | +recommended to leave the react-py `lazy-loading` flag on. This means code |
| 63 | +runner workers are only spawned when a code block is run. Without this, it |
| 64 | +is likely several of the code windows will not work on weaker |
| 65 | +browsers/computers. |
| 66 | + |
| 67 | +## A Note on Usability |
| 68 | + |
| 69 | +The default implementation of runnable and editable python code blocks on this |
| 70 | +site is keyboard-navigable. |
| 71 | + |
| 72 | +When navigating with the keyboard using <kbd>tab</kbd> or <kbd>shift + |
| 73 | +tab</kbd>, code blocks can be focused, but do NOT capture tabs to indent code. |
| 74 | +Tabs are only captured when the code block is either clicked on, or some other |
| 75 | +key is used to interact with the code block (e.g. arrow keys, typing |
| 76 | +characters) — at which point tabs and shift-tabs will indent and dedent the |
| 77 | +code. |
| 78 | +To go back to navigating the page, the escape key can be pressed — at which |
| 79 | +point tabs will work as before. |
| 80 | + |
| 81 | +Try it below: |
| 82 | + |
| 83 | +### The First Code Block |
| 84 | + |
| 85 | +```python |
| 86 | +def main(): |
| 87 | + print("Hello, Code Block 1!") |
| 88 | + |
| 89 | +main() |
| 90 | +``` |
| 91 | + |
| 92 | +### The second Code Block |
| 93 | + |
| 94 | +```python |
| 95 | +def main(): |
| 96 | + print("Hello, Code Block 2!") |
| 97 | + |
| 98 | +main() |
| 99 | +``` |
0 commit comments