0

Following this thread Adding image to pandas DataFrame I use path_to_image_html to display some png (actually charts from matplotlib) inside my df pandas dataframe.

def toto():
   df['imageUrls'] = images1
   def path_to_image_html(path):
       return '<img src="'+ path + '" width="60" >'
   return HTML(df.to_html(escape=False,formatters=dict(imageUrls=path_to_image_html)))

And then display(toto()) to display my pandas inside my jupyter notebook.

Now I would like to save my pandas in a PNG file format. Basically convert toto() to a png file.

How can I proceed ? Thanks

olivier dadoun
  • 622
  • 6
  • 22

1 Answers1

2

You could use: imgkit

import pandas as pd
import imgkit

test = pd.DataFrame({"id": [1,2,3,4], 
                     "percentage": [0.2, 0.4, 0.5, 0.6]})

imgkit.from_string(test.to_html(), 'out.png')
Daniel Wlazło
  • 1,105
  • 1
  • 8
  • 17
  • 1
    Yes and my toto() function should return directly df.to_html(escape=False,formatters=dict(imageUrls=path_to_image_html)) Thk ! – olivier dadoun Jan 24 '22 at 19:29