Select#

Select acts like a group of buttons but data is provided dynamically. It is mainly intended to use for selection a item from a list.

Normally text of selection buttons is dynamic (e.g. Format). During rendering an item text, it is passed a dictionary with:

  • item - current item itself

  • data - original window data

  • pos - position of item in current items list starting from 1

  • pos0 - position starting from 0

So the main required thing is items. You have 4 options to specify this parameter:

  • String with key in your window data (e.g. items="fruits"`). The value by this key must be a collection of any objects.

  • Static list of items (e.g. items=['apple', 'banana', 'orange']).

  • Magic filter (e.g. items=F["fruits"]). Filter has to return collection of elements.

  • Function with one parameter (data: Dict) that returns collection of elements (e.g. items=lambda d: d["fruits"])

Next important thing is ids. Besides a widget id you need a function which can return id (string or integer type) for any item.

import operator
from typing import Any

from aiogram.types import CallbackQuery

from aiogram_dialog import DialogManager
from aiogram_dialog.widgets.kbd import Select
from aiogram_dialog.widgets.text import Format


# let's assume this is our window data getter
async def get_data(**kwargs):
    fruits = [
        ("Apple", '1'),
        ("Pear", '2'),
        ("Orange", '3'),
        ("Banana", '4'),
    ]
    return {
        "fruits": fruits,
        "count": len(fruits),
    }


async def on_fruit_selected(callback: CallbackQuery, widget: Any,
                            manager: DialogManager, item_id: str):
    print("Fruit selected: ", item_id)


fruits_kbd = Select(
    Format("{item[0]} ({pos}/{data[count]})"),  # E.g `✓ Apple (1/4)`
    id="s_fruits",
    item_id_getter=operator.itemgetter(1),
    # each item is a tuple with id on a first position
    items="fruits",  # we will use items from window data at a key `fruits`
    on_click=on_fruit_selected,
)
../../../_images/select.png

Note

Select places everything in single row. If it is not suitable for your case - simply wrap it with Group or Column

Classes#

class aiogram_dialog.widgets.kbd.select.OnItemClick(*args, **kwargs)#
abstract async __call__(event, select, dialog_manager, data, /)#

Call self as a function.

Parameters:
  • event (CallbackQuery) –

  • select (ManagedT) –

  • dialog_manager (DialogManager) –

  • data (T) –

class aiogram_dialog.widgets.kbd.Select(text, id, item_id_getter, items, type_factory=<class 'str'>, on_click=None, when=None)#
Parameters:
  • text (Text) –

  • id (str) –

  • item_id_getter (Callable[[Any], str | int]) –

  • items (str | Callable[[Dict], Sequence] | MagicFilter | Sequence) –

  • type_factory (Callable[[str], T]) –

  • on_click (OnItemClick[Select[T], T] | WidgetEventProcessor | None) –

  • when (str | MagicFilter | Predicate | None) –

__init__(text, id, item_id_getter, items, type_factory=<class 'str'>, on_click=None, when=None)#
Parameters:
  • text (Text) –

  • id (str) –

  • item_id_getter (Callable[[Any], str | int]) –

  • items (str | Callable[[Dict], Sequence] | MagicFilter | Sequence) –

  • type_factory (Callable[[str], T]) –

  • on_click (OnItemClick[Select[T], T] | WidgetEventProcessor | None) –

  • when (str | MagicFilter | Predicate | None) –