Skip to content

Conversation

vkbsb
Copy link

@vkbsb vkbsb commented Dec 31, 2024

  • updated the matplotlib converter to pickup styling based on class as well.
    Current implementation picks up the details based on id. Which is still needed if there is an override but there are scenarios where the class styling has to be picked for table to look good.
    Used the following code to validate that this is working..
import numpy as npy
import pandas as pd
import dataframe_image as dfi

col_headers = {
    "selector": ".col_heading, thead",
    "props": "color: white; background-color: #1d5632; \
            font-size: 11px"
    }
col_data = {
    "selector": ".data",
    "props": "color: black; background-color: #f4f4f4; \
            font-size: 11px"
}

x,y,z = npy.random.random((3,3))
df = pd.DataFrame({
    "x":x[0:],
    "y":y[0:],
    "z":z[0:]
})
styled_df = df.style.set_table_styles([col_headers, col_data])
dfi.export(styled_df, "df_style.png", table_conversion="matplotlib")


output = {
    'Metric': ['P50_FPS', 'Batt_Drop(PM)', 'Temp_Inc(PM)'], 
    'Low': [19.58858, 0.47901234567901235, 0.2906172839506173], 
    'Mid': [29.933165, 0.3128301033724491, 0.22643293121519142], 
    'High': [29.924354, 0.36855614395331243, 0.2592294391008322], 
    'Ultra': [44.800098500000004, 0.3901357466063348, 0.3003981900452488]
    }
df = pd.DataFrame(output)
df = df.apply(lambda x: round(x, 2))
styled_df = df.style.format(precision=2).hide(axis='index')
styled_df.set_caption("Hello World")
styled_df.set_properties(subset=["Low", "High"], **{'font-size': '14pt', 'width':'40px', 'background-color':'lightblue', 'padding':'10px', 'text-align':'center'})
styled_df.set_properties(subset=["Mid", "Ultra"], **{'color': 'red', 'width':'40px', 'background-color':'lightyellow', 'padding':'10px', 'text-align':'center'})
dfi.export(styled_df, "df_style_1.png", table_conversion="matplotlib")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant