Skip to content

Commit

Permalink
Add read_json reader
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink committed Nov 2, 2022
1 parent dffc059 commit bfa32e3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
12 changes: 12 additions & 0 deletions docs/readers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"

<code>\{\{ read_json('tables/data.json') \}\}</code>

=== "Output"

{{ read_json('tables/data.json') }}


## read_excel

Expand Down
11 changes: 11 additions & 0 deletions mkdocs_table_reader_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
}


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/basic_setup/assets/tables/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"columns":["col 1","col 2"],
"index":["row 1","row 2"],
"data":[["a","1234json"],["c","d"]]
}
7 changes: 7 additions & 0 deletions tests/fixtures/basic_setup/docs/page_read_json.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# JSON

## read_json

The latest numbers using `read_json()`:

{{ read_json('assets/tables/data.json', orient='split') }}
5 changes: 5 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit bfa32e3

Please sign in to comment.