Case#

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

To use this widget you have to specify at least two parameters:

  • texts - dictionary, which contains options

  • selector - expression wich is used to get the condition value that will be used to select which option of Case widget to show.

selector can be:

  • string key - this string will be used as a key of data dictionary to get the value of condition

  • magic-filter (F) - the result of resolving mafic-filter will be used as a condition

  • function - the result of this function will be used as a condition`

Note

selector uses dynamic data. You need to either save this data in dialog_data beforehand or use getter to pass data to the widget (see passing data).

You can use ... (Ellipsis object) as a key in texts dictionary to provide default option which will be used if no suitable options found.

Code example:

from typing import Any, Dict

from magic_filter import F

from aiogram.filters.state import StatesGroup, State

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

class MySG(StatesGroup):
    window1 = State()
    window2 = State()

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


# Using string selector as a `selector`.
# The value of data["color"] will be used to select wich option of ``Case`` widget to show.
#
# `text` will produce text `Square`
text = Case(
    {
        "red": Const("Square"),
        "green": Const("Unicorn"),
        "blue": Const("Moon"),
        ...: Const("Unknown creature"),
    },
    selector="color",
)


# Using function as a `selector`.
# The result of this function will be used to select wich option of ``Case`` widget to show.
#
# `text2` 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,
)


# Using F-filters as a selector.
# The value of data["dialog_data"]["user"]["test_result"] will be used to select wich option
# of ``Case`` widget to show.
#
# `text3` will produce text `Great job!`
text3 = Case(
    {
        True: Const("Great job!"),
        False: Const("Try again.."),
    },
    selector=F["dialog_data"]["user"]["test_result"],
)


async def on_dialog_start(start_data: Any, manager: DialogManager):
    manager.dialog_data['user'] = {
        'test_result': True,
    }


dialog = Dialog(
    Window(
        text,
        text2,
        text3,
        state=MySG.window1,
        getter=get_data
    ),
    on_start=on_dialog_start
)

Result:

../../../_images/case.png
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) –