Skip to content

Commit

Permalink
Update documentation on macros compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink authored Aug 29, 2024
1 parent 2cb9b08 commit 14a6c4a
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ In your markdown files you can now use:
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).

- 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.
- `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/).
- `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.

## Documentation and how-to guides

Expand Down
39 changes: 20 additions & 19 deletions docs/howto/use_jinja2.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Use jinja2 for automation
# Compatibility with mkdocs-macros-plugin to enable further automation

{% raw %}

`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).
Expand All @@ -11,28 +12,19 @@ plugins:
- table-reader
```
Now you can do cool things like:
## Dynamically load a list of tables
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.
```markdown
# index.md
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:

{% set table_names = ["basic_table.csv","basic_table2.csv"] %}
{% for table_name in table_names %}
```jinja
!!! note "This is a note"
{{ read_csv(table_name) }}

{% endfor %}
{{ read_csv("basic_table.csv") | add_indentation(spaces=4) }}
```

The upside is you now have much more control. A couple of example use cases:

## Insert tables into content tabs

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.

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

=== "index.md"

Expand Down Expand Up @@ -68,7 +60,6 @@ To fix that, you can use the custom _filter_ `add_indendation` (a filter add to
```



## Recursively insert an entire directory of tables

[`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.
Expand Down Expand Up @@ -104,4 +95,14 @@ Now you could do something like:
{% endfor %}
```

{% endraw %}
## Filter a table before inserting it

When you enable the `macros` plugin in your `mkdocs.yml`, `table-reader` will add additional _macros_ and _filters_ (see the [readers overview](../readers.md)).

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:

```
{{ pd_read_csv("numeric_table.csv").query("column_name >= 3") | convert_to_md_table }}
```

{% endraw %}
Loading

0 comments on commit 14a6c4a

Please sign in to comment.