Conditional Example

An example of using Conditional to generate the views.

This example shows how Conditional can be used to generate views for objects which are not known until runtime.

Tip

To see this example in action, download it from conditional and run:

$ enaml-run conditional.enaml

Screenshot

../_images/ex_conditional.png

Example Enaml Code

#------------------------------------------------------------------------------
# Copyright (c) 2013-2024, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
""" An example of using Conditional to generate the views.

This example shows how Conditional can be used to generate views for
objects which are not known until runtime.

<< autodoc-me >>
"""
import random

from enaml.core.api import Conditional, Looper
from enaml.widgets.api import (
    Window, Container, Field, PushButton, CheckBox, Separator, ScrollArea
)

class Foo(object):
    text = 'foo'


class Bar(object):
    text = 'bar'


class Baz(object):
    text = 'baz'


enamldef FooView(Container):
    attr model: Foo
    PushButton: text = model.text
    PushButton: text = model.text
    PushButton: text = model.text


enamldef BarView(Container):
    attr model: Bar
    Field: text = model.text
    Field: text = model.text
    Field: text = model.text


enamldef BazView(Container):
    attr model: Baz
    CheckBox: text = model.text
    CheckBox: text = model.text
    CheckBox: text = model.text


CLASSES = [Foo, Bar, Baz]
def generate(n):
    return [random.choice(CLASSES)() for _ in range(n)]


enamldef Main(Window): main:
    attr models = generate(5)
    Container:
        CheckBox: show_bar:
            text = 'Show Bar Views'
            checked = True
        PushButton:
            text = 'Regenerate Models'
            clicked :: main.models = generate(random.randint(1, 10))
        ScrollArea:
            Container:
                Looper:
                    iterable << models
                    Conditional:
                        condition = isinstance(loop.item, Foo)
                        FooView:
                            model = loop.item
                    Conditional:
                        condition << isinstance(loop.item, Bar) and show_bar.checked
                        BarView:
                            model = loop.item
                    Conditional:
                        condition = isinstance(loop.item, Baz)
                        BazView:
                            model = loop.item
                    Conditional:
                        condition << (
                            (loop.index != len(main.models) - 1) and
                            (show_bar.checked if isinstance(loop.item, Bar) else True)
                        )
                        Separator:
                            orientation = 'horizontal'