METADATA 30 KB

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