n-riesco / ijavascript

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

How to access window from ijavascript? #105

Closed BoPeng closed 7 years ago

BoPeng commented 7 years ago

I am new to JS so please bear with me if this question is too naive. Basically I would like to use ijavascript to display nice tables in Jupyter using DataTables. I have

  1. Installed ijavascript
  2. Installed datatables, datatables.net, datatables.net-dt using npm

I am then stuck at how to load datatables to iJavascript. I tried

var $       = require( 'jquery' );
var dt      = require( 'datatables.net' )();
var buttons = require( 'datatables.net-buttons' )();

and got

TypeError: Cannot read property 'document' of undefined
    at module.exports (/Users/bpeng1/sos/node_modules/jquery/dist/jquery.js:30:12)
    at module.exports (/Users/bpeng1/sos/node_modules/datatables.net/js/jquery.dataTables.js:48:23)
    at evalmachine.<anonymous>:2:42
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at Object.runInThisContext (vm.js:95:38)
    at run ([eval]:617:19)
    at onRunRequest ([eval]:388:22)
    at onMessage ([eval]:356:17)
    at emitTwo (events.js:106:13)
    at process.emit (events.js:194:7)

I understand that we are using node.js but we are in a browser (Jupyter), so window should exist, not?

rgbkrk commented 7 years ago

The ijavascript process is a full node process and is completely separate from the browser process. For rich display, you'll have to use IJavaScript's helpers for displaying js or html: http://n-riesco.github.io/ijavascript/doc/graphics.ipynb.html

BoPeng commented 7 years ago

Ohmm, the helpers executes HTML, do you mean I need to get DataTables loaded outside of iJavaScript before I use $$HTML$$ to use them? Have you seen anyone using DataTables with iJavaScript?

BoPeng commented 7 years ago

Essentially speaking, I would like to use iJavaScript to render a JSON table as searchable tables...

n-riesco commented 7 years ago

@BoPeng In this answer in StackOverflow I gave an example of what you need to do to get IJavascript to render HTML.

But, please, bear in mind that some Jupyter frontends won't allow the injection of Javascript (I'm not familiar with DataTable).


Alternatively, some frontends (like nteract and hydrogen) can render JSON MIME data. Something like this should work when IJavascript is used in those frontends:

$$.mime({
    "application/json": {a: 1, b: 2, c: 3},
});
BoPeng commented 7 years ago

Thanks for the explanation, I think I got the generally idea. I tried to load DataTable in jsdom but got stuck at the first step:

image

after running npm install -g jsdom. This is unrelated to iJavaScript so I am closing the ticket.

rgbkrk commented 7 years ago

jsdom requires node 6 or higher