Skip to content

Commit 4902747

Browse files
authored
Use macros in table-reader docs
1 parent cd9e558 commit 4902747

File tree

8 files changed

+130
-62
lines changed

8 files changed

+130
-62
lines changed

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: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Use jinja2 for automation
2+
{% raw %}
23

34
`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).
45

@@ -14,17 +15,19 @@ Now you can do cool things like:
1415
1516
## Dynamically load a list of tables
1617
18+
1719
```markdown
1820
# index.md
1921

2022
{% set table_names = ["basic_table.csv","basic_table2.csv"] %}
2123
{% for table_name in table_names %}
2224

23-
{ { read_csv(table_name) }}
25+
{{ read_csv(table_name) }}
2426

2527
{% endfor %}
2628
```
2729

30+
2831
## Insert tables into content tabs
2932

3033
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.
@@ -39,7 +42,7 @@ To fix that, you can use the custom _filter_ `add_indendation` (a filter add to
3942

4043
=== "{{ table_name }}"
4144

42-
{ { read_csv(table_name) | add_indentation(spaces=4) }}
45+
{{ read_csv(table_name) | add_indentation(spaces=4) }}
4346

4447
{% endfor %}
4548
```
@@ -64,10 +67,6 @@ To fix that, you can use the custom _filter_ `add_indendation` (a filter add to
6467
alternate_style: true
6568
```
6669

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.
7170

7271

7372
## Recursively insert an entire directory of tables
@@ -100,12 +99,9 @@ Now you could do something like:
10099

101100
{% for table_name in listdir('docs/assets/my_tables") %}
102101

103-
{ { read_csv(table_name) }}
102+
{{ read_csv(table_name) }}
104103

105104
{% endfor %}
106105
```
107106

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

docs/options.md

Lines changed: 5 additions & 2 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

@@ -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)