Skip to content

Commit

Permalink
Dashboard: Add tooltips (#843)
Browse files Browse the repository at this point in the history
* update dashboard __init__.py import
* Add tooltips functionality to non-nested states
* Add tooltips for the custom vselects
* update type hints
  • Loading branch information
proy30 authored Feb 11, 2025
1 parent 8f03503 commit cdf408b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/python/impactx/dashboard/Input/components.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional

from .. import setup_server, vuetify
from .. import html, setup_server, vuetify
from .defaults import TooltipDefaults
from .generalFunctions import generalFunctions

server, state, ctrl = setup_server()
Expand Down Expand Up @@ -91,13 +92,18 @@ def select(
generalFunctions.get_default(f"{v_model_name}_list", "default_values"),
)

return vuetify.VSelect(
label=label,
v_model=(v_model_name,),
items=items,
dense=True,
**kwargs,
)
with vuetify.VTooltip(**TooltipDefaults.TOOLTIP_STYLE):
with vuetify.Template(v_slot_activator="{ on, attrs }"):
vuetify.VSelect(
label=label,
v_model=(v_model_name,),
items=items,
dense=True,
**kwargs,
v_on="on",
v_bind="attrs",
)
html.Span(TooltipDefaults.TOOLTIP.get(v_model_name))

@staticmethod
def text_field(
Expand All @@ -121,17 +127,22 @@ def text_field(
if v_model_name is None:
v_model_name = label.lower().replace(" ", "_")

return vuetify.VTextField(
label=label,
v_model=(v_model_name,),
error_messages=(f"{v_model_name}_error_message", []),
type="number",
step=generalFunctions.get_default(f"{v_model_name}", "steps"),
suffix=generalFunctions.get_default(f"{v_model_name}", "units"),
__properties=["step"],
dense=True,
**kwargs,
)
with vuetify.VTooltip(**TooltipDefaults.TOOLTIP_STYLE):
with vuetify.Template(v_slot_activator="{ on, attrs }"):
vuetify.VTextField(
label=label,
v_model=(v_model_name,),
error_messages=(f"{v_model_name}_error_message", []),
type="number",
step=generalFunctions.get_default(f"{v_model_name}", "steps"),
suffix=generalFunctions.get_default(f"{v_model_name}", "units"),
__properties=["step"],
dense=True,
v_on="on",
v_bind="attrs",
**kwargs,
)
html.Span(TooltipDefaults.TOOLTIP.get(v_model_name))


class NavigationComponents:
Expand Down
20 changes: 20 additions & 0 deletions src/python/impactx/dashboard/Input/defaults.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from impactx.impactx_pybind import ImpactX, RefPart

from .defaults_helper import InputDefaultsHelper


class DashboardDefaults:
"""
Defaults for input parameters in the ImpactX dashboard.
Expand Down Expand Up @@ -113,3 +118,18 @@ class DashboardDefaults:
"beta": "m",
"emitt": "m",
}


class TooltipDefaults:
"""
Defaults for input toolips in the ImpactX dashboard.
"""

TOOLTIP_STYLE = {
"bottom": True,
"nudge_top": "20",
}

TOOLTIP = InputDefaultsHelper.get_docstrings(
[RefPart, ImpactX], DashboardDefaults.DEFAULT_VALUES
)
37 changes: 37 additions & 0 deletions src/python/impactx/dashboard/Input/defaults_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import inspect
from typing import Dict, List, Type


class InputDefaultsHelper:
"""
Methods in this class are used to dynamically parse
core ImpactX data (default values, docstrings, etc.)
"""

@staticmethod
def get_docstrings(
class_names: List[Type], default_list: Dict[str, any]
) -> Dict[str, str]:
"""
Retrieves docstrings for each method and property
in the provided clases.
:param classes: The class names to parse docstrings with.
:param defaults_list: The dictionary of defaults value.
"""

docstrings = {}

for each_class in class_names:
for name, attribute in inspect.getmembers(each_class):
if name not in default_list:
continue

is_method = inspect.isfunction(attribute)
is_property = inspect.isdatadescriptor(attribute)

if is_method or is_property:
docstring = inspect.getdoc(attribute) or ""
docstrings[name] = docstring

return docstrings
2 changes: 2 additions & 0 deletions src/python/impactx/dashboard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from trame.widgets import html
from trame.widgets import vuetify as vuetify

# isort: off
Expand All @@ -18,6 +19,7 @@


__all__ = [
"html",
"JupyterApp",
"setup_server",
"vuetify",
Expand Down

0 comments on commit cdf408b

Please sign in to comment.