mirror of https://github.com/pyodide/pyodide.git
Highlight embedded python code in docs (#1252)
This commit is contained in:
parent
2771ded002
commit
264157a480
56
docs/conf.py
56
docs/conf.py
|
@ -141,3 +141,59 @@ epub_title = project
|
|||
|
||||
# A list of files that should not be packed into the epub file.
|
||||
epub_exclude_files = ["search.html"]
|
||||
|
||||
from pygments.lexer import bygroups, inherit, using
|
||||
from pygments.lexers import PythonLexer
|
||||
from pygments.lexers.javascript import JavascriptLexer
|
||||
from pygments.lexers.html import HtmlLexer
|
||||
from pygments.token import *
|
||||
|
||||
|
||||
class PyodideLexer(JavascriptLexer):
|
||||
tokens = {
|
||||
"root": [
|
||||
(
|
||||
rf"""(pyodide)(\.)(runPython|runPythonAsync)(\()(`)""",
|
||||
bygroups(
|
||||
Token.Name,
|
||||
Token.Operator,
|
||||
Token.Name,
|
||||
Token.Punctuation,
|
||||
Token.Literal.String.Single,
|
||||
),
|
||||
"python-code",
|
||||
),
|
||||
inherit,
|
||||
],
|
||||
"python-code": [
|
||||
(
|
||||
r"(.+?)(`)(\))",
|
||||
bygroups(
|
||||
using(PythonLexer), Token.Literal.String.Single, Token.Punctuation
|
||||
),
|
||||
"#pop",
|
||||
)
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
class HtmlPyodideLexer(HtmlLexer):
|
||||
tokens = {
|
||||
"script-content": [
|
||||
(
|
||||
r"(<)(\s*)(/)(\s*)(script)(\s*)(>)",
|
||||
bygroups(
|
||||
Punctuation, Text, Punctuation, Text, Name.Tag, Text, Punctuation
|
||||
),
|
||||
"#pop",
|
||||
),
|
||||
(r".+?(?=<\s*/\s*script\s*>)", using(PyodideLexer)),
|
||||
(r".+?\n", using(PyodideLexer), "#pop"),
|
||||
(r".+", using(PyodideLexer), "#pop"),
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def setup(app):
|
||||
app.add_lexer("pyodide", PyodideLexer)
|
||||
app.add_lexer("html-pyodide", HtmlPyodideLexer)
|
||||
|
|
|
@ -36,17 +36,15 @@ async function runPythonAsync(code, messageCallback, errorCallback) {
|
|||
```
|
||||
To make your own version of `runPython`:
|
||||
|
||||
```javascript
|
||||
pyodide.runPython(
|
||||
`
|
||||
```pyodide
|
||||
pyodide.runPython(`
|
||||
import pyodide
|
||||
old_eval_code = pyodide.eval_code
|
||||
def my_eval_code(code, ns):
|
||||
extra_info = None
|
||||
result = old_eval_code(code, ns)
|
||||
return [ns["extra_info"], result]
|
||||
`
|
||||
)
|
||||
`)
|
||||
|
||||
function myRunPython(code){
|
||||
return pyodide.globals.my_eval_code(code, pyodide.globals);
|
||||
|
@ -66,7 +64,7 @@ and returns a list of packages imported.
|
|||
## How can I execute code in a custom namespace?
|
||||
The second argument to `eval_code` is a namespace to execute the code in.
|
||||
The namespace is a python dictionary. So you can use:
|
||||
```javascript
|
||||
```pyodide
|
||||
pyodide.runPython(`
|
||||
my_namespace = { "x" : 2, "y" : 7 }
|
||||
def eval_in_my_namespace(code):
|
||||
|
@ -141,7 +139,7 @@ let my_module = {
|
|||
pyodide.registerJsModule("my_js_module", my_module);
|
||||
```
|
||||
You can import your package like a normal Python package:
|
||||
```
|
||||
```py
|
||||
import my_js_module
|
||||
from my_js_module.submodule import h, c
|
||||
assert my_js_module.f(7) == 50
|
||||
|
|
|
@ -22,10 +22,10 @@ is a `Promise`, which you must call `then` on to complete initialization. When
|
|||
the promise resolves, pyodide will have installed a namespace in global scope:
|
||||
`pyodide`.
|
||||
|
||||
```javascript
|
||||
```pyodide
|
||||
languagePluginLoader.then(() => {
|
||||
// pyodide is now ready to use...
|
||||
console.log(pyodide.runPython('import sys\nsys.version'));
|
||||
console.log(pyodide.runPython(`import sys\nsys.version`));
|
||||
});
|
||||
```
|
||||
|
||||
|
@ -36,7 +36,7 @@ function. It takes as input a string of Python
|
|||
code. If the code ends in an expression, it returns the result of the
|
||||
expression, converted to Javascript objects (see {ref}`type_conversions`).
|
||||
|
||||
```javascript
|
||||
```pyodide
|
||||
pyodide.runPython(`
|
||||
import sys
|
||||
sys.version
|
||||
|
@ -49,7 +49,7 @@ See {ref}`loading_packages` documentation to load additional packages.
|
|||
## Complete example
|
||||
|
||||
Create and save a test `index.html` page with the following contents:
|
||||
```html
|
||||
```html-pyodide
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
|
@ -68,7 +68,7 @@ Create and save a test `index.html` page with the following contents:
|
|||
import sys
|
||||
sys.version
|
||||
`));
|
||||
console.log(pyodide.runPython('print(1 + 2)'));
|
||||
console.log(pyodide.runPython(`print(1 + 2)`));
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
|
Loading…
Reference in New Issue