Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create an iPython notebook using JavaScript as the language in the cells?

I love IPython to explain algorithms in python. But I want to do the same using javascript. Is it possible to write a notebook where I use javascript as the cell language?

like image 924
Michael_Scharf Avatar asked Jan 22 '15 01:01

Michael_Scharf


1 Answers

You can use the %%javascript magic function for running javascript in IPython notebook. For example Paste the following code in a IPython cell and run it. You should see the output in your browser's javascript console.

%%javascript
console.log("Hello World!")

For global variables, you can add an attribute to the windows object for example, in a cell run the following code:

%%javascript
window.myvar = 12;

In another cell, run the following code and check browser's javascript console. The variable's value should be printed.

%%javascript
console.log(myvar)

Use the element variable for printing in the cell output area as shown below:

%%javascript
element.append(myvar)
like image 182
Amit Verma Avatar answered Nov 15 '22 10:11

Amit Verma