2121 FloatingActionButton , IconButton , ProgressBar , ButtonStyle
2222)
2323
24+ # Increasing the maximum message size that can be sent over the websocket.
2425os .environ ["FLET_WS_MAX_MESSAGE_SIZE" ] = "6000000"
2526
2627class ColorBrowser (UserControl ):
2728 def __init__ (self , expand = False , height = 500 ):
29+ """
30+ If the expand parameter is set to True, then the expand attribute of the object is set to True. Otherwise, the
31+ height attribute of the object is set to the value of the height parameter.
32+
33+ :param expand: If True, the widget will expand to fill its parent, defaults to False (optional)
34+ :param height: The height of the widget, defaults to 500 (optional)
35+ """
2836 super ().__init__ ()
2937 if expand :
3038 self .expand = expand
@@ -33,21 +41,28 @@ def __init__(self, expand=False, height=500):
3341
3442 def build (self ):
3543 def batches (iterable , batch_size ):
44+ """
45+ It takes an iterable and a batch size, and returns an iterator that yields batches of the iterable
46+
47+ :param iterable: An iterable object (e.g. a list)
48+ :param batch_size: The number of items to process in each batch
49+ """
3650 iterator = iter (iterable )
3751 while batch := list (islice (iterator , batch_size )):
3852 yield batch
3953
40- # fetch all icon constants from icons .py module
41- colors_list = []
54+ # fetch all icon constants from colors .py module and store them in a dict(colors_dict)
55+ colors_dict = dict ()
4256 list_started = False
4357 for key , value in vars (colors ).items ():
4458 if key == "PRIMARY" :
4559 # 'PRIMARY' is the first color-variable (our starting point)
4660 list_started = True
4761 if list_started :
48- # when this bool variable is True, we can start appending the variables in our list
49- colors_list . append ( key . lower ())
62+ # when this list_started is True, we create new key-value pair in our dictionary
63+ colors_dict [ key ] = value
5064
65+ # Creating a text field
5166 search_txt = TextField (
5267 expand = 1 , hint_text = "Enter keyword and press search button" , autofocus = True ,
5368 on_submit = lambda e : display_colors (e .control .value ), tooltip = "search field"
@@ -59,50 +74,70 @@ def search_click(e):
5974 """
6075 display_colors (search_txt .value )
6176
77+ # Creating a row with a search text field and a search button.
6278 search_query = Row (
6379 [search_txt , FloatingActionButton (icon = icons .SEARCH , on_click = search_click , tooltip = "search" )]
6480 )
6581
82+ # Creating a grid view with 10 columns and 150 pixels as the maximum extent of each column.
6683 search_results = GridView (
6784 expand = 1 , runs_count = 10 , max_extent = 150 , spacing = 5 , run_spacing = 5 , child_aspect_ratio = 1 ,
6885 )
6986 status_bar = Text ()
7087
7188 def copy_to_clipboard (e ):
72- """copies the color code to the clipboard"""
89+ """
90+ When the user clicks on a color, copy the color key to the clipboard
91+
92+ :param e: The event object
93+ """
94+
7395 color_key = e .control .data
7496 print ("Copy to clipboard:" , color_key )
7597 self .page .set_clipboard (e .control .data )
7698 self .page .show_snack_bar (SnackBar (Text (f"Copied { color_key } " ), open = True ))
7799
78100 def search_colors (search_term : str ):
79- if search_term != "" :
80- # if the sea
81- for color_name in colors_list :
82- if search_term in color_name :
83- yield color_name
101+ """
102+ It takes a search term as an argument, and then loops through the colors_dict dictionary,
103+ checking if the search term is in the color name or the color value. If it is, it yields the color key
104+
105+ :param search_term: The search term that the user entered
106+ :return color_key: str
107+ """
108+
109+ for color_key , color_value in colors_dict .items ():
110+ # the color_key has underscores while the color_value doesn't. We take this into consideration
111+ if search_term and (search_term in color_value or search_term in color_key .lower ()):
112+ yield color_key
84113
85114 def display_colors (search_term : str ):
86- """Gets the search term and tries to display the colors in the grid view"""
115+ """
116+ It takes a search term, disables the search box, cleans the search results(grid view),
117+ and then loops through the search results in batches of 40, adding each color to the search results
118+
119+ :param search_term: str
120+ """
87121
88- # clean search results
122+ # disable the text field and the search button, and clean search results
89123 search_query .disabled = True
90124 self .update ()
91125 search_results .clean ()
92126
127+ # Adding the colors to the grid view in batches of 40.
93128 for batch in batches (search_colors (search_term .lower ()), 40 ):
94- for color_name in batch :
95- color_key = f"colors.{ color_name . upper () } "
129+ for color_key in batch :
130+ flet_color_key = f"colors.{ color_key } "
96131
97132 search_results .controls .append (
98133 TextButton (
99134 content = Container (
100135 content = Column (
101136 [
102- Icon (name = icons .RECTANGLE , size = 38 , color = color_name , ),
137+ Icon (name = icons .RECTANGLE , size = 38 , color = colors_dict [ color_key ] , ),
103138 Text (
104- value = f"{ color_name } " , size = 14 , width = 100 ,
105- no_wrap = True , text_align = "center" , color = color_name ,
139+ value = f"{ colors_dict [ color_key ] } " , size = 14 , width = 100 ,
140+ no_wrap = True , text_align = "center" , color = colors_dict [ color_key ] ,
106141 ),
107142 ],
108143 spacing = 5 ,
@@ -111,14 +146,15 @@ def display_colors(search_term: str):
111146 ),
112147 alignment = alignment .center ,
113148 ),
114- tooltip = f"{ color_key } \n Click to copy to a clipboard" ,
149+ tooltip = f"{ flet_color_key } \n Click to copy to a clipboard" ,
115150 on_click = copy_to_clipboard ,
116- data = color_key ,
151+ data = flet_color_key ,
117152 )
118153 )
119154 status_bar .value = f"Colors found: { len (search_results .controls )} "
120155 self .update ()
121156
157+ # It checks if the search results are empty, and if they are, it shows a snack bar some message
122158 if len (search_results .controls ) == 0 :
123159 # if no color was found containing the user's search term
124160 self .page .show_snack_bar (SnackBar (Text ("No colors found" ), open = True ))
@@ -142,9 +178,10 @@ def main(page: Page):
142178
143179 def change_theme (e ):
144180 """
145- Changes the theme_mode and displays a progressbar while doing this.
146- :param e: the event toggler
147- :type e: ControlEvent
181+ When the button(to change theme) is clicked, the progress bar is made visible, the theme is changed,
182+ the progress bar is made invisible, and the page is updated
183+
184+ :param e: The event that triggered the function
148185 """
149186 p_bar .visible = True
150187 page .update ()
@@ -156,7 +193,7 @@ def change_theme(e):
156193
157194 # a progress bar shown when changing theme
158195 p_bar = ProgressBar (bar_height = 3.5 , visible = False )
159- # theme changer button
196+ # button to change theme_mode (from dark to light mode, or the reverse)
160197 theme_button = IconButton (icons .LIGHT_MODE , on_click = change_theme , icon_size = 40 , selected_icon = icons .DARK_MODE ,
161198 tooltip = "change theme" ,
162199 style = ButtonStyle (color = {"selected" : colors .BLACK , "" : colors .WHITE }))
@@ -169,5 +206,6 @@ def change_theme(e):
169206 )
170207
171208
209+ # Creating a web server and a web socket server (running the app)
172210flet .app (target = main )
173211# OR flet.app(target=main, view=flet.WEB_BROWSER, port=5050) then open http://localhost:5050/
0 commit comments