Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to load pyodide in browser? #905

Open
ije opened this issue Nov 13, 2024 Discussed in #902 · 0 comments
Open

How to load pyodide in browser? #905

ije opened this issue Nov 13, 2024 Discussed in #902 · 0 comments
Assignees

Comments

@ije
Copy link
Member

ije commented Nov 13, 2024

Discussed in #902

Originally posted by subframe7536 November 5, 2024
Here is my code, and it prompt: Failed to load Pyodide: TypeError: Failed to fetch dynamically imported module: https://esm.sh/[email protected]/pyodide.asm.js

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Pyodide Example with esm.sh</title>
        <script type="module">
            // Load Pyodide from esm.sh
            async function loadPyodide() {
                // Import Pyodide
                const { loadPyodide } = await import(
                    "https://esm.sh/[email protected]"
                );
                const pyodide = await loadPyodide({
                    indexURL: "https://esm.sh/[email protected]",
                });
                await pyodide.loadPackage("numpy"); // Load your desired package here
                return pyodide;
            }

            // Using the loaded Pyodide
            loadPyodide()
                .then((pyodide) => {
                    // Example: Running a simple Python code
                    const code = `
import numpy as np
a = np.array([1, 2, 3])
a.mean()
`;
                    const result = pyodide.runPython(code);
                    console.log(result); // Output the result to the console
                })
                .catch((err) => {
                    console.error("Failed to load Pyodide:", err);
                });
        </script>
    </head>
    <body>
        <h1>Pyodide with ESM.sh</h1>
        <p>Open the console to see the output.</p>
    </body>
</html>
```</div>
@ije ije self-assigned this Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant