Skip to content

Commit 6fdf150

Browse files
authored
Merge pull request #74 from timvink/73_filter_tables
Additional pandas reader macros
2 parents 47ac5d5 + 14a6c4a commit 6fdf150

File tree

18 files changed

+524
-108
lines changed

18 files changed

+524
-108
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ In your markdown files you can now use:
4040

4141
Where the path is relative to the location of your project's `mkdocs.yml` file, _or_ your project's `docs/` directory, _or_ the location of your markdown source file (all 3 possible locations will be searched, in that order).
4242

43-
- There are [readers](https://timvink.github.io/mkdocs-table-reader-plugin/readers/) for `.csv`, `.fwf`, `.json`, `.xls`, `.xlsx`, `.yaml`, `.feather` and `.tsv` files. There is also the `read_raw()` reader that will allow you to insert tables (or other content) already in markdown format.
44-
- `table-reader` is compatible with [`mkdocs-macros-plugin`](https://mkdocs-macros-plugin.readthedocs.io/en/latest/), which means you can [dynamically insert tables using jinja2 syntax](https://timvink.github.io/mkdocs-table-reader-plugin/howto/use_jinja2/).
43+
- There are [readers](https://timvink.github.io/mkdocs-table-reader-plugin/readers/) available for many common table formats, like `.csv`, `.fwf`, `.json`, `.xls`, `.xlsx`, `.yaml`, `.feather` and `.tsv`. There is also the `read_raw()` reader that will allow you to insert tables (or other content) already in markdown format.
44+
- `table-reader` is compatible with [`mkdocs-macros-plugin`](https://mkdocs-macros-plugin.readthedocs.io/en/latest/). This enables further automation like filtering tables or inserting directories of tables. See the documentation on [compatibility with macros plugin](howto/use_jinja2.md) for more examples.
4545

4646
## Documentation and how-to guides
4747

docs/howto/alternatives.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Downsides:
3939

4040
## Execute python during build
4141

42-
You could also choose to insert the markdown for tables dynamically, using packages like [markdown-exec]() or [mkdocs-macros-plugin](https://mkdocs-macros-plugin.readthedocs.io/).
42+
You could also choose to insert the markdown for tables dynamically, using packages like [markdown-exec](https://pypi.org/project/markdown-exec/).
4343

4444
For example:
4545

docs/howto/customize_tables.md

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ df = pd.read_csv('path_to_table.csv')
1212
df.to_markdown(index=False, tablefmt='pipe')
1313
```
1414

15-
Any keyword arguments you give to <code>\{\{ read_csv('path_to_your_table.csv') \}\}</code> will be matched and passed the corresponding [pandas.read_csv()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html) and/or
15+
{% raw %}
16+
Any keyword arguments you give to `{{ read_csv('path_to_your_table.csv') }}` will be matched and passed the corresponding [pandas.read_csv()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html) and/or
1617
[.to_markdown()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_markdown.html) functions.
18+
{% endraw %}
1719

1820
Pandas's `.to_markdown()` uses the [tabulate](https://pypi.org/project/tabulate/) package and any keyword arguments that are passed to it. Tabulate in turn offers many customization options, see [library usage](https://github.com/astanin/python-tabulate#library-usage).
1921

@@ -23,21 +25,33 @@ Text columns will be aligned to the left [by default](https://github.com/astanin
2325

2426
=== ":arrow_left: left"
2527

26-
<code>\{\{ read_csv('tables/basic_table.csv', colalign=("left",)) \}\}</code>
27-
28+
{% raw %}
29+
```
2830
{{ read_csv('tables/basic_table.csv', colalign=("left",)) }}
31+
```
32+
{% endraw %}
2933

30-
=== ":left_right_arrow: center"
34+
{{ read_csv('tables/basic_table.csv', colalign=("left",)) | add_indentation(spaces=4) }}
3135

32-
<code>\{\{ read_csv('tables/basic_table.csv', colalign=("center",)) \}\}</code>
36+
=== ":left_right_arrow: center"
3337

38+
{% raw %}
39+
```
3440
{{ read_csv('tables/basic_table.csv', colalign=("center",)) }}
41+
```
42+
{% endraw %}
3543

36-
=== ":arrow_right: right"
44+
{{ read_csv('tables/basic_table.csv', colalign=("center",)) | add_indentation(spaces=4) }}
3745

38-
<code>\{\{ read_csv('tables/basic_table.csv', colalign=("right",)) \}\}</code>
46+
=== ":arrow_right: right"
3947

48+
{% raw %}
49+
```
4050
{{ read_csv('tables/basic_table.csv', colalign=("right",)) }}
51+
```
52+
{% endraw %}
53+
54+
{{ read_csv('tables/basic_table.csv', colalign=("right",)) | add_indentation(spaces=4) }}
4155

4256
## Sortable tables
4357

@@ -47,21 +61,33 @@ If you use [mkdocs-material](https://squidfunk.github.io/mkdocs-material), you c
4761

4862
You can use [tabulate](https://pypi.org/project/tabulate/)'s [number formatting](https://github.com/astanin/python-tabulate#number-formatting). Example:
4963

50-
=== ":zero:"
51-
52-
<code>\{\{ read_fwf('tables/fixedwidth_table.txt', floatfmt=".0f") \}\}</code>
64+
=== "zero points"
5365

66+
{% raw %}
67+
```
5468
{{ read_fwf('tables/fixedwidth_table.txt', floatfmt=".0f") }}
69+
```
70+
{% endraw %}
5571

56-
=== ":one:"
72+
{{ read_fwf('tables/fixedwidth_table.txt', floatfmt=".0f") | add_indentation(spaces=4) }}
5773

58-
<code>\{\{ read_fwf('tables/fixedwidth_table.txt', floatfmt=".1f") \}\}</code>
74+
=== "one points"
5975

76+
{% raw %}
77+
```
6078
{{ read_fwf('tables/fixedwidth_table.txt', floatfmt=".1f") }}
79+
```
80+
{% endraw %}
6181

62-
=== ":two:"
82+
{{ read_fwf('tables/fixedwidth_table.txt', floatfmt=".1f") | add_indentation(spaces=4) }}
6383

64-
<code>\{\{ read_fwf('tables/fixedwidth_table.txt', floatfmt=".2f") \}\}</code>
84+
=== "two points"
6585

86+
{% raw %}
87+
```
6688
{{ read_fwf('tables/fixedwidth_table.txt', floatfmt=".2f") }}
89+
```
90+
{% endraw %}
91+
92+
{{ read_fwf('tables/fixedwidth_table.txt', floatfmt=".2f") | add_indentation(spaces=4) }}
6793

docs/howto/preprocess_tables.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Preprocess input tables
22

3+
{% raw %}
4+
35
`mkdocs>=1.4` supports [hooks](https://www.mkdocs.org/user-guide/configuration/#hooks), which enable you to run python scripts on `mkdocs serve` or `mkdocs build`.
46

57
Here are some example of workflows that use hooks and the `table-reader` plugin:
@@ -26,7 +28,7 @@ Here are some example of workflows that use hooks and the `table-reader` plugin:
2628

2729
<code>
2830
My table:
29-
\{\{ read_csv("docs/assets/output_table.csv") \}\}
31+
{{ read_csv("docs/assets/output_table.csv") }}
3032
</code>
3133

3234
=== "mkdocs.yml"
@@ -74,7 +76,7 @@ Here are some example of workflows that use hooks and the `table-reader` plugin:
7476

7577
<code>
7678
My table:
77-
\{\{ read_csv("docs/assets/nyc_data.csv") \}\}
79+
{{ read_csv("docs/assets/nyc_data.csv") }}
7880
</code>
7981

8082
=== "mkdocs.yml"
@@ -101,3 +103,5 @@ Here are some example of workflows that use hooks and the `table-reader` plugin:
101103
```
102104

103105
Note that during development when you use `mkdocs serve` and autoreload, you might not want to run this hook every time you make a change. You could use an environment variable inside your hook, for example something like `if os.environ['disable_hook'] == 1: return None`.
106+
107+
{% endraw %}

docs/howto/project_structure.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Choose a project structure
2+
{% raw %}
23

34
You have different possible strategies to store and load your tables. This guide gives some examples.
45

@@ -23,14 +24,14 @@ If you only want to include an occasional table in a specific markdown file, jus
2324
```md
2425
Here is the table:
2526

26-
\{\{ read_csv("another_table.csv") \}\}
27+
{{ read_csv("another_table.csv") }}
2728
```
2829

2930
In `page.md`, to read `another_table.csv`, you can choose to use:
3031

31-
- <code>\{\{ read_csv("docs/folder/another_table.csv") \}\}</code> (Path relative to mkdocs.yml)
32-
- <code>\{\{ read_csv("folder/another_table.csv") \}\}</code> (Path relative to docs/ directory)
33-
- <code>\{\{ read_csv("another_table.csv") \}\}</code> (Path relative to page source file)
32+
- `{{ read_csv("docs/folder/another_table.csv") }}` (Path relative to mkdocs.yml)
33+
- `{{ read_csv("folder/another_table.csv") }}` (Path relative to docs/ directory)
34+
- `{{ read_csv("another_table.csv") }}` (Path relative to page source file)
3435

3536
## Re-using tables across markdown files
3637

@@ -54,7 +55,8 @@ Given the following project structure:
5455

5556
In `page.md`, to read `another_table.csv`, you can choose to use:
5657

57-
- <code>\{\{ read_csv("docs/assets/tables/another_table.csv") \}\}</code> (Path relative to mkdocs.yml)
58-
- <code>\{\{ read_csv("assets/tables/another_table.csv") \}\}</code> (Path relative to docs/ directory)
59-
- <code>\{\{ read_csv("../assets/tables/another_table.csv") \}\}</code> (Path relative to page source file _(note that `..` stands for "one directory up")_)
58+
- `{{ read_csv("docs/assets/tables/another_table.csv") }}` (Path relative to mkdocs.yml)
59+
- `{{ read_csv("assets/tables/another_table.csv") }}` (Path relative to docs/ directory)
60+
- `{{ read_csv("../assets/tables/another_table.csv") }}` (Path relative to page source file _(note that `..` stands for "one directory up")_)
6061

62+
{% endraw %}

docs/howto/use_jinja2.md

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Use jinja2 for automation
1+
# Compatibility with mkdocs-macros-plugin to enable further automation
2+
3+
{% raw %}
24

35
`table-reader` supports [`mkdocs-macros-plugin`](https://mkdocs-macros-plugin.readthedocs.io/en/latest/), which enables you to use jinja2 syntax inside markdown files (among other things).
46

@@ -10,26 +12,19 @@ plugins:
1012
- table-reader
1113
```
1214
13-
Now you can do cool things like:
14-
15-
## Dynamically load a list of tables
15+
Everything will work as before, _except_ indentation will not be retrained. This means components that rely on indentation (like [Admonitions](https://squidfunk.github.io/mkdocs-material/reference/admonitions/) and [Content tabs](https://squidfunk.github.io/mkdocs-material/reference/content-tabs/#usage)) will break.
1616
17-
```markdown
18-
# index.md
17+
The solution is to use the custom _filter_ `add_indendation` (a filter added to `macros` by `table-reader` plugin, see the [readers](../readers.md)). For example:
1918

20-
{% set table_names = ["basic_table.csv","basic_table2.csv"] %}
21-
{% for table_name in table_names %}
19+
```jinja
20+
!!! note "This is a note"
2221
23-
{ { read_csv(table_name) }}
24-
25-
{% endfor %}
22+
{{ read_csv("basic_table.csv") | add_indentation(spaces=4) }}
2623
```
2724

28-
## Insert tables into content tabs
29-
30-
If you inserted content has multiple lines, then indentation will be not be retained beyond the first line. This means things like [content tabs](https://squidfunk.github.io/mkdocs-material/reference/content-tabs/#usage) will not work as expected.
25+
The upside is you now have much more control. A couple of example use cases:
3126

32-
To fix that, you can use the custom _filter_ `add_indendation` (a filter add to `macros` by `table-reader` plugin). For example:
27+
## Dynamically load a specified list of tables into tabs
3328

3429
=== "index.md"
3530

@@ -39,7 +34,7 @@ To fix that, you can use the custom _filter_ `add_indendation` (a filter add to
3934
4035
=== "{{ table_name }}"
4136
42-
{ { read_csv(table_name) | add_indentation(spaces=4) }}
37+
{{ read_csv(table_name) | add_indentation(spaces=4) }}
4338
4439
{% endfor %}
4540
```
@@ -64,11 +59,6 @@ To fix that, you can use the custom _filter_ `add_indendation` (a filter add to
6459
alternate_style: true
6560
```
6661

67-
!!! note "Note the space in { {"
68-
69-
To avoid the tables being inserted into the code example, we replaced `{{` with `{ {`.
70-
If you copy this example, make sure to fix.
71-
7262

7363
## Recursively insert an entire directory of tables
7464

@@ -100,12 +90,19 @@ Now you could do something like:
10090
10191
{% for table_name in listdir('docs/assets/my_tables") %}
10292
103-
{ { read_csv(table_name) }}
93+
{{ read_csv(table_name) }}
10494
10595
{% endfor %}
10696
```
10797

108-
!!! note "Note the space in { {"
98+
## Filter a table before inserting it
99+
100+
When you enable the `macros` plugin in your `mkdocs.yml`, `table-reader` will add additional _macros_ and _filters_ (see the [readers overview](../readers.md)).
101+
102+
You can use the `pd_<reader>` variants to read a file to a pandas dataframe. Then you can use the pandas methods like [`.query()`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.query.html). For example:
103+
104+
```
105+
{{ pd_read_csv("numeric_table.csv").query("column_name >= 3") | convert_to_md_table }}
106+
```
109107

110-
To avoid the tables being inserted into the code example, we replaced `{{` with `{ {`.
111-
If you copy this example, make sure to fix.
108+
{% endraw %}

docs/options.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ hide:
44
---
55

66
# Options
7+
{% raw %}
78

89
You can customize the plugin by setting options in `mkdocs.yml`. For example:
910

@@ -24,7 +25,7 @@ Default is `.`. Set a default path to the searched directories in order to short
2425

2526
Given a file path, `table-reader` will search for that file relative to your your project's `mkdocs.yml` and relative to your `docs/` folder. If you use a folder for all your table files you can shorten the path specification by setting the `data_path`.
2627

27-
For example, if your table is located in `docs/assets/tables/basic_table.csv`, you can set `data_path` to `docs/assets/tables/`. Then you will be able to use <code>\{\{ read_csv("basic_table.csv") \}\}</code> instead of <code>\{\{ read_csv("docs/assets/tables/basic_table.csv") \}\}</code> inside any markdown page.
28+
For example, if your table is located in `docs/assets/tables/basic_table.csv`, you can set `data_path` to `docs/assets/tables/`. Then you will be able to use `{{ read_csv("basic_table.csv") }}` instead of `{{ read_csv("docs/assets/tables/basic_table.csv") }}` inside any markdown page.
2829

2930
!!! info
3031

@@ -38,7 +39,7 @@ Default: `False`. When enabled, if a filepath is not found, the plugin will rais
3839

3940
## `select_readers`
4041

41-
Default: Selects all available readers. Specify a list of readers to improve documentation build times for very large sites.
42+
Default: Selects all available readers. Specify a list of readers to improve documentation build times for very large sites. This option is ignored when you use this plugin with `mkdocs-macros-plugin` ([read more](howto/use_jinja2.md))
4243

4344
## `enabled`
4445

@@ -57,4 +58,6 @@ Which enables you to disable the plugin locally using:
5758
```bash
5859
export ENABLED_TABLE_READER=false
5960
mkdocs serve
60-
```
61+
```
62+
63+
{% endraw %}

0 commit comments

Comments
 (0)