diff --git a/README.md b/README.md index 314eb19..2631cec 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ # mkdocs-table-reader-plugin -[MkDocs](https://www.mkdocs.org/) plugin that adds a `{{ read_csv('table.csv') }}` markdown tag to directly insert CSV files as a table into a page. Other supported table file formats are excel (`.xls`, `.xlsx`), fixed-width (`.fwf`), and yaml (`.yaml`). +[MkDocs](https://www.mkdocs.org/) plugin that adds a `{{ read_csv('table.csv') }}` markdown tag to directly insert CSV files as a table into a page. Other supported table file formats are excel (`.xls`, `.xlsx`), fixed-width (`.fwf`), json (`.json`) and yaml (`.yaml`). > For a workflow with other plugins see the blogpost [building reproducible reports with MkDocs](https://timvink.nl/reproducible-reports-with-mkdocs/) diff --git a/docs/readers.md b/docs/readers.md index f9bb3c3..ea7890a 100644 --- a/docs/readers.md +++ b/docs/readers.md @@ -58,6 +58,18 @@ The following table reader functions are available: {{ read_table('tables/basic_table.csv', sep = ',') }} +## read_json + +`{{ read_json() }}` passed to [pandas.read_json()](https://pandas.pydata.org/docs/reference/api/pandas.read_json.html). Example: + +=== "Input" + + \{\{ read_json('tables/data.json') \}\} + +=== "Output" + + {{ read_json('tables/data.json') }} + ## read_excel diff --git a/mkdocs_table_reader_plugin/plugin.py b/mkdocs_table_reader_plugin/plugin.py index 61f1c84..7ba8596 100644 --- a/mkdocs_table_reader_plugin/plugin.py +++ b/mkdocs_table_reader_plugin/plugin.py @@ -61,6 +61,16 @@ def read_fwf(*args, **kwargs): return df.to_markdown(**markdown_kwargs) +def read_json(*args, **kwargs): + read_kwargs = kwargs_in_func(kwargs, pd.read_json) + df = pd.read_json(*args, **read_kwargs) + + markdown_kwargs = kwargs_not_in_func(kwargs, pd.read_json) + if "tablefmt" not in markdown_kwargs: + markdown_kwargs["tablefmt"] = "pipe" + + return df.to_markdown(**markdown_kwargs) + def read_excel(*args, **kwargs): read_kwargs = kwargs_in_func(kwargs, pd.read_excel) @@ -96,6 +106,7 @@ def read_yaml(*args, **kwargs): "read_fwf": read_fwf, "read_excel": read_excel, "read_yaml": read_yaml, + "read_json": read_json, } diff --git a/setup.py b/setup.py index 8cc2590..d32794d 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name="mkdocs-table-reader-plugin", - version="1.1.1", + version="1.2", description="MkDocs plugin to directly insert tables from files into markdown.", long_description=long_description, long_description_content_type="text/markdown", diff --git a/tests/fixtures/basic_setup/assets/tables/data.json b/tests/fixtures/basic_setup/assets/tables/data.json new file mode 100644 index 0000000..9d9d05c --- /dev/null +++ b/tests/fixtures/basic_setup/assets/tables/data.json @@ -0,0 +1,5 @@ +{ + "columns":["col 1","col 2"], + "index":["row 1","row 2"], + "data":[["a","1234json"],["c","d"]] +} \ No newline at end of file diff --git a/tests/fixtures/basic_setup/docs/page_read_json.md b/tests/fixtures/basic_setup/docs/page_read_json.md new file mode 100644 index 0000000..72a0fc8 --- /dev/null +++ b/tests/fixtures/basic_setup/docs/page_read_json.md @@ -0,0 +1,7 @@ +# JSON + +## read_json + +The latest numbers using `read_json()`: + +{{ read_json('assets/tables/data.json', orient='split') }} diff --git a/tests/test_build.py b/tests/test_build.py index 32c8682..5afa5ac 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -128,6 +128,11 @@ def test_table_output(tmp_path): assert re.search(r"531456", contents) assert re.search(r"table1", contents) + # Make sure with markdown tag has the output + page_with_tag = tmp_proj / "site/page_read_json.html" + contents = page_with_tag.read_text() + assert re.search(r"1234json", contents) + # Make sure multiple tags are supported page_with_tag = tmp_proj / "site/page_read_two_csv.html" contents = page_with_tag.read_text()