File: magicgui_jupyter.py

package info (click to toggle)
magicgui 0.10.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,880 kB
  • sloc: python: 11,573; makefile: 11; sh: 9
file content (47 lines) | stat: -rw-r--r-- 1,215 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""# Jupyter notebooks and magicgui

This example shows magicgui widgets embedded in a jupyter notebook.

The key function here is `use_app("ipynb")`.

You can also [get this example at github](https://github.com/pyapp-kit/magicgui/blob/main/docs/examples/notebooks/magicgui_jupyter.ipynb).

```python hl_lines="4-5"
import math
from enum import Enum

from magicgui import magicgui, use_app

use_app("ipynb")


class Medium(Enum):
    # Various media and their refractive indices.
    Glass = 1.520
    Oil = 1.515
    Water = 1.333
    Air = 1.0003


@magicgui(
    call_button="calculate", result_widget=True, layout="vertical", auto_call=True
)
def snells_law(aoi=1.0, n1=Medium.Glass, n2=Medium.Water, degrees=True):
    # Calculate the angle of refraction given two media and an angle of incidence.
    if degrees:
        aoi = math.radians(aoi)
    try:
        n1 = n1.value
        n2 = n2.value
        result = math.asin(n1 * math.sin(aoi) / n2)
        return round(math.degrees(result) if degrees else result, 2)
    except ValueError:  # math domain error
        return "TIR!"


snells_law
```
"""

# %%
# ![magicgui widget embedded in the jupyter notebook](../../images/jupyter_magicgui_widget.png)