Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add icons on buttons #598

Merged
merged 10 commits into from
Oct 11, 2023
Merged

feat: add icons on buttons #598

merged 10 commits into from
Oct 11, 2023

Conversation

tlambert03
Copy link
Member

@tlambert03 tlambert03 commented Oct 10, 2023

this PR allows adding icons to buttons, it uses superqt's recently added iconify support (pyapp-kit/superqt#209) and ipywidget's built in support for fontawesome 5. So you can do something like this:

from magicgui import widgets

btn = widgets.PushButton(icon="bi:cloud")
btn.show(run=True)

Note that qt will have the full selection of all icons available in iconify, but ipywidgets only works when the icon string after the colon is valid in fontawesome 4/5. In some cases (if you don't use font-awesome prefix as shown in the example above) that will mean a different appearance in qt. If you want the icons to look similar in both backends, just restrict yourself to fontawesome-5

qt ipwidgets
Screen Shot 2023-10-10 at 1 18 29 PM Screen Shot 2023-10-10 at 1 19 53 PM

@codecov
Copy link

codecov bot commented Oct 10, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (2eaa144) 87.68% compared to head (755a28e) 87.80%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #598      +/-   ##
==========================================
+ Coverage   87.68%   87.80%   +0.11%     
==========================================
  Files          39       39              
  Lines        4573     4617      +44     
==========================================
+ Hits         4010     4054      +44     
  Misses        563      563              
Files Coverage Δ
src/magicgui/backends/_ipynb/widgets.py 65.57% <100.00%> (+1.01%) ⬆️
src/magicgui/backends/_qtpy/widgets.py 87.81% <100.00%> (+0.39%) ⬆️
src/magicgui/widgets/bases/_button_widget.py 100.00% <100.00%> (ø)
src/magicgui/widgets/protocols.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tlambert03 tlambert03 requested a review from Czaki October 10, 2023 17:23
@tlambert03 tlambert03 changed the title feat: support icons on buttons [wip] feat: support icons on buttons Oct 10, 2023
@tlambert03 tlambert03 changed the title feat: support icons on buttons feat: add icons on buttons Oct 10, 2023
Copy link
Contributor

@Czaki Czaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks nice.
Shor check of ipywidgets code suggests that it may be possible to easily contribute to enable arbitrary SVG on buttons.

@@ -1,3 +1,5 @@
# from __future__ import annotations # NO
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? (it may be nice to learn).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh just a silly reason, cause I use the annotations at runtime internally in this module. I can add a comment

Comment on lines +476 to +480
try:
return superqt.QIconifyIcon(key, color=color)
except (OSError, ValueError) as e:
warnings.warn(f"Could not set iconify icon: {e}", stacklevel=2)
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this will handle HTTP errors if someone try to use it the first time on a machine without internet access?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I believe it should (that was my intention anyway). Requests HTTP errors are subclasses of OSError

@tlambert03
Copy link
Member Author

it looks nice.

Shor check of ipywidgets code suggests that it may be possible to easily contribute to enable arbitrary SVG on buttons.

You mean like an upstream PR? That's a good idea

@tlambert03
Copy link
Member Author

I opened jupyter-widgets/ipywidgets#3850 for discussion, but probably won't wait here for it

@tlambert03 tlambert03 merged commit 2fa477d into pyapp-kit:main Oct 11, 2023
33 checks passed
@tlambert03 tlambert03 deleted the icons branch October 11, 2023 20:02
@tlambert03 tlambert03 added the enhancement New feature or request label Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants