duck.html.components.select

Select HTML Component.

This module provides reusable Select and Option components for creating dropdown menus in HTML.

Module Contents

Classes

Option

Represents an individual option within a Select dropdown.

Select

A reusable HTML <select> component for creating dropdown menus.

API

class duck.html.components.select.Option(element: Optional[str] = None, properties: Optional[Dict[str, str]] = None, props: Optional[Dict[str, str]] = None, style: Optional[Dict[str, str]] = None, inner_html: Optional[Union[str, str, float]] = None, children: Optional[List[duck.html.components.HtmlComponent]] = None, **kwargs)[source]

Bases: duck.html.components.InnerComponent

Represents an individual option within a Select dropdown.

This component is used to define selectable items inside a Select component.

Parameters:
  • text – The option’s display text (or pass inner_html directly).

  • value – Optional. The option’s value attribute, if it differs from its text.

  • selected – Optional. Whether this option is selected by default.

Example Usage:

option = Option(inner_html="Option 1")
select.add_child(option)

This generates:

<option>Option 1</option>

Returns: - An <option> HTML element.

Initialization

Initialize an HTML component.

Parameters:
  • element – The HTML element tag name (e.g., textarea, input, button). Can be None, but make sure element is returned by get_element method.

  • accept_inner_html – Whether the HTML component accepts an inner body (e.g., inner-body-here).

  • inner_html – Inner html to add to the HTML component. Defaults to None.

  • properties – Dictionary for properties to initialize the component with.

  • props – Just same as properties argument (added for simplicity).

  • style – Dictionary for style to initialize the component with.

  • event_handlers – Events to automatically bind. Each dictionary maps an event name to its handler and may include additional keyword arguments forwarded to bind(), e.g. event_handlers=[{"click": on_button_click, **extra_kwargs}].

  • **kwargs – Extra keyword arguments

Raises:

HtmlComponentError – If ‘element’ is not a string or ‘inner_html’ is set but ‘accept_inner_html’ is False.

get_element() str[source]

Returns the HTML tag for the component.

on_create() None[source]
class duck.html.components.select.Select(element: Optional[str] = None, properties: Optional[Dict[str, str]] = None, props: Optional[Dict[str, str]] = None, style: Optional[Dict[str, str]] = None, inner_html: Optional[Union[str, str, float]] = None, children: Optional[List[duck.html.components.HtmlComponent]] = None, **kwargs)[source]

Bases: duck.html.components.InnerComponent

A reusable HTML <select> component for creating dropdown menus.

This component generates a customizable <select> dropdown with options.

Parameters:
  • name – Optional. The name attribute for the select field.

  • options – Optional. A list of options, where each item is either a string/int/float (used as the option text), a dict of Option constructor kwargs, or an Option component instance.

Styling:

  • Uses default styling based on the Theme.current class, falling back to sensible defaults when the active theme doesn’t define them.

  • Can be overridden per-instance via the style kwarg, or with CSS.

Initialization

Initialize an HTML component.

Parameters:
  • element – The HTML element tag name (e.g., textarea, input, button). Can be None, but make sure element is returned by get_element method.

  • accept_inner_html – Whether the HTML component accepts an inner body (e.g., inner-body-here).

  • inner_html – Inner html to add to the HTML component. Defaults to None.

  • properties – Dictionary for properties to initialize the component with.

  • props – Just same as properties argument (added for simplicity).

  • style – Dictionary for style to initialize the component with.

  • event_handlers – Events to automatically bind. Each dictionary maps an event name to its handler and may include additional keyword arguments forwarded to bind(), e.g. event_handlers=[{"click": on_button_click, **extra_kwargs}].

  • **kwargs – Extra keyword arguments

Raises:

HtmlComponentError – If ‘element’ is not a string or ‘inner_html’ is set but ‘accept_inner_html’ is False.

DEFAULT_STYLE

None

get_element() str[source]

Returns the HTML tag for the component.

on_create() None[source]

Initializes the component with default styles and options.

to_option(option) duck.html.components.select.Option[source]

Normalizes a raw option value into an Option component.

Parameters:

option – A string/int/float, a two-item tuple/list of (value, text), a dict of Option kwargs, or an existing Option component.

Returns:

An Option component.

Raises:

ComponentError – If option isn’t one of the supported types, or a tuple/list isn’t exactly two items.