Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ipython %%javascript magic output into cell and console

How to adjust ipythons' %%javascript cell magic so that it outputs both to output cell of ipython and to console of the browser as shown belowcellmagic

I don't want to use alert(). Furthermore, I tried to get similar behavior with nodejs using %%script magic

%%script node
console.log("Hi")

But this is not what I need.

like image 262
Levon Avatar asked Feb 25 '15 05:02

Levon


People also ask

What is %% capture in Python?

IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. from __future__ import print_function import sys. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

How do you use %% Timeit in jupyter?

The “%timeit” is a line magic command in which the code consists of a single line or should be written in the same line for measuring the execution time. In the “%timeit” command, the particular code is specified after the “%timeit” is separated by a space.

What does %% do in jupyter notebook?

Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.

What is %% Writefile in jupyter notebook?

%%writefile lets you output code developed in a Notebook to a Python module. The sys library connects a Python program to the system it is running on. The list sys. argv contains the command-line arguments that a program was run with.


2 Answers

Just came across this which got me part of the way there but as noted if you use only element.text() you only get the last one. But you can use element.append() repeatedly in the same cell with the %%javascript and and all of the output will go to the following cell. Just make sure to append a <br> if you want a carriage return. Like for example

function assert(cond,msg)
{
   if (!cond)
   {
     element.append(msg + "<br>");
   }
}
like image 181
yatinla Avatar answered Oct 23 '22 06:10

yatinla


you have access to the output Dom node under the variable named element wrapped in jquery. So:

%%javascript
element.text('Hi')
console.log('There')
like image 21
Matt Avatar answered Oct 23 '22 05:10

Matt