dexplo / dataframe_image

A python package for embedding pandas DataFrames as images into pdf and markdown documents
https://dexplo.org/dataframe_image
MIT License
278 stars 41 forks source link

'SeleniumScreenshot' object has no attribute 'css' #89

Closed PaleNeutron closed 1 year ago

PaleNeutron commented 1 year ago

Hi, I tried using table_converter = 'selenium' and received the following error:

'SeleniumScreenshot' object has no attribute 'css'

The error is coming inside the def run(self, html) block. I had copy pasted the code in the _screenshot file that was posted by @wosantos95 for exporting table images using selenium. @waterbear1996

Any help with this would be appreciated. Thanks.

_Originally posted by @ksaxena26 in https://github.com/dexplo/dataframe_image/issues/79#issuecomment-1523369991_

PaleNeutron commented 1 year ago

@ksaxena26 please give me a minimal test case for this issue and I'll deal with it.

ksaxena26 commented 1 year ago

test_data.xlsx screenshot py alt code.txt

import numpy as np
import pandas as pd
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import dataframe_image as dfi

def format_table(styler, dataframe, colmap, colmap_vmax):

    styler.format(formatter = lambda x: "{0:.2f}%".format(100*x), na_rep='', precision = 0)
    styler.format(formatter = lambda x: "{0:.2f}".format(x), subset = 'Disb Amount', na_rep='')

    styler.background_gradient(cmap = colmap, 
                               axis = None,
                               vmin = dataframe.iloc[:, 1:].min().min(),
                               vmax = colmap_vmax)

    styler.set_properties(subset = 'Disb Amount', **{'background-color': 'white',
                                                     'color': 'black'}, overwrite=False) 

    styler.set_table_styles([{"selector": "th", "props": "line-height: 8px;"},
                             {"selector": "td", "props": "line-height: 8px; padding: 0.5"},
                             {'selector': 'td', 'props': 'text-align: center; font-size: 11px;'},
                             {'selector': 'th', 'props': 'font-size: 11px;'},
                             {'selector': 'th', 'props': 'column-width: 50px'},
                             {"selector": "th", 'props': "background-color: white; text-align: center"},
                            ], overwrite = False)

    styler.set_table_styles({'Disb Amount':[{'selector':'td', 'props':'font-weight: bold;'}]
                            }, overwrite=False)

    styler.highlight_null(null_color='white')

    return styler

test_data = pd.read_excel('test_data.xlsx', index_col=0)
test_data.index = test_data.index.strftime("%b'%y")
styler_obj = test_data.style.pipe(format_table, 
                                  test_data, 
                                  'RdYlGn_r', 
                                  0.27)

dfi.export(styler_obj, 'styled_table.png', table_conversion="selenium", max_rows=-1)

The error comes at the last line in dfi.export. I have also attached the screenshot.py file code that I am using.

PaleNeutron commented 1 year ago

This is a problem with your modified code.

If you move to official version which already support selenium, your code above will works well.

styled_table

BTW, I suggest latest matplotlib converter released in v0.1.11, it is slower but easy to deploy and somehow looks better.

dfi.export(styler_obj, 'styled_table_mpl.png', table_conversion="matplotlib")

styled_table_mpl

ksaxena26 commented 1 year ago

Dear @PaleNeutron - thanks a bunch! Ths works perfect now. However, i get these warnings ERROR Property: Invalid value for "CSS Level 2.1" property: 0.5 [7:3: padding] WARNING Property: Unknown Property name. [17:3: column-width] WARNING Property: Unknown Property name. [31:3: overwrite]

Is there an issue with my format_table function?

PaleNeutron commented 1 year ago

This warning is all right. Matplotlib converter is a re-implemented and simulate way of html to image and very limited css properties are supported.

But it works very well in most case.