4

I have an IPython Notebook that is using Pandas to back-test a rule-based trading system.

I have a function that accepts various scalars and functions as parameters and outputs a stats pack as some tables and a couple of plots.

For automation, I want to be able to format this nicely into a "page" and then call the function in a loop while varying the inputs and have it output a number of pages for comparison, all from a single notebook cell.

The approach I am taking is to create IpyTables and then call _repr_html_(), building up the HTML output along the way so that I can eventually return it from the function that runs the loop.

How can I capture the output of the plots this way - matplotlib subplot objects don't seem to implement _repr_html_()?

Feel free to suggest another approach entirely that you think might equally solve the problem.

TIA

Zephyr
  • 11,891
  • 53
  • 45
  • 80
ultra909
  • 1,740
  • 1
  • 23
  • 25
  • Is the output of this cell a series of static HTML files? Does it matter whether the figures also appear inside the notebook (i.e., default pylab=inline behavior)? – Garrett Jan 29 '13 at 14:51
  • ideally, it would all appear inline in the notebook. from there, export to html if desired using nbviewer or whatever. I spoke to someone offline who just suggested saving the images as PNGs locally and then constructing the HTML tags by hand. Would work I guess - seems a little hacky tho. – ultra909 Jan 29 '13 at 16:01
  • 1
    Ok, if you go that route, this answer http://stackoverflow.com/a/5314808/243434 on how to capture matplotlib figures as inline PNGs may help – Garrett Jan 29 '13 at 16:08
  • 1
    There's a slightly more direct way than saving them - encode them in base64, and use data URIs to embed them directly in your HTML. That's how IPython displays images in results anyway. – Thomas K Jan 29 '13 at 17:30
  • @crewburn: Ok, that totally works. Kudos. One small problem: now the images come out twice - once as the HTML, where I want them, and also as just a dump of images at the end after the HTML, exactly where they were originally (of course). Any ideas on how to capture the image blob without it actually outputting to the notebook? – ultra909 Jan 29 '13 at 17:52
  • @ThomasK thanks - I think you suggested the same option as crewburn – ultra909 Jan 29 '13 at 17:56
  • 1
    To prevent duplication of plots, try running with ``pylab`` disabled (double-check your config files and the command line). – Garrett Jan 30 '13 at 04:39

1 Answers1

1

Ok, if you go that route, this answer stackoverflow.com/a/5314808/243434 on how to capture >matplotlib figures as inline PNGs may help – @crewbum

To prevent duplication of plots, try running with pylab disabled (double-check your config >files and the command line). – @crewbum

--> this last requires a restart of the notebook: ipython notebook --pylab (NB no inline)

ultra909
  • 1,740
  • 1
  • 23
  • 25
  • N.B. I still get popups, but just close these automatically once I have the generated HTML, using: `plt.close()` – ultra909 Jan 31 '13 at 16:32