How can I trigger “Autosize (fit to content)” for st.data_editor columns programmatically?

Hi every1,

I’m building a local Streamlit app that reads a PDF and displays the extracted transactions in a st.data_editor table. The core issue is column width management: the table renders with column widths that are not acceptable for usability.

When I open the column menu (three dots) and click Autosize for a column, it correctly adjusts the width to fit the column’s content. However, I can’t find a way to:

  1. trigger Autosize automatically on load, or

  2. trigger Autosize via a custom button (e.g., “Autosize all columns”), or

  3. access the underlying autosize logic through Python.

Questions:

  • Does Streamlit expose any API for column autosizing (fit-to-content) in st.data_editor / st.dataframe?

  • Is there a documented minimum/maximum width behavior or layout rule that prevents small pixel widths from taking effect?

  • If there is no direct API, what’s the recommended workaround to get consistent fit-to-content widths (e.g., using width=None, forcing horizontal scroll, or any official component approach)?

I’m happy to share a minimal reproducible example if needed.

Thanks in advance.

You are looking for something like this:

st.data_editor(…, autosize=True)

st.session_state[“df_editor”].autosize()

st.data_editor(…, column_config={“col”: {“autosize”: True}})

,then i think you are out of luck since does not exist!

I don’t think there is any API exposure (Python-side or otherwise) to trigger column autosizing!

Maybe such workaround will help: function the use it in the code

def estimate_width(series, min_px=80, max_px=400):
    max_len = series.astype(str).map(len).max()
    return max(min_px, min(max_px, max_len * 8))
column_config = {
    col: st.column_config.TextColumn(
        col,
        width=estimate_width(df[col])
    )
    for col in df.columns
}

or use AgGrid with autoSizeAllColumns()

good luck

1 Like

Thanks a lot!

I’ll check tomorrow and will share the outcome.