VinceP
VinceP

Reputation: 2163

Save a pivottablejs figure to file

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.

Upvotes: 9

Views: 3335

Answers (2)

TheChetan
TheChetan

Reputation: 4596

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.

Upvotes: 4

Andrew
Andrew

Reputation: 970

you can save it as html by specifying outfile_path

pivot_ui(df, outfile_path="yourpathname.html")

Upvotes: 2

Related Questions