""" Radio Layout Generates a form interface with mutually exclusive radio button options. Features include: - Vertical or horizontal layout options - Keyboard shortcuts - Required/optional validation - Tooltip support - Free response option """ import logging from collections.abc import Mapping from potato.ai.ai_help_wrapper import get_ai_wrapper, get_dynamic_ai_help from potato.server_utils.config_module import config from .identifier_utils import ( safe_generate_layout, generate_element_identifier, generate_validation_attribute, escape_html_content, generate_layout_attributes, display_label_text, ) logger = logging.getLogger(__name__) def generate_radio_layout(annotation_scheme, horizontal=False): """ Generate HTML for a radio button selection interface. Args: annotation_scheme (dict): Configuration including: - name: Schema identifier - description: Display description - labels: List of label configurations, each either: - str: Simple label text - dict: Complex label with: - name: Label identifier - tooltip: Hover text description - tooltip_file: Path to tooltip text file - key_value: Keyboard shortcut key - label_requirement (dict): Optional validation settings - required (bool): Whether selection is mandatory - horizontal (bool): Whether to arrange options horizontally - has_free_response (dict): Optional free text input configuration - instruction: Label for free response field horizontal (bool): Override horizontal layout setting Returns: tuple: (html_string, key_bindings) html_string: Complete HTML for the radio interface key_bindings: List of (key, description) tuples for keyboard shortcuts """ return safe_generate_layout(annotation_scheme, _generate_radio_layout_internal, horizontal) def _generate_radio_layout_internal(annotation_scheme, horizontal=False): """ Internal function to generate radio layout after validation. """ logger.debug(f"Generating radio layout for schema: {annotation_scheme['name']}") # Check for horizontal layout override if annotation_scheme.get("horizontal"): horizontal = True logger.debug("Using horizontal layout") # Get layout attributes for grid positioning layout_attrs = generate_layout_attributes(annotation_scheme) # Initialize form wrapper schema_name = annotation_scheme["name"] schematic = f"""
" logger.info(f"Successfully generated radio layout for {schema_name} " f"with {len(annotation_scheme['labels'])} options") return schematic, key_bindings def _generate_tooltip(label_data): """ Generate tooltip HTML attribute from label data. Args: label_data (dict): Label configuration containing tooltip information Returns: str: Tooltip HTML attribute or empty string if no tooltip """ tooltip_text = "" if "tooltip" in label_data: tooltip_text = label_data["tooltip"] elif "tooltip_file" in label_data: try: with open(label_data["tooltip_file"], "rt", encoding="utf-8") as f: tooltip_text = "".join(f.readlines()) except Exception as e: logger.error(f"Failed to read tooltip file: {e}") return "" if tooltip_text: escaped_tooltip = escape_html_content(tooltip_text) return f'data-toggle="tooltip" data-html="true" data-placement="top" title="{escaped_tooltip}"' return ""