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

docs: Auto-generated examples gallery #571

Merged
merged 29 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e4638e3
mkdocs-gallery skeleton
GenevieveBuckley Jul 25, 2023
e7361d5
Move example scripts into example gallery docs location
GenevieveBuckley Jul 25, 2023
12065ef
Add required README files for mkdocs-gallery
GenevieveBuckley Jul 25, 2023
3be3c67
Move example files into docs subdirectory
GenevieveBuckley Jul 26, 2023
715e30b
Working examples gallery for magicgui
GenevieveBuckley Jul 26, 2023
323334c
Remove period after markdown formatting heading ====
GenevieveBuckley Jul 26, 2023
b0261ea
style(pre-commit.ci): auto fixes [...]
pre-commit-ci[bot] Jul 26, 2023
04dc230
Fix formatting of python docstrings so pre-commit doesn't overwrite
GenevieveBuckley Jul 26, 2023
9bc2d31
More meaningful file name for hotdog app example
GenevieveBuckley Jul 26, 2023
5c79ffc
Minor clarifications for examples text
GenevieveBuckley Jul 26, 2023
9ef0a68
style(pre-commit.ci): auto fixes [...]
pre-commit-ci[bot] Jul 26, 2023
9065af9
Fix formatting errors (lines too long)
GenevieveBuckley Jul 26, 2023
3300506
Merge branch 'docs-examples' of github.com:GenevieveBuckley/magicgui …
GenevieveBuckley Jul 26, 2023
b17c686
Ruff formatting rules
GenevieveBuckley Jul 26, 2023
90f339b
Fix mkdocs build --strict warnings
GenevieveBuckley Jul 26, 2023
b4781c2
Must have napari in docs requirements to build examples in docs
GenevieveBuckley Jul 26, 2023
32e07d0
update deps
tlambert03 Jul 26, 2023
93955a1
pin napari
tlambert03 Jul 26, 2023
12bbfdb
update pip
tlambert03 Jul 26, 2023
f4af6d5
fix ruff
tlambert03 Jul 26, 2023
7f39ccb
Examples jupytext - put more info in docstring so users see fewer # s…
GenevieveBuckley Jul 27, 2023
8cce64c
Merge branch 'main' into docs-examples
tlambert03 Sep 5, 2023
e6d876e
change deps
tlambert03 Sep 6, 2023
2fd9843
fix example tests
tlambert03 Sep 6, 2023
5d57367
revert pyside change
tlambert03 Sep 6, 2023
939ac1f
update image path
tlambert03 Sep 6, 2023
2a76160
remove periods
tlambert03 Sep 6, 2023
60dda82
fix links
tlambert03 Sep 6, 2023
2caf03d
test: skip rangeslider
tlambert03 Sep 6, 2023
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
10 changes: 7 additions & 3 deletions .github/workflows/deploy_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ on:

jobs:
docs:
runs-on: macos-latest # for the screenshots
runs-on: macos-latest # for the screenshots
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-python@v4
with:
python-version: '3.x'
- run: pip install -e .[docs]
python-version: "3.x"
- run: |
python -m pip install --upgrade pip
python -m pip install -e .[docs]

- name: Deploy docs to GitHub Pages
if: github.event_name == 'push'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ venv.bak/

# mkdocs documentation
/site
docs/generated*

# mypy
.mypy_cache/
Expand Down
3 changes: 3 additions & 0 deletions docs/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Getting started

A gallery of examples for magicgui.
3 changes: 3 additions & 0 deletions docs/examples/applications/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Demo applications

Example applications built with magicgui.
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
"""# Callable functions demo.

This example demonstrates handling callable functions with magicgui.
"""
from magicgui import magicgui


def f(x: int, y="a string") -> str:
"""Example function F."""
return f"{y} {x}"


def g(x: int = 6, y="another string") -> str:
"""Example function G."""
return f"{y} asdfsdf {x}"


@magicgui(call_button=True, func={"choices": ["f", "g"]})
def example(func="f"):
"""Ëxample function."""
pass


def update(f: str):
"""Update function."""
if len(example) > 2:
del example[1]
example.insert(1, magicgui(globals()[f]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
"""# Chaining functions together.

This example demonstrates chaining multiple functions together.
"""
from magicgui import magicgui, widgets


@magicgui(auto_call=True)
def func_a(x: int = 64, y: int = 64):
"""Callable function A."""
print("calling func_a")
return x + y


@magicgui(auto_call=True, input={"visible": False, "label": " ", "max": 100000})
def func_b(input: int, mult=1.0):
"""Callable function B."""
print("calling func_b")
result = input * mult
# since these function defs live in globals(), you can update them directly
Expand All @@ -30,6 +36,7 @@ def _on_func_a(value: str):
labels=False,
)
def func_c(input: int, format: str = "({} + {}) * {} is {}") -> str:
"""Callable function C."""
print("calling func_c\n")
return format.format(func_a.x.value, func_a.y.value, func_b.mult.value, input)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"""# Hotdog or not app.

Demo app to upload an image and classify if it's an hotdog or not.
"""
import pathlib
from enum import Enum

from magicgui import magicgui


class HotdogOptions(Enum):
"""All hotdog possibilities"""
"""All hotdog possibilities."""

Hotdog = 1
NotHotdog = 0
Expand Down
20 changes: 20 additions & 0 deletions docs/examples/applications/pint_quantity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""# Quantities with pint.

Pint is a Python package to define, operate and manipulate physical quantities:
the product of a numerical value and a unit of measurement.
It allows arithmetic operations between them and conversions
from and to different units.
https://pint.readthedocs.io/en/stable/
"""
from pint import Quantity

from magicgui import magicgui


@magicgui
def widget(q=Quantity("1 ms")):
"""Widget allowing users to input quantity measurements."""
print(q)


widget.show(run=True)
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Simple demonstration of magicgui."""
"""# Snell's law demonstration using magicgui.

Demo app for calculating angles of refraction according to Snell's law.
"""

import math
from enum import Enum
Expand Down
21 changes: 21 additions & 0 deletions docs/examples/applications/values_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""# Input values dialog.

A basic example of a user input dialog.

This will pause code execution until the user responds.

# ![Values input dialog](../../images/values_input.png){ width=50% }

```python linenums="1"
from magicgui.widgets import request_values

vals = request_values(
age=int,
name={"annotation": str, "label": "Enter your name:"},
title="Hi, who are you?",
)
print(repr(vals))
```
"""

# %%
tlambert03 marked this conversation as resolved.
Show resolved Hide resolved
55 changes: 0 additions & 55 deletions docs/examples/basic.md

This file was deleted.

5 changes: 5 additions & 0 deletions examples/basic.py → docs/examples/basic_example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"""# Basic example.

A basic example using magicgui.
"""
from magicgui import magicgui


@magicgui
def example(x: int, y="hi"):
"""Basic example function."""
return x, y


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"""Widget demonstration of magicgui."""
"""# Basic widgets demo.

Widget demonstration with magicgui.

This code demonstrates a few of the widget types that magicgui can create
based on the parameter types in your function.
"""
import datetime
from enum import Enum
from pathlib import Path
Expand Down
3 changes: 3 additions & 0 deletions docs/examples/demo_widgets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Demo widget types

Example gallery demonstrating the available widget types in magicgui.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""# Custom text labels for widgets.

An example showing how to create custom text labels for your widgets.
"""
from magicgui import magicgui


# use a different label than the default (the parameter name) in the UI
@magicgui(x={"label": "widget to set x"})
def example(x=1, y="hi"):
"""Example function."""
return x, y


Expand Down
10 changes: 9 additions & 1 deletion examples/choices.py → docs/examples/demo_widgets/choices.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Choices for dropdowns can be provided in a few different ways."""
"""# Dropdown selection widget.

Choices for dropdowns can be provided in a few different ways.
"""
from enum import Enum

from magicgui import magicgui, widgets
Expand All @@ -14,27 +17,32 @@ class Medium(Enum):

@magicgui(ri={"choices": ["Oil", "Water", "Air"]}, auto_call=True)
def as_list(ri="Water"):
"""Function decorated with magicgui list of choices."""
print("refractive index is", Medium[ri].value)


@magicgui(auto_call=True)
def as_enum(ri: Medium = Medium.Water):
"""Function decorated with magicgui and enumeration."""
print("refractive index is", ri.value)


@magicgui(
ri={"choices": [("Oil", 1.515), ("Water", 1.33), ("Air", 1.0)]}, auto_call=True
)
def as_2tuple(ri=1.33):
"""Function decorated with magicgui tuple of choices."""
print("refractive index is", ri)


def get_choices(gui):
"""Function returning tuple of material and refractive index value."""
return [("Oil", 1.515), ("Water", 1.33), ("Air", 1.0)]


@magicgui(ri={"choices": get_choices}, auto_call=True)
def as_function(ri: float):
"""Function to calculate refractive index."""
print("refractive index is", ri)


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""FileDialog with magicgui."""
"""# File dialog widget.

A file dialog widget example.
"""
from pathlib import Path
from typing import Sequence

Expand Down
9 changes: 6 additions & 3 deletions examples/image.py → docs/examples/demo_widgets/image.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Example of creating an Image Widget from a file.
"""# Image widget.

Example of creating an Image Widget from a file.

(This requires pillow, or that magicgui was installed as ``magicgui[image]``)
"""

from pathlib import Path

from magicgui.widgets import Image

img = Path(__file__).parent.parent / "tests" / "_test.jpg"
image = Image(value=img)
img = Path(__file__).parent.parent.parent / "images" / "_test.jpg"
image = Image(value=str(img))
image.scale_widget_to_image_size()
image.show(run=True)
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Simple demonstration of magicgui."""
"""# Log slider widget.

A logarithmic scale range slider widget.
"""
from magicgui import magicgui


Expand All @@ -8,6 +11,7 @@
input={"widget_type": "LogSlider", "max": 10000, "min": 1, "tracking": False},
)
def slider(input=1):
"""Logarithmic scale slider."""
return round(input, 4)


Expand Down
5 changes: 5 additions & 0 deletions examples/login.py → docs/examples/demo_widgets/login.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""# Password login.

A password login field widget.
"""
from magicgui import magicgui


Expand All @@ -8,6 +12,7 @@
# (unless you override "widget_type")
@magicgui(password2={"widget_type": "Password"})
def login(username: str, password: str, password2: str):
"""User login credentials."""
...


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""# Optional user choice.

Optional user input using a dropdown selection widget.
"""
from typing import Optional

from magicgui import magicgui
Expand All @@ -6,6 +10,7 @@
# Using optional will add a '----' to the combobox, which returns "None"
@magicgui(path={"choices": ["a", "b"]})
def f(path: Optional[str] = None):
"""Öptional user input function."""
print(path, type(path))


Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"""# Range slider widget.

A double ended range slider widget.
"""
from typing import Tuple

from magicgui import magicgui


@magicgui(auto_call=True, range_value={"widget_type": "RangeSlider", "max": 500})
def func(range_value: Tuple[int, int] = (20, 380)):
"""Double ended range slider."""
print(range_value)


Expand Down
Loading