CoolFace
Apppublic

LucasPlant/controlsim

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
utils.py100 linesDownload Raw Back to root
1"""2utils.py: Utility functions for creating Dash input fields.3 4This module provides common functions for creating consistent input fields5across the application, reducing code duplication.6"""7 8from dash import html, dcc9from typing import Optional10 11 12def make_input_field(name: str, props: dict, source: str = "controller", 13                    existing_values: Optional[dict] = None) -> html.Div:14    """15    Create a standardized input field based on its type and properties.16    17    This function creates consistent input fields for dropdowns and other input types18    that can be used across systems, controllers, and trajectory generators.19    20    Args:21        name: The name/id for the input field22        props: Dictionary containing field properties including:23            - "type": The input type ("dropdown", "number", "text", etc.)24            - "value": Default value25            - "description": Label text for the field26            - "options": List of options (for dropdown type only)27        source: The source category for the input ("system", "controller", "trajectory_generator")28        existing_values: Dictionary of existing values to use instead of defaults29    30    Returns:31        html.Div containing the labeled input field32    """33    if existing_values is None:34        existing_values = {}35    36    # Get the value to use (existing value or default)37    value = existing_values.get(name, props["value"])38    39    if props["type"] == "dropdown":40        return html.Div(41            [42                html.Label(props["description"]),43                dcc.Dropdown(44                    id={"type": "input", "source": source, "name": name},45                    options=[46                        {"label": k, "value": k} for k in props["options"]47                    ],48                    value=value,49                    clearable=False,50                ),51            ]52        )53    else:54        return html.Div(55            [56                html.Label(props["description"]),57                dcc.Input(58                    id={"type": "input", "source": source, "name": name},59                    type=props["type"],60                    value=value,61                    debounce=True,62                ),63            ]64        )65 66 67def make_state_input_field(name: str, props: dict, source: str = "system", 68                          existing_values: Optional[dict] = None) -> list:69    """70    Create input fields specifically for state initialization.71    72    This function creates labeled input fields for system state initialization,73    following the pattern used in BaseSystem.74    75    Args:76        name: The name/id for the input field77        props: Dictionary containing:78            - "name": Display name for the state79            - "description": Description of the state80            - "value": Default value81        source: The source category for the input82        existing_values: Dictionary of existing values to use instead of defaults83    84    Returns:85        List containing [html.Label, dcc.Input] for the state field86    """87    if existing_values is None:88        existing_values = {}89    90    value = existing_values.get(name, props["value"])91    92    return [93        html.Label(props["name"] + ": " + props["description"]),94        dcc.Input(95            id={"type": "input", "source": source, "name": name},96            type="number",97            value=value,98            debounce=True,99        ),100    ]