An intuitive and type-safe Python library for converting lists to fancy ASCII tables for displaying in the terminal or code-blocks

DenverCoder1, updated πŸ•₯ 2023-03-02 14:03:32

table2ascii

build version downloads license discord

An intuitive and type-safe library for converting 2D Python lists to fancy ASCII/Unicode tables

Documentation and examples are available at table2ascii.rtfd.io

πŸ“₯ Installation

pip install -U table2ascii

Requirements: Python 3.7+

πŸ§‘β€πŸ’» Usage

πŸš€ Convert lists to ASCII tables

```py from table2ascii import table2ascii

output = table2ascii( header=["#", "G", "H", "R", "S"], body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]], footer=["SUM", "130", "140", "135", "130"], )

print(output)

""" ╔═════════════════════════════╗ β•‘ # G H R S β•‘ β•Ÿβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’ β•‘ 1 30 40 35 30 β•‘ β•‘ 2 30 40 35 30 β•‘ β•Ÿβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’ β•‘ SUM 130 140 135 130 β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• """ ```

πŸ† Set first or last column headings

```py from table2ascii import table2ascii

output = table2ascii( body=[["Assignment", "30", "40", "35", "30"], ["Bonus", "10", "20", "5", "10"]], first_col_heading=True, )

print(output)

""" ╔════════════╦═══════════════════╗ β•‘ Assignment β•‘ 30 40 35 30 β•‘ β•‘ Bonus β•‘ 10 20 5 10 β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• """ ```

πŸ“° Set column widths and alignments

```py from table2ascii import table2ascii, Alignment

output = table2ascii( header=["Product", "Category", "Price", "Rating"], body=[ ["Milk", "Dairy", "$2.99", "6.283"], ["Cheese", "Dairy", "$10.99", "8.2"], ["Apples", "Produce", "$0.99", "10.00"], ], column_widths=[12, 12, 12, 12], alignments=[Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT, Alignment.DECIMAL], )

print(output)

""" ╔═══════════════════════════════════════════════════╗ β•‘ Product Category Price Rating β•‘ β•Ÿβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’ β•‘ Milk Dairy $2.99 6.283 β•‘ β•‘ Cheese Dairy $10.99 8.2 β•‘ β•‘ Apples Produce $0.99 10.00 β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• """ ```

🎨 Use a preset style

See a list of 30+ preset styles here.

```py from table2ascii import table2ascii, Alignment, PresetStyle

output = table2ascii( header=["First", "Second", "Third", "Fourth"], body=[["10", "30", "40", "35"], ["20", "10", "20", "5"]], column_widths=[10, 10, 10, 10], style=PresetStyle.ascii_box )

print(output)

""" +----------+----------+----------+----------+ | First | Second | Third | Fourth | +----------+----------+----------+----------+ | 10 | 30 | 40 | 35 | +----------+----------+----------+----------+ | 20 | 10 | 20 | 5 | +----------+----------+----------+----------+ """

output = table2ascii( header=["First", "Second", "Third", "Fourth"], body=[["10", "30", "40", "35"], ["20", "10", "20", "5"]], style=PresetStyle.plain, cell_padding=0, alignments=Alignment.LEFT, )

print(output)

""" First Second Third Fourth 10 30 40 35 20 10 20 5 """ ```

🎲 Define a custom style

Check TableStyle for more info and PresetStyle for examples.

```py from table2ascii import table2ascii, TableStyle

my_style = TableStyle.from_string("-..||:+-+:+ ''")

output = table2ascii( header=["First", "Second", "Third"], body=[["10", "30", "40"], ["20", "10", "20"], ["30", "20", "30"]], style=my_style )

print(output)

""" -------.--------.------- | First : Second : Third | +-------:--------:-------+ | 10 : 30 : 40 | | 20 : 10 : 20 | | 30 : 20 : 30 | -------'--------'------- """ ```

πŸͺ„ Merge adjacent cells

```py from table2ascii import table2ascii, Merge, PresetStyle

output = table2ascii( header=["#", "G", "Merge", Merge.LEFT, "S"], body=[ [1, 5, 6, 200, Merge.LEFT], [2, "E", "Long cell", Merge.LEFT, Merge.LEFT], ["Bonus", Merge.LEFT, Merge.LEFT, "F", "G"], ], footer=["SUM", "100", "200", Merge.LEFT, "300"], style=PresetStyle.double_thin_box, first_col_heading=True, )

print(output)

""" ╔═════╦═════╀═══════╀═════╗ β•‘ # β•‘ G β”‚ Merge β”‚ S β•‘ ╠═════╬═════β•ͺ═══╀═══╧═════╣ β•‘ 1 β•‘ 5 β”‚ 6 β”‚ 200 β•‘ β•Ÿβ”€β”€β”€β”€β”€β•«β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’ β•‘ 2 β•‘ E β”‚ Long cell β•‘ β•Ÿβ”€β”€β”€β”€β”€β•¨β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”€β”€β•’ β•‘ Bonus β”‚ F β”‚ G β•‘ ╠═════╦═════╀═══╧═══β•ͺ═════╣ β•‘ SUM β•‘ 100 β”‚ 200 β”‚ 300 β•‘ β•šβ•β•β•β•β•β•©β•β•β•β•β•β•§β•β•β•β•β•β•β•β•§β•β•β•β•β•β• """ ```

βš™οΈ Options

All parameters are optional. At least one of header, body, and footer must be provided.

Refer to the documentation for more information.

| Option | Supported Types | Description | | :-----------------: | :-----------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------: | | header | Sequence[SupportsStr], None
(Default: None) | First table row seperated by header row separator. Values should support str() | | body | Sequence[Sequence[SupportsStr]], None
(Default: None) | 2D List of rows for the main section of the table. Values should support str() | | footer | Sequence[SupportsStr], None
(Default: None) | Last table row seperated by header row separator. Values should support str() | | column_widths | Sequence[Optional[int]], None
(Default: None / automatic) | List of column widths in characters for each column | | alignments | Sequence[Alignment], Alignment, None
(Default: None / all centered) | Column alignments
(ex. [Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT, Alignment.DECIMAL]) | | number_alignments | Sequence[Alignment], Alignment, None
(Default: None) | Column alignments for numeric values. alignments will be used if not specified. | | style | TableStyle
(Default: double_thin_compact) | Table style to use for the table* | | first_col_heading | bool
(Default: False) | Whether to add a heading column separator after the first column | | last_col_heading | bool
(Default: False) | Whether to add a heading column separator before the last column | | cell_padding | int
(Default: 1) | The minimum number of spaces to add between the cell content and the cell border | | use_wcwidth | bool
(Default: True) | Whether to use wcwidth instead of len() to calculate cell width |

*See a list of all preset styles here.

See the API Reference for more info.

πŸ‘¨β€πŸŽ¨ Use cases

πŸ—¨οΈ Discord messages and embeds

  • Display tables nicely inside markdown code blocks on Discord
  • Useful for making Discord bots with Discord.py

image

πŸ’» Terminal outputs

  • Tables display nicely whenever monospace fonts are fully supported
  • Tables make terminal outputs look more professional

image

πŸ€— Contributing

Contributions are welcome!

See CONTRIBUTING.md for more details on how to get involved.

Issues

chore(deps-dev): bump sphinx-book-theme from 0.3.3 to 1.0.0

opened on 2023-03-02 14:03:32 by dependabot[bot]

Bumps sphinx-book-theme from 0.3.3 to 1.0.0.

Release notes

Sourced from sphinx-book-theme's releases.

v1.0.0

This is a major release that brings in the latest PyData Sphinx Theme, Bootstrap 5, and updates many features.

See the Sphinx Book Theme changelog for details.

Contributors

@​12rambau (activity) | @​AakashGfude (activity) | @​akhmerov (activity) | @​AllenDowney (activity) | @​avivajpeyi (activity) | @​bmorris3 (activity) | @​choldgraf (activity) | @​chrisjsewell (activity) | @​consideRatio (activity) | @​feanil (activity) | @​fm75 (activity) | @​ghutchis (activity) | @​guillaume-haerinck (activity) | @​haklc (activity) | @​iasbusi (activity) | @​ivirshup (activity) | @​James11222 (activity) | @​kloczek (activity) | @​ksuess (activity) | @​martinfleis (activity) | @​mathbunnyru (activity) | @​mcjulian1107 (activity) | @​melund (activity) | @​mmcky (activity) | @​paugier (activity) | @​PhilipVinc (activity) | @​pradyunsg (activity) | @​rkdarst (activity) | @​rossbar (activity) | @​scmmmh (activity) | @​sieler (activity) | @​SilverRainZ (activity) | @​stevepiercy (activity) | @​trallard (activity) | @​whyjz (activity) | @​yuvipanda (activity) | @​ZedThree (activity)

v1.0.0rc3

See https://github.com/executablebooks/sphinx-book-theme/releases/tag/v1.0.0rc1 for release notes

v1.0.0rc2

See https://github.com/executablebooks/sphinx-book-theme/releases/tag/v1.0.0rc1 for release notes

v1.0.0rc1

A release candidate for v1.0 of this theme. We're making it 1.0 because there are several major breaking changes!

pip install -U --pre sphinx-book-theme

Leave comments in this issue:

Release notes (work in progress)

Enhancements made

Bugs fixed

Maintenance and upkeep improvements

Documentation improvements

... (truncated)

Changelog

Sourced from sphinx-book-theme's changelog.

v1.0.0 -- 2023-03-01

This is a major release that brings in the latest PyData Sphinx Theme and a number of new features with it. It also overhauls and standardizes the HTML structure of the theme. Because of this large refactor, we are bumping the major version to 1.0. Note that this doesn't imply any new long-term support or stability, we will continue to try not to make major breaking changes but will continue incrementing major versions if we must do so.

Enhancements made

Bugs fixed

Maintenance and upkeep improvements

Documentation improvements

Breaking changes to structure and API

Tips for migration

Long sidebar entries now wrap. If you'd like to un-do this and revert to old behavior (where they are cut off if too long), then use the following CSS rule in your custom Sphinx CSS:

.bd-sidebar-primary a {
  word-wrap: unset;
</tr></table> 

... (truncated)

Commits


Dependabot compatibility score

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)

Releases

v1.1.1 2023-02-28 12:05:13

Bug Fixes

  • Include py.typed in package data for mypy to recognize types (https://github.com/DenverCoder1/table2ascii/pull/100)

Meta

  • All exceptions, warnings, and SupportsStr are now importable directly through the table2ascii module (https://github.com/DenverCoder1/table2ascii/pull/94)
  • All class and data references in the docs were fixed to link to the proper documentation (https://github.com/DenverCoder1/table2ascii/pull/94)
  • Changed TableStyle.set() example to one that will not throw an exception (https://github.com/DenverCoder1/table2ascii/pull/94)

Full Changelog: https://github.com/DenverCoder1/table2ascii/compare/v1.1.0...v1.1.1

1.1.0 2022-12-29 00:32:12

Features

  • Added Alignment.DECIMAL for aligning numbers to a decimal point in https://github.com/DenverCoder1/table2ascii/pull/90
  • Added ability to align all columns with a single Alignment instead of a list in https://github.com/DenverCoder1/table2ascii/pull/91
  • Support for aligning numbers separately from other strings by passing number_alignments to table2ascii in https://github.com/DenverCoder1/table2ascii/pull/92

Meta

  • Moved version number to pyproject.toml in https://github.com/DenverCoder1/table2ascii/pull/87

Full Changelog: https://github.com/DenverCoder1/table2ascii/compare/v1.0.4...v1.1.0

1.0.4 2022-12-19 21:50:08

Bug Fixes

  • Made dependencies and other build arguments static by @ohjunseung in https://github.com/DenverCoder1/table2ascii/pull/86

New Contributors

  • @ohjunseung made their first contribution in https://github.com/DenverCoder1/table2ascii/pull/86

Full Changelog: https://github.com/DenverCoder1/table2ascii/compare/v1.0.3...v1.0.4

1.0.3 2022-12-19 00:13:51

Bug Fixes

  • Fix setup error occurring when installing on Windows by @DenverCoder1 in https://github.com/DenverCoder1/table2ascii/pull/85

Meta

  • Added CI step for testing the project on Windows by @DenverCoder1 in https://github.com/DenverCoder1/table2ascii/pull/85

Full Changelog: https://github.com/DenverCoder1/table2ascii/compare/v1.0.2...v1.0.3

1.0.2 2022-12-18 01:04:12

What's Changed

  • Added invalid column width error for negative column widths by @DenverCoder1 in https://github.com/DenverCoder1/table2ascii/pull/83

Meta

  • docs(readme): fixed action build badge by @DenverCoder1 in https://github.com/DenverCoder1/table2ascii/pull/81
  • docs: Updated build status badge by @DenverCoder1 in https://github.com/DenverCoder1/table2ascii/pull/84
  • ci: Updated publish script to use build module by @DenverCoder1 in https://github.com/DenverCoder1/table2ascii/pull/82

Full Changelog: https://github.com/DenverCoder1/table2ascii/compare/1.0.1...v1.0.2

1.0.1 2022-12-14 22:54:25

What's Changed

  • Resolved pyproject and setup.py conflicts by @DenverCoder1 in https://github.com/DenverCoder1/table2ascii/pull/80

Full Changelog: https://github.com/DenverCoder1/table2ascii/compare/1.0.0...1.0.1

Jonah Lawrence

πŸ“Ί https://youtube.com/DevProTips πŸ’» Full Stack Dev 🎨 12+ years of web dev & design πŸ“š Always learning!

GitHub Repository Homepage

unicode ascii utils utilities python markdown terminal discord discord-py formatter pretty-print graphics terminal-graphics hacktoberfest