I am trying to execute python code through javascript directly:
In the javascript consolde, I type: IPython.notebook.kernel.execute("2+2")
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!
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.
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.
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!
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With