So How Do I Export Styled Pandas Tables From a Jupyter Notebook as a PNG or PDF File?

In order to render tweetable PNGs of my WRC rally stage chartables, I’ve been using selenium to render the table in its own web page and then grab a screenshot (Converting Pandas Generated HTML Data Tables to PNG Images), but that’s really clunky. So I started to wonder: are there any HTML2PNG converters out there?

This post has some handy pointers, including reference to the following packages:

  • HTML2Canvas, which allows you to take “[s]creenshots with JavaScript” and export them as PNG files;
  • TableExport, which seems to work with jspdf (“the leading HTML5 client solution for generating PDFs”) and jsPDF-AutoTable (a “jsPDF plugin for generating PDF tables with javascript”) to allow you to export an HTML table as a PDF.

The html2canvas route is also demonstrated in this Stack Overflow answer offered in answer to a query wondering how to Download table as PNG using JQuery.

So now I’m wondering a couple of things about styled pandas tables in Jupyter notebooks.

Firstly, would some magic __repr__ extensibilty that provides a button for exporting a styled pandas table as a PNG or a PDF be handy?

Secondly, how could the above recipes be woven into a simple end user developed ipywidgets powered app (such as the one I used to explore WRC stage charts) to provide a button to download a rendered, styled HTML table as a PNG or PDF?

Anyone know of such magic / extensions already out there? Or can knock up a demo, ideally a Binderised one,  showing me how to do it?

Author: Tony Hirst

I'm a Senior Lecturer at The Open University, with an interest in #opendata policy and practice, as well as general web tinkering...

%d bloggers like this: