METADATA 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. Metadata-Version: 2.4
  2. Name: prettytable
  3. Version: 3.17.0
  4. Summary: A simple Python library for easily displaying tabular data in a visually appealing ASCII table format
  5. Project-URL: Changelog, https://github.com/prettytable/prettytable/releases
  6. Project-URL: Funding, https://tidelift.com/subscription/pkg/pypi-prettytable?utm_source=pypi-prettytable&utm_medium=pypi
  7. Project-URL: Homepage, https://github.com/prettytable/prettytable
  8. Project-URL: Source, https://github.com/prettytable/prettytable
  9. Author-email: Luke Maurits <luke@maurits.id.au>
  10. Maintainer: Hugo van Kemenade
  11. License-Expression: BSD-3-Clause
  12. License-File: LICENSE
  13. Classifier: Programming Language :: Python
  14. Classifier: Programming Language :: Python :: 3 :: Only
  15. Classifier: Programming Language :: Python :: 3.10
  16. Classifier: Programming Language :: Python :: 3.11
  17. Classifier: Programming Language :: Python :: 3.12
  18. Classifier: Programming Language :: Python :: 3.13
  19. Classifier: Programming Language :: Python :: 3.14
  20. Classifier: Programming Language :: Python :: 3.15
  21. Classifier: Programming Language :: Python :: Implementation :: CPython
  22. Classifier: Programming Language :: Python :: Implementation :: PyPy
  23. Classifier: Topic :: Text Processing
  24. Classifier: Typing :: Typed
  25. Requires-Python: >=3.10
  26. Requires-Dist: wcwidth
  27. Provides-Extra: tests
  28. Requires-Dist: pytest; extra == 'tests'
  29. Requires-Dist: pytest-cov; extra == 'tests'
  30. Requires-Dist: pytest-lazy-fixtures; extra == 'tests'
  31. Description-Content-Type: text/markdown
  32. # PrettyTable
  33. [![PyPI version](https://img.shields.io/pypi/v/prettytable.svg?logo=pypi&logoColor=FFE873)](https://pypi.org/project/prettytable/)
  34. [![Supported Python versions](https://img.shields.io/pypi/pyversions/prettytable.svg?logo=python&logoColor=FFE873)](https://pypi.org/project/prettytable/)
  35. [![PyPI downloads](https://img.shields.io/pypi/dm/prettytable.svg)](https://pypistats.org/packages/prettytable)
  36. [![GitHub Actions status](https://github.com/prettytable/prettytable/workflows/Test/badge.svg)](https://github.com/prettytable/prettytable/actions)
  37. [![Codecov](https://codecov.io/gh/prettytable/prettytable/branch/main/graph/badge.svg)](https://codecov.io/gh/prettytable/prettytable)
  38. [![Licence](https://img.shields.io/github/license/prettytable/prettytable.svg)](LICENSE)
  39. [![Code style: Black](https://img.shields.io/badge/code%20style-Black-000000.svg)](https://github.com/psf/black)
  40. [![Tidelift](https://tidelift.com/badges/package/pypi/prettytable)](https://tidelift.com/subscription/pkg/pypi-prettytable?utm_source=pypi-prettytable&utm_medium=badge)
  41. PrettyTable lets you print tables in an attractive ASCII form:
  42. ```
  43. +-----------+------+------------+-----------------+
  44. | City name | Area | Population | Annual Rainfall |
  45. +-----------+------+------------+-----------------+
  46. | Adelaide | 1295 | 1158259 | 600.5 |
  47. | Brisbane | 5905 | 1857594 | 1146.4 |
  48. | Darwin | 112 | 120900 | 1714.7 |
  49. | Hobart | 1357 | 205556 | 619.5 |
  50. | Melbourne | 1566 | 3806092 | 646.9 |
  51. | Perth | 5386 | 1554769 | 869.4 |
  52. | Sydney | 2058 | 4336374 | 1214.8 |
  53. +-----------+------+------------+-----------------+
  54. ```
  55. ## Installation
  56. Install via pip:
  57. python -m pip install -U prettytable
  58. Install latest development version:
  59. python -m pip install -U git+https://github.com/prettytable/prettytable
  60. Or from `requirements.txt`:
  61. -e git://github.com/prettytable/prettytable.git#egg=prettytable
  62. ## Demo
  63. To see demo output, run:
  64. ```bash
  65. python3 -m prettytable
  66. ```
  67. ## Tutorial on how to use the PrettyTable API
  68. ### Getting your data into (and out of) the table
  69. Let's suppose you have a shiny new PrettyTable:
  70. ```python
  71. from prettytable import PrettyTable
  72. table = PrettyTable()
  73. ```
  74. and you want to put some data into it. You have a few options.
  75. #### Row by row
  76. You can add data one row at a time. To do this you can set the field names first using
  77. the `field_names` attribute, and then add the rows one at a time using the `add_row`
  78. method:
  79. ```python
  80. table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
  81. table.add_row(["Adelaide", 1295, 1158259, 600.5])
  82. table.add_row(["Brisbane", 5905, 1857594, 1146.4])
  83. table.add_row(["Darwin", 112, 120900, 1714.7])
  84. table.add_row(["Hobart", 1357, 205556, 619.5])
  85. table.add_row(["Sydney", 2058, 4336374, 1214.8])
  86. table.add_row(["Melbourne", 1566, 3806092, 646.9])
  87. table.add_row(["Perth", 5386, 1554769, 869.4])
  88. ```
  89. #### All rows at once
  90. When you have a list of rows, you can add them in one go with `add_rows`:
  91. ```python
  92. table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
  93. table.add_rows(
  94. [
  95. ["Adelaide", 1295, 1158259, 600.5],
  96. ["Brisbane", 5905, 1857594, 1146.4],
  97. ["Darwin", 112, 120900, 1714.7],
  98. ["Hobart", 1357, 205556, 619.5],
  99. ["Sydney", 2058, 4336374, 1214.8],
  100. ["Melbourne", 1566, 3806092, 646.9],
  101. ["Perth", 5386, 1554769, 869.4],
  102. ]
  103. )
  104. ```
  105. #### Column by column
  106. You can add data one column at a time as well. To do this you use the `add_column`
  107. method, which takes two arguments - a string which is the name for the field the column
  108. you are adding corresponds to, and a list or tuple which contains the column data:
  109. ```python
  110. table.add_column("City name",
  111. ["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"])
  112. table.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5386])
  113. table.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092,
  114. 1554769])
  115. table.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9,
  116. 869.4])
  117. ```
  118. #### Mixing and matching
  119. If you really want to, you can even mix and match `add_row` and `add_column` and build
  120. some of your table in one way and some of it in the other. Tables built this way are
  121. kind of confusing for other people to read, though, so don't do this unless you have a
  122. good reason.
  123. #### Importing data from a CSV file
  124. If you have your table data in a comma-separated values file (.csv), you can read this
  125. data into a PrettyTable like this:
  126. ```python
  127. from prettytable import from_csv
  128. with open("myfile.csv") as fp:
  129. mytable = from_csv(fp)
  130. ```
  131. #### Importing data from a database cursor
  132. If you have your table data in a database which you can access using a library which
  133. confirms to the Python DB-API (e.g. an SQLite database accessible using the `sqlite`
  134. module), then you can build a PrettyTable using a cursor object, like this:
  135. ```python
  136. import sqlite3
  137. from prettytable import from_db_cursor
  138. connection = sqlite3.connect("mydb.db")
  139. cursor = connection.cursor()
  140. cursor.execute("SELECT field1, field2, field3 FROM my_table")
  141. mytable = from_db_cursor(cursor)
  142. ```
  143. #### Getting data out
  144. There are three ways to get data out of a PrettyTable, in increasing order of
  145. completeness:
  146. - The `del_row` method takes an integer index of a single row to delete.
  147. - The `del_column` method takes a field name of a single column to delete.
  148. - The `clear_rows` method takes no arguments and deletes all the rows in the table - but
  149. keeps the field names as they were so you that you can repopulate it with the same
  150. kind of data.
  151. - The `clear` method takes no arguments and deletes all rows and all field names. It's
  152. not quite the same as creating a fresh table instance, though - style related
  153. settings, discussed later, are maintained.
  154. ### Displaying your table in ASCII form
  155. PrettyTable's main goal is to let you print tables in an attractive ASCII form, like
  156. this:
  157. ```
  158. +-----------+------+------------+-----------------+
  159. | City name | Area | Population | Annual Rainfall |
  160. +-----------+------+------------+-----------------+
  161. | Adelaide | 1295 | 1158259 | 600.5 |
  162. | Brisbane | 5905 | 1857594 | 1146.4 |
  163. | Darwin | 112 | 120900 | 1714.7 |
  164. | Hobart | 1357 | 205556 | 619.5 |
  165. | Melbourne | 1566 | 3806092 | 646.9 |
  166. | Perth | 5386 | 1554769 | 869.4 |
  167. | Sydney | 2058 | 4336374 | 1214.8 |
  168. +-----------+------+------------+-----------------+
  169. ```
  170. You can print tables like this to `stdout` or get string representations of them.
  171. #### Printing
  172. To print a table in ASCII form, you can just do this:
  173. ```python
  174. print(table)
  175. ```
  176. The old `table.printt()` method from versions 0.5 and earlier has been removed.
  177. To pass options changing the look of the table, use the `get_string()` method documented
  178. below:
  179. ```python
  180. print(table.get_string())
  181. ```
  182. #### Stringing
  183. If you don't want to actually print your table in ASCII form but just get a string
  184. containing what _would_ be printed if you use `print(table)`, you can use the
  185. `get_string` method:
  186. ```python
  187. mystring = table.get_string()
  188. ```
  189. This string is guaranteed to look exactly the same as what would be printed by doing
  190. `print(table)`. You can now do all the usual things you can do with a string, like write
  191. your table to a file or insert it into a GUI.
  192. The table can be displayed in several different formats using `get_formatted_string` by
  193. changing the `out_format=<text|html|json|csv|latex|mediawiki>`. This function passes
  194. through arguments to the functions that render the table, so additional arguments can be
  195. given. This provides a way to let a user choose the output formatting.
  196. ```python
  197. def my_cli_function(table_format: str = 'text'):
  198. ...
  199. print(table.get_formatted_string(table_format))
  200. ```
  201. #### Controlling which data gets displayed
  202. If you like, you can restrict the output of `print(table)` or `table.get_string` to only
  203. the fields or rows you like.
  204. The `fields` argument to these methods takes a list of field names to be printed:
  205. ```python
  206. print(table.get_string(fields=["City name", "Population"]))
  207. ```
  208. gives:
  209. ```
  210. +-----------+------------+
  211. | City name | Population |
  212. +-----------+------------+
  213. | Adelaide | 1158259 |
  214. | Brisbane | 1857594 |
  215. | Darwin | 120900 |
  216. | Hobart | 205556 |
  217. | Melbourne | 3806092 |
  218. | Perth | 1554769 |
  219. | Sydney | 4336374 |
  220. +-----------+------------+
  221. ```
  222. The `start` and `end` arguments take the index of the first and last row to print
  223. respectively. Note that the indexing works like Python list slicing - to print the 2nd,
  224. 3rd and 4th rows of the table, set `start` to 1 (the first row is row 0, so the second
  225. is row 1) and set `end` to 4 (the index of the 4th row, plus 1):
  226. ```python
  227. print(table.get_string(start=1, end=4))
  228. ```
  229. prints:
  230. ```
  231. +-----------+------+------------+-----------------+
  232. | City name | Area | Population | Annual Rainfall |
  233. +-----------+------+------------+-----------------+
  234. | Brisbane | 5905 | 1857594 | 1146.4 |
  235. | Darwin | 112 | 120900 | 1714.7 |
  236. | Hobart | 1357 | 205556 | 619.5 |
  237. +-----------+------+------------+-----------------+
  238. ```
  239. #### Filtering your table
  240. You can make sure that your tables are filtered by giving `get_string` a `row_filter`
  241. keyword argument, which must be a function with one argument `row` returning a Boolean
  242. value. The `row` is the list of fields in a row.
  243. For example, to print the example table we built earlier of Australian capital city
  244. data, so that cities with a population of at least 1,000,000, we can do this:
  245. ```python
  246. def filter_function(row: list[str]) -> bool:
  247. return row[2] > 999999
  248. print(table.get_string(row_filter=filter_function))
  249. ```
  250. to get:
  251. ```
  252. +-----------+------+------------+-----------------+
  253. | City name | Area | Population | Annual Rainfall |
  254. +-----------+------+------------+-----------------+
  255. | Adelaide | 1295 | 1158259 | 600.5 |
  256. | Brisbane | 5905 | 1857594 | 1146.4 |
  257. | Sydney | 2058 | 4336374 | 1214.8 |
  258. | Melbourne | 1566 | 3806092 | 646.9 |
  259. | Perth | 5386 | 1554769 | 869.4 |
  260. +-----------+------+------------+-----------------+
  261. ```
  262. #### Changing the alignment of columns
  263. By default, all columns in a table are centre aligned.
  264. ##### All columns at once
  265. You can change the alignment of all the columns in a table at once by assigning a one
  266. character string to the `align` attribute. The allowed strings are `"l"`, `"r"` and
  267. `"c"` for left, right and centre alignment, respectively:
  268. ```python
  269. table.align = "r"
  270. print(table)
  271. ```
  272. gives:
  273. ```
  274. +-----------+------+------------+-----------------+
  275. | City name | Area | Population | Annual Rainfall |
  276. +-----------+------+------------+-----------------+
  277. | Adelaide | 1295 | 1158259 | 600.5 |
  278. | Brisbane | 5905 | 1857594 | 1146.4 |
  279. | Darwin | 112 | 120900 | 1714.7 |
  280. | Hobart | 1357 | 205556 | 619.5 |
  281. | Melbourne | 1566 | 3806092 | 646.9 |
  282. | Perth | 5386 | 1554769 | 869.4 |
  283. | Sydney | 2058 | 4336374 | 1214.8 |
  284. +-----------+------+------------+-----------------+
  285. ```
  286. ##### One column at a time
  287. You can also change the alignment of individual columns based on the corresponding field
  288. name by treating the `align` attribute as if it were a dictionary.
  289. ```python
  290. table.align["City name"] = "l"
  291. table.align["Area"] = "c"
  292. table.align["Population"] = "r"
  293. table.align["Annual Rainfall"] = "c"
  294. print(table)
  295. ```
  296. gives:
  297. ```
  298. +-----------+------+------------+-----------------+
  299. | City name | Area | Population | Annual Rainfall |
  300. +-----------+------+------------+-----------------+
  301. | Adelaide | 1295 | 1158259 | 600.5 |
  302. | Brisbane | 5905 | 1857594 | 1146.4 |
  303. | Darwin | 112 | 120900 | 1714.7 |
  304. | Hobart | 1357 | 205556 | 619.5 |
  305. | Melbourne | 1566 | 3806092 | 646.9 |
  306. | Perth | 5386 | 1554769 | 869.4 |
  307. | Sydney | 2058 | 4336374 | 1214.8 |
  308. +-----------+------+------------+-----------------+
  309. ```
  310. ##### Sorting your table by a field
  311. You can make sure that your ASCII tables are produced with the data sorted by one
  312. particular field by giving `get_string` a `sortby` keyword argument, which must be a
  313. string containing the name of one field.
  314. For example, to print the example table we built earlier of Australian capital city
  315. data, so that the most populated city comes last, we can do this:
  316. ```python
  317. print(table.get_string(sortby="Population"))
  318. ```
  319. to get:
  320. ```
  321. +-----------+------+------------+-----------------+
  322. | City name | Area | Population | Annual Rainfall |
  323. +-----------+------+------------+-----------------+
  324. | Darwin | 112 | 120900 | 1714.7 |
  325. | Hobart | 1357 | 205556 | 619.5 |
  326. | Adelaide | 1295 | 1158259 | 600.5 |
  327. | Perth | 5386 | 1554769 | 869.4 |
  328. | Brisbane | 5905 | 1857594 | 1146.4 |
  329. | Melbourne | 1566 | 3806092 | 646.9 |
  330. | Sydney | 2058 | 4336374 | 1214.8 |
  331. +-----------+------+------------+-----------------+
  332. ```
  333. If we want the most populated city to come _first_, we can also give a
  334. `reversesort=True` argument.
  335. If you _always_ want your tables to be sorted in a certain way, you can make the setting
  336. long-term like this:
  337. ```python
  338. table.sortby = "Population"
  339. print(table)
  340. print(table)
  341. print(table)
  342. ```
  343. All three tables printed by this code will be sorted by population (you could do
  344. `table.reversesort = True` as well, if you wanted). The behaviour will persist until you
  345. turn it off:
  346. ```python
  347. table.sortby = None
  348. ```
  349. If you want to specify a custom sorting function, you can use the `sort_key` keyword
  350. argument as Pythons `sorted()` `key` parameter. The value of the `sort_key` parameter
  351. should be a function (or other callable) that takes a single argument and returns a key
  352. to use for sorting purposes.
  353. If your table has n columns, each list will have n+1 elements. Each list corresponds to
  354. one row of the table. The first element will be whatever data is in the relevant row, in
  355. the column specified by the `sort_by` argument. The remaining n elements are the data in
  356. each of the table's columns, in order, including a repeated instance of the data in the
  357. `sort_by` column.
  358. #### Adding sections to a table
  359. You can divide your table into different sections using the `add_divider` method or
  360. `divider` argument to `add_row()` or even to `add_rows()`. This will add a dividing line
  361. into the table under the row who has this field set. So we can set up a table like this:
  362. ```python
  363. table = PrettyTable()
  364. table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
  365. table.add_row(["Adelaide", 1295, 1158259, 600.5])
  366. table.add_divider()
  367. table.add_row(["Brisbane", 5905, 1857594, 1146.4], divider=True)
  368. table.add_rows(
  369. [["Darwin", 112, 120900, 1714.7],
  370. ["Hobart", 1357, 205556, 619.5]],
  371. divider=True
  372. )
  373. table.add_row(["Melbourne", 1566, 3806092, 646.9])
  374. table.add_row(["Perth", 5386, 1554769, 869.4])
  375. table.add_row(["Sydney", 2058, 4336374, 1214.8])
  376. ```
  377. to get a table like this:
  378. ```
  379. +-----------+------+------------+-----------------+
  380. | City name | Area | Population | Annual Rainfall |
  381. +-----------+------+------------+-----------------+
  382. | Adelaide | 1295 | 1158259 | 600.5 |
  383. +-----------+------+------------+-----------------+
  384. | Brisbane | 5905 | 1857594 | 1146.4 |
  385. +-----------+------+------------+-----------------+
  386. | Darwin | 112 | 120900 | 1714.7 |
  387. | Hobart | 1357 | 205556 | 619.5 |
  388. +-----------+------+------------+-----------------+
  389. | Melbourne | 1566 | 3806092 | 646.9 |
  390. | Perth | 5386 | 1554769 | 869.4 |
  391. | Sydney | 2058 | 4336374 | 1214.8 |
  392. +-----------+------+------------+-----------------+
  393. ```
  394. Any added dividers will be removed if a table is sorted.
  395. ### Changing the appearance of your table - the easy way
  396. By default, PrettyTable produces ASCII tables that look like the ones used in SQL
  397. database shells. But it can print them in a variety of other formats as well. If the
  398. format you want to use is common, PrettyTable makes this easy for you to do using the
  399. `set_style` method. If you want to produce an uncommon table, you'll have to do things
  400. slightly harder (see later).
  401. #### Setting a table style
  402. You can set the style for your table using the `set_style` method before any calls to
  403. `print` or `get_string`. Here's how to print a table in Markdown format:
  404. ```python
  405. from prettytable import TableStyle
  406. table.set_style(TableStyle.MARKDOWN)
  407. print(table)
  408. ```
  409. In addition to `MARKDOWN` you can use these in-built styles:
  410. - `DEFAULT` - The default look, used to undo any style changes you may have made
  411. - `PLAIN_COLUMNS` - A borderless style that works well with command line programs for
  412. columnar data
  413. - `MSWORD_FRIENDLY` - A format which works nicely with Microsoft Word's "Convert to
  414. table" feature
  415. - `ORGMODE` - A table style that fits [Org mode](https://orgmode.org/) syntax
  416. - `SINGLE_BORDER` and `DOUBLE_BORDER` - Styles that use continuous single/double border
  417. lines with Box drawing characters for a fancier display on terminal
  418. Other styles are likely to appear in future releases.
  419. ### Changing the appearance of your table - the hard way
  420. If you want to display your table in a style other than one of the in-built styles
  421. listed above, you'll have to set things up the hard way.
  422. Don't worry, it's not really that hard!
  423. #### Style options
  424. PrettyTable has a number of style options which control various aspects of how tables
  425. are displayed. You have the freedom to set each of these options individually to
  426. whatever you prefer. The `set_style` method just does this automatically for you.
  427. Table-specific options are:
  428. | Option | Details |
  429. | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
  430. | `HEADER`, `ALL`, `NONE` | These are variables defined inside the `prettytable` module so make sure you import them or use `prettytable.FRAME` etc. |
  431. | `_horizontal_align_char` | Single character string used to indicate column alignment in horizontal lines. Default: `:` for Markdown, otherwise `None`. |
  432. | `border` | A Boolean option (must be `True` or `False`). Controls whether a border is drawn inside and around the table. |
  433. | `bottom_junction_char` | single character string used to draw bottom line junctions. Default: `junction_char`. |
  434. | `bottom_left_junction_char` | Single character string used to draw bottom-left line junctions. Default: `junction_char`. |
  435. | `bottom_right_junction_char` | Single character string used to draw bottom-right line junctions. Default: `junction_char`. |
  436. | `break_on_hyphens` | Whether long lines are wrapped on hyphens. Default: `True`. |
  437. | `header` | A Boolean option (must be `True` or `False`). Controls whether the first row of the table is a header showing the names of all the fields. |
  438. | `horizontal_char` | Single character string used to draw horizontal lines. Default: `-`. |
  439. | `hrules` | Controls printing of horizontal rules after rows. Allowed values: `FRAME`, `HEADER`, `ALL`, `NONE`. |
  440. | `junction_char` | Single character string used to draw line junctions. Default: `+`. |
  441. | `left_junction_char` | Single character string used to draw left line junctions. Default: `junction_char`. |
  442. | `left_padding_width` | Number of spaces on left-hand side of column data. |
  443. | `max_table_width` | Number of characters used for the maximum total table width. |
  444. | `min_table_width` | Number of characters used for the minimum total table width. |
  445. | `padding_width` | Number of spaces on either side of column data (only used if left and right paddings are `None`). |
  446. | `preserve_internal_border` | A Boolean option (must be `True` or `False`). Controls whether borders are still drawn within the table even when `border=False`. |
  447. | `right_junction_char` | Single character string used to draw right line junctions. Default: `junction_char`. |
  448. | `right_padding_width` | Number of spaces on right-hand side of column data. |
  449. | `top_junction_char` | Single character string used to draw top line junctions. Default: `junction_char`. |
  450. | `top_left_junction_char` | Single character string used to draw top-left line junctions. Default: `junction_char`. |
  451. | `top_right_junction_char` | Single character string used to draw top-right line junctions. Default: `junction_char`. |
  452. | `use_header_width` | A Boolean option (must be `True` or `False`). Controls whether the width of the header is used for computing column width. Default: `True`. |
  453. | `vertical_char` | Single character string used to draw vertical lines. Default: `\|`. |
  454. | `vrules` | Controls printing of vertical rules between columns. Allowed values: `FRAME`, `ALL`, `NONE`. |
  455. For options that can be set individually for each column (`align`, `valign`,
  456. `custom_format`, `max_width`, `min_width`, `int_format`, `float_format`, `none_format`)
  457. you can either set a value, that applies to all columns or set a dict with column names
  458. and individual values.
  459. Column-specific options are:
  460. | Option | Details |
  461. | --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  462. | `align` | Controls alignment of fields, one of "l", "c", or "r" or a dictionary with column and value. |
  463. | `custom_format` | Set any format, by setting a function that gets the original value,the formatted representation and returns the new string. E.g. `pf.custom_format["my_col_int"] = lambda f, v: f"{v:,}"`. The type of the callable is `Callable[[str, Any], str]`. This also takes a dictionary with column and function. |
  464. | `float_format` | A string which controls the way floating point data is printed or a dictionary with column and value. This works like: `print("%<float_format>f" % data)`. |
  465. | `int_format` | A string which controls the way integer data is printed or a dictionary with column and value. This works like: `print("%<int_format>d" % data)`. |
  466. | `max_width` | Number of characters used for maximum width of a column or a dictionary with column and value. |
  467. | `min_width` | Number of characters used for minimum width of a column or a dictionary with column and value. |
  468. | `none_format` | Representation of None values or a dictionary with column and value. |
  469. | `valign` | Controls vertical alignment of fields, one of "t", "m", or "b" or a dictionary with column and value. |
  470. You can set the style options to your own settings in two ways:
  471. #### Setting style options for the long term
  472. If you want to print your table with a different style several times, you can set your
  473. option for the long term just by changing the appropriate attributes. If you never want
  474. your tables to have borders you can do this:
  475. ```python
  476. table.border = False
  477. print(table)
  478. print(table)
  479. print(table)
  480. ```
  481. Neither of the 3 tables printed by this will have borders, even if you do things like
  482. add extra rows in between them. The lack of borders will last until you do:
  483. ```python
  484. table.border = True
  485. ```
  486. to turn them on again. This sort of long-term setting is exactly how `set_style` works.
  487. `set_style` just sets a bunch of attributes to pre-set values for you.
  488. Note that if you know what style options you want at the moment you are creating your
  489. table, you can specify them using keyword arguments to the constructor. For example, the
  490. following two code blocks are equivalent:
  491. ```python
  492. table = PrettyTable()
  493. table.border = False
  494. table.header = False
  495. table.padding_width = 5
  496. table = PrettyTable(border=False, header=False, padding_width=5)
  497. ```
  498. #### Changing style options just once
  499. If you don't want to make long-term style changes by changing an attribute like in the
  500. previous section, you can make changes that last for just one `get_string` by giving
  501. those methods keyword arguments. To print two "normal" tables with one borderless table
  502. between them, you could do this:
  503. ```python
  504. print(table)
  505. print(table.get_string(border=False))
  506. print(table)
  507. ```
  508. ### Changing the appearance of your table - with _colors_!
  509. PrettyTable has the functionality of printing your table with ANSI color codes. This
  510. includes support for most Windows versions through
  511. [Colorama](https://pypi.org/project/colorama/). To get started, import the `ColorTable`
  512. class instead of `PrettyTable`.
  513. ```diff
  514. -from prettytable import PrettyTable
  515. +from prettytable.colortable import ColorTable
  516. ```
  517. The `ColorTable` class can be used the same as `PrettyTable`, but it adds an extra
  518. property. You can now specify a custom _theme_ that will format your table with colors.
  519. ```python
  520. from prettytable.colortable import ColorTable, Themes
  521. table = ColorTable(theme=Themes.OCEAN)
  522. print(table)
  523. ```
  524. #### Creating a custom theme
  525. The `Theme` class allows you to customize both the characters and colors used in your
  526. table.
  527. | Argument | Description |
  528. | ---------------------------------------------------------- | --------------------------------------------------------- |
  529. | `default_color` | The color to use as default |
  530. | `vertical_char`, `horizontal_char`, and `junction_char` | The characters used for creating the outline of the table |
  531. | `vertical_color`, `horizontal_color`, and `junction_color` | The colors used to style each character. |
  532. > **Note:** Colors are formatted with the `Theme.format_code(s: str)` function. It
  533. > accepts a string. If the string starts with an escape code (like `\x1b`) then it will
  534. > return the given string. If the string is just whitespace, it will return `""`. If the
  535. > string is a number (like `"34"`), it will automatically format it into an escape code.
  536. > I recommend you look into the source code for more information.
  537. ### Displaying your table in JSON
  538. PrettyTable will also print your tables in JSON, as a list of fields and an array of
  539. rows. Just like in ASCII form, you can actually get a string representation - just use
  540. `get_json_string()`.
  541. ### Displaying your table in MediaWiki markup
  542. PrettyTable can also print your tables in MediaWiki table markup, making it easy to
  543. format tables for wikis. Similar to the ASCII format, you can get a string
  544. representation using `get_mediawiki_string()`.
  545. ### Displaying your table in HTML form
  546. PrettyTable will also print your tables in HTML form, as `<table>`s. Just like in ASCII
  547. form, you can actually get a string representation - just use `get_html_string()`. HTML
  548. printing supports the `fields`, `start`, `end`, `sortby` and `reversesort` arguments in
  549. exactly the same way as ASCII printing.
  550. #### Styling HTML tables
  551. By default, PrettyTable outputs HTML for "vanilla" tables. The HTML code is quite
  552. simple. It looks like this:
  553. ```html
  554. <table>
  555. <thead>
  556. <tr>
  557. <th>City name</th>
  558. <th>Area</th>
  559. <th>Population</th>
  560. <th>Annual Rainfall</th>
  561. </tr>
  562. </thead>
  563. <tbody>
  564. <tr>
  565. <td>Adelaide</td>
  566. <td>1295</td>
  567. <td>1158259</td>
  568. <td>600.5</td>
  569. </tr>
  570. <tr>
  571. <td>Brisbane</td>
  572. <td>5905</td>
  573. <td>1857594</td>
  574. <td>1146.4</td>
  575. ...
  576. </tr>
  577. </tbody>
  578. </table>
  579. ```
  580. If you like, you can ask PrettyTable to do its best to mimic the style options that your
  581. table has set using inline CSS. This is done by giving a `format=True` keyword argument
  582. to `get_html_string` method. Note that if you _always_ want to print formatted HTML you
  583. can do:
  584. ```python
  585. table.format = True
  586. ```
  587. and the setting will persist until you turn it off.
  588. Just like with ASCII tables, if you want to change the table's style for just one
  589. `get_html_string` you can pass those methods' keyword arguments - exactly like `print`
  590. and `get_string`.
  591. #### Setting HTML attributes
  592. You can provide a dictionary of HTML attribute name/value pairs to the `get_html_string`
  593. method using the `attributes` keyword argument. This lets you specify common HTML
  594. attributes like `id` and `class` that can be used for linking to your tables or
  595. customising their appearance using CSS. For example:
  596. ```python
  597. print(table.get_html_string(attributes={"id":"my_table", "class":"red_table"}))
  598. ```
  599. will print:
  600. ```html
  601. <table id="my_table" class="red_table">
  602. <thead>
  603. <tr>
  604. <th>City name</th>
  605. <th>Area</th>
  606. <th>Population</th>
  607. <th>Annual Rainfall</th>
  608. </tr>
  609. </thead>
  610. <tbody>
  611. <tr>
  612. ... ... ...
  613. </tr>
  614. </tbody>
  615. </table>
  616. ```
  617. #### Setting HTML escaping
  618. By default, PrettyTable will escape the data contained in the header and data fields
  619. when sending output to HTML. This can be disabled by setting the `escape_header` and
  620. `escape_data` to false. For example:
  621. ```python
  622. print(table.get_html_string(escape_header=False, escape_data=False))
  623. ```
  624. ### Miscellaneous things
  625. #### Copying a table
  626. You can call the `copy` method on a PrettyTable object without arguments to return an
  627. identical independent copy of the table.
  628. If you want a copy of a PrettyTable object with just a subset of the rows, you can use
  629. list slicing notation:
  630. ```python
  631. new_table = old_table[0:5]
  632. ```
  633. ## Contributing
  634. After editing files, use the [Black](https://github.com/psf/black) linter to auto-format
  635. changed lines.
  636. ```sh
  637. python -m pip install black
  638. black prettytable*.py
  639. ```