Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython notebook ~ Using javascript to run python code?

I am trying to execute python code through javascript directly:

  1. I fire up IPython Notebook on Chrome
  2. Using chrome developer tools I open up the javascript console.

In the javascript consolde, I type: IPython.notebook.kernel.execute("2+2")

enter image description here

But I get a strange output: "6CEA73CC644648DDA978FDD6A913E519"

Is there any way to take advantage of all the IPython javascript functions available, to run python code from the javascript console as depicted in the image? I'm sure there's a way but I've been beating at it for way too long and thought I would post here.

(I need this to build an app on top of IPython)

Thanks in advance!

like image 501
applecider Avatar asked Oct 06 '15 15:10

applecider


People also ask

Can you run Python code in JavaScript?

You can use Python and its modules inside JavaScript with Promise API. You can test it with your favorite python modules such as Numpy, Pandas, pyautogui etc at this point or other built in modules if you want.

How do I run a Python script in IPython?

You can use run command in the input prompt to run a Python script. The run command is actually line magic command and should actually be written as %run. However, the %automagic mode is always on by default, so you can omit this.

How do I run a Python script from a notebook?

Using the python Command To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

Can you run a Python script in Jupyter notebook?

Jupyter Notebook (formerly known as IPython Notebook) is an interactive way of running Python code in the terminal using the REPL model (Read-Eval-Print-Loop).


2 Answers

You can call Python code execution from JavaScript with Jupyter.notebook.kernel.execute() function.

Depend on this gist from Craig Dennis you can insert this code in Jupyter cell and run it

%%javascript
window.executePython = function(python) {
    return new Promise((resolve, reject) => {
        var callbacks = {
            iopub: {
                output: (data) => resolve(data.content.text.trim())
            }
        };
        Jupyter.notebook.kernel.execute(`${python}`, callbacks);    
    });
}

function Log_out(r)
{ console.log(r); };

var code = 
'for i in range(1,6):'+
'    print( "#" + str(i))';

window.executePython( code )
    .then(result => Log_out(result)); // Log out

Result would be output to browser javascript console.

like image 88
Y.N Avatar answered Sep 19 '22 21:09

Y.N


Are you aware of this blogpost? http://jakevdp.github.io/blog/2013/06/01/ipython-notebook-javascript-python-communication/

I think the exact way he uses doesn't work anymore, but maybe it can get you a step forward

like image 26
arthaigo Avatar answered Sep 20 '22 21:09

arthaigo