-
Notifications
You must be signed in to change notification settings - Fork 822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to make DataTable
columns expand to fill available space
#5455
Comments
Oh more info based on the automated response above. I want to constraint my columns to fit in the container without a scrollbar. |
This issue of how to expand Unfortunately, currently the built-in column options are limited to either the optimal width or a fixed width. Hopefully proportional column widths will be supported in future versions. But in the meantime, here's my attempt at creating a 'stretchy' table, where all columns adapt to fill the available width. If you want only certain columns to be 'stretchy', hopefully this provides a jumping-off point. First, we need to determine the actual size of the table widget. Obviously this will change when the terminal window is resized, so we will need an event handler for the from textual import events
from textual.app import App, ComposeResult
from textual.widgets import DataTable
class StetchyDataTable(DataTable):
def on_resize(self, event: events.Resize) -> None:
self.notify(str(event.size))
class ExampleApp(App):
def compose(self) -> ComposeResult:
yield StetchyDataTable()
def on_mount(self) -> None:
table = self.query_one(StetchyDataTable)
table.add_columns(*[f"C{col}" for col in range(1, 4)])
for row in range(1, 6):
table.add_row(*[f"R{row}C{col}" for col in range(1, 4)])
if __name__ == "__main__":
app = ExampleApp()
app.run() This To calculate the 'stretchy' columns widths, divide the table width by the number of columns. column_width = event.size.width // len(self.columns) Then update the column widths and refresh the widget. class StretchyDataTable(DataTable):
def on_resize(self, event: events.Resize) -> None:
column_width = event.size.width // len(self.columns)
for column in self.columns.values():
column.auto_width = False
column.width = column_width
self.refresh() |
Oops, I realised that the above code doesn't account for the cell padding. The column width should instead be calculated something like this: total_width = event.size.width
total_padding = 2 * (self.cell_padding * len(self.columns))
column_width = (total_width - total_padding) // len(self.columns) |
Thank you so much for this solution! It helped me achieve my goal 😊 I have to mention though that if your rows have a label column you have to also remove the label column width (and padding) from total width. Thanks for pointing me in the right direction. |
Don't forget to star the repository! Follow @textualizeio for Textual updates. |
Hello,
There could be a way to do this already, but I want columns in my
DataTable
to expand to fill the available space and not their contents. Here's a minimal code that shows how I am setting my fixedwidth
values. I am trying to determine the rightwidth
.Thanks a lot
The text was updated successfully, but these errors were encountered: