TypeError: Style frame __init__ doesn’t support Styler

So I have a dataframe with multiple columns and I am converting that dataframe to excel in my first function which is beautify_output where I add colour to the entire row based on the value of the result column.

The next function is where I am facing problems, when I try to run the code, I keep getting an error whenever I try to initialise sf=StyleFrame(df). What I’m trying to do in this function is expand the width of the columns in the excel for some columns.
Here is the full code. I am on python 3.7

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from styleframe import StyleFrame
import pandas as pd
df = pd.read_csv('final_output.csv')
def beatify_output(row):
if row['result'] == 'Pass':
return ['background-color: red'] * len(row)
if row['result'] == 'Fail'
return ['background-color: green'] * len(row)
df = df.style.apply(beatify_output, axis=1)
df.to_excel('my_file.xlsx',sheet_name='Sheet1')```
def expand_columns_size():
len_max = 50
excel_writer =
StyleFrame.ExcelWriter('my_file.xlsx')
sf = StyleFrame(df)
sf.set_column_width(columns=['actual_prompt', 'expected_prompt'], width=len_max)
sf.to_excel(excel_writer=excel_writer)
excel_writer.save()
expand_columns_size()
</code>
<code>from styleframe import StyleFrame import pandas as pd df = pd.read_csv('final_output.csv') def beatify_output(row): if row['result'] == 'Pass': return ['background-color: red'] * len(row) if row['result'] == 'Fail' return ['background-color: green'] * len(row) df = df.style.apply(beatify_output, axis=1) df.to_excel('my_file.xlsx',sheet_name='Sheet1')``` def expand_columns_size(): len_max = 50 excel_writer = StyleFrame.ExcelWriter('my_file.xlsx') sf = StyleFrame(df) sf.set_column_width(columns=['actual_prompt', 'expected_prompt'], width=len_max) sf.to_excel(excel_writer=excel_writer) excel_writer.save() expand_columns_size() </code>
from styleframe import StyleFrame
import pandas as pd

df = pd.read_csv('final_output.csv')


def beatify_output(row):

 if row['result'] == 'Pass':
  return ['background-color: red'] * len(row)
 if row['result'] == 'Fail'
  return ['background-color: green'] * len(row)

df = df.style.apply(beatify_output, axis=1)

df.to_excel('my_file.xlsx',sheet_name='Sheet1')```

def expand_columns_size():
    
     len_max = 50
    
     excel_writer = 
      StyleFrame.ExcelWriter('my_file.xlsx')
    
     sf = StyleFrame(df)
    
       
     sf.set_column_width(columns=['actual_prompt', 'expected_prompt'], width=len_max)
    
     sf.to_excel(excel_writer=excel_writer)
    
     excel_writer.save()
    
expand_columns_size()

2

The error is due to the fact that styleframe does not know how to handle pandas’ styler objects as it has its own way of expressing styles.

Since you are already using styleframe it would be easier to do all the styling with it.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
from styleframe import StyleFrame, Styler
df = pd.read_csv('final_output.csv')
sf = StyleFrame(df)
sf.apply_style_by_indexes(sf[sf['result'] == 'Pass'], Styler(bg_color="green"))
sf.apply_style_by_indexes(sf[sf['result'] == 'Fail'], Styler(bg_color="red"))
# If there are ONLY 'Pass'/'Fail' values in 'result' column we can use
# sf.apply_style_by_indexes(
# indexes_to_style=sf[sf['result'] == 'Pass'],
# styler_obj=Styler(bg_color="green"),
# complement_style=Styler(bg_color="red")
# )
len_max = 50
sf.set_column_width(columns=['actual_prompt', 'expected_prompt'], width=len_max)
sf.to_excel('my_file.xlsx').close() # or .save() if close() raises an AttributeError
</code>
<code>import pandas as pd from styleframe import StyleFrame, Styler df = pd.read_csv('final_output.csv') sf = StyleFrame(df) sf.apply_style_by_indexes(sf[sf['result'] == 'Pass'], Styler(bg_color="green")) sf.apply_style_by_indexes(sf[sf['result'] == 'Fail'], Styler(bg_color="red")) # If there are ONLY 'Pass'/'Fail' values in 'result' column we can use # sf.apply_style_by_indexes( # indexes_to_style=sf[sf['result'] == 'Pass'], # styler_obj=Styler(bg_color="green"), # complement_style=Styler(bg_color="red") # ) len_max = 50 sf.set_column_width(columns=['actual_prompt', 'expected_prompt'], width=len_max) sf.to_excel('my_file.xlsx').close() # or .save() if close() raises an AttributeError </code>
import pandas as pd

from styleframe import StyleFrame, Styler

df = pd.read_csv('final_output.csv')
sf = StyleFrame(df)
sf.apply_style_by_indexes(sf[sf['result'] == 'Pass'], Styler(bg_color="green"))
sf.apply_style_by_indexes(sf[sf['result'] == 'Fail'], Styler(bg_color="red"))

# If there are ONLY 'Pass'/'Fail' values in 'result' column we can use
# sf.apply_style_by_indexes(
#     indexes_to_style=sf[sf['result'] == 'Pass'],
#     styler_obj=Styler(bg_color="green"),
#     complement_style=Styler(bg_color="red")
# )

len_max = 50
sf.set_column_width(columns=['actual_prompt', 'expected_prompt'], width=len_max)
sf.to_excel('my_file.xlsx').close()  # or .save() if close() raises an AttributeError

outputs

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật