| 1 |
Quickstart |
| 2 |
========== |
| 3 |
|
| 4 |
Displaying Tabular Data |
| 5 |
----------------------- |
| 6 |
|
| 7 |
|
| 8 |
The Basics |
| 9 |
++++++++++ |
| 10 |
|
| 11 |
CLI Helpers provides a simple way to display your tabular data (columns/rows) in a visually-appealing manner:: |
| 12 |
|
| 13 |
>>> from cli_helpers import tabular_output |
| 14 |
|
| 15 |
>>> data = [[1, 'Asgard', True], [2, 'Camelot', False], [3, 'El Dorado', True]] |
| 16 |
>>> headers = ['id', 'city', 'visited'] |
| 17 |
|
| 18 |
>>> print("\n".join(tabular_output.format_output(iter(data), headers, format_name='simple'))) |
| 19 |
|
| 20 |
id city visited |
| 21 |
---- --------- --------- |
| 22 |
1 Asgard True |
| 23 |
2 Camelot False |
| 24 |
3 El Dorado True |
| 25 |
|
| 26 |
Let's take a look at what we did there. |
| 27 |
|
| 28 |
1. We imported the :mod:`~cli_helpers.tabular_output` module. This module gives us access to the :func:`~cli_helpers.tabular_output.format_output` function. |
| 29 |
|
| 30 |
2. Next we generate some data. Plus, we need a list of headers to give our data some context. |
| 31 |
|
| 32 |
3. We format the output using the display format ``simple``. That's a nice looking table! |
| 33 |
|
| 34 |
|
| 35 |
Display Formats |
| 36 |
+++++++++++++++ |
| 37 |
|
| 38 |
To display your data, :mod:`~cli_helpers.tabular_output` uses |
| 39 |
`tabulate <https://bitbucket.org/astanin/python-tabulate>`_, |
| 40 |
`terminaltables <https://robpol86.github.io/terminaltables/>`_, :mod:`csv`, |
| 41 |
and its own vertical table layout. |
| 42 |
|
| 43 |
The best way to see the various display formats is to use the |
| 44 |
:class:`~cli_helpers.tabular_output.TabularOutputFormatter` class. This is |
| 45 |
what the :func:`~cli_helpers.tabular_output.format_output` function in our |
| 46 |
first example uses behind the scenes. |
| 47 |
|
| 48 |
Let's get a list of all the supported format names:: |
| 49 |
|
| 50 |
>>> from cli_helpers.tabular_output import TabularOutputFormatter |
| 51 |
>>> formatter = TabularOutputFormatter() |
| 52 |
>>> formatter.supported_formats |
| 53 |
('vertical', 'csv', 'tsv', 'mediawiki', 'html', 'latex', 'latex_booktabs', 'textile', 'moinmoin', 'jira', 'plain', 'minimal', 'simple', 'grid', 'fancy_grid', 'pipe', 'orgtbl', 'psql', 'psql_unicode', 'rst', 'ascii', 'double', 'github') |
| 54 |
|
| 55 |
You can format your data in any of those supported formats. Let's take the |
| 56 |
same data from our first example and put it in the ``fancy_grid`` format:: |
| 57 |
|
| 58 |
>>> data = [[1, 'Asgard', True], [2, 'Camelot', False], [3, 'El Dorado', True]] |
| 59 |
>>> headers = ['id', 'city', 'visited'] |
| 60 |
>>> print("\n".join(formatter.format_output(iter(data), headers, format_name='fancy_grid'))) |
| 61 |
╒══════╤═══════════╤═══════════╕ |
| 62 |
│ id │ city │ visited │ |
| 63 |
╞══════╪═══════════╪═══════════╡ |
| 64 |
│ 1 │ Asgard │ True │ |
| 65 |
├──────┼───────────┼───────────┤ |
| 66 |
│ 2 │ Camelot │ False │ |
| 67 |
├──────┼───────────┼───────────┤ |
| 68 |
│ 3 │ El Dorado │ True │ |
| 69 |
╘══════╧═══════════╧═══════════╛ |
| 70 |
|
| 71 |
That was easy! How about CLI Helper's vertical table layout? |
| 72 |
|
| 73 |
>>> print("\n".join(formatter.format_output(iter(data), headers, format_name='vertical'))) |
| 74 |
***************************[ 1. row ]*************************** |
| 75 |
id | 1 |
| 76 |
city | Asgard |
| 77 |
visited | True |
| 78 |
***************************[ 2. row ]*************************** |
| 79 |
id | 2 |
| 80 |
city | Camelot |
| 81 |
visited | False |
| 82 |
***************************[ 3. row ]*************************** |
| 83 |
id | 3 |
| 84 |
city | El Dorado |
| 85 |
visited | True |
| 86 |
|
| 87 |
|
| 88 |
Default Format |
| 89 |
++++++++++++++ |
| 90 |
|
| 91 |
When you create a :class:`~cli_helpers.tabular_output.TabularOutputFormatter` |
| 92 |
object, you can specify a default formatter so you don't have to pass the |
| 93 |
format name each time you want to format your data:: |
| 94 |
|
| 95 |
>>> formatter = TabularOutputFormatter(format_name='plain') |
| 96 |
>>> print("\n".join(formatter.format_output(iter(data), headers))) |
| 97 |
id city visited |
| 98 |
1 Asgard True |
| 99 |
2 Camelot False |
| 100 |
3 El Dorado True |
| 101 |
|
| 102 |
.. TIP:: |
| 103 |
You can get or set the default format whenever you'd like through |
| 104 |
:data:`TabularOutputFormatter.format_name <cli_helpers.tabular_output.TabularOutputFormatter.format_name>`. |
| 105 |
|
| 106 |
|
| 107 |
Passing Options to the Formatters |
| 108 |
+++++++++++++++++++++++++++++++++ |
| 109 |
|
| 110 |
Many of the formatters have settings that can be tweaked by passing |
| 111 |
an optional argument when you format your data. For example, |
| 112 |
if we wanted to enable or disable number parsing on any of |
| 113 |
`tabulate's <https://bitbucket.org/astanin/python-tabulate>`_ |
| 114 |
formats, we could:: |
| 115 |
|
| 116 |
>>> data = [[1, 1.5], [2, 19.605], [3, 100.0]] |
| 117 |
>>> headers = ['id', 'rating'] |
| 118 |
>>> print("\n".join(format_output(iter(data), headers, format_name='simple', disable_numparse=True))) |
| 119 |
id rating |
| 120 |
---- -------- |
| 121 |
1 1.5 |
| 122 |
2 19.605 |
| 123 |
3 100.0 |
| 124 |
>>> print("\n".join(format_output(iter(data), headers, format_name='simple', disable_numparse=False))) |
| 125 |
id rating |
| 126 |
---- -------- |
| 127 |
1 1.5 |
| 128 |
2 19.605 |
| 129 |
3 100 |
| 130 |
|
| 131 |
|
| 132 |
Lists and tuples and bytearrays. Oh my! |
| 133 |
+++++++++++++++++++++++++++++++++++++++ |
| 134 |
|
| 135 |
:mod:`~cli_helpers.tabular_output` supports any :term:`iterable`, not just |
| 136 |
a :class:`list` or :class:`tuple`. You can use a :class:`range`, |
| 137 |
:func:`enumerate`, a :class:`str`, or even a :class:`bytearray`! Here is a |
| 138 |
far-fetched example to prove the point:: |
| 139 |
|
| 140 |
>>> step = 3 |
| 141 |
>>> data = [range(n, n + step) for n in range(0, 9, step)] |
| 142 |
>>> headers = 'abc' |
| 143 |
>>> print("\n".join(format_output(iter(data), headers, format_name='simple'))) |
| 144 |
a b c |
| 145 |
--- --- --- |
| 146 |
0 1 2 |
| 147 |
3 4 5 |
| 148 |
6 7 8 |
| 149 |
|
| 150 |
Real life examples include a PyMySQL |
| 151 |
:class:`Cursor <pymysql:pymysql.cursors.Cursor>` with |
| 152 |
database results or |
| 153 |
NumPy :class:`ndarray <numpy:numpy.ndarray>` with data points. |