Skip to content

Commit 480beeb

Browse files
committed
feat: allow py as language identifier
1 parent e71429f commit 480beeb

3 files changed

Lines changed: 50 additions & 49 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ python:
4545
To add an interactive Python code runner, use the `monaco-run` directive:
4646

4747
````md
48-
```python {monaco-run}
48+
```py {monaco-run}
4949
from termcolor import colored
5050

5151
print(colored("Hello, Slidev!", "blue"))

setup/code-runners.ts

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { useNav } from '@slidev/client'
66
let pyodideCache: PyodideInterface | null = null
77
let pyodideOptionCache = "{}"
88
async function setupPyodide(options = {}, code) {
9-
109
const {
1110
installs = [],
1211
prelude = "",
@@ -61,55 +60,57 @@ warnings.filterwarnings("ignore", category=DeprecationWarning)
6160

6261
export default defineCodeRunnersSetup(() => {
6362
const { slides } = useNav()
64-
return {
65-
async python(code) {
66-
// @ts-expect-error
67-
const pyodide = await setupPyodide(slides.value[0].meta.slide.frontmatter?.python, code)
68-
const texts = ref([''])
69-
const extras = ref<CodeRunnerOutput[]>([])
70-
const decoder = new TextDecoder('utf-8');
71-
function write(buffer: Uint8Array) {
72-
const text = decoder.decode(buffer)
73-
for (const line of text.split('\n')) {
74-
texts.value[texts.value.length - 1] += line
75-
texts.value.push('')
76-
}
77-
return buffer.length
63+
async function run(code: string) {
64+
// @ts-expect-error
65+
const pyodide = await setupPyodide(slides.value[0].meta.slide.frontmatter?.python, code)
66+
const texts = ref([''])
67+
const extras = ref<CodeRunnerOutput[]>([])
68+
const decoder = new TextDecoder('utf-8');
69+
function write(buffer: Uint8Array) {
70+
const text = decoder.decode(buffer)
71+
for (const line of text.split('\n')) {
72+
texts.value[texts.value.length - 1] += line
73+
texts.value.push('')
7874
}
79-
pyodide.setStdout({
80-
write: write,
81-
isatty: true,
82-
})
83-
pyodide.setStderr({
84-
write: write,
85-
isatty: true,
86-
})
87-
pyodide.runPythonAsync(code).catch(err => {
88-
console.error(err)
89-
const str = err.toString()
90-
const matchNotFoundError = str.match(/ModuleNotFoundError: No module named '(.*)'/)
91-
if (matchNotFoundError) {
75+
return buffer.length
76+
}
77+
pyodide.setStdout({
78+
write: write,
79+
isatty: true,
80+
})
81+
pyodide.setStderr({
82+
write: write,
83+
isatty: true,
84+
})
85+
pyodide.runPythonAsync(code).catch(err => {
86+
console.error(err)
87+
const str = err.toString()
88+
const matchNotFoundError = str.match(/ModuleNotFoundError: No module named '(.*)'/)
89+
if (matchNotFoundError) {
90+
extras.value.push({
91+
html: [
92+
`<div class="text-red">${matchNotFoundError[0]}</div>`,
93+
`<div class="text-blue">Tip: This may because of this package is not a <a href="https://pyodide.org/en/stable/usage/packages-in-pyodide.html">Pyodide builtin package</a>.`,
94+
"<br>You may need to install it by adding the package name to the `python.installs` array in your headmatter.",
95+
`</div>`
96+
].join('')
97+
})
98+
} else {
99+
for (const line of str.split('\n')) {
92100
extras.value.push({
93-
html: [
94-
`<div class="text-red">${matchNotFoundError[0]}</div>`,
95-
`<div class="text-blue">Tip: This may because of this package is not a <a href="https://pyodide.org/en/stable/usage/packages-in-pyodide.html">Pyodide builtin package</a>.`,
96-
"<br>You may need to install it by adding the package name to the `python.installs` array in your headmatter.",
97-
`</div>`
98-
].join('')
101+
text: line,
102+
class: 'text-red'
99103
})
100-
} else {
101-
for (const line of str.split('\n')) {
102-
extras.value.push({
103-
text: line,
104-
class: 'text-red'
105-
})
106-
}
107104
}
108-
});
109-
return () => [
110-
...texts.value.map(text => ({ text, highlightLang: 'ansi' })),
111-
...extras.value,
112-
]
113-
},
105+
}
106+
});
107+
return () => [
108+
...texts.value.map(text => ({ text, highlightLang: 'ansi' })),
109+
...extras.value,
110+
]
111+
}
112+
return {
113+
python: run,
114+
py: run,
114115
}
115116
})

slides.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ python:
2424

2525
# Python Runner for Slidev
2626

27-
```python {monaco-run}
27+
```py {monaco-run}
2828
from termcolor import colored
2929
import pandas as pd
3030
import numpy as np

0 commit comments

Comments
 (0)