9

I have started using the package pivottablejs to manipulate and visualize pivot tables in python.

from pivottablejs import pivot_ui
pivot_ui(df)  # where df is a pandas dataframe

will produce an interactive pivot table/plot in a jupyter notebook.

Is there any pythonic way to save the figures produced by this package to (say) png from within the jupyter notebook? I am looking for something similar to the classic plt.savefig('file.png'). The front-end is essentially javascript, and I do not know how (or if it is possible) to access a javascript figure through python.

VinceP
  • 2,058
  • 2
  • 19
  • 29

2 Answers2

4

This is too long for a comment, so I'm adding it as an answer. pivottablejs creates images as svgs and they do not provide any download/export mechanism for the images. But what you can do is inject js into the cell and download the svg using that.

One simple way of doing this is using svg-crowbar.js. After you generate the chart using pivot_ui and the output is displayed on the screen, run the following in the next cell to download the svg.

In [10]: %%javascript
         var e = document.createElement('script'); e.setAttribute('src', 'https://nytimes.github.io/svg-crowbar/svg-crowbar.js'); e.setAttribute('class', 'svg-crowbar'); document.body.appendChild(e);

Note: This only works on chrome.

TheChetan
  • 4,440
  • 3
  • 32
  • 41
  • Thanks. This works (on chrome). It seems to be the best solution at the moment, although it is not exactly what I had in mind – VinceP Jul 26 '17 at 11:46
  • 1
    I understand, but maybe you can use matplotlib instead of pivottablejs. That way you can get a png that you can save. – TheChetan Jul 26 '17 at 11:48
2

you can save it as html by specifying outfile_path

pivot_ui(df, outfile_path="yourpathname.html")
Andrew
  • 950
  • 7
  • 24