Show HN: Lightweight, React IDE component which runs user written Python code

https://www.npmjs.com/package/codehelium

codehelium 👾

Codehelium is a lightweight editor component that compiles, executes and returns the outputs of any user-written code via Pyodide/Emscripten/Webassembly.

It is currently only usable as a React component with support for Python code editing/execution.

Check out the demo - it is quite "bloat-free" and can also serve as a useful site for if you need to quickly hack up and test concepts/ideas!

Installation 🖥️

To get started with using codehelium in your React application run npm i codehelium, import the component and you're good to go!

import PythonEditor from 'codehelium'
...
<PythonEditor width="90vw" height="80vh" />

More Examples 😎

Obtaining Console Outputs from Parent Components

Oftentimes, you may desire to obtain the stdout or stderr from user-written code in codehelium editor components. For a given state variable in your parent component, you can pass the corresponding setter function via the consoleOutputSetter prop to utilise console outputs from user-written code in the PythonEditor component.

import PythonEditor from 'codehelium';
import { useEffect, useState } from 'react';
function App() {
    const [consoleOutputs, setConsoleOutputs] = useState([]);
    useEffect(() => {
        console.log("Printing from App... Console output received");
        console.log(consoleOutputs);
    }, [consoleOutputs])
    return (
        <div>
            <PythonEditor width="90vw" height="80vh" 
            consoleOutputSetter={setConsoleOutputs} />
        </div>
    );
}
export default App;

Using an External Pyodide Instance

By default, the PythonEditor component will initialise its own Pyodide instance with the indexURL set to jsDelivr. However if you have initialised Pyodide elsewhere in your application, you can pass this instance to the PythonEditor component by using the pyodideInstance prop. The following example demonstrates how to do so:

import PythonEditor from 'codehelium';
import { loadPyodide } from 'pyodide';
import { useEffect, useState } from 'react';
function App() {
    const [myPyodideInstance, setMyPyodideInstance] = useState(null);
    useEffect(() => {
        async function createPyodideInstance() {
            let pyodide = await loadPyodide({
            indexURL: window.location.href + "/pyodide"
            });
            setMyPyodideInstance(pyodide);
        }
        // Initialize pyodide instance in parent component
        createPyodideInstance();
    }, []);
    return (
        <div>
            <div className="w-screen h-screen grid place-content-center">
                <PythonEditor width="90vw" height="80vh"
                pyodideInstance={myPyodideInstance}/>
            </div>
        </div>
    );
}
export default App;

This is a good practice as it allows you to have full control of the Pyodide instance whilst also being able to leverage the component's capabilities. Consider using this when developing applications that use a singleton model for Pyodide functionality.

Guide to Contributing 🫶

To get started with contributing, fork this repository and then run the following once you have cloned the forked repo:

npm install
npm start

Library components can then be changed by visiting src/lib/, with changes reflected in the development server (from running npm start). Once complete, send a PR (filled with basic details) to bring your changes into this repo!

Additional Notes 📝

codehelium is great for applications such as:

  • Educational tools requiring users to write code and get feedback
  • Building quick and personalised, web-based editors for you to quickly access and test your ideas

Future work includes:

  • Support importing external Python packages in the editor
  • Ability to code in other languages
  • Controlling editor themes and custom-styling

Support this Project 💛

Starring this repo, creating issues and sending PRs with useful features/improvements are always appreciated and welcomed!

{
"by": "dillondesilva",
"descendants": 4,
"id": 40218503,
"kids": [
40232885,
40219974
],
"score": 8,
"text": "Hi there,<p>I built a React code editor component that also compiles, executes and returns the outputs of the program written in it (via pyodide&#x2F;webassembly). I believe that this is a useful package for those building projects with web-based code editors. Expanding the library to be more flexible + have greater language support is a goal for the future.<p>Hope it helps someone out there - please feel free to reach out if I can assist. Feedback and suggestions are always welcome.",
"time": 1714526657,
"title": "Show HN: Lightweight, React IDE component which runs user written Python code",
"type": "story",
"url": "https://www.npmjs.com/package/codehelium"
}
{
"author": null,
"date": "2024-05-06T14:15:44.184Z",
"description": "A lightweight editor component that compiles, executes and returns the outputs of any user-written code via Pyodide/Emscripten/Webassembly.. Latest version: 0.1.10, last published: 4 months ago. Start using codehelium in your project by running `npm i codehelium`. There are no other projects in the npm registry using codehelium.",
"image": "https://static-production.npmjs.com/338e4905a2684ca96e08c7780fc68412.png",
"logo": "https://logo.clearbit.com/npmjs.com",
"publisher": "npm",
"title": "codehelium",
"url": "https://www.npmjs.com/package/codehelium"
}
{
"url": "https://www.npmjs.com/package/codehelium",
"title": "npm: codehelium",
"description": "A lightweight editor component that compiles, executes and returns the outputs of any user-written code via Pyodide/Emscripten/Webassembly.. Latest version: 0.1.10, last published: 4 months ago. Start using codehelium in your project by running `npm i codehelium`. There are no other projects in the npm registry using codehelium.",
"links": [
"https://www.npmjs.com/package/codehelium"
],
"image": "https://static-production.npmjs.com/338e4905a2684ca96e08c7780fc68412.png",
"content": "<div><article><div><div><h2>codehelium 👾</h2></div>\n<p>Codehelium is a lightweight editor component that compiles, executes and returns the outputs of any user-written code via Pyodide/Emscripten/Webassembly.</p>\n<p>It is currently only usable as a React component with <strong>support for Python code editing/execution</strong>.</p>\n<p>Check out the <a target=\"_blank\" href=\"https://dillondesilva.github.io/codehelium/\">demo</a> - it is quite \"bloat-free\" and <strong>can also serve as a useful site for if you need to quickly hack up and test concepts/ideas!</strong></p>\n<div><h2>Installation 🖥️</h2></div>\n<p>To get started with using codehelium in your React application run <code>npm i codehelium</code>, import the component and you're good to go!</p>\n<div><pre><span>import</span> <span>PythonEditor</span> <span>from</span> <span>'codehelium'</span>\n<span>.</span><span>.</span><span>.</span>\n<span>&lt;</span><span>PythonEditor</span> <span>width</span><span>=</span><span>\"90vw\"</span><span></span> <span>height</span><span>=</span><span>\"80vh\"</span> <span>/</span><span>&gt;</span></pre></div>\n<div><h2>More Examples 😎</h2></div>\n<div><h3>Obtaining Console Outputs from Parent Components</h3></div>\n<p>Oftentimes, you may desire to obtain the stdout or stderr from user-written code in codehelium editor components. For a given state variable in your parent component, you can pass the corresponding setter function via the <code>consoleOutputSetter</code> prop to utilise console outputs from user-written code in the PythonEditor component.</p>\n<div><pre><span>import</span> <span>PythonEditor</span> <span>from</span> <span>'codehelium'</span><span>;</span>\n<span>import</span> <span>{</span> <span>useEffect</span><span>,</span> <span>useState</span> <span>}</span> <span>from</span> <span>'react'</span><span>;</span>\n<span>function</span> <span>App</span><span>(</span><span>)</span> <span>{</span>\n <span>const</span> <span>[</span><span>consoleOutputs</span><span>,</span> <span>setConsoleOutputs</span><span>]</span> <span>=</span> <span>useState</span><span>(</span><span>[</span><span>]</span><span>)</span><span>;</span>\n <span>useEffect</span><span>(</span><span>(</span><span>)</span> <span>=&gt;</span> <span>{</span>\n <span>console</span><span>.</span><span>log</span><span>(</span><span>\"Printing from App... Console output received\"</span><span>)</span><span>;</span>\n <span>console</span><span>.</span><span>log</span><span>(</span><span>consoleOutputs</span><span>)</span><span>;</span>\n <span>}</span><span>,</span> <span>[</span><span>consoleOutputs</span><span>]</span><span>)</span>\n <span>return</span> <span>(</span>\n <span>&lt;</span><span>div</span><span>&gt;</span>\n <span>&lt;</span><span>PythonEditor</span> <span>width</span><span>=</span><span>\"90vw\"</span> <span>height</span><span>=</span><span>\"80vh\"</span> \n <span>consoleOutputSetter</span><span>=</span><span>{</span><span>setConsoleOutputs</span><span>}</span> <span>/</span><span>&gt;</span>\n <span>&lt;</span><span>/</span><span>div</span><span>&gt;</span>\n <span>)</span><span>;</span>\n<span>}</span>\n<span>export</span> <span>default</span> <span>App</span><span>;</span></pre></div>\n<div><h3>Using an External Pyodide Instance</h3></div>\n<p>By default, the <code>PythonEditor</code> component will initialise its own Pyodide instance with the indexURL set to <a target=\"_blank\" href=\"https://www.jsdelivr.com/\">jsDelivr</a>. However if you have initialised Pyodide elsewhere in your application, you can pass this instance to the <code>PythonEditor</code> component by using the <code>pyodideInstance</code> prop. The following example demonstrates how to do so:</p>\n<div><pre><span>import</span> <span>PythonEditor</span> <span>from</span> <span>'codehelium'</span><span>;</span>\n<span>import</span> <span>{</span> <span>loadPyodide</span> <span>}</span> <span>from</span> <span>'pyodide'</span><span>;</span>\n<span>import</span> <span>{</span> <span>useEffect</span><span>,</span> <span>useState</span> <span>}</span> <span>from</span> <span>'react'</span><span>;</span>\n<span>function</span> <span>App</span><span>(</span><span>)</span> <span>{</span>\n <span>const</span> <span>[</span><span>myPyodideInstance</span><span>,</span> <span>setMyPyodideInstance</span><span>]</span> <span>=</span> <span>useState</span><span>(</span><span>null</span><span>)</span><span>;</span>\n <span>useEffect</span><span>(</span><span>(</span><span>)</span> <span>=&gt;</span> <span>{</span>\n <span>async</span> <span>function</span> <span>createPyodideInstance</span><span>(</span><span>)</span> <span>{</span>\n <span>let</span> <span>pyodide</span> <span>=</span> <span>await</span> <span>loadPyodide</span><span>(</span><span>{</span>\n <span>indexURL</span>: <span>window</span><span>.</span><span>location</span><span>.</span><span>href</span> <span>+</span> <span>\"/pyodide\"</span>\n <span>}</span><span>)</span><span>;</span>\n <span>setMyPyodideInstance</span><span>(</span><span>pyodide</span><span>)</span><span>;</span>\n <span>}</span>\n <span>// Initialize pyodide instance in parent component</span>\n <span>createPyodideInstance</span><span>(</span><span>)</span><span>;</span>\n <span>}</span><span>,</span> <span>[</span><span>]</span><span>)</span><span>;</span>\n <span>return</span> <span>(</span>\n <span>&lt;</span><span>div</span><span>&gt;</span>\n <span>&lt;</span><span>div</span> <span>className</span><span>=</span><span>\"w-screen h-screen grid place-content-center\"</span><span>&gt;</span>\n <span>&lt;</span><span>PythonEditor</span> <span>width</span><span>=</span><span>\"90vw\"</span> <span>height</span><span>=</span><span>\"80vh\"</span>\n <span>pyodideInstance</span><span>=</span><span>{</span><span>myPyodideInstance</span><span>}</span><span>/</span><span>&gt;</span>\n <span>&lt;</span><span>/</span><span>div</span><span>&gt;</span>\n <span>&lt;</span><span>/</span><span>div</span><span>&gt;</span>\n <span>)</span><span>;</span>\n<span>}</span>\n<span>export</span> <span>default</span> <span>App</span><span>;</span></pre></div>\n<p>This is a <strong>good practice</strong> as it allows you to have full control of the Pyodide instance whilst also being able to leverage the component's capabilities. Consider using this when developing applications that use a <a target=\"_blank\" href=\"https://adamemery.dev/articles/pyodide-react\">singleton model for Pyodide</a> functionality.</p>\n<div><h2>Guide to Contributing 🫶</h2></div>\n<p>To get started with contributing, fork this repository and then run the following once you have cloned the forked repo:</p>\n<pre><code>npm install\nnpm start\n</code></pre>\n<p>Library components can then be changed by visiting <code>src/lib/</code>, with changes reflected in the development server (from running <code>npm start</code>). Once complete, send a PR (filled with basic details) to bring your changes into this repo!</p>\n<div><h2>Additional Notes 📝</h2></div>\n<p>codehelium is great for applications such as:</p>\n<ul>\n<li>Educational tools requiring users to write code and get feedback</li>\n<li>Building quick and personalised, web-based editors for you to quickly access and test your ideas</li>\n</ul>\n<p>Future work includes:</p>\n<ul>\n<li>Support importing external Python packages in the editor</li>\n<li>Ability to code in other languages</li>\n<li>Controlling editor themes and custom-styling</li>\n</ul>\n<div><h2>Support this Project 💛</h2></div>\n<p>Starring this repo, creating issues and sending PRs with useful features/improvements are always appreciated and welcomed!</p>\n</div></article></div>",
"author": "",
"favicon": "https://static-production.npmjs.com/da3ab40fb0861d15c83854c29f5f2962.png",
"source": "npmjs.com",
"published": "",
"ttr": 102,
"type": ""
}