Tabular

Dart library for displaying tabular data in a visually appealing ASCII table format.

Tabular is specifically designed to create tables in the Markdown format that GitHub understands.

The library is inspired by python's tabulate and pretty_table.


import 'package:tabular/tabular.dart';

var data = [
    ['Season', '#', 'Name', 'Days', 'Sun'],
    ['Winter', 1, 'January', 31, 94],
    ['Winter', 2, 'February', 28, 123],
    ['Spring', 3, 'March', 31, 42],
    ['Spring', 4, 'April', 30, 243],
    ['Spring', 5, 'May', 31, 5523],
    ['Summer', 6, 'June', 30, 11251],
    ['Summer', 7, 'July', 31, 17451],
    ['Summer', 8, 'August', 31, 18707],
    ['Autumn', 9, 'September', 30, 7025],
    ['Autumn', 10, 'October', 31, 5041],
    ['Autumn', 11, 'November', 30, 2302],
    ['Winter', 12, 'December', 31, 258],
];

var string = tabular(data);

print(string);
Season |  # | Name      | Days |   Sun
-------|----|-----------|------|------
Winter |  1 | January   |   31 |    94
Winter |  2 | February  |   28 |   123
Spring |  3 | March     |   31 |    42
Spring |  4 | April     |   30 |   243
Spring |  5 | May       |   31 |  5523
Summer |  6 | June      |   30 | 11251
Summer |  7 | July      |   31 | 17451
Summer |  8 | August    |   31 | 18707
Autumn |  9 | September |   30 |  7025
Autumn | 10 | October   |   31 |  5041
Autumn | 11 | November  |   30 |  2302
Winter | 12 | December  |   31 |   258

Formatting

Modify alignment

tabular(data, align: {'Name': Side.center, 'Sun': Side.start})
Season |  # |    Name   | Days | Sun  
-------|----|-----------|------|------
Winter |  1 |  January  |   31 | 94   
Winter |  2 |  February |   28 | 123  
Spring |  3 |   March   |   31 | 42   
Spring |  4 |   April   |   30 | 243  
Spring |  5 |    May    |   31 | 5523 
Summer |  6 |    June   |   30 | 11251
Summer |  7 |   July    |   31 | 17451
Summer |  8 |   August  |   31 | 18707
Autumn |  9 | September |   30 | 7025 
Autumn | 10 |  October  |   31 | 5041 
Autumn | 11 | November  |   30 | 2302 
Winter | 12 |  December |   31 | 258  

Add markdown ':' alignment marks

tabular(data, markdownAlign: true)
Season |  # | Name      | Days |   Sun
-------|---:|-----------|-----:|-----:
Winter |  1 | January   |   31 |    94
Winter |  2 | February  |   28 |   123
Spring |  3 | March     |   31 |    42
Spring |  4 | April     |   30 |   243
Spring |  5 | May       |   31 |  5523
Summer |  6 | June      |   30 | 11251
Summer |  7 | July      |   31 | 17451
Summer |  8 | August    |   31 | 18707
Autumn |  9 | September |   30 |  7025
Autumn | 10 | October   |   31 |  5041
Autumn | 11 | November  |   30 |  2302
Winter | 12 | December  |   31 |   258

Add horizontal dividers

tabular(data, rowDividers: [1, 3, 6, 9, 12])
Season |  # | Name      | Days |   Sun
-------|----|-----------|------|------
Winter |  1 | January   |   31 |    94
Winter |  2 | February  |   28 |   123
-------|----|-----------|------|------
Spring |  3 | March     |   31 |    42
Spring |  4 | April     |   30 |   243
Spring |  5 | May       |   31 |  5523
-------|----|-----------|------|------
Summer |  6 | June      |   30 | 11251
Summer |  7 | July      |   31 | 17451
Summer |  8 | August    |   31 | 18707
-------|----|-----------|------|------
Autumn |  9 | September |   30 |  7025
Autumn | 10 | October   |   31 |  5041
Autumn | 11 | November  |   30 |  2302
-------|----|-----------|------|------
Winter | 12 | December  |   31 |   258

Please be aware that dividers can make the table not conform to the Markdown standard.

Add outer border

tabular(data, border: Border.vertical);
| Season |  # | Name      | Days |   Sun |
|--------|----|-----------|------|-------|
| Winter |  1 | January   |   31 |    94 |
| Winter |  2 | February  |   28 |   123 |
| Spring |  3 | March     |   31 |    42 |
| Spring |  4 | April     |   30 |   243 |
| Spring |  5 | May       |   31 |  5523 |
| Summer |  6 | June      |   30 | 11251 |
| Summer |  7 | July      |   31 | 17451 |
| Summer |  8 | August    |   31 | 18707 |
| Autumn |  9 | September |   30 |  7025 |
| Autumn | 10 | October   |   31 |  5041 |
| Autumn | 11 | November  |   30 |  2302 |
| Winter | 12 | December  |   31 |   258 |

In the same way, you can add Border.horizontal or Border.all. But it can make the table not conform to the Markdown standard.

Switch border style

tabular(data, style: Style.mysql, border: Border.all)
+--------+----+-----------+------+-------+
| Season |  # | Name      | Days |   Sun |
+--------+----+-----------+------+-------+
| Winter |  1 | January   |   31 |    94 |
| Winter |  2 | February  |   28 |   123 |
| Spring |  3 | March     |   31 |    42 |
| Spring |  4 | April     |   30 |   243 |
| Spring |  5 | May       |   31 |  5523 |
| Summer |  6 | June      |   30 | 11251 |
| Summer |  7 | July      |   31 | 17451 |
| Summer |  8 | August    |   31 | 18707 |
| Autumn |  9 | September |   30 |  7025 |
| Autumn | 10 | October   |   31 |  5041 |
| Autumn | 11 | November  |   30 |  2302 |
| Winter | 12 | December  |   31 |   258 |
+--------+----+-----------+------+-------+

Custom column formatting

tabular(data, 
    format: {
        'Name': (value) => value.toUpperCase(), 
        'Sun': (value) => '+ '+value.toString() })
Season |  # | Name      | Days |     Sun
-------|----|-----------|------|--------
Winter |  1 | JANUARY   |   31 |    + 94
Winter |  2 | FEBRUARY  |   28 |   + 123
Spring |  3 | MARCH     |   31 |    + 42
Spring |  4 | APRIL     |   30 |   + 243
Spring |  5 | MAY       |   31 |  + 5523
Summer |  6 | JUNE      |   30 | + 11251
Summer |  7 | JULY      |   31 | + 17451
Summer |  8 | AUGUST    |   31 | + 18707
Autumn |  9 | SEPTEMBER |   30 |  + 7025
Autumn | 10 | OCTOBER   |   31 |  + 5041
Autumn | 11 | NOVEMBER  |   30 |  + 2302
Winter | 12 | DECEMBER  |   31 |   + 258

Sorting

Sort alphabetically by column 0

tabular(data, sort: [Sort(0)]) // 0 is 'Season'
Season |  # | Name      | Days |   Sun
-------|----|-----------|------|------
Autumn |  9 | September |   30 |  7025
Autumn | 10 | October   |   31 |  5041
Autumn | 11 | November  |   30 |  2302
Spring |  3 | March     |   31 |    42
Spring |  4 | April     |   30 |   243
Spring |  5 | May       |   31 |  5523
Summer |  6 | June      |   30 | 11251
Summer |  7 | July      |   31 | 17451
Summer |  8 | August    |   31 | 18707
Winter |  1 | January   |   31 |    94
Winter |  2 | February  |   28 |   123
Winter | 12 | December  |   31 |   258

Sort by 'Days' descending, and then by 'Sun' ascending

tabular(data, sort: [Sort('Days', ascending: false), Sort('Sun')])
Season |  # | Name      | Days |   Sun
-------|----|-----------|------|------
Spring |  3 | March     |   31 |    42
Winter |  1 | January   |   31 |    94
Winter | 12 | December  |   31 |   258
Autumn | 10 | October   |   31 |  5041
Spring |  5 | May       |   31 |  5523
Summer |  7 | July      |   31 | 17451
Summer |  8 | August    |   31 | 18707
Spring |  4 | April     |   30 |   243
Autumn | 11 | November  |   30 |  2302
Autumn |  9 | September |   30 |  7025
Summer |  6 | June      |   30 | 11251
Winter |  2 | February  |   28 |   123

Sort with custom comparison

Ordering the rows by the length of the month name.

tabular(data, 
    sort: [Sort('Name', compare: (a, b) => a.length.compareTo(b.length))])
Season |  # | Name      | Days |   Sun
-------|----|-----------|------|------
Spring |  5 | May       |   31 |  5523
Summer |  6 | June      |   30 | 11251
Summer |  7 | July      |   31 | 17451
Spring |  3 | March     |   31 |    42
Spring |  4 | April     |   30 |   243
Summer |  8 | August    |   31 | 18707
Winter |  1 | January   |   31 |    94
Autumn | 10 | October   |   31 |  5041
Winter |  2 | February  |   28 |   123
Autumn | 11 | November  |   30 |  2302
Winter | 12 | December  |   31 |   258
Autumn |  9 | September |   30 |  7025

Libraries

tabular