I want to make a jupyter notebook extension and I am following this repo . I needed to run python code using javascript for which I am trying to use the npm python-shell
To import the package, I tried using require module but its not working because it is not supposed to be run on browser and requires nodejs. I tried searching for other ways to run nodejs on browser and found stuff like browserify etc. However , the file main.js in the repo linked above is using the require module comfortably. How is it doing so? What am I missing? Below is the code and the error I get in the browser. I have installed require and python-shell module using npm already.
Code:
define(['require','PythonShell','base/js/namespace', 'base/js/events'], function (require,Jupyter, events) {
var PythonShell = require('python-shell');
PythonShell.runString('x=1;print(x)',null,function(err,results) {
console.log(results);
});
function load_ipython_extension() {
console.log("Hello World");
}
return {
load_ipython_extension: load_ipython_extension,
};
});
Error:
It looks like the library you are trying to use, python shell was designed for node.js, as stated in their package description:
A simple way to run Python scripts from Node.js with basic but efficient inter-process communication and better error handling.
Because of that, you cannot use this package in the browser. There are probably lots of alternatives, and I found this package through some quick googling: pyodide (note, I have never used this myself, I just found it).