6

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 1

8

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)
Sign up to request clarification or add additional context in comments.

4 Comments

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.
much better! -- all I need is a way to get the result printed in the cell. Like python print or sys.stdout.write
@Michael_Scharf: Check out the last example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.