Search code examples
pythonhtmlpandaspng

IPython.core.display.HTML to png


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


Solution

  • 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')