6

I have styled a dataframe output and have gotten it to display how I want it in a Jupyter Notebook but I am having issues find a good way to save this as an image. I have tried https://pypi.org/project/dataframe-image/ but the way I have this working it seem to be a NoneType as it's a styler object and errors out when trying to use this library.

This is just a snippet of the whole code, this is intended to loop through several 'col_names' and I want to save these as images (to explain some of the coding).

import pandas as pd
import numpy as np

col_name = 'TestColumn'

temp_df = pd.DataFrame({'TestColumn':['A','B','A',np.nan]})

t1 = (temp_df[col_name].fillna("Unknown").value_counts()/len(temp_df)*100).to_frame().reset_index()
t1.rename(columns={'index':' '}, inplace=True)
t1[' '] = t1[' '].astype(str) 

display(t1.style.bar(subset=[col_name], color='#5e81f2', vmax=100, vmin=0).set_table_attributes('style="font-size: 17px"').set_properties(
    **{'color': 'black !important',
       'border': '1px black solid !important'}
).set_table_styles([{
    'selector': 'th',
    'props': [('border', '1px black solid !important')]
}]).set_properties( **{'width': '500px'}).hide_index().set_properties(subset=[" "], **{'text-align': 'left'}))

[OUTPUT] enter image description here

Adam Barrett
  • 188
  • 1
  • 2
  • 7
  • 1
    `df.style.set_properties` and the like returns a style object, which has a `render` method. If you do `html_str = df.style.render()`, then `html_str` is a valid html string (try to print it). Python/Pandas doesn't render that picture, it's actually your browser that does it. That said, you can use that html to render and convert to an image, e.g. [this question](https://stackoverflow.com/questions/60598837/html-to-image-using-python), or save it in html extension (may need some additional wrapping) and open with your browser later. – Quang Hoang Jan 01 '22 at 11:09
  • See also: https://stackoverflow.com/a/50097322/11246056 – Laurent Jan 01 '22 at 11:13
  • Does this answer your question? [Export pandas Styled table to image file](https://stackoverflow.com/questions/45664519/export-pandas-styled-table-to-image-file) – Laurent Jan 01 '22 at 11:20

1 Answers1

1

Was able to change how I was using dataframe-image on the styler object and got it working. Passing it into the export() function rather than calling it off the object directly seems to be the right way to do this.

The .render() did get the HTML but was often losing much of the styling when converting it to image or when not viewed with Ipython HTML display. See comparision below. enter image description here

Working Code:

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

col_name = 'TestColumn'

temp_df = pd.DataFrame({'TestColumn':['A','B','A',np.nan]})

t1 = (temp_df[col_name].fillna("Unknown").value_counts()/len(temp_df)*100).to_frame().reset_index()
t1.rename(columns={'index':' '}, inplace=True)
t1[' '] = t1[' '].astype(str) 


style_test = t1.style.bar(subset=[col_name], color='#5e81f2', vmax=100, vmin=0).set_table_attributes('style="font-size: 17px"').set_properties(
    **{'color': 'black !important',
       'border': '1px black solid !important'}
).set_table_styles([{
    'selector': 'th',
    'props': [('border', '1px black solid !important')]
}]).set_properties( **{'width': '500px'}).hide_index().set_properties(subset=[" "], **{'text-align': 'left'})

dfi.export(style_test, 'successful_test.png')
Adam Barrett
  • 188
  • 1
  • 2
  • 7