Skip to content

Commit 8db3f0a

Browse files
committed
Dropdown in a Grid example
1 parent 4a8825b commit 8db3f0a

1 file changed

Lines changed: 203 additions & 0 deletions

File tree

examples/grid.py

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
import pglet
2+
from pglet import (
3+
Button,
4+
Checkbox,
5+
Column,
6+
Dropdown,
7+
Grid,
8+
Stack,
9+
Text,
10+
Textbox,
11+
Toolbar,
12+
dropdown,
13+
toolbar,
14+
)
15+
16+
17+
class Person:
18+
def __init__(
19+
self,
20+
first_name: str,
21+
last_name: str,
22+
age: int = None,
23+
employee: bool = False,
24+
color: str = None,
25+
):
26+
self.first_name = first_name
27+
self.last_name = last_name
28+
self.age = age
29+
self.employee = employee
30+
self.color = color
31+
32+
33+
def main(page):
34+
page.title = "Grid example"
35+
page.update()
36+
37+
# Basic grid
38+
page.add(
39+
Text("Basic grid", size="large"),
40+
Stack(
41+
width="50%",
42+
controls=[
43+
Grid(
44+
columns=[
45+
Column(name="First name", field_name="first_name"),
46+
Column(name="Last name", field_name="last_name"),
47+
Column(name="Age", field_name="age"),
48+
],
49+
items=[
50+
Person(first_name="John", last_name="Smith", age=30),
51+
Person(first_name="Samantha", last_name="Fox", age=43),
52+
Person(first_name="Alice", last_name="Brown", age=25),
53+
],
54+
)
55+
],
56+
),
57+
)
58+
59+
# Sortable grid
60+
page.add(
61+
Text("Sortable grid with resizable columns and selectable rows", size="large"),
62+
Grid(
63+
selection_mode="single",
64+
preserve_selection=True,
65+
columns=[
66+
Column(
67+
resizable=True,
68+
sortable="string",
69+
name="First name",
70+
field_name="first_name",
71+
),
72+
Column(
73+
resizable=True,
74+
sortable="string",
75+
sorted="asc",
76+
name="Last name",
77+
field_name="last_name",
78+
),
79+
Column(resizable=True, sortable="number", name="Age", field_name="age"),
80+
],
81+
items=[
82+
Person(first_name="John", last_name="Smith", age=30),
83+
Person(first_name="Samantha", last_name="Fox", age=43),
84+
Person(first_name="Alice", last_name="Brown", age=25),
85+
],
86+
),
87+
)
88+
89+
# Compact grid
90+
page.add(
91+
Text("Compact grid with no header and multiple selection", size="large"),
92+
Grid(
93+
compact=True,
94+
header_visible=False,
95+
selection_mode="multiple",
96+
preserve_selection=True,
97+
columns=[
98+
Column(max_width=100, field_name="first_name"),
99+
Column(max_width=100, field_name="last_name"),
100+
Column(max_width=100, field_name="age"),
101+
],
102+
items=[
103+
Person(first_name="John", last_name="Smith", age=30),
104+
Person(first_name="Samantha", last_name="Fox", age=43),
105+
Person(first_name="Alice", last_name="Brown", age=25),
106+
],
107+
),
108+
)
109+
110+
# Dynamic grid
111+
grid = None
112+
113+
def delete_records(e):
114+
for r in grid.selected_items:
115+
grid.items.remove(r)
116+
page.update()
117+
118+
delete_records = toolbar.Item(
119+
text="Delete records", icon="Delete", disabled=True, on_click=delete_records
120+
)
121+
grid_toolbar = Toolbar(items=[delete_records])
122+
123+
def grid_items_selected(e):
124+
delete_records.disabled = len(e.control.selected_items) == 0
125+
delete_records.update()
126+
127+
grid = Grid(
128+
selection_mode="multiple",
129+
compact=True,
130+
header_visible=True,
131+
columns=[
132+
Column(
133+
name="First name", template_controls=[Textbox(value="{first_name}")]
134+
),
135+
Column(name="Last name", template_controls=[Textbox(value="{last_name}")]),
136+
Column(name="Age", template_controls=[Text(value="{age}")]),
137+
Column(
138+
name="Favorite color",
139+
template_controls=[
140+
Dropdown(
141+
value="{color}",
142+
options=[
143+
dropdown.Option("red", "Red"),
144+
dropdown.Option("green", "Green"),
145+
dropdown.Option("blue", "Blue"),
146+
],
147+
)
148+
],
149+
),
150+
Column(
151+
name="Is employee", template_controls=[Checkbox(value_field="employee")]
152+
),
153+
],
154+
items=[
155+
Person(
156+
first_name="John",
157+
last_name="Smith",
158+
age=30,
159+
employee=False,
160+
color="blue",
161+
),
162+
Person(
163+
first_name="Jack",
164+
last_name="Brown",
165+
age=43,
166+
employee=True,
167+
color="green",
168+
),
169+
Person(first_name="Alice", last_name="Fox", age=25, employee=False),
170+
],
171+
margin=0,
172+
on_select=grid_items_selected,
173+
)
174+
175+
first_name = Textbox("First name")
176+
last_name = Textbox("Last name")
177+
age = Textbox("Age")
178+
179+
def add_record(e):
180+
grid.items.append(
181+
Person(
182+
first_name=first_name.value,
183+
last_name=last_name.value,
184+
age=age.value,
185+
employee=True,
186+
)
187+
)
188+
first_name.value = ""
189+
last_name.value = ""
190+
age.value = ""
191+
page.update()
192+
193+
page.add(
194+
Text("Dynamic grid with template columns", size="large"),
195+
grid_toolbar,
196+
grid,
197+
Text("Add new employee record", size="medium"),
198+
Stack(horizontal=True, controls=[first_name, last_name, age]),
199+
Button("Add record", on_click=add_record),
200+
)
201+
202+
203+
pglet.app("python-grid", target=main, web=False)

0 commit comments

Comments
 (0)