Possible %%script root magic for IPython Notebooks?

Hi, I finally decided to try to learn IPython notebooks (been using the interpreter for years) and I learned about a cool feature.

Scroll down to the “magics for running code under other interpreters”:
nbviewer.ipython.org/github/ipyt … gics.ipynb

A basic test works: nbviewer.ipython.org/url/www.pha … Test.ipynb

When the cells are executed locally, a TCanvas shows up and the plot is shown but then it disappears immediately when ROOT exits. On the nbviewer it would only show printed text output, not windows.

I saw that we already have a PyROOT system for notebooks, and I want to try that out too: root.cern.ch/drupal/content/ipyt … and-root-0

But I am wondering if there is a way to make the %%script root magic work in such a way that the windows are captured? Is it way too hard to do, or is there some clever way to make it work?

For running notebooks locally, is there a way to make the %%script root session not exit immediately?


You can a course save plots to a file supported by notebooks (e.g. PNG) and then display that in the notebook with e.g. IPython.display.display(IPython.display.Image(png_file)).

To be honest I don’t really see the point of using script magic when everything you need should be available from PyRoot (file a bug if something is missing), or you can use ROOT.ProcessLine to e.g. include “magic” headers like cmath.

Hi Jean-François,

rootpy now automatically detects if you are running in an IPython notebook and displays canvases, histograms, and functions as inline images instead of in a separate window.

Here is an example:


I also just started using IPython notebooks. Very cool product. I love being able to connect to my remote notebook server to interactively plot my large datasets.

Anyone willing to create a JavaScript widget to replace ROOT’s GUI so we can have interactive ROOT plots embedded in notebooks? That would be revolutionary! Imagine rotating/scaling plots in a notebook on your phone :slight_smile:

Also see rootpy’s wait() function in rootpy.interactive.

from rootpy.plotting import Canvas from rootpy.interactive import wait c = Canvas() wait() # wait for user to close all canvases before moving on

This prevents the interpreter from exiting (for example) while (non-batch) canvases are open.