n-riesco / ijavascript

IJavascript is a javascript kernel for the Jupyter notebook
Other
2.18k stars 187 forks source link

Add flag --hide-execution-result #121

Closed JocelynDelalande closed 6 years ago

JocelynDelalande commented 6 years ago

See that example:

image

I understand that ijavascript outputs the value of the latest affected variable (so displaying 11, which is the value of i).

But in the context of teaching programming basics, this is very confusing ("why does it counts till 11 instead of 10 ?" ).

So…

Short-term question

Is there a way to disable this behavior ?

Long-term question

Is this behavior desirable at all ?

FYI, ipython python core does not have this behavior:

image

Thanks by advance :-)

rgbkrk commented 6 years ago

This is part of JavaScript itself. If you try the same in the node console or in devtools for Chrome or Firefox, you'll get the same:

screen shot 2017-10-21 at 8 48 01 am

REPLs show the last result of the code. What's peculiar is that JavaScript while loops and blocks have a result. The result of a block is the result of the last statement in the block ({1; 2} --> 2). The result of a while statement is the result of the last iteration of the statement.

rgbkrk commented 6 years ago

In case you were curious, the same applies to for loops too:

screen shot 2017-10-21 at 8 50 35 am
JocelynDelalande commented 6 years ago

@rgbkrk ok, very interesting.

So my only (reasonable) way here seems to be disabling the display of all execution results, I have to search on that.

I left this issue open for a few days in case someone comes with something better.

n-riesco commented 6 years ago

@JocelynDelalande to avoid this JS behaviour, you could rewrite your code like this:

var i = 0;
while(i < 10) {
    console.log(i++);
}
JocelynDelalande commented 6 years ago

Nicolas Riesco notifications@github.com wrote:

@JocelynDelalande to avoid this JS behaviour, you could rewrite your code like this:

var i = 0;
while(i < 10) {
    console.log(i++);
}

@n-riesco Thanks, but for a gentle introduction to algorithmics, this is pretty hardcore stuff :-).

n-riesco commented 6 years ago

@JocelynDelalande Oh, I see. In that case, I'd use a for-loop:

for(var i = 0; i < 10; i++) {
    console.log(i);
}
JocelynDelalande commented 6 years ago

@n-riesco yes, but in this very case, my point si to show/experiment the while loop.

I'm trying to adapt the tools for the teaching, not the teaching to to the tools :-)

@n-riesco any idea/lead on how to disable the display of the latest evaluated expression in ijavascript notebooks ?

n-riesco commented 6 years ago

@n-riesco yes, but in this very case, my point si to show/experiment the while loop.

I'm trying to adapt the tools for the teaching, not the teaching to to the tools :-)

@n-riesco any idea/lead on how to disable the display of the latest evaluated expression in ijavascript notebooks ?

The idea behind the examples I've given above is to ensure the last statement returns undefined (by default ijsinstall installs the IJavascript kernel with the flag --hide-undefined).

I could implement a flag to hide execution results (e.g. --hide-execution-result). I don't see any harm with such an option.

Alternatively, one could develop an nbextension to hide execution results (like https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tree/master/src/jupyter_contrib_nbextensions/nbextensions/hide_input_all). This approach would work regardless the kernel.

JocelynDelalande commented 6 years ago

@n-riesco great, thanks :-)

n-riesco commented 6 years ago

@JocelynDelalande I haven't published a new release in npm yet. I wanted to update IJavascript's documentation before making a new release. But, if you're a going to use this feature straight away, I could make a release today.

JocelynDelalande commented 6 years ago

I'm fine, I'll maybe use it in several months :-)