Skip to content

Commit 88941b6

Browse files
hannahblairgradio-pr-botabidlabs
authored
Remove fixed layouts from dataframe (#10786)
* remove fixed layout * add changeset * tweak * - remove css widths on change * tweak * allow % widths * add story * dataframe --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com> Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
1 parent cae5517 commit 88941b6

File tree

6 files changed

+63
-19
lines changed

6 files changed

+63
-19
lines changed

.changeset/cold-paws-cheer.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gradio/dataframe": patch
3+
"gradio": patch
4+
---
5+
6+
fix:Remove fixed layouts from dataframe

gradio/components/dataframe.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def __init__(
129129
key: if assigned, will be used to assume identity across a re-render. Components that have the same key across a re-render will have their value preserved.
130130
wrap: If True, the text in table cells will wrap when appropriate. If False and the `column_width` parameter is not set, the column widths will expand based on the cell contents and the table may need to be horizontally scrolled. If `column_width` is set, then any overflow text will be hidden.
131131
line_breaks: If True (default), will enable Github-flavored Markdown line breaks in chatbot messages. If False, single new lines will be ignored. Only applies for columns of type "markdown."
132-
column_widths: An optional list representing the width of each column. The elements of the list should be in the format "100px" (ints are also accepted and converted to pixel values) or "10%". If not provided, the column widths will be automatically determined based on the content of the cells. Setting this parameter will cause the browser to try to fit the table within the page width.
132+
column_widths: An optional list representing the width of each column. The elements of the list should be in the format "100px" (ints are also accepted and converted to pixel values) or "10%". The percentage width is calculated based on the viewport width of the table. If not provided, the column widths will be automatically determined based on the content of the cells.
133133
show_fullscreen_button: If True, will show a button to view the values in the table in fullscreen mode.
134134
show_copy_button: If True, will show a button to copy the table data to the clipboard.
135135
show_row_numbers: If True, will display row numbers in a separate column.
@@ -174,7 +174,11 @@ def __init__(
174174
self.max_height = max_height
175175
self.line_breaks = line_breaks
176176
self.column_widths = [
177-
w if isinstance(w, str) else f"{w}px" for w in (column_widths or [])
177+
w
178+
if isinstance(w, str)
179+
and (w.endswith("px") or w.endswith("%") or w == "auto")
180+
else f"{w}px"
181+
for w in (column_widths or [])
178182
]
179183
self.show_fullscreen_button = show_fullscreen_button
180184
self.show_copy_button = show_copy_button

js/dataframe/Dataframe.stories.svelte

+14
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,20 @@
585585
}}
586586
/>
587587

588+
<Story
589+
name="Dataframe with pixel and percentage column widths set"
590+
args={{
591+
values: [
592+
[1, 2, 3, 4, 5],
593+
[6, 7, 8, 9, 10]
594+
],
595+
headers: ["10%", "50%", "40%", "100px", "100px"],
596+
col_count: [5, "dynamic"],
597+
row_count: [2, "dynamic"],
598+
column_widths: ["10%", "50%", "40%", "100px", "100px"]
599+
}}
600+
/>
601+
588602
<Story
589603
name="Dataframe with drag selection"
590604
args={{

js/dataframe/shared/EditableCell.svelte

+6-5
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,18 @@
216216
}
217217
218218
.multiline {
219-
white-space: pre-line;
220-
overflow: visible;
219+
white-space: pre;
220+
overflow: hidden;
221+
text-overflow: ellipsis;
221222
}
222223
223224
.header {
224225
transform: translateX(0);
225226
font-weight: var(--weight-bold);
226-
white-space: normal;
227-
word-break: break-word;
227+
white-space: nowrap;
228+
overflow: hidden;
229+
text-overflow: ellipsis;
228230
margin-left: var(--size-1);
229-
overflow: visible;
230231
}
231232
232233
.edit {

js/dataframe/shared/Table.svelte

+26-12
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@
172172
}[][] = [[]];
173173
174174
$: if (!dequal(values, old_val)) {
175+
if (parent) {
176+
for (let i = 0; i < 50; i++) {
177+
parent.style.removeProperty(`--cell-width-${i}`);
178+
}
179+
}
175180
// only reset sort state when values are changed
176181
const is_reset =
177182
values.length === 0 || (values.length === 1 && values[0].length === 0);
@@ -198,6 +203,10 @@
198203
if ($df_state.current_search_query) {
199204
df_actions.handle_search(null);
200205
}
206+
207+
if (parent && cells.length > 0) {
208+
set_cell_widths();
209+
}
201210
}
202211
203212
$: if ($df_state.current_search_query !== undefined) {
@@ -365,20 +374,29 @@
365374
if (show_row_numbers) {
366375
parent.style.setProperty(`--cell-width-row-number`, `${widths[0]}px`);
367376
}
377+
378+
for (let i = 0; i < 50; i++) {
379+
if (!column_widths[i]) {
380+
parent.style.removeProperty(`--cell-width-${i}`);
381+
} else if (column_widths[i].endsWith("%")) {
382+
const percentage = parseFloat(column_widths[i]);
383+
const pixel_width = Math.floor((percentage / 100) * parent.clientWidth);
384+
parent.style.setProperty(`--cell-width-${i}`, `${pixel_width}px`);
385+
} else {
386+
parent.style.setProperty(`--cell-width-${i}`, column_widths[i]);
387+
}
388+
}
389+
368390
widths.forEach((width, i) => {
369391
if (!column_widths[i]) {
370-
parent.style.setProperty(
371-
`--cell-width-${i}`,
372-
`${width - scrollbar_width / widths.length}px`
373-
);
392+
const calculated_width = `${Math.max(width, 45)}px`;
393+
parent.style.setProperty(`--cell-width-${i}`, calculated_width);
374394
}
375395
});
376396
}
377397
378398
function get_cell_width(index: number): string {
379-
return column_widths[index]
380-
? `${column_widths[index]}`
381-
: `var(--cell-width-${index})`;
399+
return `var(--cell-width-${index})`;
382400
}
383401
384402
let table_height: number =
@@ -725,7 +743,7 @@
725743
role="grid"
726744
tabindex="0"
727745
>
728-
<table bind:this={table} class:fixed-layout={column_widths.length != 0}>
746+
<table bind:this={table}>
729747
{#if label && label.length !== 0}
730748
<caption class="sr-only">{label}</caption>
731749
{/if}
@@ -1021,10 +1039,6 @@
10211039
border-collapse: separate;
10221040
}
10231041
1024-
table.fixed-layout {
1025-
table-layout: fixed;
1026-
}
1027-
10281042
thead {
10291043
position: sticky;
10301044
top: 0;

js/dataframe/shared/TableHeader.svelte

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
event.preventDefault();
8080
event.stopPropagation();
8181
}}
82+
title={value}
8283
>
8384
<div class="cell-wrap">
8485
<div class="header-content">
@@ -89,6 +90,7 @@
8990
event.preventDefault();
9091
event.stopPropagation();
9192
}}
93+
title={value}
9294
>
9395
<EditableCell
9496
{max_chars}
@@ -209,8 +211,11 @@
209211
}
210212
211213
.header-button {
214+
display: flex;
212215
text-align: left;
213216
width: 100%;
217+
overflow: hidden;
218+
text-overflow: ellipsis;
214219
display: flex;
215220
align-items: center;
216221
position: relative;

0 commit comments

Comments
 (0)