Skip to content

Commit 3646bd6

Browse files
committed
DataTable basic test
also fix bug with using str for DataCell content and formatting
1 parent a5d7a9c commit 3646bd6

File tree

3 files changed

+67
-23
lines changed

3 files changed

+67
-23
lines changed
9.61 KB
Loading
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
3+
import flet as ft
4+
import flet.testing as ftt
5+
6+
7+
@pytest.mark.asyncio(loop_scope="module")
8+
async def test_data_table_basic(flet_app: ftt.FletTestApp, request):
9+
flet_app.page.theme_mode = ft.ThemeMode.LIGHT
10+
await flet_app.assert_control_screenshot(
11+
request.node.name,
12+
ft.DataTable(
13+
columns=[
14+
ft.DataColumn(label="Column 1"),
15+
ft.DataColumn(label=ft.Text("Column 2")),
16+
ft.DataColumn(label=ft.Text("Column 3")),
17+
],
18+
rows=[
19+
ft.DataRow(
20+
cells=[
21+
ft.DataCell("Item 1"),
22+
ft.DataCell(ft.Text("Item 2")),
23+
ft.DataCell(ft.Text("Item 3")),
24+
]
25+
),
26+
],
27+
),
28+
)

sdk/python/packages/flet/src/flet/controls/material/datatable.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ class DataCell(Control):
9292
The data for a cell of a [`DataTable`][flet.DataTable].
9393
9494
Raises:
95-
AssertionError: If the [`content`][(c).] is neither a string nor a visible control.
95+
AssertionError: If the [`content`][(c).] is neither a string nor a visible
96+
control.
9697
"""
9798

9899
content: StrOrControl
@@ -102,11 +103,12 @@ class DataCell(Control):
102103
Typically a [`Text`][flet.Text] control or a [`Dropdown`][flet.Dropdown] control.
103104
104105
If the cell has no data, then a `Text` widget with placeholder text should be
105-
provided instead, and [`placeholder`][flet.DataCell.placeholder] should be set to `True`.
106+
provided instead, and [`placeholder`][flet.DataCell.placeholder] should be set to
107+
`True`.
106108
107109
To lay out multiple children, let this
108-
control's child be a container-like control such as [`Row`][flet.Row], [`Column`][flet.Column],
109-
or [`Stack`][flet.Stack], which have `controls` property.
110+
control's child be a container-like control such as [`Row`][flet.Row],
111+
[`Column`][flet.Column], or [`Stack`][flet.Stack], which have `controls` property.
110112
"""
111113

112114
placeholder: bool = False
@@ -196,7 +198,9 @@ class DataCell(Control):
196198

197199
def before_update(self):
198200
super().before_update()
199-
assert self.content.visible, "content must be visible"
201+
assert isinstance(self.content, str) or (
202+
isinstance(self.content, Control) and self.content.visible
203+
), "content must be a string or a visible control"
200204

201205

202206
@control("DataRow")
@@ -247,7 +251,8 @@ class DataRow(Control):
247251
"""
248252
Called if the row is long-pressed.
249253
250-
If a [`DataCell`][flet.DataCell] in the row has its [`DataCell.on_tap`][flet.DataCell.on_tap],
254+
If a [`DataCell`][flet.DataCell] in the row has its
255+
[`DataCell.on_tap`][flet.DataCell.on_tap],
251256
[`DataCell.on_double_tap`][flet.DataCell.on_double_tap],
252257
[`DataCell.on_long_press`][flet.DataCell.on_long_press],
253258
[`DataCell.on_tap_cancel`][flet.DataCell.on_tap_cancel]
@@ -260,8 +265,8 @@ class DataRow(Control):
260265
"""
261266
Called when the user selects or unselects a selectable row.
262267
263-
If this is not null, then this row is selectable. The current selection state of this
264-
row is given by selected.
268+
If this is not null, then this row is selectable. The current selection state of
269+
this row is given by selected.
265270
266271
If any row is selectable, then the table's heading row will have a checkbox that
267272
can be checked to select all selectable rows (and which is checked if all the rows
@@ -272,7 +277,8 @@ class DataRow(Control):
272277
273278
If a [`DataCell`][flet.DataCell] in the row has its
274279
[`DataCell.on_tap`][flet.DataCell.on_tap] callback defined, that
275-
callback behavior overrides the gesture behavior of the row for that particular cell.
280+
callback behavior overrides the gesture behavior of the row for that particular
281+
cell.
276282
"""
277283

278284
def __contains__(self, item):
@@ -292,8 +298,10 @@ class DataTable(ConstrainedControl):
292298
293299
Raises:
294300
AssertionError: If there are no visible [`columns`][(c).].
295-
AssertionError: If any visible row does not contain exactly as many visible [`cells`][flet.DataRow.cells] as there are visible [`columns`][(c).].
296-
AssertionError: If [`data_row_min_height`][(c).] is greater than [`data_row_max_height`][(c).].
301+
AssertionError: If any visible row does not contain exactly as many visible
302+
[`cells`][flet.DataRow.cells] as there are visible [`columns`][(c).].
303+
AssertionError: If [`data_row_min_height`][(c).] is greater than
304+
[`data_row_max_height`][(c).].
297305
AssertionError: If [`divider_thickness`][(c).] is negative.
298306
AssertionError: If [`sort_column_index`][(c).] is out of range.
299307
"""
@@ -311,7 +319,8 @@ class DataTable(ConstrainedControl):
311319

312320
sort_ascending: bool = False
313321
"""
314-
Whether the column mentioned in [`sort_column_index`][flet.DataTable.sort_column_index],
322+
Whether the column mentioned in
323+
[`sort_column_index`][flet.DataTable.sort_column_index],
315324
if any, is sorted in ascending order.
316325
317326
If `True`, the order is ascending (meaning the rows with the smallest values for
@@ -390,7 +399,8 @@ class DataTable(ConstrainedControl):
390399
"""
391400
The background color for the data rows.
392401
393-
The effective background color can be made to depend on the [`ControlState`][flet.ControlState]
402+
The effective background color can be made to depend on the
403+
[`ControlState`][flet.ControlState]
394404
state, i.e. if the row is selected, pressed, hovered, focused, disabled or enabled.
395405
The color is painted as an overlay to the row. To make sure that the row's InkWell
396406
is visible (when pressed, hovered and focused), it is recommended to use a
@@ -401,7 +411,8 @@ class DataTable(ConstrainedControl):
401411
"""
402412
The minimum height of each row (excluding the row that contains column headings).
403413
404-
Defaults to `48.0` and must be less than or equal to [`data_row_max_height`][flet.DataTable.data_row_max_height].
414+
Defaults to `48.0` and must be less than or equal to
415+
[`data_row_max_height`][flet.DataTable.data_row_max_height].
405416
"""
406417

407418
data_row_max_height: Optional[Number] = None
@@ -440,10 +451,11 @@ class DataTable(ConstrainedControl):
440451
"""
441452
The background color for the heading row.
442453
443-
The effective background color can be made to depend on the [`ControlState`][flet.ControlState]
444-
state, i.e. if the row is pressed, hovered, focused when sorted. The color is
445-
painted as an overlay to the row. To make sure that the row's InkWell is visible
446-
(when pressed, hovered and focused), it is recommended to use a translucent color.
454+
The effective background color can be made to depend on the
455+
[`ControlState`][flet.ControlState] state, i.e. if the row is pressed, hovered,
456+
focused when sorted. The color is painted as an overlay to the row. To make sure
457+
that the row's InkWell is visible (when pressed, hovered and focused), it is
458+
recommended to use a translucent color.
447459
"""
448460

449461
heading_row_height: Optional[Number] = None
@@ -475,7 +487,8 @@ class DataTable(ConstrainedControl):
475487
Invoked when the user selects or unselects every row, using the checkbox in the
476488
heading row.
477489
478-
If this is `None`, then the [`DataRow.on_select_change`][flet.DataRow.on_select_change]
490+
If this is `None`, then the
491+
[`DataRow.on_select_change`][flet.DataRow.on_select_change]
479492
callback of every row in the table is invoked appropriately instead.
480493
481494
To control whether a particular row is selectable or not, see
@@ -509,14 +522,17 @@ def before_update(self):
509522
or self.data_row_max_height is None
510523
or (self.data_row_min_height <= self.data_row_max_height)
511524
), (
512-
f"data_row_min_height ({self.data_row_min_height}) must be less than or equal to data_row_max_height ({self.data_row_max_height})"
525+
f"data_row_min_height ({self.data_row_min_height}) must be less than or "
526+
f"equal to data_row_max_height ({self.data_row_max_height})"
513527
)
514528
assert self.divider_thickness is None or self.divider_thickness >= 0, (
515-
f"divider_thickness must be greater than or equal to 0, got {self.divider_thickness}"
529+
f"divider_thickness must be greater than or equal to 0, "
530+
f"got {self.divider_thickness}"
516531
)
517532
assert self.sort_column_index is None or (
518533
0 <= self.sort_column_index < visible_columns_count
519534
), (
520-
f"sort_column_index ({self.sort_column_index}) must be greater than or equal to 0 and less than the "
521-
f"number of visible columns ({visible_columns_count})"
535+
f"sort_column_index ({self.sort_column_index}) must be greater than or "
536+
f"equal to 0 and less than the number of visible columns "
537+
f"({visible_columns_count})"
522538
)

0 commit comments

Comments
 (0)