Skip to content

Commit 0b6201d

Browse files
authored
Bugfix: Support non-string table headers
1 parent 9f5c1aa commit 0b6201d

File tree

8 files changed

+29
-3
lines changed

8 files changed

+29
-3
lines changed

mkdocs_table_reader_plugin/markdown.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def convert_to_md_table(df: pd.DataFrame, markdown_kwargs: Dict) -> str:
2424
"""
2525
# Escape any pipe characters, | to \|
2626
# See https://github.com/astanin/python-tabulate/issues/241
27-
df.columns = [replace_unescaped_pipes(c) for c in df.columns]
27+
df.columns = [replace_unescaped_pipes(c) if isinstance(c, str) else c for c in df.columns]
2828

2929
# Avoid deprecated applymap warning on pandas>=2.0
3030
# See https://github.com/timvink/mkdocs-table-reader-plugin/issues/55

mkdocs_table_reader_plugin/plugin.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ def on_page_markdown(self, markdown, page, config, files, **kwargs):
122122
# note we use the first valid file paths,
123123
# where we first search the 'data_path' and then the page's directory.
124124
markdown_table = function(valid_file_paths[0], *pd_args, **pd_kwargs)
125-
126125
markdown_table = fix_indentation(leading_spaces, markdown_table)
127126

128127
# Insert markdown table

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="mkdocs-table-reader-plugin",
8-
version="2.2.1",
8+
version="2.2.2",
99
description="MkDocs plugin to directly insert tables from files into markdown.",
1010
long_description=long_description,
1111
long_description_content_type="text/markdown",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A,B,C
2+
1,2,3
3+
4242,5,6
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# CSV with non string headers
2+
3+
{{ read_csv('assets/tables/test.csv', header=None) }}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
site_name: test git_table_reader site
2+
use_directory_urls: false
3+
4+
plugins:
5+
- search
6+
- table-reader

tests/test_build.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,15 @@ def test_mixed_quotation_marks(tmp_path):
362362
page_with_tag = tmp_proj / "site/index.html"
363363
contents = page_with_tag.read_text()
364364
assert re.search(r"56", contents)
365+
366+
def test_csv_with_no_string_headers(tmp_path):
367+
tmp_proj = setup_clean_mkdocs_folder(
368+
"tests/fixtures/nonstringheaders/mkdocs.yml", tmp_path
369+
)
370+
result = build_docs_setup(tmp_proj)
371+
assert result.exit_code == 0, "'mkdocs build' command failed"
372+
373+
# Make sure the file.csv is inserted
374+
page_with_tag = tmp_proj / "site/index.html"
375+
contents = page_with_tag.read_text()
376+
assert re.search(r"4242", contents)

tests/test_kwargs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ def test_parse_argkwarg():
2121
assert parse_argkwarg('"file.csv", usecols=[\'A\',\'B\']') == (['file.csv'], {'usecols': ['A', 'B']})
2222
assert parse_argkwarg("'assets/tables/table_with_carriage_return.csv', sep = ','") == (['assets/tables/table_with_carriage_return.csv'], {'sep': ','})
2323
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')})
24+
assert parse_argkwarg('"file.csv", header=None') == (['file.csv'], {'header': None})
25+
assert parse_argkwarg("'Example.xlsx', sheet_name = 'test', header = None") == (['Example.xlsx'], {'sheet_name': 'test', 'header': None})
26+
assert parse_argkwarg("'test.csv', header = None, names = ['a', 'b', 'c']") == (['test.csv'], {'header': None, 'names': ['a', 'b', 'c']})

0 commit comments

Comments
 (0)