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

Unable to adjust table width and height in Jupyter and Quarto using gt package

Open hasibagen opened this issue 7 months ago • 4 comments

Hello,

I am trying to use the gt package to display tables in both Jupyter and Quarto. However, I am unable to modify the table's width and height despite following the documentation. The table remains too small, and changes to its dimensions are not reflected in the output.

Here is the code I am using:

import pandas as pd
from great_tables import GT

# Minimal example data in English
data = {
    "Region": ["Prefrontal", "Left Temporal", "Right Temporal"],
    "under_curve": [4.988946, -15.664746, -11.586672],
    "t_centroid": [56.0, 41.0, 27.0],
    "slop": [0.000383, 0.000160, 0.000138],
    "mean": [0.004870, -0.027652, -0.013757],
    "peak": [0.037685, 0.008440, 0.036546]
}

# Create the DataFrame
feature_df = pd.DataFrame(data)

# Create the GT table
feature_result = GT(
    pd.DataFrame(
        {
            "Region": feature_df["Region"],
            "S Score": feature_df["under_curve"].round(0).astype(int),  # rounding to integer
            "T Centroid": feature_df["t_centroid"].round(0).astype(int),  # rounding to integer
            "Mean": feature_df["mean"].round(2),
            "Peak": feature_df["peak"].round(2),
        }
    )
)

# Attempting to adjust the table width
feature_result.tab_options(table_width="300%")

# Display the table
feature_result

Expected Behavior: I expect the table to have a specific width and height based on the options I set using tab_options. Specifically, I want the table to adjust to a wider layout to make it more readable.

Actual Behavior: The table still appears too small, and there is no change in width or height in both Jupyter and Quarto outputs. Despite using the tab_options method with different width settings, the appearance remains the same.

Steps to Reproduce:

Run the code provided in a Jupyter notebook or Quarto document.

Observe that the table does not change size despite using tab_options to set the table width.

What I Have Tried:

I have tried using tab_options(table_width="30px") and tab_options(table_width="300%") as per the documentation, but neither resulted in the table adjusting to the desired size.

I have checked the official reference and followed the instructions for adjusting the table dimensions, but the changes don't take effect.

Environment:

great-tables version: 0.17.0 Python version: 3.12.9 | packaged by conda-forge | (main, Mar 4 2025, 22:48:41) [GCC 13.3.0] Jupyter version: 1.1.1 Quarto version:1.7.27

Request: Could you please provide guidance on how to adjust the table size in both Jupyter and Quarto? Is there any additional step I need to follow or any potential issues with great-tables that prevent these options from working properly?

Thanks for your help!

hasibagen avatar Apr 21 '25 07:04 hasibagen

@hasibagen If I understand correctly, feature_result.tab_options(table_width="300%") should do what you want. However, in the last line, you're displaying the original feature_result, not the modified version—so the table appears unchanged.

With Great Tables, most functions return a new object rather than modifying the original in place. In your case, try commenting out the final line and displaying the updated object instead. That should apply the resized width as expected.

jrycw avatar Apr 22 '25 01:04 jrycw

@hasibagen If I understand correctly, feature_result.tab_options(table_width="300%") should do what you want. However, in the last line, you're displaying the original feature_result, not the modified version—so the table appears unchanged.

With Great Tables, most functions return a new object rather than modifying the original in place. In your case, try commenting out the final line and displaying the updated object instead. That should apply the resized width as expected.

I have just created new variables and output them according to your suggestion. When I adjust different table_widths, the width of the table generated by quarto is the same.

The width changes can be seen in the real-time display results of vscode, but it seems that the width remains the same in the generated word file. Does this mean that it is a problem with quarto?

hasibagen avatar Apr 22 '25 02:04 hasibagen

Thanks for the feedback—this reminded me that we do have a warning message for .cols_width() here. I haven’t used this function often myself, but you might try setting quarto_disable_processing=True to see if that resolves the issue.

jrycw avatar Apr 22 '25 04:04 jrycw

This question was also posted in Quarto repo. You can see my answer there:

  • https://github.com/quarto-dev/quarto-cli/discussions/12594#discussioncomment-13129416

I'll try to sum-up the main points here to help answer some questions:

  • great_tables produces HTML tables. In Quarto, using format: html' will be okay; however, when targeting another format like format: pdforformat: docx`, this is obviously a different story. Quarto provides an HTML table parsing feature so that such tables can be seen as a Tables element by Pandoc and then written in the correct output syntax for the targeted format that does not support HTML. This conversion from HTML tables may not be lossless for obvious reasons, especially for anything related to styles (like CSS).

  • Quarto offers support for some CSS translation from HTML to Typst to improve this, so some formats may behave better than others when including a style great_tables in a Quarto document.

  • you might try setting quarto_disable_processing=True to see if that resolves the issue.

    This could be helpful, but only in a format that supports HTML tables produced by great_tables—for other formats that do not support HTML, this would make the great_tables not be included.

  • feature_result.tab_options(table_width="300%"): This will set some CSS on the <style> element. So it will probably work for Quarto documents targeting HTML output, but not for other formats.

    The width changes can be seen in the real-time display results of vscode, but it seems that the width remains the same in the generated word file.

    And it seems the targeted format was Word document here.

Using .cols_width() will be supported, though, in most formats because Pandoc's Tables elements support ColWidth as a table attribute. However, for now, this only works when the width is in Percent (hence the warning added).

Hope it helps understand a bit more how great_tables objects and Quarto interact

cderv avatar May 13 '25 10:05 cderv