Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ repos:
files: "^\\.github/workflows/.*\\.ya?ml$"

- repo: https://github.com/adhtruong/mirrors-typos
rev: v1.49.0
rev: v1.50.1
hooks:
- id: typos
args: [--force-exclude] # omitting --write-changes

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.16.4
rev: v0.16.6
hooks:
- id: ruff-check
args: ["--fix", "--unsafe-fixes"]
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ conda install -c conda-forge magicgui pyqt # or pyside6 instead of pyqt
from magicgui import magicgui
from enum import Enum


class Medium(Enum):
Glass = 1.520
Oil = 1.515
Water = 1.333
Air = 1.0003


# decorate your function with the @magicgui decorator
@magicgui(call_button="calculate", result_widget=True)
def snells_law(aoi=30.0, n1=Medium.Glass, n2=Medium.Water, degrees=True):
Expand All @@ -83,6 +85,7 @@ def snells_law(aoi=30.0, n1=Medium.Glass, n2=Medium.Water, degrees=True):
except ValueError:
return "Total internal reflection!"


# your function is now capable of showing a GUI
snells_law.show(run=True)
```
Expand Down
35 changes: 19 additions & 16 deletions docs/api/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ value directly, you can do one of two things:

```python title="👍 New Method (>= v0.3.0)"
@widget.changed.connect
def my_callback(new_value: int):
... # use new_value directly
def my_callback(new_value: int): ... # use new_value directly


# or, if you don't need to use new_value
@widget.changed.connect
Expand All @@ -50,13 +50,13 @@ For the few packages who were manually emitting change events,
you should no longer provide the `value=` keyword when emitting.

```python title="👎 Old Method (< v0.3.0)"
widget.changed(value='whatever')
widget.changed(value="whatever")
```

```python title="👍 New Method (>= v0.3.0)"
widget.changed.emit('whatever')
widget.changed.emit("whatever")
# OR (if you prefer the direct __call__ syntax)
widget.changed('whatever')
widget.changed("whatever")
```

## v0.2.0 migration guide
Expand All @@ -76,9 +76,10 @@ instantiated [`magicgui.widgets.Widget`][magicgui.widgets.Widget].
```python title="👎 Old Method (< v0.2.0)"
from magicgui import magicgui, event_loop


@magicgui
def function(x, y):
...
def function(x, y): ...


with event_loop():
gui = function.Gui(show=True)
Expand All @@ -87,9 +88,10 @@ with event_loop():
```python title="👍 New Method (>= v0.2.0)"
from magicgui import magicgui


@magicgui
def function(x, y):
...
def function(x, y): ...


function.show(run=True)
```
Expand All @@ -109,11 +111,11 @@ to use `widget.native` instead of `widget`
```python
from magicgui import magicgui, use_app

use_app('qt')
use_app("qt")


@magicgui
def function(x, y):
...
def function(x, y): ...
```

```python
Expand All @@ -134,13 +136,14 @@ show *multiple* widgets next to each other, then you would still want to use the
```python
from magicgui import magicgui, event_loop


@magicgui
def function_a(x=1, y=3):
...
def function_a(x=1, y=3): ...


@magicgui
def function_b(z='asdf'):
...
def function_b(z="asdf"): ...


with event_loop():
function_a.show()
Expand Down
20 changes: 13 additions & 7 deletions docs/dataclasses.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ boilerplate.
``` python title="Example dataclass"
from dataclasses import dataclass


@dataclass # (1)!
class Person:
name: str # (2)!
name: str # (2)!
age: int = 0 # (3)!

p = Person(name='John', age=30) # (4)!
print(p) # (5)!

p = Person(name="John", age=30) # (4)!
print(p) # (5)!
```

1. The `@dataclass` decorator is used to mark a class as a dataclass. This
Expand Down Expand Up @@ -88,12 +90,14 @@ that has two additional features:
``` python
from magicgui.experimental import guiclass


@guiclass
class MyDataclass:
a: int = 0
b: str = 'hello'
b: str = "hello"
c: bool = True


obj = MyDataclass()
obj.gui.show()
```
Expand All @@ -110,7 +114,7 @@ As you interact programmatically with the `obj` instance, the widgets in the

``` python
obj = MyDataclass(a=10)
obj.b = 'world'
obj.b = "world"
obj.c = False

obj.gui.show()
Expand Down Expand Up @@ -141,15 +145,17 @@ Any additional keyword arguments to the `button` decorator will be passed to the
``` python
from magicgui.experimental import guiclass, button


@guiclass
class Greeter:
first_name: str

@button
def say_hello(self):
print(f'Hello {self.first_name}')
print(f"Hello {self.first_name}")


greeter = Greeter('Talley')
greeter = Greeter("Talley")
greeter.gui.show()
```

Expand Down
19 changes: 15 additions & 4 deletions docs/decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import math
from enum import Enum
from magicgui import magicgui


# dropdown boxes are best made by creating an enum
class Medium(Enum):
Glass = 1.520
Oil = 1.515
Water = 1.333
Air = 1.0003


# decorate your function with the @magicgui decorator
@magicgui(call_button="calculate")
def snells_law(aoi=30.0, n1=Medium.Glass, n2=Medium.Water, degrees=True):
Expand All @@ -40,7 +42,8 @@ def snells_law(aoi=30.0, n1=Medium.Glass, n2=Medium.Water, degrees=True):
# beyond the critical angle
return "Total internal reflection!"

snells_law.show() # leave open

snells_law.show() # leave open
```

The object returned by the `magicgui` decorator is an instance of [`magicgui.widgets.FunctionGui`][magicgui.widgets.FunctionGui]. It can still be called like the original function, but it also knows how to present itself as a GUI.
Expand Down Expand Up @@ -80,7 +83,7 @@ We can invoke the function in a few ways:
* We can call the object just like the original function.

```python
snells_law() # 34.7602
snells_law() # 34.7602
snells_law(aoi=12) # 13.7142
```

Expand Down Expand Up @@ -133,6 +136,7 @@ def my_callback(value: str):
# of the function call in the `value` attribute
print(f"Your function was called! The result is: {value}")


result = snells_law()
```

Expand All @@ -150,6 +154,7 @@ to the `<parameter_name>.changed` signal:
def _on_n1_changed(x: Medium):
print(f"n1 was changed to {x}")


snells_law.n1.value = Medium.Air
```

Expand Down Expand Up @@ -180,6 +185,7 @@ is equivalent to this:
def function():
pass


function = magicgui(function, auto_call=True)
```

Expand Down Expand Up @@ -230,13 +236,16 @@ or connect [events](events.md).
```python
from magicgui import magic_factory


def _on_init(widget):
print("widget created!", widget)
widget.y.changed.connect(lambda x: print("y changed!", x))


@magic_factory(widget_init=_on_init)
def my_factory(x: int, y: str): ...


new_widget = my_factory()
```

Expand All @@ -258,17 +267,19 @@ from magicgui.widgets import create_widget, Container
from magicgui.types import Undefined


def pseudo_magicgui(func: 'Callable'):
def pseudo_magicgui(func: "Callable"):
return Container(
widgets=[
create_widget(p.default, annotation=p.annotation, name=p.name)
for p in signature(func).parameters.values()
]
)

def some_func(x: int = 2, y: str = 'hello'):

def some_func(x: int = 2, y: str = "hello"):
return x, y


my_widget = pseudo_magicgui(some_func)
my_widget.show()
```
Expand Down
16 changes: 10 additions & 6 deletions docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ widget's `changed` event:
```python
from magicgui import widgets

text = widgets.LineEdit(value='type something')
text = widgets.LineEdit(value="type something")
text.changed.connect(lambda val: print(f"Text changed to: {val}"))
```

Expand All @@ -32,9 +32,10 @@ widget's `changed` event:
```python
from magicgui import magicgui


@magicgui
def my_function(text: str):
...
def my_function(text: str): ...


my_function.text.changed.connect(lambda val: print(f"Text changed to: {val}"))
```
Expand All @@ -44,12 +45,14 @@ widget's `changed` event:
```python
from magicgui import magic_factory


def _on_init(widget):
widget.text.changed.connect(lambda val: print(f"Text changed to: {val}"))


@magic_factory(widget_init=_on_init)
def my_function(text: str):
...
def my_function(text: str): ...


my_widget = my_function()
```
Expand All @@ -67,11 +70,12 @@ widget's `changed` event:
use it as a decorator if you prefer.

```python
text = widgets.LineEdit(value='type something')
text = widgets.LineEdit(value="type something")

# this works
text.changed.connect(lambda val: print(f"Text changed to: {val}"))


# so does this
@text.changed.connect
def on_text_changed(val):
Expand Down
Loading
Loading