Skip to content

Commit 40bf33f

Browse files
authored
Merge pull request #161 from mwcraig/broken-links-to-guide
Add copies of layout reference guides to layout folder
2 parents 70dfe71 + fece9d2 commit 40bf33f

File tree

2 files changed

+722
-0
lines changed

2 files changed

+722
-0
lines changed
Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## The Flexbox layout\n",
8+
"\n",
9+
"The `HBox` and `VBox` classes above are special cases of the `Box` widget.\n",
10+
"\n",
11+
"The `Box` widget enables the entire CSS flexbox spec as well as the Grid layout spec, enabling rich reactive layouts in the Jupyter notebook. It aims at providing an efficient way to lay out, align and distribute space among items in a container.\n",
12+
"\n",
13+
"Again, the whole flexbox spec is exposed via the `layout` attribute of the container widget (`Box`) and the contained items. One may share the same `layout` attribute among all the contained items.\n",
14+
"\n",
15+
"### Acknowledgement\n",
16+
"\n",
17+
"The following flexbox tutorial on the flexbox layout follows the lines of the article [A Complete Guide to Flexbox](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) by Chris Coyier, and uses text and various images from the article [with permission](https://css-tricks.com/license/).\n",
18+
"\n",
19+
"### Basics and terminology\n",
20+
"\n",
21+
"Since flexbox is a whole module and not a single property, it involves a lot of things including its whole set of properties. Some of them are meant to be set on the container (parent element, known as \"flex container\") whereas the others are meant to be set on the children (known as \"flex items\").\n",
22+
"If regular layout is based on both block and inline flow directions, the flex layout is based on \"flex-flow directions\". Please have a look at this figure from the specification, explaining the main idea behind the flex layout.\n",
23+
"\n",
24+
"![Flexbox](images/flexbox.png)\n",
25+
"\n",
26+
"Basically, items will be laid out following either the `main axis` (from `main-start` to `main-end`) or the `cross axis` (from `cross-start` to `cross-end`).\n",
27+
"\n",
28+
"- `main axis` - The main axis of a flex container is the primary axis along which flex items are laid out. Beware, it is not necessarily horizontal; it depends on the flex-direction property (see below).\n",
29+
"- `main-start | main-end` - The flex items are placed within the container starting from main-start and going to main-end.\n",
30+
"- `main size` - A flex item's width or height, whichever is in the main dimension, is the item's main size. The flex item's main size property is either the ‘width’ or ‘height’ property, whichever is in the main dimension.\n",
31+
"cross axis - The axis perpendicular to the main axis is called the cross axis. Its direction depends on the main axis direction.\n",
32+
"- `cross-start | cross-end` - Flex lines are filled with items and placed into the container starting on the cross-start side of the flex container and going toward the cross-end side.\n",
33+
"- `cross size` - The width or height of a flex item, whichever is in the cross dimension, is the item's cross size. The cross size property is whichever of ‘width’ or ‘height’ that is in the cross dimension.\n",
34+
"\n",
35+
"### Properties of the parent\n",
36+
"\n",
37+
"![Container](images/flex-container.svg)\n",
38+
"\n",
39+
"\n",
40+
"#### display\n",
41+
"\n",
42+
"`display` can be `flex` or `inline-flex`. This defines a flex container (block or inline).\n",
43+
"\n",
44+
"#### flex-flow\n",
45+
"\n",
46+
"`flex-flow` is a shorthand for the `flex-direction` and `flex-wrap` properties, which together define the flex container's main and cross axes. Default is `row nowrap`.\n",
47+
"\n",
48+
"- `flex-direction` (column-reverse | column | row | row-reverse )\n",
49+
"\n",
50+
" This establishes the main-axis, thus defining the direction flex items are placed in the flex container. Flexbox is (aside from optional wrapping) a single-direction layout concept. Think of flex items as primarily laying out either in horizontal rows or vertical columns.\n",
51+
"![Direction](images/flex-direction1.svg)\n",
52+
"\n",
53+
"- `flex-wrap` (nowrap | wrap | wrap-reverse)\n",
54+
"\n",
55+
" By default, flex items will all try to fit onto one line. You can change that and allow the items to wrap as needed with this property. Direction also plays a role here, determining the direction new lines are stacked in.\n",
56+
"![Wrap](images/flex-wrap.svg)\n",
57+
"\n",
58+
"#### justify-content\n",
59+
"\n",
60+
"`justify-content` can be one of `flex-start`, `flex-end`, `center`, `space-between`, `space-around`. This defines the alignment along the main axis. It helps distribute extra free space left over when either all the flex items on a line are inflexible, or are flexible but have reached their maximum size. It also exerts some control over the alignment of items when they overflow the line.\n",
61+
" ![Justify](images/justify-content.svg)\n",
62+
"\n",
63+
"#### align-items\n",
64+
"\n",
65+
"`align-items` can be one of `flex-start`, `flex-end`, `center`, `baseline`, `stretch`. This defines the default behaviour for how flex items are laid out along the cross axis on the current line. Think of it as the justify-content version for the cross-axis (perpendicular to the main-axis).\n",
66+
" ![Items](images/align-items.svg)\n",
67+
" \n",
68+
"#### align-content\n",
69+
"`align-content` can be one of `flex-start`, `flex-end`, `center`, `baseline`, `stretch`. This aligns a flex container's lines within when there is extra space in the cross-axis, similar to how justify-content aligns individual items within the main-axis.\n",
70+
"![Items](images/align-content.svg)\n",
71+
"\n",
72+
"**Note**: this property has no effect when there is only one line of flex items.\n",
73+
"\n",
74+
"### Properties of the items\n",
75+
"\n",
76+
"![Item](images/flex-items.svg)\n",
77+
"\n",
78+
"The flexbox-related CSS properties of the items have no impact if the parent element is not a flexbox container (i.e. has a `display` attribute equal to `flex` or `inline-flex`).\n",
79+
"\n",
80+
"#### order\n",
81+
"By default, flex items are laid out in the source order. However, the `order` property controls the order in which they appear in the flex container.\n",
82+
"<img src=\"./images/order-2.svg\" alt=\"Order\" style=\"width: 500px;\"/>\n",
83+
" \n",
84+
"#### flex\n",
85+
"`flex` is shorthand for three properties, `flex-grow`, `flex-shrink` and `flex-basis` combined. The second and third parameters (`flex-shrink` and `flex-basis`) are optional. Default is `0 1 auto`.\n",
86+
" \n",
87+
" - `flex-grow`\n",
88+
"\n",
89+
" This defines the ability for a flex item to grow if necessary. It accepts a unitless value that serves as a proportion. It dictates what amount of the available space inside the flex container the item should take up.\n",
90+
"\n",
91+
" If all items have flex-grow set to 1, the remaining space in the container will be distributed equally to all children. If one of the children a value of 2, the remaining space would take up twice as much space as the others (or it will try to, at least).\n",
92+
" ![Grow](images/flex-grow.svg)\n",
93+
"\n",
94+
" - `flex-shrink`\n",
95+
"\n",
96+
" This defines the ability for a flex item to shrink if necessary.\n",
97+
"\n",
98+
" - `flex-basis`\n",
99+
"\n",
100+
" This defines the default size of an element before the remaining space is distributed. It can be a length (e.g. `20%`, `5rem`, etc.) or a keyword. The `auto` keyword means *\"look at my width or height property\"*.\n",
101+
" \n",
102+
"#### align-self\n",
103+
"\n",
104+
"`align-self` allows the default alignment (or the one specified by align-items) to be overridden for individual flex items.\n",
105+
"\n",
106+
"![Align](images/align-self.svg)\n",
107+
"\n",
108+
"### The VBox and HBox helpers\n",
109+
"\n",
110+
"The `VBox` and `HBox` helper classes provide simple defaults to arrange child widgets in vertical and horizontal boxes. They are roughly equivalent to:\n",
111+
"\n",
112+
"```Python\n",
113+
"def VBox(*pargs, **kwargs):\n",
114+
" \"\"\"Displays multiple widgets vertically using the flexible box model.\"\"\"\n",
115+
" box = Box(*pargs, **kwargs)\n",
116+
" box.layout.display = 'flex'\n",
117+
" box.layout.flex_flow = 'column'\n",
118+
" box.layout.align_items = 'stretch'\n",
119+
" return box\n",
120+
"\n",
121+
"def HBox(*pargs, **kwargs):\n",
122+
" \"\"\"Displays multiple widgets horizontally using the flexible box model.\"\"\"\n",
123+
" box = Box(*pargs, **kwargs)\n",
124+
" box.layout.display = 'flex'\n",
125+
" box.layout.align_items = 'stretch'\n",
126+
" return box\n",
127+
"```\n",
128+
"\n",
129+
"\n",
130+
"### Examples"
131+
]
132+
},
133+
{
134+
"cell_type": "markdown",
135+
"metadata": {},
136+
"source": [
137+
"**Four buttons in a VBox. Items stretch to the maximum width, in a vertical box taking `50%` of the available space.**"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {},
144+
"outputs": [],
145+
"source": [
146+
"from ipywidgets import Layout, Button, Box\n",
147+
"\n",
148+
"items_layout = Layout( width='auto') # override the default width of the button to 'auto' to let the button grow\n",
149+
"\n",
150+
"box_layout = Layout(display='flex',\n",
151+
" flex_flow='column', \n",
152+
" align_items='stretch', \n",
153+
" border='solid',\n",
154+
" width='50%')\n",
155+
"\n",
156+
"words = ['correct', 'horse', 'battery', 'staple']\n",
157+
"items = [Button(description=word, layout=items_layout, button_style='danger') for word in words]\n",
158+
"box = Box(children=items, layout=box_layout)\n",
159+
"box"
160+
]
161+
},
162+
{
163+
"cell_type": "markdown",
164+
"metadata": {},
165+
"source": [
166+
"**Three buttons in an HBox. Items flex proportionally to their weight.**"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": null,
172+
"metadata": {},
173+
"outputs": [],
174+
"source": [
175+
"from ipywidgets import Layout, Button, Box, VBox\n",
176+
"\n",
177+
"# Items flex proportionally to the weight and the left over space around the text \n",
178+
"items_auto = [\n",
179+
" Button(description='weight=1; auto', layout=Layout(flex='1 1 auto', width='auto'), button_style='danger'),\n",
180+
" Button(description='weight=3; auto', layout=Layout(flex='3 1 auto', width='auto'), button_style='danger'),\n",
181+
" Button(description='weight=1; auto', layout=Layout(flex='1 1 auto', width='auto'), button_style='danger'),\n",
182+
" ]\n",
183+
"\n",
184+
"# Items flex proportionally to the weight \n",
185+
"items_0 = [\n",
186+
" Button(description='weight=1; 0%', layout=Layout(flex='1 1 0%', width='auto'), button_style='danger'),\n",
187+
" Button(description='weight=3; 0%', layout=Layout(flex='3 1 0%', width='auto'), button_style='danger'),\n",
188+
" Button(description='weight=1; 0%', layout=Layout(flex='1 1 0%', width='auto'), button_style='danger'),\n",
189+
" ]\n",
190+
"box_layout = Layout(display='flex',\n",
191+
" flex_flow='row', \n",
192+
" align_items='stretch', \n",
193+
" width='70%')\n",
194+
"box_auto = Box(children=items_auto, layout=box_layout)\n",
195+
"box_0 = Box(children=items_0, layout=box_layout)\n",
196+
"VBox([box_auto, box_0])"
197+
]
198+
},
199+
{
200+
"cell_type": "markdown",
201+
"metadata": {},
202+
"source": [
203+
"**A more advanced example: a reactive form.**\n",
204+
"\n",
205+
"The form is a `VBox` of width '50%'. Each row in the VBox is an HBox, that justifies the content with space between.."
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": null,
211+
"metadata": {},
212+
"outputs": [],
213+
"source": [
214+
"from ipywidgets import Layout, Button, Box, FloatText, Textarea, Dropdown, Label, IntSlider\n",
215+
"\n",
216+
"form_item_layout = Layout(\n",
217+
" display='flex',\n",
218+
" flex_flow='row',\n",
219+
" justify_content='space-between'\n",
220+
")\n",
221+
"\n",
222+
"form_items = [\n",
223+
" Box([Label(value='Age of the captain'), IntSlider(min=40, max=60)], layout=form_item_layout),\n",
224+
" Box([Label(value='Egg style'), \n",
225+
" Dropdown(options=['Scrambled', 'Sunny side up', 'Over easy'])], layout=form_item_layout),\n",
226+
" Box([Label(value='Ship size'), \n",
227+
" FloatText()], layout=form_item_layout),\n",
228+
" Box([Label(value='Information'), \n",
229+
" Textarea()], layout=form_item_layout)\n",
230+
"]\n",
231+
"\n",
232+
"form = Box(form_items, layout=Layout(\n",
233+
" display='flex',\n",
234+
" flex_flow='column',\n",
235+
" border='solid 2px',\n",
236+
" align_items='stretch',\n",
237+
" width='50%'\n",
238+
"))\n",
239+
"form"
240+
]
241+
},
242+
{
243+
"cell_type": "markdown",
244+
"metadata": {},
245+
"source": [
246+
"**A more advanced example: a carousel.**"
247+
]
248+
},
249+
{
250+
"cell_type": "code",
251+
"execution_count": null,
252+
"metadata": {},
253+
"outputs": [],
254+
"source": [
255+
"from ipywidgets import Layout, Button, Box, Label\n",
256+
"\n",
257+
"item_layout = Layout(height='100px', min_width='40px')\n",
258+
"items = [Button(layout=item_layout, description=str(i), button_style='warning') for i in range(40)]\n",
259+
"box_layout = Layout(overflow_x='scroll',\n",
260+
" border='3px solid black',\n",
261+
" width='500px',\n",
262+
" height='',\n",
263+
" flex_flow='row',\n",
264+
" display='flex')\n",
265+
"carousel = Box(children=items, layout=box_layout)\n",
266+
"VBox([Label('Scroll horizontally:'), carousel])"
267+
]
268+
},
269+
{
270+
"cell_type": "markdown",
271+
"metadata": {},
272+
"source": [
273+
"#### *Compatibility note*\n",
274+
"\n",
275+
"The `overflow_x` and `overflow_y` options are deprecated in ipywidgets `7.5`. Instead, use the shorthand property `overflow='scroll hidden'`. The first part specificies overflow in `x`, the second the overflow in `y`."
276+
]
277+
},
278+
{
279+
"cell_type": "markdown",
280+
"metadata": {},
281+
"source": [
282+
"## A widget for exploring layout options\n",
283+
"\n",
284+
"The widgets below was written by ipywidgets user [Doug Redden (@DougRzz)](https://github.com/DougRzz).\n",
285+
"\n",
286+
"Use the dropdowns and sliders in the widget to change the layout of the box containing the five colored buttons. Many of the CSS layout optoins described above are available, and the Python code to generate a `Layout` object reflecting the settings is in a `TextArea` in the widget."
287+
]
288+
},
289+
{
290+
"cell_type": "code",
291+
"execution_count": null,
292+
"metadata": {},
293+
"outputs": [],
294+
"source": [
295+
"from layout_preview import layout\n",
296+
"layout"
297+
]
298+
}
299+
],
300+
"metadata": {
301+
"kernelspec": {
302+
"display_name": "widgets-tutorial",
303+
"language": "python",
304+
"name": "widgets-tutorial"
305+
},
306+
"language_info": {
307+
"codemirror_mode": {
308+
"name": "ipython",
309+
"version": 3
310+
},
311+
"file_extension": ".py",
312+
"mimetype": "text/x-python",
313+
"name": "python",
314+
"nbconvert_exporter": "python",
315+
"pygments_lexer": "ipython3",
316+
"version": "3.8.3"
317+
}
318+
},
319+
"nbformat": 4,
320+
"nbformat_minor": 4
321+
}

0 commit comments

Comments
 (0)