ui.tree

Tree
main.py
from nicegui import ui

ui.tree([
    {'id': 'numbers', 'children': [{'id': '1'}, {'id': '2'}]},
    {'id': 'letters', 'children': [{'id': 'A'}, {'id': 'B'}]},
], label_key='id', on_select=lambda e: ui.notify(e.value))

ui.run()
NiceGUI
Tree with custom header and body
main.py
from nicegui import ui

tree = ui.tree([
    {'id': 'numbers', 'description': 'Just some numbers', 'children': [
        {'id': '1', 'description': 'The first number'},
        {'id': '2', 'description': 'The second number'},
    ]},
    {'id': 'letters', 'description': 'Some latin letters', 'children': [
        {'id': 'A', 'description': 'The first letter'},
        {'id': 'B', 'description': 'The second letter'},
    ]},
], label_key='id', on_select=lambda e: ui.notify(e.value))

tree.add_slot('default-header', '''
    <span :props="props">Node <strong>{{ props.node.id }}</strong></span>
''')
tree.add_slot('default-body', '''
    <span :props="props">Description: "{{ props.node.description }}"</span>
''')

ui.run()
NiceGUI
Tree with checkboxes
main.py
from nicegui import ui

ui.tree([
    {'id': 'A', 'children': [{'id': 'A1'}, {'id': 'A2'}]},
    {'id': 'B', 'children': [{'id': 'B1'}, {'id': 'B2'}]},
], label_key='id', tick_strategy='leaf', on_tick=lambda e: ui.notify(e.value))

ui.run()
NiceGUI
Expand/collapse programmatically
main.py
from nicegui import ui

t = ui.tree([
    {'id': 'A', 'children': [{'id': 'A1'}, {'id': 'A2'}], 'disabled': True},
    {'id': 'B', 'children': [{'id': 'B1'}, {'id': 'B2'}]},
], label_key='id').expand()

with ui.row():
    ui.button('+ all', on_click=t.expand)
    ui.button('- all', on_click=t.collapse)
    ui.button('+ A', on_click=lambda: t.expand(['A']))
    ui.button('- A', on_click=lambda: t.collapse(['A']))

ui.run()
NiceGUI
Select/deselect programmatically
main.py
from nicegui import ui

t = ui.tree([
    {'id': 'numbers', 'children': [{'id': '1'}, {'id': '2'}]},
    {'id': 'letters', 'children': [{'id': 'A'}, {'id': 'B'}]},
], label_key='id').expand()

with ui.row():
    ui.button('Select A', on_click=lambda: t.select('A'))
    ui.button('Deselect A', on_click=t.deselect)

ui.run()
NiceGUI
Tick/untick programmatically
main.py
from nicegui import ui

t = ui.tree([
    {'id': 'numbers', 'children': [{'id': '1'}, {'id': '2'}]},
    {'id': 'letters', 'children': [{'id': 'A'}, {'id': 'B'}]},
], label_key='id', tick_strategy='leaf').expand()

with ui.row():
    ui.button('Tick 1, 2 and B', on_click=lambda: t.tick(['1', '2', 'B']))
    ui.button('Untick 2 and B', on_click=lambda: t.untick(['2', 'B']))
with ui.row():
    ui.button('Tick all', on_click=t.tick)
    ui.button('Untick all', on_click=t.untick)

ui.run()
NiceGUI
Filter nodes
main.py
from nicegui import ui

t = ui.tree([
    {'id': 'fruits', 'children': [{'id': 'Apple'}, {'id': 'Banana'}]},
    {'id': 'vegetables', 'children': [{'id': 'Potato'}, {'id': 'Tomato'}]},
], label_key='id').expand()
ui.input('filter').bind_value_to(t, 'filter')

ui.run()
NiceGUI
Reference
Initializer
Properties

classes: 'Classes[Self]'

filter: BindableProperty

is_deleted: 'bool'

is_ignoring_events: 'bool'

props: 'Props[Self]'

style: 'Style[Self]'

visible: BindableProperty

Methods

add_resource(path: Union[str, Path]) -> None

add_slot(name: str, template: Optional[str] = None) -> Slot

ancestors(include_self: bool = False) -> Iterator[Element]

bind_filter(target_object: Any, target_name: str = 'filter', forward: Callable[..., Any] = [...], backward: Callable[..., Any] = [...]) -> Self

bind_filter_from(target_object: Any, target_name: str = 'filter', backward: Callable[..., Any] = [...]) -> Self

bind_filter_to(target_object: Any, target_name: str = 'filter', forward: Callable[..., Any] = [...]) -> Self

bind_visibility(target_object: Any, target_name: str = 'visible', forward: Callable[..., Any] = [...], backward: Callable[..., Any] = [...], value: Any = None) -> Self

bind_visibility_from(target_object: Any, target_name: str = 'visible', backward: Callable[..., Any] = [...], value: Any = None) -> Self

bind_visibility_to(target_object: Any, target_name: str = 'visible', forward: Callable[..., Any] = [...]) -> Self

clear() -> None

collapse(node_keys: Optional[List[str]] = None) -> Self

default_classes(add: Optional[str] = None, remove: Optional[str] = None, toggle: Optional[str] = None, replace: Optional[str] = None) -> type[Self]

default_props(add: Optional[str] = None, remove: Optional[str] = None) -> type[Self]

default_style(add: Optional[str] = None, remove: Optional[str] = None, replace: Optional[str] = None) -> type[Self]

delete() -> None

descendants(include_self: bool = False) -> Iterator[Element]

deselect() -> Self

expand(node_keys: Optional[List[str]] = None) -> Self

get_computed_prop(prop_name: str, timeout: float = 1) -> AwaitableResponse

mark(*markers: str) -> Self

move(target_container: Optional[Element] = None, target_index: int = -1, target_slot: Optional[str] = None) -> None

on(type: str, handler: Optional[events.Handler[events.GenericEventArguments]] = None, args: Union[None, Sequence[str], Sequence[Optional[Sequence[str]]]] = None, throttle: float = 0.0, leading_events: bool = True, trailing_events: bool = True, js_handler: Optional[str] = None) -> Self

on_expand(callback: Union[Callable[[nicegui.events.ValueChangeEventArguments], Any], Callable[[], Any]]) -> Self

on_select(callback: Union[Callable[[nicegui.events.ValueChangeEventArguments], Any], Callable[[], Any]]) -> Self

on_tick(callback: Union[Callable[[nicegui.events.ValueChangeEventArguments], Any], Callable[[], Any]]) -> Self

remove(element: Union[Element, int]) -> None

run_method(name: str, *args: Any, timeout: float = 1) -> AwaitableResponse

select(node_key: Optional[str]) -> Self

set_filter(filter_: str) -> None

set_visibility(visible: bool) -> None

tick(node_keys: Optional[List[str]] = None) -> Self

tooltip(text: str) -> Self

untick(node_keys: Optional[List[str]] = None) -> Self

update() -> None

Inheritance
  • FilterElement
  • Element
  • Visibility