Skip to content

DataTable headers don't wrap text #6199

@aperepel

Description

@aperepel

Hey, I'm trying to display a table with longer column headers and they just get cut off. Is there a way to make header text wrap to multiple lines?

I can get regular cells to wrap just fine by setting column width and using Text objects with newlines:

from textual.app import App
from textual.widgets import DataTable
from rich.text import Text

class MyApp(App):
    def compose(self):
        yield DataTable()

    def on_mount(self):
        table = self.query_one(DataTable)

        # Try header with newlines and see if height helps
        header = Text("Very Long\nHeader Text\nThat Wraps")
        table.add_column(header, width=20)

        # Cell content wraps fine when you set row height
        wrapped_cell = Text("This wraps\nto multiple\nlines")
        table.add_row(wrapped_cell, height=3)  # height allows multi-line display

        # Another example
        cell2 = Text("More text\nthat spans\nseveral lines")
        table.add_row(cell2, height=3)

MyApp().run()
Image

In the screenshot above, one can clearly see regular cells wrapping without issues, but the header always has a fixed height and truncates the text.

I tried passing a Text object with newlines to add_column() too but the header still shows as a single line and gets truncated.

Is there a way to make headers wrap like regular cells do?

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