Skip to content

Add format customization #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

snowinmars
Copy link

@snowinmars snowinmars commented Apr 3, 2020

Hi.

I use the tabulate lib to create tables for Wikipedia.
The're several formats that doesn't supported by tabulate right now.

So, there're two options:

  • I can add a new format and another one and another one,
  • I can add a format customization.

I think, I need a way to override any char in the table format.

That's how I use it right now, but I'm not an experience python dev, so maybe you can suggest a better way to implement it.

If it's ok, I will write a documentation. If it's not - I can refactor the code.

The CI fails due to a required package is not installed. I don't like the idea to add a non-default package, but I will leave it as is for now.

@astanin
Copy link
Owner

astanin commented Apr 7, 2020

Hi,

I am reluctant to add a new dependency to replace namedtuple with a mutable recordclass. It may address your specific use case, but I suppose performance and compatibility consequences are not easy to predict. I would consider using new Python dataclasses if they were backwards compatible.

In your case I suggest to create a mini library with the formats you need, and then pass TableFormat value as tablefmt parameter.

# myformat.py
import tabulate as T

myformat = T.TableFormat(
         lineabove=T.Line("+", "-", "+", "+"),
         linebelowheader=T.Line("+", "=", "+", "+"),
         linebetweenrows=T.Line("+", "-", "+", "+"),
         linebelow=T.Line("+", "-", "+", "+"),
         headerrow=T.DataRow("|", "|", "|"),
         datarow=T.DataRow("|", "|", "|"),
         padding=1,
         with_header_hide=None)
>>> from tabulate import tabulate
>>> from myformat import *
>>> print(tabulate([[1,"foo"],[2,"bar"]], tablefmt=myformat))
+---+-----+
| 1 | foo |
+---+-----+
| 2 | bar |
+---+-----+

@astanin astanin closed this Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants