n-riesco / ijavascript

IJavascript is a javascript kernel for the Jupyter notebook
Other
2.19k stars 185 forks source link

_ #106

Open ghost opened 7 years ago

ghost commented 7 years ago

_

n-riesco commented 7 years ago

I was prototyping this notebook to run other notebooks and came across this odd behavior when I run a fairly common use case for $$.async()

It's the expected behaviour. The function () => { throw 'should only fail per cell'; } in setTimeout is run asynchronously (i.e. the exception isn't thrown while the cell code is executed). To send an exception back to a cell, you need to use $$.sendError, like this:

const $1 = $$;

$1.async();

setTimeout(() => {
    $1.sendError(new Error("this exception is shown in cell 1"));
});

Where are the functions for $$ in the source?

They are provided by the nel module.


I wanted reply to your comment in more detail but I'm pressed for time. Let me give you two pointers:

ghost commented 7 years ago

_

n-riesco commented 7 years ago

[...] Once I am done prototyping this, I'd like to turn notebooks in to modules [...]

For that, you only need to parse the notebook, extract the code and generate a package.json and a index.js. No need to execute the notebook (and thus no need for NEL).

Is there a good way to install additional packages from the NEL side?

(Assuming npm) As far as I know is undocumented, but something like this works for me:

var npm = require("npm");

npm.load(function() {
    npm.commands.install(["lodash"], function(err, installed, tree) {
        console.log("install:", arguments);
    });
});
ghost commented 7 years ago

_

ghost commented 7 years ago

_

n-riesco commented 7 years ago

A few quick comments:

var $1 = $$;
$1.async();
somePromise.catch($1.sendError);

or alternatively using an IIFE:

(function($$) {
    $$.async();
    somePromise.catch($$.sendError);
})($$);

Is there a better way to share object information between the current scope and inside the NEL context? Right now it is just encoding the first 5 arguments to JSON.

I think this should do the trick:

> function toArray() { return Array.prototype.slice.apply(arguments); }
undefined
> toArray(1,2,3,4,5,6)
[ 1, 2, 3, 4, 5, 6 ]

Pretty amazing what you can do with so few lines of code. I am shooting for the sequel to this article: https://blog.ibmjstart.net/2016/01/28/jupyter-notebooks-as-restful-microservices/

Link seems broken.


I haven't read your code very carefully. I'll come back to you with more comments after I do.

ghost commented 7 years ago

Oops. Http: http://blog.ibmjstart.net/2016/01/28/jupyter-notebooks-as-restful-microservices/

On Sat, Jun 3, 2017 at 12:09 PM, Nicolas Riesco notifications@github.com wrote:

A few quick comments:

-

The block try { ... } catch(e) { $$.sendError(e) } isn't necessary. Errors that happen during the execution of a cell are automatically reported back to the cell (no need for $$.sendError(e)).

$$.sendError(e) is necessary inside a promise (because that code is run asynchronously, after the cell has been run). But you need to make a copy of $$ during the execution of the cell (because $$ changes every time a cell is run). E.g:

var $1 = $$;$1.async();somePromise.catch($1.sendError);

or alternatively using an IIFE:

(function($$) { $$.async(); somePromise.catch($$.sendError); })($$);

Is there a better way to share object information between the current scope and inside the NEL context? Right now it is just encoding the first 5 arguments to JSON.

I think this should do the trick:

function toArray() { return Array.prototype.slice.apply(arguments); } undefined toArray(1,2,3,4,5,6) [ 1, 2, 3, 4, 5, 6 ]

Pretty amazing what you can do with so few lines of code. I am shooting for the sequel to this article: https://blog.ibmjstart.net/2016/01/28/jupyter-notebooks- as-restful-microservices/

Link seems broken.

I haven't read your code very carefully. I'll come back to you with more comments after I do.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/n-riesco/ijavascript/issues/106#issuecomment-305995216, or mute the thread https://github.com/notifications/unsubscribe-auth/AX5Xbs-6ucfUV5XYfefzH5O34Q29uezPks5sAa9ygaJpZM4Npoj8 .