|
| 1 | +from typing import List, Optional, Union |
| 2 | + |
| 3 | +from ipywidgets import Box, Widget |
| 4 | +from traitlets.utils.sentinel import Sentinel |
| 5 | + |
| 6 | + |
| 7 | +class CueBox(Box): |
| 8 | + """ |
| 9 | + A box around the widget :param widget_to_cue: that adds a visual cue defined in the |
| 10 | + :param css_style: when the trait :param traits_to_observe: in the widget :param |
| 11 | + widget_to_observe: changes. |
| 12 | +
|
| 13 | + :param widget_to_observe: |
| 14 | + The widget to observa if the :param traits_to_observe: has changed. |
| 15 | + :param traits_to_observe: |
| 16 | + The trait from the :param widget_to_observe: to observe if changed. |
| 17 | + Specify `traitlets.All` to observe all traits. |
| 18 | + :param widget_to_cue: |
| 19 | + The widget to wrap the box around to give a visual cue, once :param |
| 20 | + traits_to_observe: has changed |
| 21 | + If None, then the :param widget_to_cue: is set to :param widget_to_observe:. |
| 22 | + :param css_syle: |
| 23 | + - **initialization**: the css style of the box during initialization |
| 24 | + - **on_trait_changed**: the css style that is added when :param |
| 25 | + traits_to_observe: in widget :param widget_to_observe: changes. |
| 26 | + It is supposed to change the style of the box such that the user has a visual |
| 27 | + cue that :param widget_to_cue: has changed. |
| 28 | +
|
| 29 | + Further accepts the same (keyword) arguments as :py:class:`ipywidgets.Box`. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__( |
| 33 | + self, |
| 34 | + widget_to_observe: Widget, |
| 35 | + traits_to_observe: Union[str, List[str], Sentinel, None] = "value", |
| 36 | + widget_to_cue: Optional[Widget] = None, |
| 37 | + css_style: Optional[dict] = None, |
| 38 | + *args, |
| 39 | + **kwargs, |
| 40 | + ): |
| 41 | + self._widget_to_observe = widget_to_observe |
| 42 | + self._traits_to_observe = traits_to_observe |
| 43 | + |
| 44 | + if widget_to_cue is None: |
| 45 | + self._widget_to_cue = widget_to_observe |
| 46 | + else: |
| 47 | + self._widget_to_cue = widget_to_cue |
| 48 | + |
| 49 | + self._widget_to_observe = widget_to_observe |
| 50 | + |
| 51 | + if css_style is None: |
| 52 | + self._css_style = { |
| 53 | + "base": "scwidget-cue-box", |
| 54 | + "cue": "scwidget-cue-box--cue", |
| 55 | + } |
| 56 | + else: |
| 57 | + self._css_style = css_style |
| 58 | + |
| 59 | + super().__init__([self._widget_to_cue], *args, **kwargs) |
| 60 | + |
| 61 | + self._widget_to_observe.observe( |
| 62 | + self._on_traits_to_observe_changed, traits_to_observe |
| 63 | + ) |
| 64 | + self.add_class(self._css_style["base"]) |
| 65 | + |
| 66 | + @property |
| 67 | + def widget_to_observe(self): |
| 68 | + return self._widget_to_observe |
| 69 | + |
| 70 | + @property |
| 71 | + def traits_to_observe(self): |
| 72 | + return self._traits_to_observe |
| 73 | + |
| 74 | + @property |
| 75 | + def widget_to_cue(self): |
| 76 | + return self._widget_to_cue |
| 77 | + |
| 78 | + def _on_traits_to_observe_changed(self, change: dict): |
| 79 | + self.add_class(self._css_style["cue"]) |
| 80 | + |
| 81 | + def remove_cue(self): |
| 82 | + self.remove_class(self._css_style["cue"]) |
| 83 | + |
| 84 | + |
| 85 | +class SaveCueBox(CueBox): |
| 86 | + """ |
| 87 | + A box around the widget :param widget_to_cue: that adds a visual cue defined in the |
| 88 | + :param css_style: when the trait :param traits_to_observe: in the widget :param |
| 89 | + widget_to_observe: changes. |
| 90 | +
|
| 91 | + :param widget_to_observe: |
| 92 | + The widget to observa if the :param traits_to_observe: has changed. |
| 93 | + :param traits_to_observe: |
| 94 | + The trait from the :param widget_to_observe: to observe if changed. |
| 95 | + Specify `traitlets.All` to observe all traits. |
| 96 | + :param widget_to_cue: |
| 97 | + The widget to wrap the box around to give a visual cue, once :param |
| 98 | + traits_to_observe: has changed |
| 99 | + If None, then the :param widget_to_cue: is set to :param widget_to_observe:. |
| 100 | +
|
| 101 | + Further accepts the same (keyword) arguments as :py:class:`ipywidgets.Box`. |
| 102 | + """ |
| 103 | + |
| 104 | + def __init__( |
| 105 | + self, |
| 106 | + widget_to_observe: Widget, |
| 107 | + traits_to_observe: Union[str, List[str], Sentinel] = "value", |
| 108 | + widget_to_cue: Optional[Widget] = None, |
| 109 | + *args, |
| 110 | + **kwargs, |
| 111 | + ): |
| 112 | + css_style = { |
| 113 | + "base": "scwidget-save-cue-box", |
| 114 | + "cue": "scwidget-save-cue-box--cue", |
| 115 | + } |
| 116 | + super().__init__(widget_to_observe, traits_to_observe, widget_to_cue, css_style) |
| 117 | + |
| 118 | + |
| 119 | +class CheckCueBox(CueBox): |
| 120 | + """ |
| 121 | + A box around the widget :param widget_to_cue: that adds a visual cue defined in the |
| 122 | + :param css_style: when the trait :param traits_to_observe: in the widget :param |
| 123 | + widget_to_observe: changes. |
| 124 | +
|
| 125 | + :param widget_to_observe: |
| 126 | + The widget to observa if the :param traits_to_observe: has changed. |
| 127 | + :param traits_to_observe: |
| 128 | + The trait from the :param widget_to_observe: to observe if changed. |
| 129 | + Specify `traitlets.All` to observe all traits. |
| 130 | + :param widget_to_cue: |
| 131 | + The widget to wrap the box around to give a visual cue, once :param |
| 132 | + traits_to_observe: has changed |
| 133 | + If None, then the :param widget_to_cue: is set to :param widget_to_observe:. |
| 134 | +
|
| 135 | + Further accepts the same (keyword) arguments as :py:class:`ipywidgets.Box`. |
| 136 | + """ |
| 137 | + |
| 138 | + def __init__( |
| 139 | + self, |
| 140 | + widget_to_observe: Widget, |
| 141 | + traits_to_observe: Union[str, List[str], Sentinel] = "value", |
| 142 | + widget_to_cue: Optional[Widget] = None, |
| 143 | + *args, |
| 144 | + **kwargs, |
| 145 | + ): |
| 146 | + css_style = { |
| 147 | + "base": "scwidget-check-cue-box", |
| 148 | + "cue": "scwidget-check-cue-box--cue", |
| 149 | + } |
| 150 | + super().__init__(widget_to_observe, traits_to_observe, widget_to_cue, css_style) |
| 151 | + |
| 152 | + |
| 153 | +class UpdateCueBox(CueBox): |
| 154 | + """ |
| 155 | + A box around the widget :param widget_to_cue: that adds a visual cue defined in the |
| 156 | + :param css_style: when the trait :param traits_to_observe: in the widget :param |
| 157 | + widget_to_observe: changes. |
| 158 | +
|
| 159 | + :param widget_to_observe: |
| 160 | + The widget to observa if the :param traits_to_observe: has changed. |
| 161 | + :param traits_to_observe: |
| 162 | + The trait from the :param widget_to_observe: to observe if changed. |
| 163 | + Specify `traitlets.All` to observe all traits. |
| 164 | + :param widget_to_cue: |
| 165 | + The widget to wrap the box around to give a visual cue, once :param |
| 166 | + traits_to_observe: has changed |
| 167 | + If None, then the :param widget_to_cue: is set to :param widget_to_observe:. |
| 168 | +
|
| 169 | + Further accepts the same (keyword) arguments as :py:class:`ipywidgets.Box`. |
| 170 | + """ |
| 171 | + |
| 172 | + def __init__( |
| 173 | + self, |
| 174 | + widget_to_observe: Widget, |
| 175 | + traits_to_observe: Union[str, List[str], Sentinel] = "value", |
| 176 | + widget_to_cue: Optional[Widget] = None, |
| 177 | + *args, |
| 178 | + **kwargs, |
| 179 | + ): |
| 180 | + css_style = { |
| 181 | + "base": "scwidget-update-cue-box", |
| 182 | + "cue": "scwidget-update-cue-box--cue", |
| 183 | + } |
| 184 | + super().__init__(widget_to_observe, traits_to_observe, widget_to_cue, css_style) |
0 commit comments