Skip to content

Commit 47ac5d5

Browse files
authored
update docs on how to use jinja, see #72
1 parent 5dcaeca commit 47ac5d5

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

docs/howto/preprocess_tables.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ Here are some example of workflows that use hooks and the `table-reader` plugin:
5454
└── mkdocs.yml
5555
```
5656

57+
!!! note "Alternative: use jinja"
58+
59+
You can also use jinja2 to display a list of tables. See how to [use jinja2 for automation](use_jinja2.md).
60+
5761
## Download a table from an API
5862

5963
=== "hooks.py"

docs/howto/use_jinja2.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Using jinja2
1+
# Use jinja2 for automation
22

33
`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).
44

@@ -10,8 +10,9 @@ plugins:
1010
- table-reader
1111
```
1212
13-
Now you can do cool things like dynamically load a list of tables:
13+
Now you can do cool things like:
1414
15+
## Dynamically load a list of tables
1516
1617
```markdown
1718
# index.md
@@ -22,10 +23,9 @@ Now you can do cool things like dynamically load a list of tables:
2223
{ { read_csv(table_name) }}
2324

2425
{% endfor %}
25-
2626
```
2727

28-
## Indented content like content tabs
28+
## Insert tables into content tabs
2929

3030
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.
3131

@@ -69,3 +69,43 @@ To fix that, you can use the custom _filter_ `add_indendation` (a filter add to
6969
To avoid the tables being inserted into the code example, we replaced `{{` with `{ {`.
7070
If you copy this example, make sure to fix.
7171

72+
73+
## Recursively insert an entire directory of tables
74+
75+
[`mkdocs-macros-plugin`](https://mkdocs-macros-plugin.readthedocs.io/en/latest/) enables you to define additional functions (called _macros_) that you will be able to use within your markdown files.
76+
See their documentation on how to set this up. Here's an example with some functions to interact with the filesystem:
77+
78+
```python
79+
def define_env(env):
80+
"""
81+
Register additional mkdocs-macros-plugin functions that can be used as macros in markdown files.
82+
"""
83+
@env.macro
84+
def listdir(path):
85+
return os.listdir(path)
86+
87+
@env.macro
88+
def path_exists(path):
89+
return Path(path).exists()
90+
91+
@env.macro
92+
def is_file(path):
93+
return Path(path).is_file()
94+
```
95+
96+
Now you could do something like:
97+
98+
```markdown
99+
# index.md
100+
101+
{% for table_name in listdir('docs/assets/my_tables") %}
102+
103+
{ { read_csv(table_name) }}
104+
105+
{% endfor %}
106+
```
107+
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.

0 commit comments

Comments
 (0)