Skip to content

Commit

Permalink
Update template to respect pyodide v0.18 recommended style
Browse files Browse the repository at this point in the history
  • Loading branch information
xhlulu committed Sep 19, 2021
1 parent 175379b commit b71d199
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
6 changes: 1 addition & 5 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<script type="text/javascript">
// set the pyodide files URL (packages.json, pyodide.asm.data etc)
window.languagePluginUrl = 'https://cdn.jsdelivr.net/pyodide/v0.17.0/full/';
</script>
<script src="https://cdn.jsdelivr.net/pyodide/v0.17.0/full/pyodide.js"></script>
<script src="https://cdn.jsdelivr.net/pyodide/v0.18.1/full/pyodide.js"></script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
33 changes: 18 additions & 15 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { useEffect, useState } from 'react';
import script from './python/script.py';
import script from './python/main.py';
import logo from './logo.svg';
import './App.css';

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

const runScript = code => {
window.pyodide.loadPackage([]).then(() => {
const output = window.pyodide.runPython(code);
setOutput(output);
})
}
return await pyodide.runPythonAsync(code);
}

const App = () => {
const [output, setOutput] = useState("(loading...)");

useEffect(() => {
window.languagePluginLoader.then(() => {
fetch(script)
.then(src => src.text())
.then(runScript)
})
})
const run = async () => {
const scriptText = await (await fetch(script)).text();
const out = await runScript(scriptText);
setOutput(out);
}
run();

}, []);

return (
<div className="App">
Expand Down
File renamed without changes.

0 comments on commit b71d199

Please sign in to comment.