timvink / mkdocs-table-reader-plugin

MkDocs plugin that enables a markdown tag like {{ read_csv('table.csv') }} to directly insert various table formats into a page
https://timvink.github.io/mkdocs-table-reader-plugin/
MIT License
120 stars 20 forks source link

Be able to filter tables #73

Closed timvink closed 2 weeks ago

timvink commented 2 weeks ago

With macros enabled, we want to be able to do something like this:

{{ read_csv("basic_table.csv").query("a > 40") }}

That fails with jinja2.exceptions.UndefinedError: 'str object' has no attribute 'query'.

That's because read_csv is actually doing both reading the file and creating the markdown table, and returning the string.

We could register a couple of additional macros, one for each reader, such as: pd_read_csv. That will return a pandas DataFrame that we can process with .query for example.

We also then need to register a filter like convert_to_md_table that does the tabulation for us.

What then would work is:

{{ pd_read_csv("basic_table.csv").query("a > 40") | convert_to_md_table }}

After we get that working, we should also: