0

I would like to export a pandas df with barcharts (https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Bar-charts) using imgkit as follows but the barcharts are not included in the final pdf.

enter image description here

I tried to pass the css separately to webkit with the css=<filename> kwarg without success. Other styled pd.DataFrames from the style manual seem to work.

Does anybody see why the CSS is not applied? Any alternatives to export styled tables to png?

import pandas as pd
import numpy as np

np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
               axis=1)
df.iloc[3, 3] = np.nan
df.iloc[0, 2] = np.nan

s = df.style.bar(subset=['A', 'B'], align='mid', color=['#d65f5f', '#5fba7d'])
html = s.render()

import imgkit
imgkit.from_string(html, 'styled_table3.png')
bjonen
  • 1,503
  • 16
  • 24
  • Hi Bjonen, have you found a solution to this? I want to do this as well and the only solution that worked was from here:https://blog.ouseful.info/2019/01/16/converting-pandas-generated-html-data-tables-to-png-images/. It works as a standalone solution, but doesn't integrate well with dash as part of webapp. – Alex M Jul 02 '20 at 16:46
  • The automated part that takes a snapshot of the styled dataframe rendered in a web-browser, causes the the rest of the web-app to reset in some way i don't understand. I've played with imagekit encoding options, but don't think that this is the issue. There is some custom css in the barcharts that just doesn't translate. – Alex M Jul 02 '20 at 16:46
  • Thanks for the update. I haven't found a solution either. – bjonen Jul 02 '20 at 16:50

0 Answers0