mirror of
https://github.com/johndoe6345789/snippet-pastebin.git
synced 2026-04-24 13:34:55 +00:00
Generated by Spark: Fix all reported errors.
This commit is contained in:
@@ -44,7 +44,7 @@ export function PythonOutput({ code }: PythonOutputProps) {
|
||||
|
||||
try {
|
||||
const result = await runPythonCode(code)
|
||||
setOutput(result.output)
|
||||
setOutput(result.output || '')
|
||||
if (result.error) {
|
||||
setError(result.error)
|
||||
}
|
||||
|
||||
@@ -1,66 +1,55 @@
|
||||
import { loadPyodide, PyodideInterface } from 'pyodide'
|
||||
|
||||
let pyodideInstance: PyodideInterface | null = null
|
||||
let pyodideLoading: Promise<PyodideInterface> | null = null
|
||||
|
||||
if (pyodideLoading) {
|
||||
}
|
||||
pyodideLoading = loadPyo
|
||||
}
|
||||
|
||||
if (pyodideLoading) {
|
||||
return pyodideLoading
|
||||
}
|
||||
|
||||
pyodideLoading = loadPyodide({
|
||||
indexURL: 'https://cdn.jsdelivr.net/pyodide/v0.29.1/full/',
|
||||
}).then((pyodide) => {
|
||||
pyodideInstance = pyodide
|
||||
return pyodide
|
||||
})
|
||||
|
||||
output: stdout
|
||||
|
||||
|
||||
|
||||
if
|
||||
}
|
||||
|
||||
return {
|
||||
erro
|
||||
}
|
||||
|
||||
return pyodideInstanc
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
output: stdout || '',
|
||||
error: stderr || (err instanceof Error ? err.message : String(err))
|
||||
}
|
||||
}
|
||||
|
||||
const stdout = pyodide.runPython('sys.stdout.getvalue()')
|
||||
|
||||
let output = stdout || ''
|
||||
if (result !== undefined && result !== null) {
|
||||
output += (output ? '\n' : '') + String(result)
|
||||
}
|
||||
|
||||
return { output: output || '(no output)' }
|
||||
} catch (err) {
|
||||
return {
|
||||
output: '',
|
||||
error: err instanceof Error ? err.message : String(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function isPyodideReady(): boolean {
|
||||
return pyodideInstance !== null
|
||||
}
|
||||
import { loadPyodide, PyodideInterface } from 'pyodide'
|
||||
|
||||
let pyodideInstance: PyodideInterface | null = null
|
||||
let pyodideLoading: Promise<PyodideInterface> | null = null
|
||||
|
||||
export async function getPyodide(): Promise<PyodideInterface> {
|
||||
if (pyodideInstance) {
|
||||
return pyodideInstance
|
||||
}
|
||||
|
||||
if (pyodideLoading) {
|
||||
return pyodideLoading
|
||||
}
|
||||
|
||||
pyodideLoading = loadPyodide({
|
||||
indexURL: 'https://cdn.jsdelivr.net/pyodide/v0.29.1/full/',
|
||||
}).then((pyodide) => {
|
||||
pyodideInstance = pyodide
|
||||
return pyodide
|
||||
})
|
||||
|
||||
return pyodideLoading
|
||||
}
|
||||
|
||||
export async function runPythonCode(code: string): Promise<{ output?: string; error?: string }> {
|
||||
try {
|
||||
const pyodide = await getPyodide()
|
||||
|
||||
pyodide.runPython(`
|
||||
import sys
|
||||
from io import StringIO
|
||||
sys.stdout = StringIO()
|
||||
sys.stderr = StringIO()
|
||||
`)
|
||||
|
||||
const result = pyodide.runPython(code)
|
||||
const stdout = pyodide.runPython('sys.stdout.getvalue()')
|
||||
|
||||
let output = stdout || ''
|
||||
if (result !== undefined && result !== null) {
|
||||
output += (output ? '\n' : '') + String(result)
|
||||
}
|
||||
|
||||
return { output: output || '(no output)' }
|
||||
} catch (err) {
|
||||
return {
|
||||
output: '',
|
||||
error: err instanceof Error ? err.message : String(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function isPyodideReady(): boolean {
|
||||
return pyodideInstance !== null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user