[IPython-dev] custom mime types in the notebook

Andrew Payne andy at payne.org
Sat Jul 12 16:47:55 EDT 2014


> >> What's the best way to do it? Should I create a custom mime type
> >> "application/myapp", and extend the notebook to use the JS app for
> >> that mime type (if that's even possible)?
> >
> > I think this opens up a can of worms (certainly relative to the approach
> > above).   Going this route, you start to introduce dependencies on how
> the
> > notebook is served, and you make the notebook itself less portable.
>
> Thanks. Could you elaborate on why this approach would make the
> notebook less portable?
>

Right now, IPython supports plain text, markdown, HTML, "JSON", Javascript,
PDF, PNG, JPEG, and SVG with "formatters".  (See:
http://ipython.org/ipython-doc/dev/api/generated/IPython.core.formatters.html)
 You *could* write your own new formatter for your datatype, but then
you're reaching into lesser-used code areas that have changed in the past.
 In my experience, those are areas that *tend* to have future change risk.
 (Also, your suggestion of a MIME type suggested (to me) that you were
getting into dynamically probing some rendering capability in the browser,
which gets more complicated).

>From the use case you describe (a JSON model rendered my three.js), I would
just do your approach #2:  create a completely self-contained Javascript()
cell result.  The sample Javascript notebook I originally mentioned uses
three.js as the example of an external library, and shows you how to
navigate some of the nuances with that (e.g. the event loop):
http://nbviewer.ipython.org/github/payne92/notebooks/blob/master/00%20Javascript%20In%20Notebooks.ipynb

If you care about non-WebGL cases, your Javascript code should probe for
WebGL and fall back to rendering your PNG if it is not present.

If you care about non-Web IPython notebooks (e.g. qtconsole), return a
Python object with two repr methods:  _repr_javascript_() and _repr_png_().
 The notebook runtime will pick the "best" based on what's available:  PNG
on qtconsole, and Javascript on the Web.  (Personally, I wouldn't worry too
much about this:  I think the Web notebook is the future).

-andy









> Let's take an example. Imagine I want to display 3D models in a
> notebook using a JS library (like three.js).
>
> Approach 1:
>
> * My cell's output contains data in several mime types: PNG with a
> screenshot, and OBJ (for example) with the full 3D model. It seems to
> me that the nbformat is designed for this (multiple MIME types
> representing the same logical data).
>
> * If the client understands the OBJ mime type, it displays it using
> three.js.
>
> * Otherwise, it just displays the PNG screenshot.
>
> Approach 2 would be to embed the entire Javascript code in the output
> + <script include jslibraries> (like you suggest, and like mpld3 does,
> unless I'm mistaken)
>
> I guess my questions are:
>
> * Which of those two approaches is recommended by IPython devs?
> * Is the notebook designed for approach 1 use-case or not?
>
> It seems to me that approach 1 is cleaner while approach 2 looks like
> a hack, but that's just my opinion.
>
> Best,
> Cyrille
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20140712/73081101/attachment.html>


More information about the IPython-dev mailing list