Skip to content

Commit

Permalink
feat: add password field (#515)
Browse files Browse the repository at this point in the history
* feat: add password field

* docs: add docs

* docs: fix protocols

* docs remove old chapter

* docs: add label
  • Loading branch information
tlambert03 committed Nov 25, 2022
1 parent 6a77140 commit 5edccca
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 8 deletions.
3 changes: 0 additions & 3 deletions docs/_toc.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
format: jb-book
root: index
parts:
- caption: How it Works
chapters:
- file: how_works/
- caption: Usage
chapters:
- file: usage/installation
Expand Down
4 changes: 2 additions & 2 deletions docs/api/protocols.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# magicgui.widgets._protocols
# magicgui.widgets.protocols

```{eval-rst}
.. automodule:: magicgui.widgets._protocols
.. automodule:: magicgui.widgets.protocols
.. autosummary::
:nosignatures:
Expand Down
1 change: 1 addition & 0 deletions docs/api/widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Value Widgets
Label
LineEdit
LiteralEvalLineEdit
Password
TextEdit
FileEdit
RangeEdit
Expand Down
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ jupytext==1.13.8
numpy
imageio
sphinx-autodoc-typehints==1.18.3
pint
3 changes: 3 additions & 0 deletions docs/usage/widget_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ following `ValueWidgets` track some `value`:
Label
LineEdit
LiteralEvalLineEdit
Password
TextEdit
FileEdit
RangeEdit
Expand Down Expand Up @@ -170,6 +171,7 @@ import datetime
wdg_list = [
widgets.Label(value="label value", label="Label:"),
widgets.LineEdit(value="line edit value", label="LineEdit:"),
widgets.Password(value="super-secret!", label="Password:"),
widgets.TextEdit(value="text edit value...", label="TextEdit:"),
widgets.FileEdit(value="/home", label="FileEdit:"),
widgets.RangeEdit(value=range(0, 10, 2), label="RangeEdit:"),
Expand All @@ -179,6 +181,7 @@ wdg_list = [
),
widgets.DateEdit(value=datetime.date(81, 2, 18), label="DateEdit:"),
widgets.TimeEdit(value=datetime.time(12, 20), label="TimeEdit:"),
widgets.QuantityEdit(value='12 seconds', label="Quantity:")
]
container = widgets.Container(widgets=wdg_list)
container.max_height = 300
Expand Down
14 changes: 14 additions & 0 deletions examples/login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from magicgui import magicgui


# note that "password" is a special keyword argument
# it will create a password field in the gui by default
# (unless you override "widget_type")
# whereas "password2" will be a normal text field
# (unless you override "widget_type")
@magicgui(password2={"widget_type": "Password"})
def login(username: str, password: str, password2: str):
...


login.show(run=True)
2 changes: 2 additions & 0 deletions src/magicgui/backends/_ipynb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Label,
LineEdit,
LiteralEvalLineEdit,
Password,
PushButton,
RadioButton,
Select,
Expand Down Expand Up @@ -38,6 +39,7 @@
"Label",
"LineEdit",
"LiteralEvalLineEdit",
"Password",
"PushButton",
"RadioButton",
"Select",
Expand Down
4 changes: 4 additions & 0 deletions src/magicgui/backends/_ipynb/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ class LineEdit(_IPyStringWidget):
_ipywidget: ipywdg.Text


class Password(_IPyStringWidget):
_ipywidget: ipywdg.Password


class LiteralEvalLineEdit(_IPyStringWidget):
_ipywidget: ipywdg.Text

Expand Down
2 changes: 2 additions & 0 deletions src/magicgui/backends/_qtpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
LineEdit,
LiteralEvalLineEdit,
MainWindow,
Password,
ProgressBar,
PushButton,
QuantityEdit,
Expand Down Expand Up @@ -49,6 +50,7 @@
"LineEdit",
"LiteralEvalLineEdit",
"MainWindow",
"Password",
"ProgressBar",
"PushButton",
"QuantityEdit",
Expand Down
6 changes: 6 additions & 0 deletions src/magicgui/backends/_qtpy/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ def __init__(self, **kwargs):
super().__init__(QtW.QLineEdit, "text", "setText", "textChanged", **kwargs)


class Password(LineEdit):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._qwidget.setEchoMode(QtW.QLineEdit.Password)


class LiteralEvalLineEdit(QBaseStringWidget):
_qwidget: QtW.QLineEdit

Expand Down
2 changes: 1 addition & 1 deletion src/magicgui/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from magicgui.widgets.protocols import WidgetProtocol

#: A :class:`~magicgui.widgets._bases.Widget` class or a
#: :class:`~magicgui.widgets._protocols.WidgetProtocol`
#: :class:`~magicgui.widgets.protocols.WidgetProtocol`
WidgetClass = Union[Type["Widget"], Type["WidgetProtocol"]]
#: A generic reference to a :attr:`WidgetClass` as a string, or the class itself.
WidgetRef = Union[str, WidgetClass]
Expand Down
2 changes: 2 additions & 0 deletions src/magicgui/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
LiteralEvalLineEdit,
LogSlider,
MainWindow,
Password,
ProgressBar,
PushButton,
QuantityEdit,
Expand Down Expand Up @@ -91,6 +92,7 @@
"LogSlider",
"MainFunctionGui",
"MainWindow",
"Password",
"PushButton",
"ProgressBar",
"QuantityEdit",
Expand Down
5 changes: 5 additions & 0 deletions src/magicgui/widgets/_concrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ class LineEdit(ValueWidget):
"""A one-line text editor."""


@backend_widget
class Password(ValueWidget):
"""A one-line text editor that obscures input."""


@backend_widget
class LiteralEvalLineEdit(ValueWidget):
"""A one-line text editor that evaluates strings as python literals."""
Expand Down
2 changes: 1 addition & 1 deletion src/magicgui/widgets/bases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
exported in :mod:`magicgui.widgets`.
All magicgui :class:`Widget` bases comprise a backend widget that implements one of the
widget protocols defined in :mod:`magicgui.widgets._protocols`. The basic composition
widget protocols defined in :mod:`magicgui.widgets.protocols`. The basic composition
pattern is as follows:
.. code-block:: python
Expand Down
2 changes: 1 addition & 1 deletion src/magicgui/widgets/bases/_container_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ContainerWidget(Widget, _OrientationMixin, MutableSequence[WidgetVar]):
"""Widget that can contain other widgets.
Wraps a widget that implements
:class:`~magicgui.widgets._protocols.ContainerProtocol`.
:class:`~magicgui.widgets.protocols.ContainerProtocol`.
A ``ContainerWidget`` behaves like a python list of :class:`Widget` objects.
Subwidgets can be accessed using integer or slice-based indexing (``container[0]``),
Expand Down
8 changes: 8 additions & 0 deletions src/magicgui/widgets/bases/_create_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ def create_widget(

if widget_type:
options["widget_type"] = widget_type
# special case parameters named "password" with annotation of str
if (
not options.get("widget_type")
and (name or "").lower() == "password"
and annotation is str
):
options["widget_type"] = "Password"

wdg_class, opts = get_widget_class(
value, annotation, options, is_result, raise_on_unknown
)
Expand Down

0 comments on commit 5edccca

Please sign in to comment.