Skip to content

Commit ef697ef

Browse files
committed
Icons-browser v2 with page-level locks
1 parent b68a8af commit ef697ef

File tree

2 files changed

+53
-44
lines changed

2 files changed

+53
-44
lines changed

sdk/python/flet/page.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(self, conn: Connection, session_id):
4848
self._last_event = None
4949
self._event_available = threading.Event()
5050
self._fetch_page_details()
51+
self.lock = threading.Lock()
5152

5253
self.__offstage = Offstage()
5354
self.__theme = None

sdk/python/playground/icons-browser.py

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import logging
12
import os
3+
from itertools import islice
24

35
import flet
46
from flet import (
@@ -18,7 +20,7 @@
1820
icons,
1921
)
2022

21-
# logging.basicConfig(level=logging.DEBUG)
23+
# logging.basicConfig(level=logging.INFO)
2224

2325
# fetch all icon constants from icons.py module
2426
icons_list = []
@@ -32,6 +34,12 @@
3234
os.environ["FLET_WS_MAX_MESSAGE_SIZE"] = "8000000"
3335

3436

37+
def batches(iterable, batch_size):
38+
iterator = iter(iterable)
39+
while batch := list(islice(iterator, batch_size)):
40+
yield batch
41+
42+
3543
def main(page: Page):
3644
page.title = "Flet icons browser"
3745
page.theme_mode = "light"
@@ -53,59 +61,59 @@ def main(page: Page):
5361
status_bar = Text()
5462

5563
def copy_to_clipboard(e):
56-
icon_key = e.control.data
57-
print("Copy to clipboard:", icon_key)
58-
page.clipboard = e.control.data
59-
page.snack_bar = SnackBar(Text(f"Copied {icon_key}"), open=True)
60-
page.update()
64+
with page.lock:
65+
icon_key = e.control.data
66+
print("Copy to clipboard:", icon_key)
67+
page.clipboard = e.control.data
68+
page.snack_bar = SnackBar(Text(f"Copied {icon_key}"), open=True)
69+
page.update()
70+
71+
def search_icons(search_term: str):
72+
for icon_name in icons_list:
73+
if search_term != "" and search_term in icon_name:
74+
yield icon_name
6175

6276
def display_icons(search_term: str):
6377

6478
# clean search results
6579
search_results.clean()
6680

67-
# add matching icons
68-
for i in range(0, len(icons_list)):
69-
if search_term != "" and search_term in icons_list[i]:
70-
icon_name = icons_list[i]
71-
icon_key = f"icons.{icon_name.upper()}"
72-
search_results.controls.append(
73-
TextButton(
74-
content=Container(
75-
content=Column(
76-
[
77-
Icon(name=icon_name, size=30),
78-
Text(
79-
value=icon_name,
80-
size=12,
81-
width=100,
82-
no_wrap=True,
83-
text_align="center",
84-
color=colors.ON_SURFACE_VARIANT,
85-
),
86-
],
87-
spacing=5,
88-
alignment="center",
89-
horizontal_alignment="center",
81+
for batch in batches(search_icons(search_term), 200):
82+
with page.lock:
83+
for icon_name in batch:
84+
icon_key = f"icons.{icon_name.upper()}"
85+
search_results.controls.append(
86+
TextButton(
87+
content=Container(
88+
content=Column(
89+
[
90+
Icon(name=icon_name, size=30),
91+
Text(
92+
value=f"{icon_name}",
93+
size=12,
94+
width=100,
95+
no_wrap=True,
96+
text_align="center",
97+
color=colors.ON_SURFACE_VARIANT,
98+
),
99+
],
100+
spacing=5,
101+
alignment="center",
102+
horizontal_alignment="center",
103+
),
104+
alignment=alignment.center,
90105
),
91-
alignment=alignment.center,
92-
),
93-
tooltip=f"{icon_key}\nClick to copy to a clipboard",
94-
on_click=copy_to_clipboard,
95-
data=icon_key,
106+
tooltip=f"{icon_key}\nClick to copy to a clipboard",
107+
on_click=copy_to_clipboard,
108+
data=icon_key,
109+
)
96110
)
97-
)
111+
status_bar.value = f"Icons found: {len(search_results.controls)}"
112+
page.update()
98113

99-
# update page on every 500 icons added
100-
if i > 0 and i % 500 == 0:
101-
status_bar.value = f"Icons found: {len(search_results.controls)}"
102-
page.update()
103-
status_bar.value = f"Icons found: {len(search_results.controls)}"
104114
if len(search_results.controls) == 0:
105-
search_results.controls.append(
106-
Text(f'No icons found with text "{search_term}".')
107-
)
108-
page.update()
115+
page.snack_bar = SnackBar(Text("No icons found"), open=True)
116+
page.update()
109117

110118
def search_click(e):
111119
display_icons(search_txt.value)

0 commit comments

Comments
 (0)