ezinput.ezinput
1import os 2import sys 3import yaml 4import ipywidgets as widgets 5 6from pathlib import Path 7from typing import Optional 8 9from .ezinput_prompt import EZInputPrompt 10from .ezinput_jupyter import EZInputJupyter 11 12try: 13 from google.colab import output 14 15 output.enable_custom_widget_manager() 16except ImportError: 17 pass 18 19""" 20A module to help simplify the create of GUIs in Jupyter notebooks and CLIs. 21""" 22 23CONFIG_PATH = Path.home() / ".ezinput" 24 25if not os.path.exists(CONFIG_PATH): 26 os.makedirs(CONFIG_PATH) 27 28 29class EZInput: 30 def __init__( 31 self, 32 title: str = "base", 33 width: str = "50%", 34 mode=None, 35 params_file: Optional[str] = None, 36 ): 37 """ 38 Initializes an instance of the EZInput class. 39 Args: 40 title (str): The title of the input interface. Defaults to "base". 41 width (str): The width of the input interface layout. Defaults to "50%". 42 """ 43 44 self.title = title 45 self.mode = None 46 self._nLabels = 0 47 self.cfg = self._get_config(title) 48 if params_file is not None: 49 self.params = self._load_params(params_file) 50 print(self.params) 51 else: 52 self.params = None 53 self.elements = {} 54 55 if mode is None: 56 self._detect_env(width) 57 58 def _load_params(self, params_file: Optional[str] = None): 59 """ 60 Loads parameters from a YAML file. 61 Args: 62 params_file (str): The path to the YAML file containing parameters. 63 """ 64 if os.path.exists(params_file): 65 with open(params_file, "r") as stream: 66 return yaml.safe_load(stream) 67 else: 68 return None 69 70 def _get_config(self, title: Optional[str]) -> dict: 71 """ 72 Get the configuration dictionary without needing to initialize the GUI. 73 74 Parameters 75 ---------- 76 title : str, optional 77 The title of the GUI. If None, returns the entire configuration. 78 79 Returns 80 ------- 81 dict 82 The configuration dictionary. 83 """ 84 85 config_file = CONFIG_PATH / f"{title}.yml" 86 87 if not config_file.exists(): 88 return {} 89 90 with open(config_file, "r") as f: 91 return yaml.load(f, Loader=yaml.SafeLoader) 92 93 def save_settings(self): 94 """ 95 @unified 96 Save the widget values to the configuration file. 97 """ 98 for tag in self.elements: 99 if tag.startswith("label_"): 100 pass 101 elif hasattr(self.elements[tag], "value"): 102 self.cfg[tag] = self.elements[tag].value 103 self._save_config(self.title, self.cfg) 104 105 def _save_config(self, title: str, cfg: dict): 106 """ 107 @unified 108 Save the configuration dictionary to file. 109 110 Parameters 111 ---------- 112 title : str 113 The title of the GUI. 114 cfg : dict 115 The configuration dictionary. 116 """ 117 config_file = CONFIG_PATH / f"{title}.yml" 118 config_file.parent.mkdir(exist_ok=True) 119 120 base_config = self._get_config(title) # loads the config file 121 for key, value in cfg.items(): 122 base_config[key] = value 123 124 with open(config_file, "w") as f: 125 yaml.dump(base_config, f) 126 127 def _detect_env(self, width): 128 try: 129 get_ipython = sys.modules["IPython"].get_ipython 130 if "IPKernelApp" in get_ipython().config: 131 self._layout = widgets.Layout(width=width) 132 self._style = {"description_width": "initial"} 133 self._main_display = widgets.VBox() 134 self.mode = "jupyter" 135 self.__class__ = EZInputJupyter 136 else: 137 self.mode = "prompt" 138 self.__class__ = EZInputPrompt 139 except Exception: 140 self.mode = "prompt" 141 self.__class__ = EZInputPrompt
CONFIG_PATH =
PosixPath('/home/runner/.ezinput')
class
EZInput:
30class EZInput: 31 def __init__( 32 self, 33 title: str = "base", 34 width: str = "50%", 35 mode=None, 36 params_file: Optional[str] = None, 37 ): 38 """ 39 Initializes an instance of the EZInput class. 40 Args: 41 title (str): The title of the input interface. Defaults to "base". 42 width (str): The width of the input interface layout. Defaults to "50%". 43 """ 44 45 self.title = title 46 self.mode = None 47 self._nLabels = 0 48 self.cfg = self._get_config(title) 49 if params_file is not None: 50 self.params = self._load_params(params_file) 51 print(self.params) 52 else: 53 self.params = None 54 self.elements = {} 55 56 if mode is None: 57 self._detect_env(width) 58 59 def _load_params(self, params_file: Optional[str] = None): 60 """ 61 Loads parameters from a YAML file. 62 Args: 63 params_file (str): The path to the YAML file containing parameters. 64 """ 65 if os.path.exists(params_file): 66 with open(params_file, "r") as stream: 67 return yaml.safe_load(stream) 68 else: 69 return None 70 71 def _get_config(self, title: Optional[str]) -> dict: 72 """ 73 Get the configuration dictionary without needing to initialize the GUI. 74 75 Parameters 76 ---------- 77 title : str, optional 78 The title of the GUI. If None, returns the entire configuration. 79 80 Returns 81 ------- 82 dict 83 The configuration dictionary. 84 """ 85 86 config_file = CONFIG_PATH / f"{title}.yml" 87 88 if not config_file.exists(): 89 return {} 90 91 with open(config_file, "r") as f: 92 return yaml.load(f, Loader=yaml.SafeLoader) 93 94 def save_settings(self): 95 """ 96 @unified 97 Save the widget values to the configuration file. 98 """ 99 for tag in self.elements: 100 if tag.startswith("label_"): 101 pass 102 elif hasattr(self.elements[tag], "value"): 103 self.cfg[tag] = self.elements[tag].value 104 self._save_config(self.title, self.cfg) 105 106 def _save_config(self, title: str, cfg: dict): 107 """ 108 @unified 109 Save the configuration dictionary to file. 110 111 Parameters 112 ---------- 113 title : str 114 The title of the GUI. 115 cfg : dict 116 The configuration dictionary. 117 """ 118 config_file = CONFIG_PATH / f"{title}.yml" 119 config_file.parent.mkdir(exist_ok=True) 120 121 base_config = self._get_config(title) # loads the config file 122 for key, value in cfg.items(): 123 base_config[key] = value 124 125 with open(config_file, "w") as f: 126 yaml.dump(base_config, f) 127 128 def _detect_env(self, width): 129 try: 130 get_ipython = sys.modules["IPython"].get_ipython 131 if "IPKernelApp" in get_ipython().config: 132 self._layout = widgets.Layout(width=width) 133 self._style = {"description_width": "initial"} 134 self._main_display = widgets.VBox() 135 self.mode = "jupyter" 136 self.__class__ = EZInputJupyter 137 else: 138 self.mode = "prompt" 139 self.__class__ = EZInputPrompt 140 except Exception: 141 self.mode = "prompt" 142 self.__class__ = EZInputPrompt
EZInput( title: str = 'base', width: str = '50%', mode=None, params_file: Optional[str] = None)
31 def __init__( 32 self, 33 title: str = "base", 34 width: str = "50%", 35 mode=None, 36 params_file: Optional[str] = None, 37 ): 38 """ 39 Initializes an instance of the EZInput class. 40 Args: 41 title (str): The title of the input interface. Defaults to "base". 42 width (str): The width of the input interface layout. Defaults to "50%". 43 """ 44 45 self.title = title 46 self.mode = None 47 self._nLabels = 0 48 self.cfg = self._get_config(title) 49 if params_file is not None: 50 self.params = self._load_params(params_file) 51 print(self.params) 52 else: 53 self.params = None 54 self.elements = {} 55 56 if mode is None: 57 self._detect_env(width)
Initializes an instance of the EZInput class. Args: title (str): The title of the input interface. Defaults to "base". width (str): The width of the input interface layout. Defaults to "50%".
def
save_settings(self):
94 def save_settings(self): 95 """ 96 @unified 97 Save the widget values to the configuration file. 98 """ 99 for tag in self.elements: 100 if tag.startswith("label_"): 101 pass 102 elif hasattr(self.elements[tag], "value"): 103 self.cfg[tag] = self.elements[tag].value 104 self._save_config(self.title, self.cfg)
@unified Save the widget values to the configuration file.