Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get embed_notebook() working inside nteract #768

Closed
jackparmer opened this issue Oct 23, 2016 · 11 comments
Closed

Get embed_notebook() working inside nteract #768

jackparmer opened this issue Oct 23, 2016 · 11 comments

Comments

@jackparmer
Copy link
Contributor

embed_notebook() currently doesn't work out-of-the-box in nteract

Here's a screenshot of what you see currently:

image

Here's a workaround currently being proposed for plotly.py on the Python side:

plotly/plotly.py#562 (comment)

@rgbkrk
Copy link

rgbkrk commented Oct 24, 2016

For those implementing this nteract accepts a payload when in the R kernel with mimetype application/vnd.plotly.v1+json:

library(IRdisplay)

data <- list(list(x=c(1999, 2000, 2001, 2002), y=c(10, 15, 13, 17), type='scatter'))
figure <- list(data=data)

mimebundle <- list('application/vnd.plotly.v1+json'=figure)
#mimebundle[[]] <- figure
IRdisplay::publish_mimebundle(mimebundle)

It assumes the Plotly figure from the Plotly JSON chart spec. Result:

screen shot 2016-10-23 at 8 18 41 pm

@souravsingh
Copy link

I would like to take a stab at this

@souravsingh
Copy link

How do I start with the issue?

@rgbkrk
Copy link

rgbkrk commented Nov 13, 2016

Within embed_notebook you'll want to use IRDisplay with publish_mimebundle as outlined above to embed the direct JSON (and likely the HTML as well). Feel free to cc me in your PR.

@cpsievert
Copy link
Collaborator

cpsievert commented Dec 21, 2016

I was surprised to find that Jupyter Notebooks appear to now have "native" support for all htmlwidgets, which should make a lot of folks happy, and removes any need for a embed_notebook()-like hack. Here is a gif:

jupyter

I'm guessing the notebook somehow knows to call repr::repr_html() when the output is of class "htmlwidgets". It would be awesome if nteract could do something similar so printing htmlwidgets just works™️ in same way it does for other contexts (e.g., R console, rmarkdown, shiny, Jupyter, etc).

PS. @rgbkrk's suggested approach above wouldn't lead to first-class support for a couple reasons

(1) The R package has an extra layer of JavaScript that plotly.js doesn't know about -- meaning some R specific functionality (such as the linked views stuff) wouldn't work.
(2) The R package bundles a specific version of plotly.js that might conflict with the version nteract uses.
(3) Requires the user to call an extraneous function to embed a plot.

@rgbkrk, I'd be happy to elaborate/discuss anything further to help nteract gain first-class support for all htmlwidgets, but @flying-sheep would probably be more helpful/knowledgable on that front. If and when that happens, we should probably deprecate embed_notebook().

@cpsievert cpsievert changed the title First-class support for embed_notebook in nteract Work towards deprecating plotly::embed_notebook() Dec 21, 2016
@rgbkrk
Copy link

rgbkrk commented Dec 21, 2016

The R package has an extra layer of JavaScript that plotly.js doesn't know about -- meaning some R specific functionality (such as the linked views stuff) wouldn't work.

That's fine, it can keep on embedding HTML on the page. Does it require define and require to be available? The require "available" in the nteract app is the nodejs/commonjs require (and not suitable for use), which means that some amount of type checking is required (or a UMD bundle).

The R package bundles a specific version of plotly.js that might conflict with the version nteract uses.

nteract's is at least namespaced, there is no global plotly object on the page. The bundled version allows for straight plotly spec -> render using jupyter's mimetype setup.

I'll upgrade my IRKernel setup to make sure I'm up to date and try this out.

@flying-sheep
Copy link

flying-sheep commented Dec 21, 2016

I'm guessing the notebook somehow knows to call repr::repr_html() when the output is of class "htmlwidgets".

jep, that’s how I designed the IRkernel display system:

  1. mime2repr is a list of repr_* functions.
  2. IRkernel basically calls all functions for the mimetypes returned by getOption('jupyter.display_mimetypes').
  3. since those are S3 generics, the existence of a repr_html.myclass means that objects of class myclass will have this function called.

repr is explicitly designed not to be tied to IRkernel, so if anything wants to define a rich display/render system based on it, please go ahead!

@flying-sheep
Copy link

flying-sheep commented Dec 21, 2016

things unrelated to above exposition:

cpsievert added a commit that referenced this issue Jan 23, 2017
@cpsievert cpsievert changed the title Work towards deprecating plotly::embed_notebook() Get embed_notebook() working inside nteract Mar 20, 2017
@cpsievert
Copy link
Collaborator

For those interested, with #859, one can now embed any (local) R plotly graph in nteract via embed_notebook() (remote coming soon):

nteract

As it turns out, the problem is related to the fact that nteract doesn't wrap the resulting HTML in an iframe (but Jupyter does)...

Before officially closing this issue, it'd be nice to have a mechanism to tip off nteract users that they need to use embed_notebook() (maybe via a message when loading the package) -- @rgbkrk is there anyway to detect whether the R session in running in an nteract environment? Maybe (hopefully) through an environment variable?

@rgbkrk
Copy link

rgbkrk commented Mar 20, 2017

Jupyter doesn't put HTML in iframes unless instructed to. Everything has global access across the entire page. I actually wish that all HTML outputs were in null origin iframes.

There is no way to detect if an R session is running inside nteract, the jupyter notebook, or any other frontend (at least, not without doing JS/frontend fingerprinting...)

@jackparmer
Copy link
Contributor Author

This works now on the dev. version 👍
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants