Customize markdown tables
You can customize the resulting markdown tables!
Theory
Under the hood mkdocs-table-reader-plugin
is basically doing:
import pandas as pd
df = pd.read_csv('path_to_table.csv')
df.to_markdown(index=False, tablefmt='pipe')
Any keyword arguments you give to {{ read_csv('path_to_your_table.csv') }}
will be matched and passed the corresponding pandas.read_csv() and/or
.to_markdown() functions.
Pandas's .to_markdown()
uses the tabulate package and any keyword arguments that are passed to it. Tabulate in turn offers many customization options, see library usage.
Aligning columns
Text columns will be aligned to the left by default, whilst columns which contain only numbers will be aligned to the right. You can override this behaviour using tabulate's custom column alignment. Example:
{{ read_csv('tables/basic_table.csv', colalign=("left",)) }}
a | b |
---|---|
40 | 73 |
50 | 52 |
531456.123 | 80 |
name | table1 |
{{ read_csv('tables/basic_table.csv', colalign=("center",)) }}
a | b |
---|---|
40 | 73 |
50 | 52 |
531456.123 | 80 |
name | table1 |
{{ read_csv('tables/basic_table.csv', colalign=("right",)) }}
a | b |
---|---|
40 | 73 |
50 | 52 |
531456.123 | 80 |
name | table1 |
Sortable tables
If you use mkdocs-material, you can configure sortable tables.
Number formatting
You can use tabulate's number formatting. Example:
{{ read_fwf('tables/fixedwidth_table.txt', floatfmt=".0f") }}
Brand | Price |
---|---|
Honda Civic | 22001 |
Toyota Corolla | 25000 |
Ford Focus | 27000 |
Audi A4 | 35000 |
{{ read_fwf('tables/fixedwidth_table.txt', floatfmt=".1f") }}
Brand | Price |
---|---|
Honda Civic | 22001.0 |
Toyota Corolla | 25000.0 |
Ford Focus | 27000.0 |
Audi A4 | 35000.0 |
{{ read_fwf('tables/fixedwidth_table.txt', floatfmt=".2f") }}
Brand | Price |
---|---|
Honda Civic | 22001.00 |
Toyota Corolla | 25000.00 |
Ford Focus | 27000.00 |
Audi A4 | 35000.00 |