Skip to content

Commit ae2cf1a

Browse files
committed
update datatable2 examples
1 parent 0c5682c commit ae2cf1a

13 files changed

Lines changed: 219 additions & 72 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import flet as ft
2+
import flet_datatable2 as fdt
3+
4+
5+
def main(page: ft.Page):
6+
def cell_text(value: str) -> ft.Text:
7+
"""A helper to truncate any overflowing cell text with an ellipsis."""
8+
return ft.Text(value, overflow=ft.TextOverflow.ELLIPSIS, max_lines=1)
9+
10+
page.add(
11+
ft.SafeArea(
12+
expand=True,
13+
content=fdt.DataTable2(
14+
expand=True,
15+
min_width=600,
16+
columns=[
17+
# Absolute pixel width — best for predictable, short fields.
18+
fdt.DataColumn2(label="Name", fixed_width=140),
19+
# Relative size S — compact, auto-fits the remaining space.
20+
fdt.DataColumn2(label="Role", size=fdt.DataColumnSize.S),
21+
# Relative size L — takes the lion's share of what's left.
22+
fdt.DataColumn2(label="Recent work", size=fdt.DataColumnSize.L),
23+
],
24+
rows=[
25+
ft.DataRow(
26+
cells=[
27+
ft.DataCell(cell_text("Alice Nakamura")),
28+
ft.DataCell(cell_text("Engineer")),
29+
ft.DataCell(
30+
cell_text(
31+
"Led the migration of our checkout service "
32+
"to a set of composable workers, cutting "
33+
"p99 latency in half."
34+
)
35+
),
36+
]
37+
),
38+
ft.DataRow(
39+
cells=[
40+
# Longer than 140px — shows ellipsis in a fixed column.
41+
ft.DataCell(cell_text("Bartholomew Laurent-Fitzgerald")),
42+
ft.DataCell(cell_text("Designer")),
43+
ft.DataCell(
44+
cell_text(
45+
"Rebuilt the onboarding flow and maintains "
46+
"the internal design-system token registry."
47+
)
48+
),
49+
]
50+
),
51+
ft.DataRow(
52+
cells=[
53+
ft.DataCell(cell_text("Chen")),
54+
ft.DataCell(cell_text("PM")),
55+
ft.DataCell(
56+
cell_text("Owns the Platform Reliability roadmap.")
57+
),
58+
]
59+
),
60+
],
61+
),
62+
)
63+
)
64+
65+
66+
if __name__ == "__main__":
67+
ft.run(main)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[project]
2+
name = "datatable2-column-widths"
3+
version = "1.0.0"
4+
description = "Demonstrates DataTable2 column widths (fixed pixels and relative S/M/L) with ellipsis truncation for overflowing cell text."
5+
requires-python = ">=3.10"
6+
keywords = ["datatable2", "column width", "ellipsis", "overflow", "extension"]
7+
authors = [{ name = "Flet team", email = "hello@flet.dev" }]
8+
dependencies = ["flet", "flet-datatable2"]
9+
10+
[dependency-groups]
11+
dev = ["flet-cli", "flet-desktop", "flet-web"]
12+
13+
[tool.flet.gallery]
14+
categories = ["Layout/DataTable2"]
15+
16+
[tool.flet.metadata]
17+
title = "Column widths"
18+
controls = ["SafeArea", "DataTable2", "DataColumn2", "DataCell", "Text"]
19+
layout_pattern = "table-view"
20+
complexity = "basic"
21+
features = ["fixed column width", "relative column size", "text ellipsis overflow"]
22+
23+
[tool.flet]
24+
org = "dev.flet"
25+
company = "Flet"
26+
copyright = "Copyright (C) 2023-2026 by Flet"

sdk/python/examples/controls/datatable2/example_1/main.py renamed to sdk/python/examples/controls/datatable2/empty_state/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
def main(page: ft.Page):
66
page.add(
77
ft.SafeArea(
8+
expand=True,
89
content=fdt.DataTable2(
10+
expand=True,
911
empty=ft.Text("This table is empty."),
1012
columns=[
1113
fdt.DataColumn2(label=ft.Text("First name")),

sdk/python/examples/controls/datatable2/example_1/pyproject.toml renamed to sdk/python/examples/controls/datatable2/empty_state/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[project]
2-
name = "datatable2-example-1"
2+
name = "datatable2-empty-state"
33
version = "1.0.0"
44
description = "Shows an empty DataTable2 state with configured columns and placeholder content."
55
requires-python = ">=3.10"
@@ -14,7 +14,7 @@ dev = ["flet-cli", "flet-desktop", "flet-web"]
1414
categories = ["Layout/DataTable2"]
1515

1616
[tool.flet.metadata]
17-
title = "Example 1"
17+
title = "Empty state"
1818
controls = ["SafeArea", "DataTable2", "DataColumn2", "Text"]
1919
layout_pattern = "table-view"
2020
complexity = "basic"

sdk/python/examples/controls/datatable2/example_2/data.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

sdk/python/examples/controls/datatable2/media/example_2.gif renamed to sdk/python/examples/controls/datatable2/media/sortable_and_selectable.gif

File renamed without changes.

sdk/python/examples/controls/datatable2/example_2/main.py renamed to sdk/python/examples/controls/datatable2/sortable_and_selectable/main.py

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,66 @@
1-
from data import desserts
1+
from dataclasses import dataclass
22

33
import flet as ft
44
import flet_datatable2 as ftd
55

66

7+
@dataclass
8+
class Dessert:
9+
name: str
10+
calories: float
11+
fat: float
12+
carbs: float
13+
protein: float
14+
sodium: float
15+
calcium: float
16+
iron: float
17+
18+
19+
desserts = [
20+
Dessert("Frozen Yogurt", 159, 6.0, 24, 4.0, 87, 14, 1),
21+
Dessert("Ice Cream Sandwich", 237, 9.0, 37, 4.3, 129, 8, 1),
22+
Dessert("Eclair", 262, 16.0, 24, 6.0, 337, 6, 7),
23+
Dessert("Cupcake", 305, 3.7, 67, 4.3, 413, 3, 8),
24+
Dessert("Gingerbread", 356, 16.0, 49, 3.9, 327, 7, 16),
25+
Dessert("Jelly Bean", 375, 0.0, 94, 0.0, 50, 0, 0),
26+
Dessert("Lollipop", 392, 0.2, 98, 0.0, 38, 0, 2),
27+
Dessert("Honeycomb", 408, 3.2, 87, 6.5, 562, 0, 45),
28+
Dessert("Donut", 452, 25.0, 51, 4.9, 326, 2, 22),
29+
Dessert("Apple Pie", 518, 26.0, 65, 7.0, 54, 12, 6),
30+
Dessert("Frozen Yougurt with sugar", 168, 6.0, 26, 4.0, 87, 14, 1),
31+
Dessert("Ice Cream Sandwich with sugar", 246, 9.0, 39, 4.3, 129, 8, 1),
32+
Dessert("Eclair with sugar", 271, 16.0, 26, 6.0, 337, 6, 7),
33+
Dessert("Cupcake with sugar", 314, 3.7, 69, 4.3, 413, 3, 8),
34+
Dessert("Gingerbread with sugar", 345, 16.0, 51, 3.9, 327, 7, 16),
35+
Dessert("Jelly Bean with sugar", 364, 0.0, 96, 0.0, 50, 0, 0),
36+
Dessert("Lollipop with sugar", 401, 0.2, 100, 0.0, 38, 0, 2),
37+
Dessert("Honeycomb with sugar", 417, 3.2, 89, 6.5, 562, 0, 45),
38+
Dessert("Donut with sugar", 461, 25.0, 53, 4.9, 326, 2, 22),
39+
Dessert("Apple pie with sugar", 527, 26.0, 67, 7.0, 54, 12, 6),
40+
Dessert("Frozen yougurt with honey", 223, 6.0, 36, 4.0, 87, 14, 1),
41+
Dessert("Ice Cream Sandwich with honey", 301, 9.0, 49, 4.3, 129, 8, 1),
42+
Dessert("Eclair with honey", 326, 16.0, 36, 6.0, 337, 6, 7),
43+
Dessert("Cupcake with honey", 369, 3.7, 79, 4.3, 413, 3, 8),
44+
Dessert("Gingerbread with honey", 420, 16.0, 61, 3.9, 327, 7, 16),
45+
Dessert("Jelly Bean with honey", 439, 0.0, 106, 0.0, 50, 0, 0),
46+
Dessert("Lollipop with honey", 456, 0.2, 110, 0.0, 38, 0, 2),
47+
Dessert("Honeycomb with honey", 472, 3.2, 99, 6.5, 562, 0, 45),
48+
Dessert("Donut with honey", 516, 25.0, 63, 4.9, 326, 2, 22),
49+
Dessert("Apple pie with honey", 582, 26.0, 77, 7.0, 54, 12, 6),
50+
]
51+
52+
753
def main(page: ft.Page):
854
page.vertical_alignment = ft.MainAxisAlignment.CENTER
955
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
1056

1157
sorted_desserts = list(desserts)
12-
data_table: ftd.DataTable2 | None = None
1358

1459
def handle_row_selection_change(e: ft.Event[ftd.DataRow2]) -> None:
1560
e.control.selected = not e.control.selected
1661
e.control.update()
1762

1863
def sort_column(e: ft.DataColumnSortEvent) -> None:
19-
if data_table is None:
20-
return
21-
2264
sorters = [
2365
lambda d: d.name.lower(),
2466
lambda d: d.calories,
@@ -30,10 +72,10 @@ def sort_column(e: ft.DataColumnSortEvent) -> None:
3072
lambda d: d.iron,
3173
]
3274
sorted_desserts.sort(key=sorters[e.column_index], reverse=not e.ascending)
33-
data_table.rows = get_data_rows(sorted_desserts)
34-
data_table.sort_column_index = e.column_index
35-
data_table.sort_ascending = e.ascending
36-
data_table.update()
75+
table.rows = get_data_rows(sorted_desserts)
76+
table.sort_column_index = e.column_index
77+
table.sort_ascending = e.ascending
78+
table.update()
3779

3880
def get_data_columns() -> list[ftd.DataColumn2]:
3981
return [
@@ -84,9 +126,9 @@ def get_data_rows(items: list) -> list[ftd.DataRow2]:
84126
for dessert in items
85127
]
86128

87-
data_table = ftd.DataTable2(
88-
show_checkbox_column=True,
129+
table = ftd.DataTable2(
89130
expand=True,
131+
show_checkbox_column=True,
90132
column_spacing=0,
91133
heading_row_color=ft.Colors.SECONDARY_CONTAINER,
92134
horizontal_margin=12,
@@ -98,11 +140,7 @@ def get_data_rows(items: list) -> list[ftd.DataRow2]:
98140
rows=get_data_rows(sorted_desserts),
99141
)
100142

101-
page.add(
102-
ft.SafeArea(
103-
content=data_table,
104-
)
105-
)
143+
page.add(ft.SafeArea(expand=True, content=table))
106144

107145

108146
if __name__ == "__main__":

sdk/python/examples/controls/datatable2/example_2/pyproject.toml renamed to sdk/python/examples/controls/datatable2/sortable_and_selectable/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[project]
2-
name = "datatable2-example-2"
2+
name = "datatable2-sortable-and-selectable"
33
version = "1.0.0"
44
description = "Builds an interactive DataTable2 with sortable nutrition columns and selectable rows."
55
requires-python = ">=3.10"
@@ -14,7 +14,7 @@ dev = ["flet-cli", "flet-desktop", "flet-web"]
1414
categories = ["Layout/DataTable2"]
1515

1616
[tool.flet.metadata]
17-
title = "Example 2"
17+
title = "Sortable and selectable"
1818
controls = ["SafeArea", "DataTable2", "DataColumn2", "DataRow2", "DataCell", "Text"]
1919
layout_pattern = "table-view"
2020
complexity = "basic"
47.4 KB
Loading
14.9 KB
Loading

0 commit comments

Comments
 (0)