ijavascript icon indicating copy to clipboard operation
ijavascript copied to clipboard

Support .load

Open rgbkrk opened this issue 9 years ago • 5 comments

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

rgbkrk avatar Feb 11 '16 22:02 rgbkrk

For the time being, here are two workarounds:

  • Use eval:
    eval(require("fs").readFileSync("f.js").toString());
  • Use the flag --ijs-startup-script=path:
    $ ijs --ijs-startup-script=f.js

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)

n-riesco avatar Feb 12 '16 19:02 n-riesco

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.

rgbkrk avatar Feb 12 '16 19:02 rgbkrk

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());

n-riesco avatar Feb 12 '16 19:02 n-riesco

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.

rgbkrk avatar Feb 12 '16 20:02 rgbkrk

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.

n-riesco avatar Feb 12 '16 21:02 n-riesco