Introducing templates for Jupyter widgets layouts

Bartosz Telenczuk
Jupyter Blog
Published in
4 min readJul 4, 2019

--

Notebooks come alive with Jupyter widgets, which allow users to produce interactive GUIs inline in the Jupyter notebook or JupyterLab.

You can either use them to add a few interactive controls and plots in notebooks or to create fully-fledged applications and interactive dashboards. Both can be built with components from the core built-in widgets such as buttons, sliders, and dropdowns, or with the rich ecosystem of custom widget libraries that built upon the Jupyter widgets framework, such as interactive maps with ipyleaflet or 2-D plots with bqplot. You can also combine several types of widgets together to create even richer applications.

Have you ever tried creating complex widget layouts with multiple widgets placed at specific locations? The preferred approach so far has been to use nested HBox and VBox widgets to compose your layout, which can make creating complex applications a tedious task. We now have a more flexible solution: the layout templates, which just landed with the latest release of the ipywidgets package.

The power of CSS, the simplicity of Python

Layout templates are a set of predefined layouts that allow you to combine multiple widgets on a single screen and arrange them visually. They leverage the powerful CSS Grid Layout specification, which is supported on most current browsers (yes, we are looking at you IE).

While the CSS Grid properties were first introduced in ipywidgets 7.3, they were tricky to use as they were transparently reflecting the CSS Grid Spec API and required the knowledge of the CSS. The new layout templates of ipywidgets wrap the CSS properties with a pythonic interface and sensible defaults, so they never expose the user to the nasty CSS spec. However, they inherit all the advantages of the Grid being fully responsive (they adapt to the screen size) and super easy to use!

Comparing the Python code with the generated CSS layout.

Application-like UIs in Jupyter

AppLayout consists of a header, two side panes, a central pane, and a footer.

If you want to create a simple application-like layout, you can use AppLayout, which consists of a header, a footer, two side panes, and a central pane. You can create the layout and populate it with widgets in a single command:

AppLayout(header=header,
left_sidebar=prev_button,
center=image,
right_sidebar=next_button,
footer=footer,
grid_gap='20px',
justify_items='center',
align_items='center')

Importantly, if your application does not need all the panes defined by AppLayout, the layout has also some sensible defaults so that it can automatically merge widget locations that were not assigned.

Widgets on a grid

GridspecLayout places widgets on a rectangular grid. A single widget can span several rows or columns (or both).

If you require more flexibility to arrange widgets, you can also try GridLayout. First, you define the dimensions of a rectangular grid. Then you can place widgets on the grid either in a single cell of the grid or spanning several rows or columns (or both). This is easily achieved using the same slice-based API that you already use to select items from a NumPy array (or Python lists). If you already know matplotlib’s GridSpec feature, the syntax may look familiar:

# create a 10x2 grid layout
grid = GridspecLayout(10, 2)
# fill it in with widgets
grid[:, 0] = map
grid[0, 1] = zoom_slider
grid[1, 1] = basemap_selector
grid[2:, 1] = fig
# set the widget properties
grid[:, 0].layout.height = 'auto'

Style me up

The layouts are very configurable and can be easily tuned to the needs of your application. To change the sizes of the layout and the grid intervals, you can use style attributes, such as height , width, and gap-size options:

AppLayout(grid_gap='20px',
height="200px",
width="50%")

The size units are directly inherited from the CSS standard. More examples of style attributes can be found in the documentation.

So please go ahead and install the pre-release of ipywidgets that includes this new feature (pip install --upgrade ipywidgets) and take the new layout templates for a spin. We are looking forward to your feedback!

Acknowledgments

The author is a seasoned Python developer and a data scientist. He loves contributing to open source software; among others he is the creator and maintainer of the svgutils library.

The development of the ipywidgets layout templates was kindly supported by QuantStack.

--

--

Data scientist, Python developer and ML engineer. Once a researcher in computational neuroscience he now helps companies to leverage data in their products.