diff --git a/mkdocs_table_reader_plugin/markdown.py b/mkdocs_table_reader_plugin/markdown.py index 13a2cef..399cca6 100644 --- a/mkdocs_table_reader_plugin/markdown.py +++ b/mkdocs_table_reader_plugin/markdown.py @@ -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 diff --git a/mkdocs_table_reader_plugin/plugin.py b/mkdocs_table_reader_plugin/plugin.py index 9c8692c..c2c6dce 100644 --- a/mkdocs_table_reader_plugin/plugin.py +++ b/mkdocs_table_reader_plugin/plugin.py @@ -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 diff --git a/setup.py b/setup.py index 624a196..432e09d 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/tests/fixtures/nonstringheaders/assets/tables/test.csv b/tests/fixtures/nonstringheaders/assets/tables/test.csv new file mode 100644 index 0000000..99b4b46 --- /dev/null +++ b/tests/fixtures/nonstringheaders/assets/tables/test.csv @@ -0,0 +1,3 @@ +A,B,C +1,2,3 +4242,5,6 \ No newline at end of file diff --git a/tests/fixtures/nonstringheaders/docs/index.md b/tests/fixtures/nonstringheaders/docs/index.md new file mode 100644 index 0000000..3463c19 --- /dev/null +++ b/tests/fixtures/nonstringheaders/docs/index.md @@ -0,0 +1,3 @@ +# CSV with non string headers + +{{ read_csv('assets/tables/test.csv', header=None) }} diff --git a/tests/fixtures/nonstringheaders/mkdocs.yml b/tests/fixtures/nonstringheaders/mkdocs.yml new file mode 100644 index 0000000..a8cedf8 --- /dev/null +++ b/tests/fixtures/nonstringheaders/mkdocs.yml @@ -0,0 +1,6 @@ +site_name: test git_table_reader site +use_directory_urls: false + +plugins: + - search + - table-reader \ No newline at end of file diff --git a/tests/test_build.py b/tests/test_build.py index caa0d23..88fcd37 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -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) diff --git a/tests/test_kwargs.py b/tests/test_kwargs.py index 3cb2961..2521d2f 100644 --- a/tests/test_kwargs.py +++ b/tests/test_kwargs.py @@ -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']})