{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [Layout and Styling of Jupyter widgets](06.00-Layout-and-Styling-Overview.ipynb) | [Contents](00.00-index.ipynb) | [OPTIONAL - Widget label styling](06.02-OPTIONAL-widget-label-styling.ipynb) >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Layout and Styling of Jupyter widgets\n", "\n", "This section presents how to layout and style Jupyter interactive widgets to build rich and *reactive* widget-based applications.\n", "\n", "Every Jupyter widget has two attributes to customize layout and styling of the widget. They are `layout` and `style` attributes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The `style` attribute\n", "\n", "The `style` attribute is used to expose non-layout related styling attributes of widgets. For most widgets, the only style that can be modified is `description_width`, which is the width of the description label for the widget.\n", "\n", "However, a few widgets have additional style settings, as described below." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Style Example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import Button, ButtonStyle\n", "b2 = Button(description='Custom color', style=dict(button_color='lightgreen'))\n", "b2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b2.style.button_color = 'yellow'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can get a list of the style attributes for a widget with the `keys` property." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b2.style.keys" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Like the `layout` attribute, widget styles can be assigned to other widgets." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b3 = Button(description='Another button', style=b2.style)\n", "b3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Widget styling attributes are specific to each widget type." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import IntSlider\n", "s1 = IntSlider(description='Blue handle')\n", "s1.style.handle_color = 'lightblue'\n", "s1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is a [list of all style keys](Table_of_widget_keys_and_style_keys.ipynb#Style-keys)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `button_style` and `bar_style` attributes\n", "These attributes let you style some widgets with pre-defined settings that are `theme aware`. These properties affect both background color and text color of widgets. These attributes are available for the widgets listed below. Available options for these styles are `success`, `info`, `warning`, `danger`. Buttons also have option `primary`\n", "- **button_style** is available for: Button, ToggleButton, ToggleButtons, FileUpload\n", "- **bar_style** is available for: FloatProgress, IntProgress" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b4 = Button(description='Yet another button')\n", "b4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b4.button_style = 'warning'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that setting the `style` of a button overrides the `button_style`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b4.style.button_color = 'red' # Makes the color red\n", "b4.button_style = 'success' # Does not turn the color green because the color has been explicitly set in the style" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `layout` attribute: the foundation of widget layout\n", "\n", "Jupyter interactive widgets have a `layout` attribute exposing a number of CSS properties that impact how widgets are laid out.\n", "\n", "### Exposed CSS properties\n", "\n", "