Skip to content

Resize DataTable columns to fit smaller column content #6247

@anlar

Description

@anlar

Continued from #6246.

When using DataTable to show rows it resize table column only when adding rows for the first time. If I then clear table and add rows with smaller content length, table will still have long columns.

Code example:

from textual.app import App, ComposeResult
from textual.widgets import Button, DataTable

class TableApp(App):

    def compose(self) -> ComposeResult:
        yield Button('Clear table and add short lines')
        yield DataTable()

    def on_mount(self) -> None:
        table = self.query_one(DataTable)
        table.add_columns('1', '2', '3', '4' )
        table.add_row('1', '2', '3', 20 * 'long line')

    def on_button_pressed(self, event: Button.Pressed) -> None:
        table = self.query_one(DataTable)
        table.clear()
        table.add_row('1', '2', '3', 'short line')

app = TableApp()

if __name__ == "__main__":
    app.run()

Initial layout (when long rows added) - OK:

Image

After clean and add small rows - still have scrollbar:

Image

Textual Diagnostics

Versions

Name Value
Textual 6.6.0
Rich 14.2.0

Python

Name Value
Version 3.12.3
Implementation CPython
Compiler GCC 13.3.0
Executable /home/anlar/dev/anlar/tewi/venv/bin/python3

Operating System

Name Value
System Linux
Release 6.14.0-36-generic
Version #36~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Oct 15 15:45:17 UTC 2

Terminal

Name Value
Terminal Application tmux (3.4)
TERM tmux-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=173, height=40
legacy_windows False
min_width 1
max_width 173
is_terminal True
encoding utf-8
max_height 40
justify None
overflow None
no_wrap False
highlight None
markup None
height None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions