blob: 83e9ee8b1bef7b2d4a781ee792d63972de15721c (
plain)
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
|
.. _quickstart:
==========
Quickstart
==========
This section will go over the basics of terminaltables.
Make sure that you've already :ref:`installed <install>` it.
Table with Default Settings
===========================
Let's begin by importing AsciiTable, which just uses ``+``, ``-``, and ``|`` characters.
.. code-block:: pycon
>>> from terminaltables import AsciiTable
Now let's define the table data in a variable called ``data``. We'll do it the long way by creating an empty list
representing the entire table. Then we'll add rows one by one. Each row is a list representing table cells.
.. code-block:: pycon
>>> data = []
>>> data.append(['Row one column one', 'Row one column two'])
>>> data.append(['Row two column one', 'Row two column two'])
>>> data.append(['Row three column one', 'Row three column two'])
Next we can use AsciiTable to format the table properly and then we can just print it. ``table.table`` gives you just
one long string with newline characters so you can easily print it.
.. code-block:: pycon
>>> table = AsciiTable(data)
>>> print table.table
+----------------------+----------------------+
| Row one column one | Row one column two |
+----------------------+----------------------+
| Row two column one | Row two column two |
| Row three column one | Row three column two |
+----------------------+----------------------+
By default the first row of the table is considered the heading. This can be turned off.
Changing Table Settings
=======================
There are more options available to change how your tables are formatted. Say your table doesn't really have a heading
row; all rows are just data.
.. code-block:: pycon
>>> table.inner_heading_row_border = False
>>> print table.table
+----------------------+----------------------+
| Row one column one | Row one column two |
| Row two column one | Row two column two |
| Row three column one | Row three column two |
+----------------------+----------------------+
Now you want to add a title to the table:
.. code-block:: pycon
>>> table.title = 'My Table'
>>> print table.table
+My Table--------------+----------------------+
| Row one column one | Row one column two |
| Row two column one | Row two column two |
| Row three column one | Row three column two |
+----------------------+----------------------+
Maybe you want lines in between all rows:
.. code-block:: pycon
>>> table.inner_row_border = True
>>> print table.table
+My Table--------------+----------------------+
| Row one column one | Row one column two |
+----------------------+----------------------+
| Row two column one | Row two column two |
+----------------------+----------------------+
| Row three column one | Row three column two |
+----------------------+----------------------+
There are many more settings available. You can find out more by reading the :ref:`settings` section. Each table style
pretty much shares the same settings but there are a few minor exceptions. Refer to each table style's documentation on
the sidebar.
Other Table Styles
==================
Terminaltables comes with a few other table styles than just ``AsciiTable``. All table styles more or less have the same
API.
.. code-block:: pycon
>>> from terminaltables import SingleTable
>>> table = SingleTable(data)
>>> print table.table
┌──────────────────────┬──────────────────────┐
│ Row one column one │ Row one column two │
├──────────────────────┼──────────────────────┤
│ Row two column one │ Row two column two │
│ Row three column one │ Row three column two │
└──────────────────────┴──────────────────────┘
You can find documentation for all table styles on the sidebar.
|