nanopyx.core.utils.easy_gui
A module to help simplify the create of GUIs in Jupyter notebooks using ipywidgets.
1""" 2A module to help simplify the create of GUIs in Jupyter notebooks using ipywidgets. 3""" 4 5import os 6import yaml 7import platform 8import numpy as np 9from ipyfilechooser import FileChooser 10from skimage.exposure import rescale_intensity 11import warnings 12 13# import cache if python >= 3.9, otherwise import lru_cache 14if platform.python_version_tuple() >= ("3", "9"): 15 from functools import cache 16else: 17 from functools import lru_cache as cache 18 19warnings.warn( 20 "The EasyGui class is deprecated.\nConsider using ezinput (the updated version of EasyGui) instead.\nYou can install ezinput with 'pip install ezinput'.\nThe API is similar to EasyGui, but with additional features and improvements.\nIt also allows to run the same widget code in both Jupyter Notebooks and on the terminal.\nFor more information: https://github.com/henriqueslab/ezinput", 21 DeprecationWarning, 22 stacklevel=2, 23) 24 25try: 26 import ipywidgets as widgets 27 from IPython import display as dp 28 from IPython.display import display, clear_output 29 from matplotlib import pyplot as plt 30except ImportError: 31 print( 32 "jupyter optional-dependencies not installed, conside installing with 'pip install nanopyx[jupyter]'" 33 ) 34 raise ImportError 35 36 37class EasyGui: 38 """ 39 A class to simplify the creation of GUIs in Jupyter notebooks using ipywidgets. 40 41 Args: 42 title (str): The title of the GUI. 43 width (str): The width of the widget container (e.g., "50%"). 44 45 Attributes: 46 _layout (ipywidgets.Layout): The layout for widgets. 47 _style (dict): Style configuration for widgets. 48 _widgets (dict): A dictionary to store widgets. 49 _nLabels (int): The number of labels added to the GUI. 50 _main_display (ipywidgets.VBox): The main output widget for the GUI. 51 _title (str): The title of the GUI. 52 _cfg (dict): Configuration dictionary to store widget values. 53 cfg (dict): Alias for the _cfg dictionary associated with the GUI. 54 55 Methods: 56 add_label: Add a label widget to the GUI. 57 add_button: Add a button widget to the GUI. 58 add_text: Add a text widget to the GUI. 59 add_int_slider: Add an integer slider widget to the GUI. 60 add_float_slider: Add a float slider widget to the GUI. 61 add_checkbox: Add a checkbox widget to the GUI. 62 add_int_text: Add an integer text widget to the GUI. 63 add_float_text: Add a float text widget to the GUI. 64 add_dropdown: Add a dropdown widget to the GUI. 65 add_file_upload: Add a file upload widget to the GUI. 66 save_settings: Save widget values to a configuration file. 67 show: Display the GUI with its widgets. 68 clear: Clear all widgets from the GUI. 69 70 Note: 71 This class simplifies the creation of GUIs in Jupyter notebooks using ipywidgets. It provides a variety of methods for adding different types of widgets to the GUI, and it allows for saving and loading widget values to maintain user settings across sessions. 72 """ 73 74 def __init__(self, title="basic_gui", width="50%"): 75 """ 76 Container for widgets. 77 :param width: width of the widget container 78 """ 79 self._layout = widgets.Layout(width=width) 80 self._style = {"description_width": "initial"} 81 self._widgets = {} 82 self._nLabels = 0 83 self._main_display = widgets.VBox() 84 self._title = title 85 self._cfg = {title: {}} 86 self.cfg = self._cfg[title] 87 88 # Get the user's home folder 89 self._home_folder = os.path.expanduser("~") 90 self._config_folder = os.path.join(self._home_folder, ".nanopyx") 91 if not os.path.exists(self._config_folder): 92 os.makedirs(self._config_folder) 93 94 self._config_file = os.path.join(self._config_folder, "easy_gui.yml") 95 if os.path.exists(self._config_file): 96 with open(self._config_file, "r") as f: 97 self._cfg = yaml.load(f, Loader=yaml.FullLoader) 98 if title in self._cfg: 99 self.cfg = self._cfg[title] 100 101 def __getitem__(self, tag: str) -> widgets.Widget: 102 return self._widgets[tag] 103 104 def __len__(self) -> int: 105 return len(self._widgets) 106 107 def add_label(self, *args, **kwargs): 108 """ 109 Add a label widget to the container. 110 :param args: args for the widget 111 :param kwargs: kwargs for the widget 112 """ 113 self._nLabels += 1 114 self._widgets[f"label_{self._nLabels}"] = widgets.Label( 115 *args, **kwargs, layout=self._layout, style=self._style 116 ) 117 118 def add_button(self, tag, *args, **kwargs): 119 """ 120 Add a button widget to the container. 121 :param tag: tag to identify the widget 122 :param args: args for the widget 123 :param kwargs: kwargs for the widget 124 """ 125 self._widgets[tag] = widgets.Button( 126 *args, **kwargs, layout=self._layout, style=self._style 127 ) 128 129 def add_text(self, tag, *args, **kwargs): 130 """ 131 Add a text widget to the container. 132 :param tag: tag to identify the widget 133 :param args: args for the widget 134 :param kwargs: kwargs for the widget 135 """ 136 self._widgets[tag] = widgets.Text( 137 *args, **kwargs, layout=self._layout, style=self._style 138 ) 139 140 def add_int_slider(self, tag, *args, remember_value=False, **kwargs): 141 """ 142 Add a integer slider widget to the container. 143 :param tag: tag to identify the widget 144 :param args: args for the widget 145 :param remember_value: remember the last value 146 :param kwargs: kwargs for the widget 147 """ 148 if ( 149 remember_value 150 and tag in self.cfg 151 and kwargs["min"] <= self.cfg[tag] <= kwargs["max"] 152 ): 153 kwargs["value"] = self.cfg[tag] 154 self._widgets[tag] = widgets.IntSlider( 155 *args, **kwargs, layout=self._layout, style=self._style 156 ) 157 158 def add_float_slider(self, tag, *args, remember_value=False, **kwargs): 159 """ 160 Add a float slider widget to the container. 161 :param tag: tag to identify the widget 162 :param args: args for the widget 163 :param remember_value: remember the last value 164 :param kwargs: kwargs for the widget 165 """ 166 if remember_value and tag in self.cfg: 167 kwargs["value"] = self.cfg[tag] 168 self._widgets[tag] = widgets.FloatSlider( 169 *args, **kwargs, layout=self._layout, style=self._style 170 ) 171 172 def add_checkbox(self, tag, *args, remember_value=False, **kwargs): 173 """ 174 Add a checkbox widget to the container. 175 :param tag: tag to identify the widget 176 :param args: args for the widget 177 :param remember_value: remember the last value 178 :param kwargs: kwargs for the widget 179 """ 180 if remember_value and tag in self.cfg: 181 kwargs["value"] = self.cfg[tag] 182 self._widgets[tag] = widgets.Checkbox( 183 *args, **kwargs, layout=self._layout, style=self._style 184 ) 185 186 def add_int_text(self, tag, *args, remember_value=False, **kwargs): 187 """ 188 Add a integer text widget to the container. 189 :param tag: tag to identify the widget 190 :param args: args for the widget 191 :param remember_value: remember the last value 192 :param kwargs: kwargs for the widget 193 """ 194 if remember_value and tag in self.cfg: 195 kwargs["value"] = self.cfg[tag] 196 197 self._widgets[tag] = widgets.IntText( 198 *args, **kwargs, layout=self._layout, style=self._style 199 ) 200 201 def add_float_text(self, tag, *args, remember_value=False, **kwargs): 202 """ 203 Add a float text widget to the container. 204 :param tag: tag to identify the widget 205 :param args: args for the widget 206 :param remember_value: remember the last value 207 :param kwargs: kwargs for the widget 208 """ 209 if remember_value and tag in self.cfg: 210 kwargs["value"] = self.cfg[tag] 211 self._widgets[tag] = widgets.FloatText( 212 *args, **kwargs, layout=self._layout, style=self._style 213 ) 214 215 def add_dropdown(self, tag, *args, remember_value=False, **kwargs): 216 """ 217 Add a dropdown widget to the container. 218 :param tag: tag to identify the widget 219 :param args: args for the widget 220 :param remember_value: remember the last value 221 :param kwargs: kwargs for the widget 222 """ 223 if ( 224 remember_value 225 and tag in self.cfg 226 and self.cfg[tag] in kwargs["options"] 227 ): 228 kwargs["value"] = self.cfg[tag] 229 self._widgets[tag] = widgets.Dropdown( 230 *args, **kwargs, layout=self._layout, style=self._style 231 ) 232 233 def add_file_upload( 234 self, tag, *args, accept=None, multiple=False, **kwargs 235 ): 236 """ 237 Add a file upload widget to the container. 238 :param tag: tag to identify the widget 239 :param args: args for the widget 240 :param accept: file types to accept 241 :param multiple: allow multiple files to be uploaded 242 :param kwargs: kwargs for the widget 243 """ 244 self._widgets[tag] = FileChooser() 245 if accept is not None: 246 self._widgets[tag].filter_pattern = accept 247 248 def save_settings(self): 249 # remember widget values for next time and store them in a config file 250 for tag in self._widgets: 251 if tag.startswith("label_"): 252 pass 253 elif hasattr(self._widgets[tag], "value"): 254 self.cfg[tag] = self._widgets[tag].value 255 self._cfg[self._title] = self.cfg 256 with open(self._config_file, "w") as f: 257 yaml.dump(self._cfg, f) 258 259 def show(self): 260 """ 261 Show the widgets in the container. 262 """ 263 # display the widgets 264 self._main_display.children = tuple(self._widgets.values()) 265 clear_output() 266 display(self._main_display) 267 268 def clear(self): 269 """ 270 Clear the widgets in the container. 271 """ 272 self._widgets = {} 273 self._nLabels = 0 274 self._main_display.children = ()
38class EasyGui: 39 """ 40 A class to simplify the creation of GUIs in Jupyter notebooks using ipywidgets. 41 42 Args: 43 title (str): The title of the GUI. 44 width (str): The width of the widget container (e.g., "50%"). 45 46 Attributes: 47 _layout (ipywidgets.Layout): The layout for widgets. 48 _style (dict): Style configuration for widgets. 49 _widgets (dict): A dictionary to store widgets. 50 _nLabels (int): The number of labels added to the GUI. 51 _main_display (ipywidgets.VBox): The main output widget for the GUI. 52 _title (str): The title of the GUI. 53 _cfg (dict): Configuration dictionary to store widget values. 54 cfg (dict): Alias for the _cfg dictionary associated with the GUI. 55 56 Methods: 57 add_label: Add a label widget to the GUI. 58 add_button: Add a button widget to the GUI. 59 add_text: Add a text widget to the GUI. 60 add_int_slider: Add an integer slider widget to the GUI. 61 add_float_slider: Add a float slider widget to the GUI. 62 add_checkbox: Add a checkbox widget to the GUI. 63 add_int_text: Add an integer text widget to the GUI. 64 add_float_text: Add a float text widget to the GUI. 65 add_dropdown: Add a dropdown widget to the GUI. 66 add_file_upload: Add a file upload widget to the GUI. 67 save_settings: Save widget values to a configuration file. 68 show: Display the GUI with its widgets. 69 clear: Clear all widgets from the GUI. 70 71 Note: 72 This class simplifies the creation of GUIs in Jupyter notebooks using ipywidgets. It provides a variety of methods for adding different types of widgets to the GUI, and it allows for saving and loading widget values to maintain user settings across sessions. 73 """ 74 75 def __init__(self, title="basic_gui", width="50%"): 76 """ 77 Container for widgets. 78 :param width: width of the widget container 79 """ 80 self._layout = widgets.Layout(width=width) 81 self._style = {"description_width": "initial"} 82 self._widgets = {} 83 self._nLabels = 0 84 self._main_display = widgets.VBox() 85 self._title = title 86 self._cfg = {title: {}} 87 self.cfg = self._cfg[title] 88 89 # Get the user's home folder 90 self._home_folder = os.path.expanduser("~") 91 self._config_folder = os.path.join(self._home_folder, ".nanopyx") 92 if not os.path.exists(self._config_folder): 93 os.makedirs(self._config_folder) 94 95 self._config_file = os.path.join(self._config_folder, "easy_gui.yml") 96 if os.path.exists(self._config_file): 97 with open(self._config_file, "r") as f: 98 self._cfg = yaml.load(f, Loader=yaml.FullLoader) 99 if title in self._cfg: 100 self.cfg = self._cfg[title] 101 102 def __getitem__(self, tag: str) -> widgets.Widget: 103 return self._widgets[tag] 104 105 def __len__(self) -> int: 106 return len(self._widgets) 107 108 def add_label(self, *args, **kwargs): 109 """ 110 Add a label widget to the container. 111 :param args: args for the widget 112 :param kwargs: kwargs for the widget 113 """ 114 self._nLabels += 1 115 self._widgets[f"label_{self._nLabels}"] = widgets.Label( 116 *args, **kwargs, layout=self._layout, style=self._style 117 ) 118 119 def add_button(self, tag, *args, **kwargs): 120 """ 121 Add a button widget to the container. 122 :param tag: tag to identify the widget 123 :param args: args for the widget 124 :param kwargs: kwargs for the widget 125 """ 126 self._widgets[tag] = widgets.Button( 127 *args, **kwargs, layout=self._layout, style=self._style 128 ) 129 130 def add_text(self, tag, *args, **kwargs): 131 """ 132 Add a text widget to the container. 133 :param tag: tag to identify the widget 134 :param args: args for the widget 135 :param kwargs: kwargs for the widget 136 """ 137 self._widgets[tag] = widgets.Text( 138 *args, **kwargs, layout=self._layout, style=self._style 139 ) 140 141 def add_int_slider(self, tag, *args, remember_value=False, **kwargs): 142 """ 143 Add a integer slider widget to the container. 144 :param tag: tag to identify the widget 145 :param args: args for the widget 146 :param remember_value: remember the last value 147 :param kwargs: kwargs for the widget 148 """ 149 if ( 150 remember_value 151 and tag in self.cfg 152 and kwargs["min"] <= self.cfg[tag] <= kwargs["max"] 153 ): 154 kwargs["value"] = self.cfg[tag] 155 self._widgets[tag] = widgets.IntSlider( 156 *args, **kwargs, layout=self._layout, style=self._style 157 ) 158 159 def add_float_slider(self, tag, *args, remember_value=False, **kwargs): 160 """ 161 Add a float slider widget to the container. 162 :param tag: tag to identify the widget 163 :param args: args for the widget 164 :param remember_value: remember the last value 165 :param kwargs: kwargs for the widget 166 """ 167 if remember_value and tag in self.cfg: 168 kwargs["value"] = self.cfg[tag] 169 self._widgets[tag] = widgets.FloatSlider( 170 *args, **kwargs, layout=self._layout, style=self._style 171 ) 172 173 def add_checkbox(self, tag, *args, remember_value=False, **kwargs): 174 """ 175 Add a checkbox widget to the container. 176 :param tag: tag to identify the widget 177 :param args: args for the widget 178 :param remember_value: remember the last value 179 :param kwargs: kwargs for the widget 180 """ 181 if remember_value and tag in self.cfg: 182 kwargs["value"] = self.cfg[tag] 183 self._widgets[tag] = widgets.Checkbox( 184 *args, **kwargs, layout=self._layout, style=self._style 185 ) 186 187 def add_int_text(self, tag, *args, remember_value=False, **kwargs): 188 """ 189 Add a integer text widget to the container. 190 :param tag: tag to identify the widget 191 :param args: args for the widget 192 :param remember_value: remember the last value 193 :param kwargs: kwargs for the widget 194 """ 195 if remember_value and tag in self.cfg: 196 kwargs["value"] = self.cfg[tag] 197 198 self._widgets[tag] = widgets.IntText( 199 *args, **kwargs, layout=self._layout, style=self._style 200 ) 201 202 def add_float_text(self, tag, *args, remember_value=False, **kwargs): 203 """ 204 Add a float text widget to the container. 205 :param tag: tag to identify the widget 206 :param args: args for the widget 207 :param remember_value: remember the last value 208 :param kwargs: kwargs for the widget 209 """ 210 if remember_value and tag in self.cfg: 211 kwargs["value"] = self.cfg[tag] 212 self._widgets[tag] = widgets.FloatText( 213 *args, **kwargs, layout=self._layout, style=self._style 214 ) 215 216 def add_dropdown(self, tag, *args, remember_value=False, **kwargs): 217 """ 218 Add a dropdown widget to the container. 219 :param tag: tag to identify the widget 220 :param args: args for the widget 221 :param remember_value: remember the last value 222 :param kwargs: kwargs for the widget 223 """ 224 if ( 225 remember_value 226 and tag in self.cfg 227 and self.cfg[tag] in kwargs["options"] 228 ): 229 kwargs["value"] = self.cfg[tag] 230 self._widgets[tag] = widgets.Dropdown( 231 *args, **kwargs, layout=self._layout, style=self._style 232 ) 233 234 def add_file_upload( 235 self, tag, *args, accept=None, multiple=False, **kwargs 236 ): 237 """ 238 Add a file upload widget to the container. 239 :param tag: tag to identify the widget 240 :param args: args for the widget 241 :param accept: file types to accept 242 :param multiple: allow multiple files to be uploaded 243 :param kwargs: kwargs for the widget 244 """ 245 self._widgets[tag] = FileChooser() 246 if accept is not None: 247 self._widgets[tag].filter_pattern = accept 248 249 def save_settings(self): 250 # remember widget values for next time and store them in a config file 251 for tag in self._widgets: 252 if tag.startswith("label_"): 253 pass 254 elif hasattr(self._widgets[tag], "value"): 255 self.cfg[tag] = self._widgets[tag].value 256 self._cfg[self._title] = self.cfg 257 with open(self._config_file, "w") as f: 258 yaml.dump(self._cfg, f) 259 260 def show(self): 261 """ 262 Show the widgets in the container. 263 """ 264 # display the widgets 265 self._main_display.children = tuple(self._widgets.values()) 266 clear_output() 267 display(self._main_display) 268 269 def clear(self): 270 """ 271 Clear the widgets in the container. 272 """ 273 self._widgets = {} 274 self._nLabels = 0 275 self._main_display.children = ()
A class to simplify the creation of GUIs in Jupyter notebooks using ipywidgets.
Args: title (str): The title of the GUI. width (str): The width of the widget container (e.g., "50%").
Attributes: _layout (ipywidgets.Layout): The layout for widgets. _style (dict): Style configuration for widgets. _widgets (dict): A dictionary to store widgets. _nLabels (int): The number of labels added to the GUI. _main_display (ipywidgets.VBox): The main output widget for the GUI. _title (str): The title of the GUI. _cfg (dict): Configuration dictionary to store widget values. cfg (dict): Alias for the _cfg dictionary associated with the GUI.
Methods: add_label: Add a label widget to the GUI. add_button: Add a button widget to the GUI. add_text: Add a text widget to the GUI. add_int_slider: Add an integer slider widget to the GUI. add_float_slider: Add a float slider widget to the GUI. add_checkbox: Add a checkbox widget to the GUI. add_int_text: Add an integer text widget to the GUI. add_float_text: Add a float text widget to the GUI. add_dropdown: Add a dropdown widget to the GUI. add_file_upload: Add a file upload widget to the GUI. save_settings: Save widget values to a configuration file. show: Display the GUI with its widgets. clear: Clear all widgets from the GUI.
Note: This class simplifies the creation of GUIs in Jupyter notebooks using ipywidgets. It provides a variety of methods for adding different types of widgets to the GUI, and it allows for saving and loading widget values to maintain user settings across sessions.
75 def __init__(self, title="basic_gui", width="50%"): 76 """ 77 Container for widgets. 78 :param width: width of the widget container 79 """ 80 self._layout = widgets.Layout(width=width) 81 self._style = {"description_width": "initial"} 82 self._widgets = {} 83 self._nLabels = 0 84 self._main_display = widgets.VBox() 85 self._title = title 86 self._cfg = {title: {}} 87 self.cfg = self._cfg[title] 88 89 # Get the user's home folder 90 self._home_folder = os.path.expanduser("~") 91 self._config_folder = os.path.join(self._home_folder, ".nanopyx") 92 if not os.path.exists(self._config_folder): 93 os.makedirs(self._config_folder) 94 95 self._config_file = os.path.join(self._config_folder, "easy_gui.yml") 96 if os.path.exists(self._config_file): 97 with open(self._config_file, "r") as f: 98 self._cfg = yaml.load(f, Loader=yaml.FullLoader) 99 if title in self._cfg: 100 self.cfg = self._cfg[title]
Container for widgets.
Parameters
- width: width of the widget container
108 def add_label(self, *args, **kwargs): 109 """ 110 Add a label widget to the container. 111 :param args: args for the widget 112 :param kwargs: kwargs for the widget 113 """ 114 self._nLabels += 1 115 self._widgets[f"label_{self._nLabels}"] = widgets.Label( 116 *args, **kwargs, layout=self._layout, style=self._style 117 )
Add a label widget to the container.
Parameters
- args: args for the widget
- kwargs: kwargs for the widget
130 def add_text(self, tag, *args, **kwargs): 131 """ 132 Add a text widget to the container. 133 :param tag: tag to identify the widget 134 :param args: args for the widget 135 :param kwargs: kwargs for the widget 136 """ 137 self._widgets[tag] = widgets.Text( 138 *args, **kwargs, layout=self._layout, style=self._style 139 )
Add a text widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- kwargs: kwargs for the widget
141 def add_int_slider(self, tag, *args, remember_value=False, **kwargs): 142 """ 143 Add a integer slider widget to the container. 144 :param tag: tag to identify the widget 145 :param args: args for the widget 146 :param remember_value: remember the last value 147 :param kwargs: kwargs for the widget 148 """ 149 if ( 150 remember_value 151 and tag in self.cfg 152 and kwargs["min"] <= self.cfg[tag] <= kwargs["max"] 153 ): 154 kwargs["value"] = self.cfg[tag] 155 self._widgets[tag] = widgets.IntSlider( 156 *args, **kwargs, layout=self._layout, style=self._style 157 )
Add a integer slider widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- remember_value: remember the last value
- kwargs: kwargs for the widget
159 def add_float_slider(self, tag, *args, remember_value=False, **kwargs): 160 """ 161 Add a float slider widget to the container. 162 :param tag: tag to identify the widget 163 :param args: args for the widget 164 :param remember_value: remember the last value 165 :param kwargs: kwargs for the widget 166 """ 167 if remember_value and tag in self.cfg: 168 kwargs["value"] = self.cfg[tag] 169 self._widgets[tag] = widgets.FloatSlider( 170 *args, **kwargs, layout=self._layout, style=self._style 171 )
Add a float slider widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- remember_value: remember the last value
- kwargs: kwargs for the widget
173 def add_checkbox(self, tag, *args, remember_value=False, **kwargs): 174 """ 175 Add a checkbox widget to the container. 176 :param tag: tag to identify the widget 177 :param args: args for the widget 178 :param remember_value: remember the last value 179 :param kwargs: kwargs for the widget 180 """ 181 if remember_value and tag in self.cfg: 182 kwargs["value"] = self.cfg[tag] 183 self._widgets[tag] = widgets.Checkbox( 184 *args, **kwargs, layout=self._layout, style=self._style 185 )
Add a checkbox widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- remember_value: remember the last value
- kwargs: kwargs for the widget
187 def add_int_text(self, tag, *args, remember_value=False, **kwargs): 188 """ 189 Add a integer text widget to the container. 190 :param tag: tag to identify the widget 191 :param args: args for the widget 192 :param remember_value: remember the last value 193 :param kwargs: kwargs for the widget 194 """ 195 if remember_value and tag in self.cfg: 196 kwargs["value"] = self.cfg[tag] 197 198 self._widgets[tag] = widgets.IntText( 199 *args, **kwargs, layout=self._layout, style=self._style 200 )
Add a integer text widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- remember_value: remember the last value
- kwargs: kwargs for the widget
202 def add_float_text(self, tag, *args, remember_value=False, **kwargs): 203 """ 204 Add a float text widget to the container. 205 :param tag: tag to identify the widget 206 :param args: args for the widget 207 :param remember_value: remember the last value 208 :param kwargs: kwargs for the widget 209 """ 210 if remember_value and tag in self.cfg: 211 kwargs["value"] = self.cfg[tag] 212 self._widgets[tag] = widgets.FloatText( 213 *args, **kwargs, layout=self._layout, style=self._style 214 )
Add a float text widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- remember_value: remember the last value
- kwargs: kwargs for the widget
216 def add_dropdown(self, tag, *args, remember_value=False, **kwargs): 217 """ 218 Add a dropdown widget to the container. 219 :param tag: tag to identify the widget 220 :param args: args for the widget 221 :param remember_value: remember the last value 222 :param kwargs: kwargs for the widget 223 """ 224 if ( 225 remember_value 226 and tag in self.cfg 227 and self.cfg[tag] in kwargs["options"] 228 ): 229 kwargs["value"] = self.cfg[tag] 230 self._widgets[tag] = widgets.Dropdown( 231 *args, **kwargs, layout=self._layout, style=self._style 232 )
Add a dropdown widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- remember_value: remember the last value
- kwargs: kwargs for the widget
234 def add_file_upload( 235 self, tag, *args, accept=None, multiple=False, **kwargs 236 ): 237 """ 238 Add a file upload widget to the container. 239 :param tag: tag to identify the widget 240 :param args: args for the widget 241 :param accept: file types to accept 242 :param multiple: allow multiple files to be uploaded 243 :param kwargs: kwargs for the widget 244 """ 245 self._widgets[tag] = FileChooser() 246 if accept is not None: 247 self._widgets[tag].filter_pattern = accept
Add a file upload widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- accept: file types to accept
- multiple: allow multiple files to be uploaded
- kwargs: kwargs for the widget
249 def save_settings(self): 250 # remember widget values for next time and store them in a config file 251 for tag in self._widgets: 252 if tag.startswith("label_"): 253 pass 254 elif hasattr(self._widgets[tag], "value"): 255 self.cfg[tag] = self._widgets[tag].value 256 self._cfg[self._title] = self.cfg 257 with open(self._config_file, "w") as f: 258 yaml.dump(self._cfg, f)