Case#

To select one of the texts depending on some condition you should use Case.

The condition can be either a data key, magic-filter or a function. The result of selector is check against provided texts dictionary.

You can use ... (Ellipsis object) as a key to provide default text which is used when no suitable options found.

Code example:

from typing import Dict

from aiogram_dialog import DialogManager
from aiogram_dialog.widgets.text import Case, Const, Format


# let's assume this is our window data getter
async def get_data(**kwargs):
    return {"color": "red", "number": 42}


# This will produce text `Square`
text = Case(
    {
        "red": Const("Square"),
        "green": Const("Unicorn"),
        "blue": Const("Moon"),
        ...: Const("Unknown creature"),
    },
    selector="color",
)


# This one will produce text `42 is even!`
def parity_selector(data: Dict, case: Case, manager: DialogManager):
    return data["number"] % 2


text2 = Case(
    {
        0: Format("{number} is even!"),
        1: Const("It is Odd"),
    },
    selector=parity_selector,
)
class aiogram_dialog.widgets.text.Case(texts, selector, when=None)#
Parameters:
  • texts (Dict[Any, Text]) –

  • selector (str | Callable[[Dict, Case, DialogManager], Hashable] | MagicFilter) –

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

__init__(texts, selector, when=None)#
Parameters:
  • texts (Dict[Any, Text]) –

  • selector (str | Callable[[Dict, Case, DialogManager], Hashable] | MagicFilter) –

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