33

The code below when run in jupyter notebook renders a table with a colour gradient format that I would like to export to an image file.

The resulting 'styled_table' object that notebook renders is a pandas.io.formats.style.Styler type.

I have not been able to find a way to export the Styler to an image.

I hope someone can share a working example of an export, or give me some pointers.

import pandas as pd
import seaborn as sns

data = {('count', 's25'): 
       {('2017-08-11', 'Friday'): 88.0,
        ('2017-08-12', 'Saturday'): 90.0,
        ('2017-08-13', 'Sunday'): 93.0},
        ('count', 's67'): 
       {('2017-08-11', 'Friday'): 404.0,
        ('2017-08-12', 'Saturday'): 413.0,
        ('2017-08-13', 'Sunday'): 422.0},
        ('count', 's74'): 
       {('2017-08-11', 'Friday'): 203.0,
        ('2017-08-12', 'Saturday'): 227.0,
        ('2017-08-13', 'Sunday'): 265.0},
        ('count', 's79'): 
       {('2017-08-11', 'Friday'): 53.0,
        ('2017-08-12', 'Saturday'): 53.0,
        ('2017-08-13', 'Sunday'): 53.0}}

table = pd.DataFrame.from_dict(data)
table.sort_index(ascending=False, inplace=True)

cm = sns.light_palette("seagreen", as_cmap=True)
styled_table = table.style.background_gradient(cmap=cm)
styled_table

enter image description here

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
user1243477
  • 547
  • 1
  • 4
  • 7
  • 1
    you may want to check [this answer](https://stackoverflow.com/a/39358752/5741205) – MaxU - stand with Ukraine Aug 13 '17 at 21:10
  • Check https://stackoverflow.com/questions/35634238/how-to-save-a-pandas-dataframe-table-as-a-png – TYZ Feb 26 '18 at 16:33
  • 2
    My guess is that the last statement returns HTML code via [Styler.render](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.formats.style.Styler.render.html), so you need a Python way to convert HTML to an image, like [imgkit](https://pypi.python.org/pypi/imgkit). – ojdo Apr 16 '18 at 13:50

2 Answers2

24

As mentioned in the comments, you can use the render property to obtain an HTML of the styled table:

html = styled_table.render()

You can then use a package that converts html to an image. For example, IMGKit: Python library of HTML to IMG wrapper. Bear in mind that this solution requires the installation of wkhtmltopdf, a command line tool to render HTML into PDF and various image formats. It is all described in the IMGKit page.

Once you have that, the rest is straightforward:

import imgkit
imgkit.from_string(html, 'styled_table.png')
Shovalt
  • 6,407
  • 2
  • 36
  • 51
  • I'm getting this error `If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - http://wkhtmltopdf.org` do you know how can I fix it? – Enzo Dtz Jan 12 '19 at 04:57
  • 1
    @EnzoDtz it seems that either wkhtmltopdf is not installed, or that it is not accessible via the system path. You need to be able in a command line to enter e.g. `wkhtmltopdf --help` and see the help menu. – Shovalt Jan 13 '19 at 07:54
  • 1
    If others don't want to deal with installs, @Shovalt's process is available inside your browser with active Jupyter sessions launched from [here](https://github.com/fomightez/dataframe2img), taking advantage of the MyBinder.org system. – Wayne Jun 18 '19 at 16:04
  • I can find the pdf but not the image one of this executable – thistleknot May 17 '21 at 02:40
  • @thistleknot - I think it is the same executable. Just make sure that `wkhtmltopdf --help` works for you in the command line. If it doesn't work it means that you need to add the parent directory to your PATH environment variable. – Shovalt May 18 '21 at 09:58
  • 1
    that was the answer. My jupyter was being run from a service and i used os.system(echo $PATH) and saw there was none despite seeing it myself with which(wkhtmltopdf). So yeah, once I added environmentPath... yada yada to my .service file, jupyter picked up the path correctly. – thistleknot May 18 '21 at 14:36
19

You can use dexplo's dataframe_image from https://github.com/dexplo/dataframe_image. After installing the package, it also lets you save styler objects as images like in this example from the README:

import numpy as np
import pandas as pd
import dataframe_image as dfi

df = pd.DataFrame(np.random.rand(6,4))
df_styled = df.style.background_gradient()

dfi.export(df_styled, 'df_styled.png')
Nic Moetsch
  • 846
  • 6
  • 9
  • The previous answer was losing a lot of the formatting, this solution worked perfectly instead. Side note: I had to upgrade pip before I was able to install this package with `pip install --upgrade pip`. – Julien Massardier May 05 '21 at 09:59
  • As of 2021, this should be the top answer. Compared to the `imgkit` solution, it mimics Jupyter's style and doesn't require external dependencies. – Thrastylon Dec 13 '21 at 12:09
  • The doesn't render the LaTeX in my styled `DataFrame`. – James Hirschorn Aug 31 '22 at 14:53