Repository: timvink/mkdocs-table-reader-plugin

Issue:
Add a `read_yaml` function
Would be nice to convert `.yml` files to markdown tables.

For the function itself, we can use `pd.io.json.json_normalize`, see:
https://stackoverflow.com/a/35968457/5525118

Discussion:

Patch:
diff --git a/README.md b/README.md
index eef6015..c497bb9 100644
--- a/README.md
+++ b/README.md
@@ -60,6 +60,7 @@ The table reader functions implemented from `pandas`:
 - `{{ read_table() }}` passed to [pandas.read_table()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_table.html).
 - `{{ read_fwf() }}` passed to [pandas.read_fwf()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_fwf.html).
 - `{{ read_excel() }}` passed to [pandas.read_excel()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html).
+- `{{ read_yaml() }}` is parsed with [yaml.safe_load()](https://pyyaml.org/wiki/PyYAMLDocumentation#loading-yaml) and passed to [pandas.json_normalize()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.json_normalize.html).
 
 #### Reading xlsx files
 
diff --git a/mkdocs_table_reader_plugin/plugin.py b/mkdocs_table_reader_plugin/plugin.py
index e74af2a..ac60ba5 100644
--- a/mkdocs_table_reader_plugin/plugin.py
+++ b/mkdocs_table_reader_plugin/plugin.py
@@ -1,6 +1,7 @@
 import os
 import re
 import pandas as pd
+import yaml
 from mkdocs.plugins import BasePlugin
 from mkdocs.config import config_options
 from mkdocs.exceptions import ConfigurationError
@@ -28,11 +29,19 @@ def read_excel(*args, **kwargs):
     return df.to_markdown(index=False, tablefmt="pipe")
 
 
+def read_yaml(*args, **kwargs):
+    with open(args[0], "r") as f:
+        df = pd.json_normalize(yaml.safe_load(f), **kwargs)
+
+    return df.to_markdown(index=False, tablefmt="pipe")
+
+
 READERS = {
     "read_csv": read_csv,
     "read_table": read_table,
     "read_fwf": read_fwf,
     "read_excel": read_excel,
+    "read_yaml": read_yaml,
 }
 
 
diff --git a/setup.py b/setup.py
index aebcd01..9653347 100644
--- a/setup.py
+++ b/setup.py
@@ -24,7 +24,7 @@ setup(
         "Programming Language :: Python :: 3.8",
         "License :: OSI Approved :: MIT License",
     ],
-    install_requires=["mkdocs>=1.0", "pandas>=1.1", "tabulate>=0.8.7"],
+    install_requires=["mkdocs>=1.0", "pandas>=1.1", "tabulate>=0.8.7", "PyYAML>=5.4.1"],
     packages=find_packages(),
     entry_points={
         "mkdocs.plugins": [


Test patch:
diff --git a/tests/fixtures/basic_setup/assets/tables/yaml_table.yml b/tests/fixtures/basic_setup/assets/tables/yaml_table.yml
new file mode 100644
index 0000000..a733f2b
--- /dev/null
+++ b/tests/fixtures/basic_setup/assets/tables/yaml_table.yml
@@ -0,0 +1,8 @@
+- a: 40
+  b: 73
+- a: "50"
+  b: "52"
+- a: 531456
+  b: 80
+- a: name
+  b: table1
diff --git a/tests/fixtures/basic_setup/docs/page_read_yaml.md b/tests/fixtures/basic_setup/docs/page_read_yaml.md
new file mode 100644
index 0000000..893947c
--- /dev/null
+++ b/tests/fixtures/basic_setup/docs/page_read_yaml.md
@@ -0,0 +1,7 @@
+# YAML
+
+## read_yaml
+
+The latest numbers using `read_yaml()`:
+
+{{ read_yaml('assets/tables/yaml_table.yml') }}
diff --git a/tests/test_build.py b/tests/test_build.py
index 87654d3..0b68cf2 100644
--- a/tests/test_build.py
+++ b/tests/test_build.py
@@ -122,6 +122,12 @@ def test_table_output(tmp_path):
     assert re.search(r"35000", contents)
     assert re.search(r"Audi A4", contents)
 
+    # Make sure with markdown tag has the output
+    page_with_tag = tmp_proj / "site/page_read_yaml/index.html"
+    contents = page_with_tag.read_text()
+    assert re.search(r"531456", contents)
+    assert re.search(r"table1", contents)
+
     # Make sure multiple tags are supported
     page_with_tag = tmp_proj / "site/page_read_two_csv/index.html"
     contents = page_with_tag.read_text()