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?
1 Answer
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)
4 Comments
Michael_Scharf
Not quote, because that is not building context between sessions. If I assign a variable in one cell,I cannot access it in the next cell.
Michael_Scharf
much better! -- all I need is a way to get the result printed in the cell. Like python
print or sys.stdout.writeAmit
@Michael_Scharf: Check out the last example.
Just M
See nbviewer.ipython.org/gist/minrk/5632595 for in browser js kernel, nbviewer.ipython.org/gist/Carreau/4279371/node-kernel.ipynb for an example of node.js kernel.