Skip to content

Instantly share code, notes, and snippets.

@edisongustavo
Created March 16, 2021 20:24
Show Gist options
  • Save edisongustavo/d8116e9dc41a9a509a6f2b7c7d74f299 to your computer and use it in GitHub Desktop.
Save edisongustavo/d8116e9dc41a9a509a6f2b7c7d74f299 to your computer and use it in GitHub Desktop.
Prints tables generated with python-tabulate side by side
from tabulate import tabulate
def print_tables_side_by_side(*tables, spacing: int = 3):
string_tables_split = [tabulate(t, headers="firstrow").splitlines() for t in tables]
spacing_str = " " * spacing
num_lines = max(map(len, string_tables_split))
paddings = [max(map(len, s_lines)) for s_lines in string_tables_split]
for i in range(num_lines):
line_each_table = []
for padding, table_lines in zip(paddings, string_tables_split):
if len(table_lines) <= i:
line_each_table.append(" " * (padding + spacing))
else:
line_table_string = table_lines[i]
line_len = len(line_table_string)
line_each_table.append(
line_table_string + (" " * (padding - line_len)) + spacing_str
)
final_line_string = "".join(line_each_table)
print(final_line_string)
@edisongustavo
Copy link
Author

Example:

tables = [
    [["table 0 - name"], ["table 0 - a"]],
    #
    [["table 1 - name", "table 1 - number"], ["table 1 - a", 3], ["table 1 - b", 5]],
    #
    [["table 2 - name"], ["table 2 - a"], ["table 2 - b"], ["table 2 - c"]],
    #
    [
        ["table 3 - name", "table 3 - number", "table 4 - reviews"],
        ["table 3 - a", 103, 105.0],
        ["table 3 - b", 105, 103.0],
        ["table 3 - b", 105, 103.0],
        ["table 3 - b", 105, 103.0],
        ["table 3 - b", 105, 103.0],
    ],
]
print_tables_side_by_side(tables[0], spacing=10)
print()
print("*" * 120)
print()
print_tables_side_by_side(tables[1], spacing=10)
print()
print("*" * 120)
print()
print_tables_side_by_side(*tables[0:2], spacing=10)
print()
print("*" * 120)
print()
print_tables_side_by_side(*tables, spacing=10)

prints:

table 0 - name            
----------------          
table 0 - a               

************************************************************************************************************************

table 1 - name      table 1 - number          
----------------  ------------------          
table 1 - a                        3          
table 1 - b                        5          

************************************************************************************************************************

table 0 - name            table 1 - name      table 1 - number          
----------------          ----------------  ------------------          
table 0 - a               table 1 - a                        3          
                          table 1 - b                        5          

************************************************************************************************************************

table 0 - name            table 1 - name      table 1 - number          table 2 - name            table 3 - name      table 3 - number    table 4 - reviews          
----------------          ----------------  ------------------          ----------------          ----------------  ------------------  -------------------          
table 0 - a               table 1 - a                        3          table 2 - a               table 3 - a                      103                  105          
                          table 1 - b                        5          table 2 - b               table 3 - b                      105                  103          
                                                                        table 2 - c               table 3 - b                      105                  103          
                                                                                                  table 3 - b                      105                  103          
                                                                                                  table 3 - b                      105                  103          


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment