Skip to content

Data tables

Material for MkDocs defines default styles for data tables – an excellent way of rendering tabular data in project documentation. Furthermore, customizations like sortable tables can be achieved with a third-party library and some additional JavaScript.

Configuration

This configuration enables Markdown table support, which should normally be enabled by default, but to be sure, add the following lines to mkdocs.yml:

markdown_extensions:
  - tables

See additional configuration options:

Usage

Data tables can be used at any position in your project documentation and can contain arbitrary Markdown, including inline code blocks, as well as icons and emojis:

Data table
| Method      | Description                          |
| ----------- | ------------------------------------ |
| `GET`       | :material-check:     Fetch resource  |
| `PUT`       | :material-check-all: Update resource |
| `DELETE`    | :material-close:     Delete resource |
Method Description
GET Fetch resource
PUT Update resource
DELETE Delete resource

Column alignment

If you want to align a specific column to the left, center or right, you can use the regular Markdown syntax placing : characters at the beginning and/or end of the divider.

Data table, columns aligned to left
| Method      | Description                          |
| :---------- | :----------------------------------- |
| `GET`       | :material-check:     Fetch resource  |
| `PUT`       | :material-check-all: Update resource |
| `DELETE`    | :material-close:     Delete resource |
Method Description
GET Fetch resource
PUT Update resource
DELETE Delete resource
Data table, columns centered
| Method      | Description                          |
| :---------: | :----------------------------------: |
| `GET`       | :material-check:     Fetch resource  |
| `PUT`       | :material-check-all: Update resource |
| `DELETE`    | :material-close:     Delete resource |
Method Description
GET Fetch resource
PUT Update resource
DELETE Delete resource
Data table, columns aligned to right
| Method      | Description                          |
| ----------: | -----------------------------------: |
| `GET`       | :material-check:     Fetch resource  |
| `PUT`       | :material-check-all: Update resource |
| `DELETE`    | :material-close:     Delete resource |
Method Description
GET Fetch resource
PUT Update resource
DELETE Delete resource

Customization

Sortable tables

If you want to make data tables sortable, you can add tablesort, which is natively integrated with Material for MkDocs and will also work with instant loading via additional JavaScript:

document$.subscribe(function() {
  var tables = document.querySelectorAll("article table:not([class])")
  tables.forEach(function(table) {
    new Tablesort(table)
  })
})
extra_javascript:
  - https://unpkg.com/tablesort@5.3.0/dist/tablesort.min.js
  - javascripts/tablesort.js

After applying the customization, data tables can be sorted by clicking on a column:

Data table, columns sortable
| Method      | Description                          |
| ----------- | ------------------------------------ |
| `GET`       | :material-check:     Fetch resource  |
| `PUT`       | :material-check-all: Update resource |
| `DELETE`    | :material-close:     Delete resource |
Method Description
GET Fetch resource
PUT Update resource
DELETE Delete resource

Note that tablesort provides alternative comparison implementations like numbers, filesizes, dates and month names. See the tablesort documentation for more information.

Import table from file

Plugin

You can also import data from a CSV or Excel file using the plugin mkdocs-table-reader-plugin.

First, you will need to install it with pip:

pip install mkdocs-table-reader-plugin

Then extend the mkdocs.yml file like this:

plugins:
  - table-reader

Then, it is a simple process to import the data in to the Markdown files.

Let's use a CSV in the local directory. The file may look like this:

./data.csv
col1,col2,col3
r1c1,r1c2,r1c3
r2c1,r2c2,r2c3
r3c1,r3c2,r3c3

You can then add it to your Markdown page like this:

./markdown.md
...

{{ read_csv('./data.csv') }}

...

...

col1 col2 col3
r1c1 r1c2 r1c3
r2c1 r2c2 r2c3
r3c1 r3c2 r3c3

...

Let's use an Excel file in the local directory. The file may look like this:

And you can add it to your Markdown page like this:

./markdown.md
...

{{ read_excel('./Book1.xlsx', engine='openpyxl') }}

...

It will then return a result like this:

col1 col2 col3
r1c1 r1c2 r1c3
r2c1 r2c2 r2c3
r3c1 r3c2 r3c3

Warning

You may receive an error if you use engine='openpyxl'.

If this happens, you can resolve it by installing it using pip:

pip install openpyxl

Read more here: pandas.read_excel

Pro Tip: Multiple Sheets

If your Excel file contains multiple sheets, you may want to extend the function by adding the sheet_name parameter.

It would look like this:

./markdown.md
...

{{ read_excel('./Book1.xlsx', engine='openpyxl', sheet_name="Sheet1") }}

...

By default, Pandas will grab the first sheet in the workbook.

Read more here: pandas.read_excel

The plugin mkdocs-table-reader-plugin also provides readers for other formats:

You can read more on their Docs website: mkdocs-table-reader-plugin


Last update: April 14, 2023
Created: April 14, 2023