Filter Dialog#

bs.ask_filter() opens a multi-select dialog and returns the chosen values. FilterDialog gives the same dialog as a reusable object.

Filter Dialog — light theme Filter Dialog — dark theme

Usage#

Basic usage#

Pass a list of strings. ask_filter() returns the list of checked values, or None if the user cancels.

countries = ["Australia", "Canada", "France", "Germany", "UK", "USA"]
selected = bs.ask_filter(countries, title="Filter Countries")

Search and select-all#

Enable the search box and/or the “Select All” checkbox:

selected = bs.ask_filter(
    countries,
    title="Filter Countries",
    enable_search=True,
    enable_select_all=True,
)

Dict items#

Pass dicts to separate display labels from returned values, or to pre-check some items:

items = [
    {"text": "Active",   "value": "active",   "selected": True},
    {"text": "Inactive", "value": "inactive"},
    {"text": "Pending",  "value": "pending",  "selected": True},
]
selected = bs.ask_filter(items, title="Status Filter")
# returns e.g. ['active', 'pending']

Each item dict supports:

Key

Description

text

Display label (required).

value

Value returned when checked. Defaults to text.

selected

Initial check state. Defaults to False.

Reusable dialog object#

Use FilterDialog to inspect the result after show():

from bootstack.dialogs import FilterDialog

dlg = FilterDialog(
    title="Status Filter",
    items=items,
    enable_select_all=True,
)
dlg.show()

if dlg.result is not None:
    apply_filter(dlg.result)

See also#

Input Dialogsask_item() for choosing a single item from a list.

DialogDialog for fully custom dialog layouts.

API#

The complete reference for ask_filter() and FilterDialog lives on the Dialogs API page. At a glance:

ask_filter

Show a multi-select filter dialog.

FilterDialog

A dialog for selecting multiple items from a list.

Full Example#

 1
 2from bootstack.dialogs import FilterDialog
 3COUNTRIES = ["Australia", "Canada", "France", "Germany", "India",
 4             "Japan", "Mexico", "Spain", "UK", "USA"]
 5
 6STATUS_ITEMS = [
 7    {"text": "Active",   "value": "active",   "selected": True},
 8    {"text": "Inactive", "value": "inactive", "selected": False},
 9    {"text": "Pending",  "value": "pending",  "selected": True},
10    {"text": "Archived", "value": "archived", "selected": False},
11]
12
13def show_basic():
14    result = bs.ask_filter(COUNTRIES, title="Filter Countries")
15    if result is not None:
16        print(f"Selected: {result}")
17
18def show_with_search():
19    result = bs.ask_filter(
20        COUNTRIES,
21        title="Filter Countries",
22        enable_search=True,
23        enable_select_all=True,
24    )
25    if result is not None:
26        print(f"Selected: {result}")
27
28def show_dict_items():
29    dlg = FilterDialog(
30        title="Filter by Status",
31        items=STATUS_ITEMS,
32        enable_select_all=True,
33    )
34    dlg.show()
35    if dlg.result is not None:
36        print(f"Selected: {dlg.result}")
37
38with bs.App(title="Filter Dialog", size=(680, 160), padding=20, gap=16) as app:
39
40    bs.Label("Filter Dialog", font="heading-sm")
41    with bs.HStack(gap=8):
42        bs.Button("Basic",            on_click=show_basic)
43        bs.Button("Search and select all", on_click=show_with_search)
44        bs.Button("Dict items",       on_click=show_dict_items)
45
46app.run()