Skip to content

Commit

Permalink
Bugfix: Support non-string table headers
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink authored May 20, 2024
1 parent 9f5c1aa commit 0b6201d
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mkdocs_table_reader_plugin/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def convert_to_md_table(df: pd.DataFrame, markdown_kwargs: Dict) -> str:
"""
# Escape any pipe characters, | to \|
# See https://github.com/astanin/python-tabulate/issues/241
df.columns = [replace_unescaped_pipes(c) for c in df.columns]
df.columns = [replace_unescaped_pipes(c) if isinstance(c, str) else c for c in df.columns]

# Avoid deprecated applymap warning on pandas>=2.0
# See https://github.com/timvink/mkdocs-table-reader-plugin/issues/55
Expand Down
1 change: 0 additions & 1 deletion mkdocs_table_reader_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ def on_page_markdown(self, markdown, page, config, files, **kwargs):
# note we use the first valid file paths,
# where we first search the 'data_path' and then the page's directory.
markdown_table = function(valid_file_paths[0], *pd_args, **pd_kwargs)

markdown_table = fix_indentation(leading_spaces, markdown_table)

# Insert markdown table
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="2.2.1",
version="2.2.2",
description="MkDocs plugin to directly insert tables from files into markdown.",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/nonstringheaders/assets/tables/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A,B,C
1,2,3
4242,5,6
3 changes: 3 additions & 0 deletions tests/fixtures/nonstringheaders/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# CSV with non string headers

{{ read_csv('assets/tables/test.csv', header=None) }}
6 changes: 6 additions & 0 deletions tests/fixtures/nonstringheaders/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
site_name: test git_table_reader site
use_directory_urls: false

plugins:
- search
- table-reader
12 changes: 12 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,15 @@ def test_mixed_quotation_marks(tmp_path):
page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text()
assert re.search(r"56", contents)

def test_csv_with_no_string_headers(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/nonstringheaders/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"

# Make sure the file.csv is inserted
page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text()
assert re.search(r"4242", contents)
3 changes: 3 additions & 0 deletions tests/test_kwargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ def test_parse_argkwarg():
assert parse_argkwarg('"file.csv", usecols=[\'A\',\'B\']') == (['file.csv'], {'usecols': ['A', 'B']})
assert parse_argkwarg("'assets/tables/table_with_carriage_return.csv', sep = ','") == (['assets/tables/table_with_carriage_return.csv'], {'sep': ','})
assert parse_argkwarg("'includes/statistics.csv', keep_default_na=False, colalign=('center','center','center','center','center','center','center')") == (['includes/statistics.csv'], {'keep_default_na': False, 'colalign': ('center','center','center','center','center','center','center')})
assert parse_argkwarg('"file.csv", header=None') == (['file.csv'], {'header': None})
assert parse_argkwarg("'Example.xlsx', sheet_name = 'test', header = None") == (['Example.xlsx'], {'sheet_name': 'test', 'header': None})
assert parse_argkwarg("'test.csv', header = None, names = ['a', 'b', 'c']") == (['test.csv'], {'header': None, 'names': ['a', 'b', 'c']})

0 comments on commit 0b6201d

Please sign in to comment.