Skip to content

Commit

Permalink
Elevate _control_item function to class ControlItem, delete unus…
Browse files Browse the repository at this point in the history
…ed code
  • Loading branch information
Wiebke committed Mar 5, 2024
1 parent b51c998 commit b3915e7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 100 deletions.
7 changes: 3 additions & 4 deletions callbacks/control_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,16 +921,15 @@ def update_current_annotated_slices_values(all_classes):
Output("gui-layouts", "children"),
Input("model-list", "value"),
)
def update_gui_parameters(model_name):
def update_model_parameters(model_name):
data = models.models[model_name]
if data["gui_parameters"]:
item_list = JSONParameterEditor(
_id={"type": str(uuid.uuid4())}, # pattern match _id (base id), name
_id={"type": str(uuid.uuid4())},
json_blob=models.remove_key_from_dict_list(
data["gui_parameters"], "comp_group"
),
)
# item_list.init_callbacks(app)
return [html.H4("Model Parameters"), item_list]
return [item_list]
else:
return [""]
35 changes: 9 additions & 26 deletions components/control_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dash_iconify import DashIconify

from components.annotation_class import annotation_class_item
from components.dash_component_editor import ControlItem
from constants import ANNOT_ICONS, KEYBINDS
from utils.content_registry import models
from utils.data_utils import tiled_dataset
Expand All @@ -19,24 +20,6 @@ def _tooltip(text, children):
)


def _control_item(title, title_id, item):
"""
Returns a customized layout for a control item
"""
return dmc.Grid(
[
dmc.Text(
title,
id=title_id,
size="sm",
style={"width": "100px", "margin": "auto", "paddingRight": "5px"},
align="right",
),
html.Div(item, style={"width": "265px", "margin": "auto"}),
]
)


def _accordion_item(title, icon, value, children, id):
"""
Returns a customized layout for an accordion item
Expand Down Expand Up @@ -79,7 +62,7 @@ def layout():
id="data-selection-controls",
children=[
dmc.Space(h=5),
_control_item(
ControlItem(
"Dataset",
"image-selector",
dmc.Grid(
Expand Down Expand Up @@ -115,7 +98,7 @@ def layout():
),
),
dmc.Space(h=25),
_control_item(
ControlItem(
"Slice 1",
"image-selection-text",
[
Expand Down Expand Up @@ -178,7 +161,7 @@ def layout():
],
),
dmc.Space(h=25),
_control_item(
ControlItem(
_tooltip(
"Jump to your annotated slices",
"Annotated slices",
Expand Down Expand Up @@ -208,7 +191,7 @@ def layout():
children=html.Div(
[
dmc.Space(h=5),
_control_item(
ControlItem(
"Brightness",
"bightness-text",
[
Expand Down Expand Up @@ -252,7 +235,7 @@ def layout():
],
),
dmc.Space(h=20),
_control_item(
ControlItem(
"Contrast",
"contrast-text",
dmc.Grid(
Expand Down Expand Up @@ -607,7 +590,7 @@ def layout():
"run-model",
id="model-configuration",
children=[
_control_item(
ControlItem(
"Model",
"model-selector",
dmc.Select(
Expand Down Expand Up @@ -646,7 +629,7 @@ def layout():
styles={"trackLabel": {"cursor": "pointer"}},
),
dmc.Space(h=25),
_control_item(
ControlItem(
"Results",
"",
dmc.Select(
Expand All @@ -655,7 +638,7 @@ def layout():
),
),
dmc.Space(h=25),
_control_item(
ControlItem(
"Opacity",
"",
dmc.Slider(
Expand Down
92 changes: 22 additions & 70 deletions components/dash_component_editor.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import base64
from inspect import _empty, signature
from typing import Callable

import dash_bootstrap_components as dbc
import dash_daq as daq
import dash_mantine_components as dmc
from dash import ALL, Input, Output, State, dcc, html


class ControlItem(dmc.Grid):
"""
Customized layout for a control item
"""

def __init__(self, title, title_id, item, **kwargs):
super(ControlItem, self).__init__(
[
dmc.Text(
title,
id=title_id,
size="sm",
style={"width": "100px", "margin": "auto", "paddingRight": "5px"},
align="right",
),
html.Div(item, style={"width": "265px", "margin": "auto"}),
],
**kwargs,
)


class SimpleItem(dbc.Col):
def __init__(
self,
Expand Down Expand Up @@ -155,46 +173,6 @@ def __init__(
)


class ImgItem(dbc.Col):
def __init__(
self,
name,
src,
base_id,
title=None,
param_key=None,
width="100px",
visible=True,
**kwargs,
):
if param_key is None:
param_key = name

if not (width.endswith("px") or width.endswith("%")):
width = width + "px"

self.label = dbc.Label(title)

encoded_image = base64.b64encode(open(src, "rb").read())
self.src = "data:image/png;base64,{}".format(encoded_image.decode())
self.input_img = html.Img(
id={**base_id, "name": name, "param_key": param_key, "layer": "input"},
src=self.src,
style={"height": "auto", "width": width},
**kwargs,
)

style = {}
if not visible:
style["display"] = "none"

super(ImgItem, self).__init__(
id={**base_id, "name": name, "param_key": param_key, "layer": "form_group"},
children=[self.label, self.input_img],
style=style,
)


class ParameterEditor(dbc.Form):
type_map = {
float: FloatItem,
Expand Down Expand Up @@ -264,7 +242,6 @@ class JSONParameterEditor(ParameterEditor):
"dropdown": DropdownItem,
"radio": RadioItem,
"bool": BoolItem,
"img": ImgItem,
}

def __init__(self, _id, json_blob, **kwargs):
Expand All @@ -289,28 +266,3 @@ def build_children(self, values=None):
children.append(item)

return children


class KwargsEditor(ParameterEditor):
def __init__(self, instance_index, func: Callable, **kwargs):
self.func = func
self._instance_index = instance_index

parameters = [
{"name": name, "value": param.default}
for name, param in signature(func).parameters.items()
if param.default is not _empty
]

super(KwargsEditor, self).__init__(
dict(index=instance_index, type="kwargs-editor"),
parameters=parameters,
**kwargs,
)

def new_record(self):
return {
name: p.default
for name, p in signature(self.func).parameters.items()
if p.default is not _empty
}

0 comments on commit b3915e7

Please sign in to comment.