great-tables icon indicating copy to clipboard operation
great-tables copied to clipboard

Tables getting cut/blanked when `.save()` because of `style.text()`

Open ramibd opened this issue 6 months ago • 7 comments

Prework

Description

I'm making 2 very very simple tables, both with a few topics and sub-topics. For better visualization purposes, i want to highlight and have a bold text on the topics.

Everything went well for the first table, but when i fineshed the second one and .save() it, for some reason the bottom of the table got cut/blanked out of the .PNG file.

I investigated why this was happening and discovered that this behavior is caused by style.text(color="black", weight='bold'). I can't tell exactly when it happens but i will leave some exemples below.

(Observation 1) -> If you open the table with the cut, you will see that it wasn't cutted, but blanked out.

(Observation 2) -> Comments on the code were made for better understandment of the code and issue.

Table Code

With issue:

table = (GT(dados_v_mom, rowname_col='index')
# --- Add header
    .tab_header(title=md('**Brasil: Pesquisa Mensal de Serviços**'), subtitle='Volume de serviços - Variação MoM%')
# --- Formating numbers cells
    .fmt_number(columns=list(dados_v_mom.columns[1:]),decimals=1)
    .fmt_percent(columns=list(dados_v_mom.columns[1:]),decimals=1)
# --- Hide cells
    .cols_hide(columns=list(dados_v_mom.columns[1:-6]))
# --- Add source note tab
    .tab_source_note(source_note="Última atualização: "+ str(dt.date.today()))
# --- Add row padding configuration
    .tab_options(heading_background_color='#000077', data_row_padding='1px', row_group_padding='2px')

# --- Topics:
    # 0.
    .tab_style(style=style.fill(color="#000077"), locations=[loc.body(rows=[0]), loc.stub(rows=[0])])
    .tab_style(style=style.text(color="white", weight='bold'), locations=[loc.body(rows=[0]), loc.stub(rows=[0])])
    # 1.
    .tab_style(style=style.fill(color="#FF8000"), locations=[loc.body(rows=[1]), loc.stub(rows=[1])])
    .tab_style(style=style.text(color="black", weight='bold'), locations=loc.stub(rows=[1]))
    # 2.
    .tab_style(style=style.fill(color="#FF8000"), locations=[loc.body(rows=[4]), loc.stub(rows=[4])])
    .tab_style(style=style.text(color="black", weight='bold'), locations=loc.stub(rows=[4]))
    # 3.
    .tab_style(style=style.fill(color="#FF8000"), locations=[loc.body(rows=[9]), loc.stub(rows=[9])])
    .tab_style(style=style.text(color="black", weight='bold'), locations=loc.stub(rows=[9]))
    # 4.
    .tab_style(style=style.fill(color="#FF8000"), locations=[loc.body(rows=[14]), loc.stub(rows=[14])])
    .tab_style(style=style.text(color="black", weight='bold'), locations=loc.stub(rows=[14]))
    # 5.
    .tab_style(style=style.fill(color="#FF8000"), locations=[loc.body(rows=[19]), loc.stub(rows=[19])])
    .tab_style(style=style.text(color="black", weight='bold'), locations=loc.stub(rows=[19]))

    # --- If I add the segment below, the table will be cut even further:
    .tab_style(style=(style.text(align='center', weight='bold')),locations=loc.column_labels())
    )

table.save(
        file='PATH\table_name.png',
        selector='table',
        scale=1.0,
        expand=5,
        web_driver='edge',
        encoding='utf-8',
    )

Without issue:

By "Without issue" i mean without beeing cut on the bottom. For that, I have to give up bold text on rows=[9] and rows=[14] (specifically). On the code above I simply comment those segments.

    # 3.
    .tab_style(style=style.fill(color="#FF8000"), locations=[loc.body(rows=[9]), loc.stub(rows=[9])])
    # .tab_style(style=style.text(color="black", weight='bold'), locations=loc.stub(rows=[9]))
    # 4.
    .tab_style(style=style.fill(color="#FF8000"), locations=[loc.body(rows=[14]), loc.stub(rows=[14])])
    # .tab_style(style=style.text(color="black", weight='bold'), locations=loc.stub(rows=[14]))

Results

With issue:

Image

Without issue:

Image

What i found out:

As you can see, by commenting the .tab_style(style=style.text(color="black", weight='bold'), locations=loc.stub(rows=[9])) I was able to partially solve the issue, but i couldn't figure out why this segment was effecting the .save() method, and why it was specifically rows=[9] and rows=[14].

  • I did some tweeking on my code and found out that a few other "subtopic" rows could have the same behavior, if i were to apply this .tab_style() to them, for exemple rows=[8].

  • I found out that if I were to apply .tab_style(style=style.text(color="black", weight='bold'), locations=loc.stub(rows=[9])) to all rows, the issue would be solved.

  • By formatting the column lables with .tab_style(style=(style.text(align='center', weight='bold')),locations=loc.column_labels()) the cut gets much worse.

Development environment

  • Operating System: [Windows]
  • great_tables Version: [0.14.0]

ramibd avatar May 14 '25 14:05 ramibd