Skip to content

Commit

Permalink
update ipywidgets
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Oct 10, 2023
1 parent b3a29ec commit d48b10d
Showing 1 changed file with 11 additions and 48 deletions.
59 changes: 11 additions & 48 deletions src/magicgui/backends/_ipynb/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,64 +264,27 @@ def _mgui_get_text(self) -> str:
class _IPySupportsIcon(protocols.SupportsIcon):
"""Widget that can show an icon."""

_ipywidget: ipywdg.Widget
_ipywidget: ipywdg.Button

def _mgui_set_icon(self, value: str, color: str) -> None:
"""Set icon."""
# not all ipywidget buttons support icons (like checkboxes),
# but our button protocol does.
# only ipywdg.Button actually supports icons.
# but our button protocol allows it for all buttons subclasses
# so we need this method in the concrete subclasses, but we
# can't actually set the icon for anything but ipywdg.Button
if hasattr(self._ipywidget, "icon"):

Check warning on line 275 in src/magicgui/backends/_ipynb/widgets.py

View check run for this annotation

Codecov / codecov/patch

src/magicgui/backends/_ipynb/widgets.py#L275

Added line #L275 was not covered by tests
self._ipywidget.icon = value.replace("fa-", "")
# by splitting on ":" we allow for "prefix:icon-name" syntax
# which works for iconify icons served by qt, while still
# allowing for bare "icon-name" syntax which works for ipywidgets.
# note however... only fa4/5 icons will work for ipywidgets.
self._ipywidget.icon = value.replace("fa-", "").split(":", 1)[-1]
self._ipywidget.style.text_color = color

Check warning on line 281 in src/magicgui/backends/_ipynb/widgets.py

View check run for this annotation

Codecov / codecov/patch

src/magicgui/backends/_ipynb/widgets.py#L280-L281

Added lines #L280 - L281 were not covered by tests


class _IPyCategoricalWidget(_IPyValueWidget, _IPySupportsChoices):
pass


class IconButton(ipywdg.HTML):
TEMPLATE = """<style>{style}</style>
<button class="lm-Widget jupyter-widgets jupyter-button widget-button">{body}
</button>
"""

def __init__(self, description: str = "", icon: str = "", **kwargs):
from pyconify import css

selector = f"{icon.replace(' ', '--').replace(':', '--')}"
styles = css(icon, selector=f".{selector}")
styles = styles.replace("}", "margin: 0px 3px -2px}")
value = self.TEMPLATE.format(
body=f'<span class="{selector}"></span>{description}', style=styles
)
super().__init__(value=value, **kwargs)
self._click_handlers = ipywdg.CallbackDispatcher()
self.on_msg(self._handle_button_msg)

def on_click(self, callback, remove=False):
"""Register a callback to execute when the button is clicked.
The callback will be called with one argument, the clicked button
widget instance.
Parameters
----------
remove: bool (optional)
Set to true to remove the callback from the list of callbacks.
"""
self._click_handlers.register_callback(callback, remove=remove)

def _handle_button_msg(self, _, content, buffers):
"""Handle a msg from the front-end.
Parameters
----------
content: dict
Content of the msg.
"""
if content.get("event", "") == "click":
self._click_handlers(self)


class _IPyButtonWidget(_IPyValueWidget, _IPySupportsText, _IPySupportsIcon):
pass

Expand Down

0 comments on commit d48b10d

Please sign in to comment.