Debian Codegraph @

docs/source/layout.rst

raw
1 Layout
2 ======
3
4 Rich offers a :class:`~rich.layout.Layout` class which can be used to divide the screen area in to parts, where each part may contain independent content. It can be used with :ref:`Live` to create full-screen "applications" but may be used standalone.
5
6 To see an example of a Layout, run the following from the command line::
7
8 python -m rich.layout
9
10 Creating layouts
11 ----------------
12
13 To define a layout, construct a Layout object and print it::
14
15 from rich import print
16 from rich.layout import Layout
17
18 layout = Layout()
19 print(layout)
20
21 This will draw a box the size of the terminal with some information regarding the layout. The box is a "placeholder" because we have yet to add any content to it. Before we do that, let's create a more interesting layout by calling the :meth:`~rich.layout.Layout.split_column` method to divide the layout in to two sub-layouts::
22
23 layout.split_column(
24 Layout(name="upper"),
25 Layout(name="lower")
26 )
27 print(layout)
28
29 This will divide the terminal screen in to two equal sized portions, one on top of the other. The ``name`` attribute is an internal identifier we can use to look up the sub-layout later. Let's use that to create another split, this time we will call :meth:`~rich.layout.Layout.split_row` to split the lower layout in to a row of two sub-layouts::
30
31 layout["lower"].split_row(
32 Layout(name="left"),
33 Layout(name="right"),
34 )
35 print(layout)
36
37 You should now see the screen area divided in to 3 portions; an upper half and a lower half that is split in to two quarters.
38
39 .. raw:: html
40
41 <pre style="font-size:90%;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="color: #000080">╭─────────────────────────────── </span><span style="color: #008000">'upper'</span><span style="color: #000080"> </span><span style="color: #000080; font-weight: bold">(</span><span style="color: #000080; font-weight: bold">84</span><span style="color: #000080"> x </span><span style="color: #000080; font-weight: bold">13</span><span style="color: #000080; font-weight: bold">)</span><span style="color: #000080"> ────────────────────────────────╮</span>
42 <span style="color: #000080">│</span> <span style="color: #000080">│</span>
43 <span style="color: #000080">│</span> <span style="color: #000080">│</span>
44 <span style="color: #000080">│</span> <span style="color: #000080">│</span>
45 <span style="color: #000080">│</span> <span style="color: #000080">│</span>
46 <span style="color: #000080">│</span> <span style="color: #000080">│</span>
47 <span style="color: #000080">│</span> <span style="font-weight: bold">{</span><span style="color: #008000">'size'</span>: <span style="color: #800080; font-style: italic">None</span>, <span style="color: #008000">'minimum_size'</span>: <span style="color: #000080; font-weight: bold">1</span>, <span style="color: #008000">'ratio'</span>: <span style="color: #000080; font-weight: bold">1</span>, <span style="color: #008000">'name'</span>: <span style="color: #008000">'upper'</span><span style="font-weight: bold">}</span> <span style="color: #000080">│</span>
48 <span style="color: #000080">│</span> <span style="color: #000080">│</span>
49 <span style="color: #000080">│</span> <span style="color: #000080">│</span>
50 <span style="color: #000080">│</span> <span style="color: #000080">│</span>
51 <span style="color: #000080">│</span> <span style="color: #000080">│</span>
52 <span style="color: #000080">│</span> <span style="color: #000080">│</span>
53 <span style="color: #000080">╰──────────────────────────────────────────────────────────────────────────────────╯</span>
54 <span style="color: #000080">╭─────────── </span><span style="color: #008000">'left'</span><span style="color: #000080"> </span><span style="color: #000080; font-weight: bold">(</span><span style="color: #000080; font-weight: bold">42</span><span style="color: #000080"> x </span><span style="color: #000080; font-weight: bold">14</span><span style="color: #000080; font-weight: bold">)</span><span style="color: #000080"> ───────────╮╭────────── </span><span style="color: #008000">'right'</span><span style="color: #000080"> </span><span style="color: #000080; font-weight: bold">(</span><span style="color: #000080; font-weight: bold">42</span><span style="color: #000080"> x </span><span style="color: #000080; font-weight: bold">14</span><span style="color: #000080; font-weight: bold">)</span><span style="color: #000080"> ───────────╮</span>
55 <span style="color: #000080">│</span> <span style="color: #000080">││</span> <span style="color: #000080">│</span>
56 <span style="color: #000080">│</span> <span style="color: #000080">││</span> <span style="color: #000080">│</span>
57 <span style="color: #000080">│</span> <span style="color: #000080">││</span> <span style="color: #000080">│</span>
58 <span style="color: #000080">│</span> <span style="font-weight: bold">{</span> <span style="color: #000080">││</span> <span style="font-weight: bold">{</span> <span style="color: #000080">│</span>
59 <span style="color: #000080">│</span> <span style="color: #008000">'size'</span>: <span style="color: #800080; font-style: italic">None</span>, <span style="color: #000080">││</span> <span style="color: #008000">'size'</span>: <span style="color: #800080; font-style: italic">None</span>, <span style="color: #000080">│</span>
60 <span style="color: #000080">│</span> <span style="color: #008000">'minimum_size'</span>: <span style="color: #000080; font-weight: bold">1</span>, <span style="color: #000080">││</span> <span style="color: #008000">'minimum_size'</span>: <span style="color: #000080; font-weight: bold">1</span>, <span style="color: #000080">│</span>
61 <span style="color: #000080">│</span> <span style="color: #008000">'ratio'</span>: <span style="color: #000080; font-weight: bold">1</span>, <span style="color: #000080">││</span> <span style="color: #008000">'ratio'</span>: <span style="color: #000080; font-weight: bold">1</span>, <span style="color: #000080">│</span>
62 <span style="color: #000080">│</span> <span style="color: #008000">'name'</span>: <span style="color: #008000">'left'</span> <span style="color: #000080">││</span> <span style="color: #008000">'name'</span>: <span style="color: #008000">'right'</span> <span style="color: #000080">│</span>
63 <span style="color: #000080">│</span> <span style="font-weight: bold">}</span> <span style="color: #000080">││</span> <span style="font-weight: bold">}</span> <span style="color: #000080">│</span>
64 <span style="color: #000080">│</span> <span style="color: #000080">││</span> <span style="color: #000080">│</span>
65 <span style="color: #000080">│</span> <span style="color: #000080">││</span> <span style="color: #000080">│</span>
66 <span style="color: #000080">│</span> <span style="color: #000080">││</span> <span style="color: #000080">│</span>
67 <span style="color: #000080">╰────────────────────────────────────────╯╰────────────────────────────────────────╯</span>
68 </pre>
69
70 You can continue to call split() in this way to create as many parts to the screen as you wish.
71
72 Setting renderables
73 -------------------
74
75 The first position argument to ``Layout`` can be any Rich renderable, which will be sized to fit within the layout's area. Here's how we might divide the "right" layout in to two panels::
76
77 from rich.panel import Panel
78
79 layout["right"].split(
80 Layout(Panel("Hello")),
81 Layout(Panel("World!"))
82 )
83
84 You can also call :meth:`~rich.layout.Layout.update` to set or replace the current renderable::
85
86 layout["left"].update(
87 "The mystery of life isn't a problem to solve, but a reality to experience."
88 )
89 print(layout)
90
91 Fixed size
92 ----------
93
94 You can set a layout to use a fixed size by setting the ``size`` argument on the Layout constructor or by setting the attribute. Here's an example::
95
96 layout["upper"].size = 10
97 print(layout)
98
99 This will set the upper portion to be exactly 10 rows, no matter the size of the terminal. If the parent layout is horizontal rather than vertical, then the size applies to the number of characters rather that rows.
100
101 Ratio
102 -----
103
104 In addition to a fixed size, you can also make a flexible layout setting the ``ratio`` argument on the constructor or by assigning to the attribute. The ratio defines how much of the screen the layout should occupy in relation to other layouts. For example, let's reset the size and set the ratio of the upper layout to 2::
105
106 layout["upper"].size = None
107 layout["upper"].ratio = 2
108 print(layout)
109
110 This makes the top layout take up two thirds of the space. This is because the default ratio is 1, giving the upper and lower layouts a combined total of 3. As the upper layout has a ratio of 2, it takes up two thirds of the space, leaving the remaining third for the lower layout.
111
112 A layout with a ratio set may also have a minimum size to prevent it from getting too small. For instance, here's how we could set the minimum size of the lower sub-layout so that it won't shrink beyond 10 rows::
113
114 layout["lower"].minimum_size = 10
115
116 Visibility
117 ----------
118
119 You can make a layout invisible by setting the ``visible`` attribute to False. Here's an example::
120
121 layout["upper"].visible = False
122 print(layout)
123
124 The top layout is now invisible, and the "lower" layout will expand to fill the available space. Set ``visible`` to True to bring it back::
125
126 layout["upper"].visible = True
127 print(layout)
128
129 You could use this to toggle parts of your interface based on your application's configuration.
130
131 Tree
132 ----
133
134 To help visualize complex layouts you can print the ``tree`` attribute which will display a summary of the layout as a tree::
135
136 print(layout.tree)
137
138
139 Example
140 -------
141
142 See `fullscreen.py <https://github.com/willmcgugan/rich/blob/master/examples/fullscreen.py>`_ for an example that combines :class:`~rich.layout.Layout` and :class:`~rich.live.Live` to create a fullscreen "application".