-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xlwings Lite: Replace PyScript with (updated) Pyodide (#233)
Pyscript removed and Pyodide upgraded from 0.26.4 to 0.27.2
- Loading branch information
Showing
84 changed files
with
440 additions
and
1,002 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
xlwings==0.33.6 | ||
pandas==2.2.0 | ||
pandas==2.2.3 | ||
python-dotenv==1.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
async function initPyodide() { | ||
if (config.onLite === false) { | ||
return; | ||
} | ||
const globalStatusAlert = document.querySelector("#global-status-alert"); | ||
if (globalStatusAlert) { | ||
globalStatusAlert.classList.remove("d-none"); | ||
globalStatusAlert.querySelector("span").innerHTML = ` | ||
<div class="spinner-border spinner-border-sm text-alert me-1" role="status"> | ||
<span class="visually-hidden">Loading...</span> | ||
</div> | ||
Loading... | ||
`; | ||
} | ||
|
||
let pyodide = await loadPyodide(); | ||
const pyodideConfigResponse = await fetch( | ||
config.appPath + "/xlwings/pyodide.json", | ||
); | ||
const pyodideConfigData = await pyodideConfigResponse.json(); | ||
// Install dependencies | ||
await pyodide.loadPackage("micropip"); | ||
const micropip = pyodide.pyimport("micropip"); | ||
let packages = pyodideConfigData["packages"]; | ||
await micropip.install(packages); | ||
// Python files | ||
const files = pyodideConfigData["files"]; | ||
function createDirectories(files) { | ||
const createdDirs = new Set(); | ||
Object.values(files).forEach((localPath) => { | ||
const parts = localPath.split("/"); | ||
if (parts.length > 1) { | ||
const dirPath = parts.slice(0, parts.length - 1).join("/"); | ||
if (!createdDirs.has(dirPath)) { | ||
try { | ||
// TODO: does this work for nested dirs? Also, ./ is probably wrong | ||
if (dirPath !== ".") { | ||
pyodide.FS.mkdir(dirPath); | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
try { | ||
if (dirPath !== ".") { | ||
pyodide.FS.mount(pyodide.FS.filesystems.MEMFS, {}, dirPath); | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
createdDirs.add(dirPath); | ||
} | ||
} | ||
}); | ||
} | ||
createDirectories(files); | ||
for (const [endpoint, localPath] of Object.entries(files)) { | ||
const response = await fetch(config.appPath + endpoint); | ||
const content = await response.text(); | ||
pyodide.FS.writeFile(localPath, content); | ||
} | ||
try { | ||
// Entrypoint | ||
let mainText = pyodide.FS.readFile("./main.py", { encoding: "utf8" }); | ||
await pyodide.runPythonAsync(mainText); | ||
// Functions | ||
const liteCustomFunctionsCall = pyodide.globals.get( | ||
"custom_functions_call", | ||
); | ||
const liteCustomScriptsCall = pyodide.globals.get("custom_scripts_call"); | ||
// You can't simply export custom_functions_call as it will be null when used in | ||
// custom-functions-code.js (it's only assigned the function here). | ||
globalThis.liteCustomFunctionsCall = liteCustomFunctionsCall; | ||
globalThis.liteCustomScriptsCall = liteCustomScriptsCall; | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
|
||
// Loading status | ||
if (globalStatusAlert) { | ||
globalStatusAlert.classList.add("d-none"); | ||
} | ||
|
||
return pyodide; | ||
} | ||
|
||
// Call as follows: | ||
// let pyodide = await pyodideReadyPromise; | ||
export let pyodideReadyPromise = initPyodide(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.