Skip to content

Commit

Permalink
Add ipywidgets backend (#87)
Browse files Browse the repository at this point in the history
* wip

* better error on incorrect protocol

* members -> methods

* methods

* one good working widget

* implement lots of widgets

* add _IPySupportsText

* add note

* working container

* add notebook example

* update notebook

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* kinda working again

* fix tests

* remove guimodel test

* remove comment

* add ipywidgets to testing

* fix type annotation

* Update magicgui/widgets/_bases/button_widget.py

Co-authored-by: Grzegorz Bokota <bokota+github@gmail.com>

* add extra

* fix qwidg

* change test

* move test

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Grzegorz Bokota <bokota+github@gmail.com>
  • Loading branch information
3 people committed Sep 21, 2022
1 parent 284999e commit 83f9ab0
Show file tree
Hide file tree
Showing 12 changed files with 648 additions and 35 deletions.
80 changes: 80 additions & 0 deletions examples/magicgui_jupyter.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1df96bcb5361429faddb252e3acfa608",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HBox(children=(Label(value='aoi', layout=Layout(min_width='40px')), FloatText(value=1.0, step=1…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import math\n",
"from enum import Enum\n",
"\n",
"from magicgui import magicgui, use_app\n",
"use_app(\"ipynb\")\n",
"\n",
"class Medium(Enum):\n",
" \"\"\"Enum for various media and their refractive indices.\"\"\"\n",
"\n",
" Glass = 1.520\n",
" Oil = 1.515\n",
" Water = 1.333\n",
" Air = 1.0003\n",
"\n",
"\n",
"@magicgui(\n",
" call_button=\"calculate\", result_widget=True, layout='vertical', auto_call=True\n",
")\n",
"def snells_law(aoi=1.0, n1=Medium.Glass, n2=Medium.Water, degrees=True):\n",
" \"\"\"Calculate the angle of refraction given two media and an AOI.\"\"\"\n",
" if degrees:\n",
" aoi = math.radians(aoi)\n",
" try:\n",
" n1 = n1.value\n",
" n2 = n2.value\n",
" result = math.asin(n1 * math.sin(aoi) / n2)\n",
" return round(math.degrees(result) if degrees else result, 2)\n",
" except ValueError: # math domain error\n",
" return \"TIR!\"\n",
"\n",
"\n",
"snells_law"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
2 changes: 1 addition & 1 deletion magicgui/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Backend modules implementing applications and widgets."""

BACKENDS = {"Qt": ("_qtpy", "qtpy")}
BACKENDS = {"Qt": ("_qtpy", "qtpy"), "ipynb": ("_ipynb", "ipynb")}

for key, value in list(BACKENDS.items()):
if not key.islower():
Expand Down
47 changes: 47 additions & 0 deletions magicgui/backends/_ipynb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from .application import ApplicationBackend
from .widgets import (
CheckBox,
ComboBox,
Container,
DateEdit,
DateTimeEdit,
EmptyWidget,
FloatSlider,
FloatSpinBox,
Label,
LineEdit,
LiteralEvalLineEdit,
PushButton,
RadioButton,
Slider,
SpinBox,
TextEdit,
TimeEdit,
get_text_width,
)

# DateTimeEdit
# show_file_dialog,

__all__ = [
"ApplicationBackend",
"CheckBox",
"ComboBox",
"Container",
"DateEdit",
"TimeEdit",
"DateTimeEdit",
"EmptyWidget",
"FloatSlider",
"FloatSpinBox",
"Label",
"LineEdit",
"LiteralEvalLineEdit",
"PushButton",
"RadioButton",
"Slider",
"SpinBox",
"TextEdit",
"get_text_width",
"show_file_dialog",
]
24 changes: 24 additions & 0 deletions magicgui/backends/_ipynb/application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from magicgui.widgets._protocols import BaseApplicationBackend


class ApplicationBackend(BaseApplicationBackend):
def _mgui_get_backend_name(self):
return "ipynb"

def _mgui_process_events(self):
raise NotImplementedError()

def _mgui_run(self):
pass # We run in IPython, so we don't run!

def _mgui_quit(self):
pass # We don't run so we don't quit!

def _mgui_get_native_app(self):
return self

def _mgui_start_timer(self, interval=0, on_timeout=None, single=False):
raise NotImplementedError()

def _mgui_stop_timer(self):
raise NotImplementedError()
Loading

0 comments on commit 83f9ab0

Please sign in to comment.