Save a pivottablejs figure to file

此生再无相见时 提交于 2020-01-02 02:20:33

问题


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.


回答1:


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.




回答2:


you can save it as html by specifying outfile_path

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


来源:https://stackoverflow.com/questions/43758224/save-a-pivottablejs-figure-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!