n-riesco / ijavascript

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

Support .load #60

Open rgbkrk opened 8 years ago

rgbkrk commented 8 years ago

The node repl lets you load a local file by using .load filename:

$ echo 'x = 2' > f.js
$ node
> .load f.js
> x = 2
2
> x
2
n-riesco commented 8 years ago

For the time being, here are two workarounds:

I don't feel keen on messing with the Javascript grammar to support magics (IPython's % or Node.js's .). Up to now, the approach followed in IJavascript is to define a global object to communicate with the IJavascript kernel ($$mime$$, $$async$$, $$done$$(), ...). We could define the global $$session$$ and provide methods such as $$session$$.load(filename) and $$session$$.save(filename).

I realise that the number of IJavascript globals is increasing and I've been thinking for a while that, at some point in the future, we should define only one global. For example:

$$.async

$$.load(filename)
$$.save(filename)

$$.msg_id
$$.write(msg_id, data)
$$.writeToStdout(msg_id, data)
$$.writeToStderr(msg_id, data)
$$.close(msg_id)
$$.closeStdout(msg_id)
$$.closeStderr(msg_id)
$$.writeAsMime(msg_id, mime)
$$.writeAsHtml(msg_id, html)
$$.writeAsPng(msg_id, png)
rgbkrk commented 8 years ago

I'm not a fan of any globals, though I hear ya. I'll rely on the eval trick.

Most of my suggestions are going to be about mirroring node's repl as much as possible, while extending it with rich display.

n-riesco commented 8 years ago

Magics have their own kind of problems:

$ node
> .load f.js
> x = 2
2
> .load "f" + ".js"
Failed to load:"f" + ".js"
> 

whereas globals blend nicely with Javascript because they are Javascript:

eval(require("fs").readFileSync("f" + ".js").toString());
rgbkrk commented 8 years ago

They definitely do. I even despise them being within IPython as it breaks compatibility with plain Python scripts.

What I want to make sure of is that if a node user was used to being able to do something at the command line and they start using it in a notebook (or console), that we don't break their normal flow. That's my two cents. If it belongs in a different kernel, that's ok. I'm just going to raise things as I use ijavascript in my daily flow and submit PRs when it seems like you're happy with the ideas.

n-riesco commented 8 years ago

I will leave the issue open. I would like to provide this functionality. I'm keener to use a global, but there are more possibilities.