Skip to content

Commit b71d199

Browse files
author
xhlulu
committed
Update template to respect pyodide v0.18 recommended style
1 parent 175379b commit b71d199

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

public/index.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@
2525
Learn how to configure a non-root public URL by running `npm run build`.
2626
-->
2727
<title>React App</title>
28-
<script type="text/javascript">
29-
// set the pyodide files URL (packages.json, pyodide.asm.data etc)
30-
window.languagePluginUrl = 'https://cdn.jsdelivr.net/pyodide/v0.17.0/full/';
31-
</script>
32-
<script src="https://cdn.jsdelivr.net/pyodide/v0.17.0/full/pyodide.js"></script>
28+
<script src="https://cdn.jsdelivr.net/pyodide/v0.18.1/full/pyodide.js"></script>
3329
</head>
3430
<body>
3531
<noscript>You need to enable JavaScript to run this app.</noscript>

src/App.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import { useEffect, useState } from 'react';
2-
import script from './python/script.py';
2+
import script from './python/main.py';
33
import logo from './logo.svg';
44
import './App.css';
55

6-
function App() {
7-
const [output, setOutput] = useState("(loading...)");
6+
const runScript = async (code) => {
7+
const pyodide = await window.loadPyodide({
8+
indexURL : "https://cdn.jsdelivr.net/pyodide/v0.18.1/full/"
9+
});
810

9-
const runScript = code => {
10-
window.pyodide.loadPackage([]).then(() => {
11-
const output = window.pyodide.runPython(code);
12-
setOutput(output);
13-
})
14-
}
11+
return await pyodide.runPythonAsync(code);
12+
}
13+
14+
const App = () => {
15+
const [output, setOutput] = useState("(loading...)");
1516

1617
useEffect(() => {
17-
window.languagePluginLoader.then(() => {
18-
fetch(script)
19-
.then(src => src.text())
20-
.then(runScript)
21-
})
22-
})
18+
const run = async () => {
19+
const scriptText = await (await fetch(script)).text();
20+
const out = await runScript(scriptText);
21+
setOutput(out);
22+
}
23+
run();
24+
25+
}, []);
2326

2427
return (
2528
<div className="App">
File renamed without changes.

0 commit comments

Comments
 (0)