@@ -6,7 +6,6 @@ import { useNav } from '@slidev/client'
66let pyodideCache : PyodideInterface | null = null
77let pyodideOptionCache = "{}"
88async function setupPyodide ( options = { } , code ) {
9-
109 const {
1110 installs = [ ] ,
1211 prelude = "" ,
@@ -61,55 +60,57 @@ warnings.filterwarnings("ignore", category=DeprecationWarning)
6160
6261export 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 ( / M o d u l e N o t F o u n d E r r o r : N o m o d u l e n a m e d ' ( .* ) ' / )
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 ( / M o d u l e N o t F o u n d E r r o r : N o m o d u l e n a m e d ' ( .* ) ' / )
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} )
0 commit comments