##// END OF EJS Templates
Cleanup examples...
Cleanup examples - Fixed output numbering. - Multiple typo fixes. - Added index notebook. - Added easy navigation to tutorials (next and back links).

File last commit:

r14724:796c0edb
r14724:796c0edb
Show More
Part 5 - Alignment.ipynb
326 lines | 10.2 KiB | text/plain | TextLexer
/ examples / widgets / Part 5 - Alignment.ipynb

[< Back to Part 4](Part 4 - Styles.ipynb) or Index

In [1]:
from IPython.html import widgets # Widget definitions
from IPython.display import display # Used to display widgets in the notebook

Alignment

Most widgets have a description property which allows a label for the widget to be defined. The label of the widget has a fixed minimum width. The text of the label is always right aligned and the widget is left aligned (as seen below)

In [2]:
display(widgets.TextBoxWidget(description="a:"))
display(widgets.TextBoxWidget(description="aa:"))
display(widgets.TextBoxWidget(description="aaa:"))

If a label is longer than the minimum width, the widget is shifted to the right (as seen below).

In [3]:
display(widgets.TextBoxWidget(description="a:"))
display(widgets.TextBoxWidget(description="aa:"))
display(widgets.TextBoxWidget(description="aaa:"))
display(widgets.TextBoxWidget(description="aaaaaaaaaaaaaaaaaa:"))

If a description is not set for the widget, the label is not displayed (as seen below).

In [4]:
display(widgets.TextBoxWidget(description="a:"))
display(widgets.TextBoxWidget(description="aa:"))
display(widgets.TextBoxWidget(description="aaa:"))
display(widgets.TextBoxWidget())

Custom Alignment

ContainerWidgets allow for custom alignment of widgets. The hbox and vbox DOM classes cause the ContainerWidget to both horizontally and vertically align its children. The following example compares vbox to hbox.

In [5]:
child_style = {
    'background': '#77CC77',
    'padding': '25px',
    'margin': '5px',
    'font-size': 'xx-large',
    'color': 'white',
}

def make_container(title):
    header = widgets.LatexWidget(value=title) 
    display(header)
    header.set_css({
        'font-size': '30pt',
        'margin-top': '40pt',
        'margin-bottom': '20pt',
    })
    
    container = widgets.ContainerWidget()
    container.set_css('background', '#999999')
    display(container)
    return container

def fill_container(container):
    components = []
    for i in range(3):
        components.append(widgets.LatexWidget(value="ABC"[i]))
        components[i].set_css(child_style)
    container.children = components
    
container = make_container('VBox')
container.add_class('vbox')
fill_container(container)

container = make_container('HBox')
container.add_class('hbox')
fill_container(container)

The start, center, and end DOM classes adjust the alignment of the widgets on the axis that they are being rendered on. Below is an example of the different alignments.

In [6]:
container = make_container('HBox Pack Start')
container.add_class('hbox')
container.add_class('start')
fill_container(container)
    
container = make_container('HBox Pack Center')
container.add_class('hbox')
container.add_class('center')
fill_container(container)
    
container = make_container('HBox Pack End')
container.add_class('hbox')
container.add_class('end')
fill_container(container)

The box-flex0, box-flex1, and box-flex2 DOM classes modify the container's flexibility. Changing a container flexibility affects how and if the container will occupy the remaining space. Applying box-flex0 has the same result as not applying flex. Below is an example of different flex configurations. The number on the boxes correspond to the applied flex.

In [7]:
def fill_container(container, flexes):
    components = []
    for i in range(len(flexes)):
        components.append(widgets.ContainerWidget())
        components[i].set_css(child_style)
        
        label = widgets.LatexWidget(value=str(flexes[i]))
        components[i].children = [label]
    container.children = components
        
    for i in range(len(flexes)):
        if flexes[i] == 0:
            components[i].add_class('box-flex0')
        elif flexes[i] == 1:
            components[i].add_class('box-flex1')
        elif flexes[i] == 2:
            components[i].add_class('box-flex2')
        
container = make_container('Different Flex Configurations')
container.add_class('hbox')
fill_container(container, [0, 0, 0])
        
container = make_container('')
container.add_class('hbox')
fill_container(container, [0, 0, 1])
        
container = make_container('')
container.add_class('hbox')
fill_container(container, [0, 1, 1])
        
container = make_container('')
container.add_class('hbox')
fill_container(container, [0, 2, 2])
        
container = make_container('')
container.add_class('hbox')
fill_container(container, [0, 1, 2])
        
container = make_container('')
container.add_class('hbox')
fill_container(container, [1, 1, 2])

The align_start, align_center, and align_end DOM classes adjust the alignment of the widgets on the axis perpindicular to the one that they are being rendered on. Below is an example of the different alignments.

In [8]:
def fill_container(container):
    components = []
    for i in range(3):
        components.append(widgets.LatexWidget(parent=container, value="ABC"[i]))
        components[i].set_css(child_style)
        components[i].set_css('height', str((i+1) * 50) + 'px')
    container.children = components

container = make_container('HBox Align Start')
container.add_class("hbox")
container.add_class("align-start")
fill_container(container)
    
container = make_container('HBox Align Center')
container.add_class("hbox")
container.add_class("align-center")
fill_container(container)
    
container = make_container('HBox Align End')
container.add_class("hbox")
container.add_class("align-end")
fill_container(container)

By default the widget area is a vbox; however, there are many uses for a hbox. The example below uses a hbox to display a set of vertical sliders, like an equalizer.

In [9]:
container = widgets.ContainerWidget()
container.children=[widgets.FloatSliderWidget(orientation='vertical', description=str(i+1), value=50.0) 
                    for i in range(15)]
display(container)
container.add_class('hbox')

In [Part 6](Part 6 - Custom Widget.ipynb) of this series, you will learn how to create your own custom widget.