posit-dev / great-tables

Make awesome display tables using Python.
https://posit-dev.github.io/great-tables/
MIT License
1.87k stars 70 forks source link

Header not showing when printing PNG file #448

Open ricardocosta15 opened 1 month ago

ricardocosta15 commented 1 month ago

Description

When printing a simple table example (the sp500 example), using chrome and saving it as png file, the table header and titles do not show

image

Reproducible example

` start_date = "2010-06-07" end_date = "2010-06-14"

sp500_mini = sp500[(sp500["date"] >= start_date) & (sp500["date"] <= end_date)] print(type(sp500)) ( GT(sp500_mini) .tab_header(title="S&P 500", subtitle=f"{start_date} to {end_date}") .fmt_currency(columns=["open", "high", "low", "close"]) .fmt_date(columns="date", date_style="wd_m_day_year") .fmt_number(columns="volume", compact=True) .cols_hide(columns="adj_close")

)

great_tables.GT(sp500_mini).save(file='image.png', web_driver="chrome")`

Development environment

jrycw commented 1 month ago

It seems the main issue is that you're saving the image from GT(sp500_mini), which doesn’t include the header. I believe what you're aiming to achieve can be done with the following code:

from great_tables import GT
from great_tables.data import sp500

# Define the start and end dates for the data range
start_date = "2010-06-07"
end_date = "2010-06-14"

# Filter sp500 using Pandas to dates between `start_date` and `end_date`
sp500_mini = sp500[(sp500["date"] >= start_date) & (sp500["date"] <= end_date)]

# Create a display table based on the `sp500_mini` table data
(
    GT(sp500_mini)
    .tab_header(title="S&P 500", subtitle=f"{start_date} to {end_date}")
    .fmt_currency(columns=["open", "high", "low", "close"])
    .fmt_date(columns="date", date_style="wd_m_day_year")
    .fmt_number(columns="volume", compact=True)
    .cols_hide(columns="adj_close")
    .save(file='image.png', web_driver="chrome")
)