2020-08-04 22:22:47 +02:00
|
|
|
|
---
|
2021-12-16 17:08:57 +01:00
|
|
|
|
icon: material/table-edit
|
2020-08-04 22:22:47 +02:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# Data tables
|
|
|
|
|
|
|
|
|
|
Material for MkDocs defines default styles for data tables – an excellent way
|
|
|
|
|
of rendering tabular data in project documentation. Furthermore, customizations
|
2021-10-03 18:29:50 +02:00
|
|
|
|
like [sortable tables] can be achieved with a third-party library and some
|
|
|
|
|
[additional JavaScript].
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
2021-10-03 18:29:50 +02:00
|
|
|
|
[sortable tables]: #sortable-tables
|
|
|
|
|
[additional JavaScript]: ../customization.md#additional-javascript
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
2021-10-03 18:29:50 +02:00
|
|
|
|
This configuration enables Markdown table support, which should normally be
|
|
|
|
|
enabled by default, but to be sure, add the following lines to `mkdocs.yml`:
|
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
markdown_extensions:
|
|
|
|
|
- tables
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
See additional configuration options:
|
|
|
|
|
|
|
|
|
|
- [Tables]
|
|
|
|
|
|
|
|
|
|
[Tables]: ../setup/extensions/python-markdown.md#tables
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
|
|
|
|
## 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
|
2022-01-10 14:31:58 +01:00
|
|
|
|
emojis]:
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
``` markdown title="Data table"
|
2020-08-04 22:22:47 +02:00
|
|
|
|
| Method | Description |
|
|
|
|
|
| ----------- | ------------------------------------ |
|
|
|
|
|
| `GET` | :material-check: Fetch resource |
|
|
|
|
|
| `PUT` | :material-check-all: Update resource |
|
|
|
|
|
| `DELETE` | :material-close: Delete resource |
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
|
|
|
|
| Method | Description |
|
|
|
|
|
| ----------- | ------------------------------------ |
|
|
|
|
|
| `GET` | :material-check: Fetch resource |
|
|
|
|
|
| `PUT` | :material-check-all: Update resource |
|
|
|
|
|
| `DELETE` | :material-close: Delete resource |
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
2021-10-03 18:29:50 +02:00
|
|
|
|
[icons and emojis]: icons-emojis.md
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
|
|
|
|
### Column alignment
|
|
|
|
|
|
|
|
|
|
If you want to align a specific column to the `left`, `center` or `right`, you
|
2021-10-03 18:29:50 +02:00
|
|
|
|
can use the [regular Markdown syntax] placing `:` characters at the beginning
|
2020-08-04 22:22:47 +02:00
|
|
|
|
and/or end of the divider.
|
|
|
|
|
|
|
|
|
|
=== "Left"
|
|
|
|
|
|
2022-01-11 10:19:33 +01:00
|
|
|
|
``` markdown hl_lines="2" title="Data table, columns aligned to left"
|
2020-08-04 22:22:47 +02:00
|
|
|
|
| Method | Description |
|
|
|
|
|
| :---------- | :----------------------------------- |
|
|
|
|
|
| `GET` | :material-check: Fetch resource |
|
|
|
|
|
| `PUT` | :material-check-all: Update resource |
|
|
|
|
|
| `DELETE` | :material-close: Delete resource |
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
|
|
|
|
| Method | Description |
|
|
|
|
|
| :---------- | :----------------------------------- |
|
|
|
|
|
| `GET` | :material-check: Fetch resource |
|
|
|
|
|
| `PUT` | :material-check-all: Update resource |
|
|
|
|
|
| `DELETE` | :material-close: Delete resource |
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
=== "Center"
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
2022-01-11 10:19:33 +01:00
|
|
|
|
``` markdown hl_lines="2" title="Data table, columns centered"
|
2020-08-04 22:22:47 +02:00
|
|
|
|
| Method | Description |
|
|
|
|
|
| :---------: | :----------------------------------: |
|
|
|
|
|
| `GET` | :material-check: Fetch resource |
|
|
|
|
|
| `PUT` | :material-check-all: Update resource |
|
|
|
|
|
| `DELETE` | :material-close: Delete resource |
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
|
|
|
|
| Method | Description |
|
|
|
|
|
| :---------: | :----------------------------------: |
|
|
|
|
|
| `GET` | :material-check: Fetch resource |
|
|
|
|
|
| `PUT` | :material-check-all: Update resource |
|
|
|
|
|
| `DELETE` | :material-close: Delete resource |
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
=== "Right"
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
2022-01-11 10:19:33 +01:00
|
|
|
|
``` markdown hl_lines="2" title="Data table, columns aligned to right"
|
2020-08-04 22:22:47 +02:00
|
|
|
|
| Method | Description |
|
|
|
|
|
| ----------: | -----------------------------------: |
|
|
|
|
|
| `GET` | :material-check: Fetch resource |
|
|
|
|
|
| `PUT` | :material-check-all: Update resource |
|
|
|
|
|
| `DELETE` | :material-close: Delete resource |
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
|
|
|
|
| Method | Description |
|
|
|
|
|
| ----------: | -----------------------------------: |
|
|
|
|
|
| `GET` | :material-check: Fetch resource |
|
|
|
|
|
| `PUT` | :material-check-all: Update resource |
|
|
|
|
|
| `DELETE` | :material-close: Delete resource |
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
2021-10-03 18:29:50 +02:00
|
|
|
|
[regular Markdown syntax]: https://www.markdownguide.org/extended-syntax/#tables
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
|
|
|
|
## Customization
|
|
|
|
|
|
|
|
|
|
### Sortable tables
|
|
|
|
|
|
2021-10-03 18:29:50 +02:00
|
|
|
|
If you want to make data tables sortable, you can add [tablesort], which is
|
2020-08-06 08:59:27 +02:00
|
|
|
|
natively integrated with Material for MkDocs and will also work with [instant
|
2021-10-03 18:29:50 +02:00
|
|
|
|
loading] via [additional JavaScript]:
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
=== ":octicons-file-code-16: `docs/javascripts/tablesort.js`"
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
|
|
|
|
``` js
|
2021-02-22 22:27:30 +01:00
|
|
|
|
document$.subscribe(function() {
|
2021-12-06 23:51:34 +01:00
|
|
|
|
var tables = document.querySelectorAll("article table:not([class])")
|
2020-08-04 22:22:47 +02:00
|
|
|
|
tables.forEach(function(table) {
|
|
|
|
|
new Tablesort(table)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
```
|
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
=== ":octicons-file-code-16: `mkdocs.yml`"
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
extra_javascript:
|
2022-04-04 09:46:22 +02:00
|
|
|
|
- https://unpkg.com/tablesort@5.3.0/dist/tablesort.min.js
|
2021-10-10 12:19:14 +02:00
|
|
|
|
- javascripts/tablesort.js
|
2020-08-04 22:22:47 +02:00
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
After applying the customization, data tables can be sorted by clicking on a
|
|
|
|
|
column:
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
2022-01-11 10:19:33 +01:00
|
|
|
|
``` markdown title="Data table, columns sortable"
|
2020-08-04 22:22:47 +02:00
|
|
|
|
| Method | Description |
|
|
|
|
|
| ----------- | ------------------------------------ |
|
|
|
|
|
| `GET` | :material-check: Fetch resource |
|
|
|
|
|
| `PUT` | :material-check-all: Update resource |
|
|
|
|
|
| `DELETE` | :material-close: Delete resource |
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
<div class="result" markdown>
|
2020-08-04 22:22:47 +02:00
|
|
|
|
|
|
|
|
|
| Method | Description |
|
|
|
|
|
| ----------- | ------------------------------------ |
|
|
|
|
|
| `GET` | :material-check: Fetch resource |
|
|
|
|
|
| `PUT` | :material-check-all: Update resource |
|
|
|
|
|
| `DELETE` | :material-close: Delete resource |
|
|
|
|
|
|
2022-01-10 14:31:58 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
Note that [tablesort] provides alternative comparison implementations like
|
|
|
|
|
numbers, filesizes, dates and month names. See the [tablesort documentation]
|
|
|
|
|
[tablesort] for more information.
|
|
|
|
|
|
2022-04-04 09:46:22 +02:00
|
|
|
|
<script src="https://unpkg.com/tablesort@5.3.0/dist/tablesort.min.js"></script>
|
2020-08-04 22:22:47 +02:00
|
|
|
|
<script>
|
|
|
|
|
var tables = document.querySelectorAll("article table")
|
|
|
|
|
new Tablesort(tables.item(tables.length - 1));
|
|
|
|
|
</script>
|
|
|
|
|
|
2021-10-03 18:29:50 +02:00
|
|
|
|
[tablesort]: http://tristen.ca/tablesort/demo/
|
|
|
|
|
[instant loading]: ../setup/setting-up-navigation.md#instant-loading
|
2023-03-05 17:50:19 +01:00
|
|
|
|
|
|
|
|
|
### Import table from file
|
|
|
|
|
|
|
|
|
|
[:octicons-cpu-24: Plugin][table-reader-docs]
|
|
|
|
|
|
|
|
|
|
You can also import data from a CSV or Excel file using the plugin [`mkdocs-table-reader-plugin`][table-reader-docs].
|
|
|
|
|
|
|
|
|
|
First, you will need to install it with `pip`:
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
pip install mkdocs-table-reader-plugin
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Then extend the `mkdocs.yml` file like this:
|
|
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
|
plugins:
|
|
|
|
|
- table-reader
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Then, it is a simple process to import the data in to the Markdown files.
|
|
|
|
|
|
|
|
|
|
=== "Import data from :fontawesome-solid-file-csv: CSV file"
|
|
|
|
|
|
|
|
|
|
Let's use a :fontawesome-solid-file-csv: CSV in the local directory. The file may look like this:
|
|
|
|
|
|
|
|
|
|
```csv title="./data.csv"
|
|
|
|
|
col1,col2,col3
|
|
|
|
|
r1c1,r1c2,r1c3
|
|
|
|
|
r2c1,r2c2,r2c3
|
|
|
|
|
r3c1,r3c2,r3c3
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
You can then add it to your :fontawesome-solid-file-arrow-down: Markdown page like this:
|
|
|
|
|
|
|
|
|
|
```md title="./markdown.md"
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
{{ read_csv('./data.csv') }}
|
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
<div class="result" markdown>
|
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
col1|col2|col3
|
|
|
|
|
----|----|----
|
|
|
|
|
r1c1|r1c2|r1c3
|
|
|
|
|
r2c1|r2c2|r2c3
|
|
|
|
|
r3c1|r3c2|r3c3
|
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
=== "Import data from :fontawesome-solid-file-excel: Excel file"
|
|
|
|
|
|
|
|
|
|
Let's use an :fontawesome-solid-file-excel: Excel file in the local directory. The file may look like this:
|
|
|
|
|
|
|
|
|
|
![][excel-file]{width="300px"}
|
|
|
|
|
|
|
|
|
|
[excel-file]: https://i.stack.imgur.com/f32ks.png
|
|
|
|
|
|
|
|
|
|
And you can add it to your :fontawesome-solid-file-arrow-down: Markdown page like this:
|
|
|
|
|
|
|
|
|
|
```md title="./markdown.md"
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
{{ read_excel('./Book1.xlsx', engine='openpyxl') }}
|
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
<div class="result" markdown>
|
|
|
|
|
|
|
|
|
|
It will then return a result like this:
|
|
|
|
|
|
|
|
|
|
col1|col2|col3
|
|
|
|
|
----|----|----
|
|
|
|
|
r1c1|r1c2|r1c3
|
|
|
|
|
r2c1|r2c2|r2c3
|
|
|
|
|
r3c1|r3c2|r3c3
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
!!! warning "Warning"
|
|
|
|
|
|
|
|
|
|
You may receive an error if you use `engine='openpyxl'`.
|
|
|
|
|
|
|
|
|
|
If this happens, you can resolve it by installing it using `pip`:
|
|
|
|
|
|
|
|
|
|
```sh
|
2023-04-05 11:44:22 +02:00
|
|
|
|
pip install openpyxl
|
2023-03-05 17:50:19 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Read more here: [pandas.read_excel][pandas-read_excel-engine]
|
|
|
|
|
|
|
|
|
|
[pandas-read_excel-engine]: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html#:~:text=enginestr%2C%20default%20None
|
|
|
|
|
|
|
|
|
|
!!! tip "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:
|
|
|
|
|
|
|
|
|
|
```md title="./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][pandas-read_excel-sheet_name]
|
|
|
|
|
|
|
|
|
|
[pandas-read_excel-sheet_name]: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html#:~:text=sheet_namestr%2C%20int%2C%20list%2C%20or%20None%2C%20default%200
|
|
|
|
|
|
|
|
|
|
=== "Import data from other file types"
|
|
|
|
|
|
|
|
|
|
The plugin [`mkdocs-table-reader-plugin`][table-reader-docs] also provides readers for other formats:
|
|
|
|
|
|
|
|
|
|
<div class="mdx-columns" markdown>
|
|
|
|
|
|
|
|
|
|
- [`read_csv`][table-reader-read_csv]
|
|
|
|
|
- [`read_fwf`][table-reader-read_fwf]
|
|
|
|
|
- [`read_yaml`][table-reader-read_yaml]
|
|
|
|
|
- [`read_table`][table-reader-read_table]
|
|
|
|
|
- [`read_json`][table-reader-read_json]
|
|
|
|
|
- [`read_excel`][table-reader-read_excel]
|
|
|
|
|
- [`read_raw`][table-reader-read_raw]
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
You can read more on their Docs website: [mkdocs-table-reader-plugin][table-reader-docs]
|
|
|
|
|
|
|
|
|
|
[table-reader-docs]: https://timvink.github.io/mkdocs-table-reader-plugin/
|
|
|
|
|
[table-reader-read_csv]: https://timvink.github.io/mkdocs-table-reader-plugin/readers/#read_csv
|
|
|
|
|
[table-reader-read_fwf]: https://timvink.github.io/mkdocs-table-reader-plugin/readers/#read_fwf
|
|
|
|
|
[table-reader-read_yaml]: https://timvink.github.io/mkdocs-table-reader-plugin/readers/#read_yaml
|
|
|
|
|
[table-reader-read_table]: https://timvink.github.io/mkdocs-table-reader-plugin/readers/#read_table
|
|
|
|
|
[table-reader-read_json]: https://timvink.github.io/mkdocs-table-reader-plugin/readers/#read_json
|
|
|
|
|
[table-reader-read_excel]: https://timvink.github.io/mkdocs-table-reader-plugin/readers/#read_excel
|
|
|
|
|
[table-reader-read_raw]: https://timvink.github.io/mkdocs-table-reader-plugin/readers/#read_raw
|