File: table.rst

package info (click to toggle)
python-cleo 2.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,120 kB
  • sloc: python: 8,293; makefile: 22; sh: 2
file content (241 lines) | stat: -rw-r--r-- 7,796 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
Table
#####

When building a console application it may be useful to display tabular data:

.. code-block:: text

    +---------------+--------------------------+------------------+
    | ISBN          | Title                    | Author           |
    +---------------+--------------------------+------------------+
    | 99921-58-10-7 | Divine Comedy            | Dante Alighieri  |
    | 9971-5-0210-0 | A Tale of Two Cities     | Charles Dickens  |
    | 960-425-059-0 | The Lord of the Rings    | J. R. R. Tolkien |
    | 80-902734-1-6 | And Then There Were None | Agatha Christie  |
    +---------------+--------------------------+------------------+

To display a table, use the ``table()`` method, set the headers, set the rows and then render the table:

.. code-block:: python

    def handle(self):
        table = self.table()

        table.set_headers(['ISBN', 'Title', 'Author'])
        table.set_rows([
            ['99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'],
            ['9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'],
            ['960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'],
            ['80-902734-1-6', 'And Then There Were None', 'Agatha Christie']
        ])

        table.render(self.io)

.. tip::

    All these steps can be done in one go using the ``render_table`` method:

    .. code-block:: python

        self.render_table(
            ['ISBN', 'Title', 'Author'],
            [
                ['99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'],
                ['9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'],
                ['960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'],
                ['80-902734-1-6', 'And Then There Were None', 'Agatha Christie']
            ]
        )

You can add a table separator anywhere in the output by using ``table_separator()``,
which returns a ``TableSeparator``, as a row:

.. code-block:: python

    table.set_rows([
        ['99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'],
        ['9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'],
        self.table_separator(),
        ['960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'],
        ['80-902734-1-6', 'And Then There Were None', 'Agatha Christie']
    ])

.. code-block:: text

    +---------------+--------------------------+------------------+
    | ISBN          | Title                    | Author           |
    +---------------+--------------------------+------------------+
    | 99921-58-10-7 | Divine Comedy            | Dante Alighieri  |
    | 9971-5-0210-0 | A Tale of Two Cities     | Charles Dickens  |
    +---------------+--------------------------+------------------+
    | 960-425-059-0 | The Lord of the Rings    | J. R. R. Tolkien |
    | 80-902734-1-6 | And Then There Were None | Agatha Christie  |
    +---------------+--------------------------+------------------+

The table style can be changed to any built-in styles via ``set_style()``:

.. code-block:: python

    # same as calling nothing
    table.set_style('default')

    # changes the default style to compact
    table.set_style('compact')

This code results in:

.. code-block:: text

    ISBN          Title                    Author
    99921-58-10-7 Divine Comedy            Dante Alighieri
    9971-5-0210-0 A Tale of Two Cities     Charles Dickens
    960-425-059-0 The Lord of the Rings    J. R. R. Tolkien
    80-902734-1-6 And Then There Were None Agatha Christie

You can also set the style to ``borderless``:

.. code-block:: python

    table.set_style('borderless')

which outputs:

.. code-block:: text

    =============== ========================== ==================
     ISBN            Title                      Author
    =============== ========================== ==================
     99921-58-10-7   Divine Comedy              Dante Alighieri
     9971-5-0210-0   A Tale of Two Cities       Charles Dickens
     960-425-059-0   The Lord of the Rings      J. R. R. Tolkien
     80-902734-1-6   And Then There Were None   Agatha Christie
    =============== ========================== ==================

If the built-in styles do not fit your need, define your own:

.. code-block:: python

    # by default, this is based on the default style
    style = self.table_style()

    # customize the style
    style.set_horizontal_border_char('<fg=magenta>|</>')
    style.set_vertical_border_char('<fg=magenta>-</>')
    style.set_crossing_char(' ')

    # use the style for this table
    table.set_style(style)

Here is a full list of things you can customize:

*  ``set_adding_char()``
*  ``set_horizontal_border_char()``
*  ``set_vertical_border_char()``
*  ``set_crossing_char()``
*  ``set_cell_header_format()``
*  ``set_cell_row_format()``
*  ``set_border_format()``
*  ``set_pad_type()``

.. tip::

    The style can also be passed as a keyword argument to ``render_table()``

    .. code-block:: python

        self.render_table(
            ['ISBN', 'Title', 'Author'],
            [
                ['99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'],
                ['9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'],
                ['960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'],
                ['80-902734-1-6', 'And Then There Were None', 'Agatha Christie']
            ]
            style='borderless'
        )


Spanning Multiple Columns and Rows
==================================

To make a table cell that spans multiple columns you can use ``table_cell()``,
which returns a ``TableCell`` instance:

.. code-block:: python

    table = self.table()

    table.set_headers(['ISBN', 'Title', 'Author'])
    table.set_rows([
        ['99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'],
        self.table_separator(),
        [self.table_cell('This value spans 3 columns.', colspan=3)]
    ])

    table.render()

This results in:

.. code-block:: text

    +---------------+---------------+-----------------+
    | ISBN          | Title         | Author          |
    +---------------+---------------+-----------------+
    | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
    +---------------+---------------+-----------------+
    | This value spans 3 columns.                     |
    +---------------+---------------+-----------------+

.. tip::

    You can create a multiple-line page title using a header cell that spans the entire table width:

    .. code-block:: python

        table.set_headers([
            [self.table_cell('Main table title', colspan=3)],
            ['ISBN', 'Title', 'Author']
        ])

    This generate:

    .. code-block:: text

        +-------+-------+--------+
        | Main table title       |
        +-------+-------+--------+
        | ISBN  | Title | Author |
        +-------+-------+--------+
        | ...                    |
        +-------+-------+--------+

In a similar way you can span multiple rows:

.. code-block:: python

    table = self.table()

    table.set_headers(['ISBN', 'Title', 'Author'])
    table.set_rows([
        [
            '978-0521567817',
            'De Monarchia',
            self.table_cell('Dante Alighieri\nspans multiple rows', rowspan=2)
        ]
    ])

    table.render()

This outputs:

.. code-block:: text

    +----------------+---------------+---------------------+
    | ISBN           | Title         | Author              |
    +----------------+---------------+---------------------+
    | 978-0521567817 | De Monarchia  | Dante Alighieri     |
    | 978-0804169127 | Divine Comedy | spans multiple rows |
    +----------------+---------------+---------------------+

You can use the ``colspan`` and ``rowspan`` options at the same time
which allows you to create any table layout you may wish.