Skip to content

Commit

Permalink
Merge pull request #268 from maxfordham/264-make-button_bar_config-co…
Browse files Browse the repository at this point in the history
…nfigurable

🐛 Implement CrudView into CrudButtonBar
  • Loading branch information
ollyhensby authored Jan 22, 2024
2 parents 3b030a9 + 39dcad9 commit b6e5444
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 66 deletions.
159 changes: 159 additions & 0 deletions docs/buttonbar_extended.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "ea9dfff3-99b1-4269-a658-617f5868009e",
"metadata": {},
"outputs": [],
"source": [
"%run __init__.py"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7be455ab-fbf6-4577-a913-adb37ccebfde",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"for maplocal to load openpath and runcmd callable, maplocal_example.py must exist with functions `openpath` and `runcmd`\n"
]
}
],
"source": [
"import ipywidgets as w\n",
"import traitlets as tr\n",
"from ipyautoui.constants import IMAGE_BUTTON_KWARGS\n",
"from ipyautoui.custom.buttonbars import CrudButtonBar, CrudOptions, CrudView"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5c7104a4-ad43-42de-b264-a408ac7f3189",
"metadata": {},
"outputs": [],
"source": [
"EXTENDED_BUTTONBAR_CONFIG = CrudView(\n",
" images=CrudOptions(\n",
" tooltip=\"Add image\",\n",
" tooltip_clicked=\"Go back to table\",\n",
" button_style=\"info\",\n",
" message=\"📷 <i>Adding image</i>\",\n",
" ),\n",
" add=CrudOptions(\n",
" tooltip=\"Add item\",\n",
" tooltip_clicked=\"Go back to table\",\n",
" button_style=\"success\",\n",
" message=\"➕ <i>Adding data</i>\",\n",
" ),\n",
" edit=CrudOptions(\n",
" tooltip=\"Edit item\",\n",
" tooltip_clicked=\"Go back to table\",\n",
" button_style=\"warning\",\n",
" message=\"✏️ <i>Editing data</i>\",\n",
" ),\n",
" copy=CrudOptions(\n",
" tooltip=\"Copy item\",\n",
" tooltip_clicked=\"Go back to table\",\n",
" button_style=\"primary\",\n",
" message=\"📝 <i>Copying data</i>\",\n",
" ),\n",
" delete=CrudOptions(\n",
" tooltip=\"Delete item\",\n",
" tooltip_clicked=\"Go back to table\",\n",
" button_style=\"danger\",\n",
" message=\"🗑️ <i>Deleting data</i>\",\n",
" ),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "7150ceed-9f0c-469b-a0be-7409b9f3b2a1",
"metadata": {},
"outputs": [],
"source": [
"class CrudButtonBarExtended(CrudButtonBar):\n",
" fn_images = tr.Callable(default_value=lambda: print(\"add image\"))\n",
" \n",
" def __init__(self, **kwargs):\n",
" self.images = w.ToggleButton(**IMAGE_BUTTON_KWARGS)\n",
" self.images.observe(self._images, \"value\")\n",
" super().__init__(**kwargs | {\"crud_view\": EXTENDED_BUTTONBAR_CONFIG})\n",
" self.children = [\n",
" self.images,\n",
" self.add,\n",
" self.edit,\n",
" self.copy,\n",
" self.delete,\n",
" self.reload,\n",
" self.message,\n",
" ]\n",
"\n",
" def _images(self, onchange):\n",
" self._onclick(\"images\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "07ee94ee-f525-4bce-b928-6437675b6e59",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b3f3f2525ff142fb9f0e65453cf79fbd",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"CrudButtonBarExtended(children=(ToggleButton(value=False, button_style='info', icon='image', layout=Layout(wid…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"if __name__ == \"__main__\":\n",
" display(CrudButtonBarExtended())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "06afa3ff-9ef7-44e2-90cd-9c37f5050d4a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
2 changes: 1 addition & 1 deletion src/ipyautoui/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
icon="edit",
style={},
button_style="warning",
tooltip="add item",
tooltip="edit item",
layout={"width": BUTTON_WIDTH_MIN}, # , "height": BUTTON_HEIGHT_MIN
disabled=False,
)
Expand Down
139 changes: 81 additions & 58 deletions src/ipyautoui/custom/buttonbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
from ipyautoui.constants import (
BUTTON_WIDTH_MIN,
TOGGLEBUTTON_ONCLICK_BORDER_LAYOUT,
ADD_BUTTON_KWARGS,
EDIT_BUTTON_KWARGS,
COPY_BUTTON_KWARGS,
DELETE_BUTTON_KWARGS,
RELOAD_BUTTON_KWARGS,
)

from IPython.display import display
Expand Down Expand Up @@ -141,7 +146,6 @@ def fns_onrevert_add_action(
overwrite_dupes=overwrite_dupes,
to_beginning=to_beginning,
)

# -

if __name__ == "__main__":
Expand Down Expand Up @@ -252,54 +256,58 @@ def _observe_tgl_unsaved_changes(self, onchange):
sb.unsaved_changes = True

# +
BUTTONBAR_CONFIG = {
"add": {
"tooltip": "add item",
"tooltip_clicked": "Go back to table",
"button_style": "success",
"message": " ➕ <i>Adding Value</i>",
},
"edit": {
"tooltip": "edit item",
"tooltip_clicked": "Go back to table",
"button_style": "success",
"message": " ✏️ <i>Editing Value</i>",
},
"copy": {
"tooltip": "copy item",
"tooltip_clicked": "Go back to table",
"button_style": "success",
"message": " 📝 <i>Copying Value</i>",
},
"delete": {
"tooltip": "delete item",
"tooltip_clicked": "Go back to table",
"button_style": "success",
"message": " 🗑️ <i>Deleting Value</i>",
},
}


class StrEnum(str, Enum):
pass


CrudView = StrEnum(
"CrudView", {l: n for n, l in enumerate(list(BUTTONBAR_CONFIG.keys()))}
class CrudOptions(ty.TypedDict):
tooltip: str
tooltip_clicked: str
button_style: str
message: str


class CrudView(ty.TypedDict):
add: CrudOptions
edit: CrudOptions
copy: CrudOptions
delete: CrudOptions


DEFAULT_BUTTONBAR_CONFIG = CrudView(
add=CrudOptions(
tooltip="Add item",
tooltip_clicked="Go back to table",
button_style="success",
message="➕ <i>Adding Value</i>",
),
edit=CrudOptions(
tooltip="Edit item",
tooltip_clicked="Go back to table",
button_style="warning",
message="✏️ <i>Editing Value</i>",
),
copy=CrudOptions(
tooltip="Copy item",
tooltip_clicked="Go back to table",
button_style="primary",
message="📝 <i>Copying Value</i>",
),
delete=CrudOptions(
tooltip="Delete item",
tooltip_clicked="Go back to table",
button_style="danger",
message="🗑️ <i>Deleting Value</i>",
),
)


# +
from ipyautoui import constants

# -

class CrudButtonBar(w.HBox):
active = tr.UseEnum(CrudView, allow_none=True, default_value=None)
active = tr.Unicode(default_value=None, allow_none=True)
crud_view = tr.Dict(default_value=DEFAULT_BUTTONBAR_CONFIG)
fn_add = tr.Callable(default_value=lambda: print("add"))
fn_edit = tr.Callable(default_value=lambda: print("edit"))
fn_copy = tr.Callable(default_value=lambda: print("copy"))
fn_delete = tr.Callable(default_value=lambda: print("delete"))
fn_backward = tr.Callable(default_value=lambda: print("backward")) # TODO
fn_backward = tr.Callable(default_value=lambda: print("backward"))
fn_reload = tr.Callable(default_value=None, allow_none=True)

@tr.observe("fn_reload")
Expand All @@ -309,11 +317,16 @@ def _observe_fn_reload(self, change):
else:
self.reload.layout.display = ""

@tr.observe("active")
def _observe_active(self, change):
if change["new"] is None:
self.message.value = ""
logging.info(f"active CRUD view = {change['new']}")
@tr.observe("crud_view")
def _observe_crud_view(self, change):
self._set_crud_view_options()

@property
def active_index(self):
if self.active is None:
return None
else:
return list(self.crud_view.keys()).index(self.active)

def __init__(
self,
Expand All @@ -334,13 +347,14 @@ def __init__(

def _init_form(self):
# self.transpose w.ToggleButton(icon="arrow-right")
self.add = w.ToggleButton(**constants.ADD_BUTTON_KWARGS)
self.edit = w.ToggleButton(**constants.EDIT_BUTTON_KWARGS)
self.copy = w.ToggleButton(**constants.COPY_BUTTON_KWARGS)
self.delete = w.ToggleButton(**constants.DELETE_BUTTON_KWARGS)
self.reload = w.Button(**constants.RELOAD_BUTTON_KWARGS)
self.add = w.ToggleButton(**ADD_BUTTON_KWARGS)
self.edit = w.ToggleButton(**EDIT_BUTTON_KWARGS)
self.copy = w.ToggleButton(**COPY_BUTTON_KWARGS)
self.delete = w.ToggleButton(**DELETE_BUTTON_KWARGS)
self.reload = w.Button(**RELOAD_BUTTON_KWARGS)
self.reload.layout.display = "None"
self.message = w.HTML()
self._set_crud_view_options()

def _init_controls(self):
self.add.observe(self._add, "value")
Expand All @@ -355,13 +369,13 @@ def _onclick(self, button_name):
if w.value:
self.reset_toggles_except(button_name)
self.active = button_name
w.tooltip = BUTTONBAR_CONFIG[button_name]["tooltip_clicked"]
w.tooltip = self.crud_view[button_name]["tooltip_clicked"]
w.layout.border = TOGGLEBUTTON_ONCLICK_BORDER_LAYOUT
self.message.value = BUTTONBAR_CONFIG[button_name]["message"]
self.message.value = self.crud_view[button_name]["message"]
fn()
else:
self.active = None
w.tooltip = BUTTONBAR_CONFIG[button_name]["tooltip"]
w.tooltip = self.crud_view[button_name]["tooltip"]
w.layout.border = None
self.fn_backward()

Expand All @@ -377,9 +391,18 @@ def _copy(self, onchange):
def _delete(self, onchange):
self._onclick("delete")

def reset_toggles_except(self, name):
names = ["add", "edit", "delete", "copy"]
if name not in names:
def _set_crud_view_options(self):
for button_name in self.crud_view.keys():
w = getattr(self, button_name)
for k, v in self.crud_view[button_name].items():
if k in dir(w):
setattr(w, k, self.crud_view[button_name][k])

def reset_toggles_except(self, name=None):
names = self.crud_view.keys()
if name is None:
names = names
elif name not in names:
raise ValueError(f"`name` must be in {str(names)}. {name} given")
names = [n for n in names if n != name]
for n in names:
Expand All @@ -390,8 +413,6 @@ def _reload(self, on_click):
self.fn_reload()


# -

if __name__ == "__main__":

def add():
Expand Down Expand Up @@ -419,3 +440,5 @@ def backward():
)

display(buttonbar)


Loading

0 comments on commit b6e5444

Please sign in to comment.