timvink / mkdocs-table-reader-plugin

MkDocs plugin that enables a markdown tag like {{ read_csv('table.csv') }} to directly insert various table formats into a page
https://timvink.github.io/mkdocs-table-reader-plugin/
MIT License
111 stars 18 forks source link

Update allow_missing_files handling #60

Closed heavyneptune closed 1 month ago

heavyneptune commented 2 months ago

The allow_missing_files configuration is typically used when the creation of the content by mkdocs is more important than the availability of the file. It does not handle malformed files. Can you update the function TableReaderPlugin.on_page_markdown() to properly handle the Pandas errors by supplementing markdown_table = function(valid_file_paths[0], *pd_args, **pd_kwargs)?

import pandas.errors as pderrors

.....

 try:
    markdown_table = function(valid_file_paths[0], *pd_args, **pd_kwargs)
except (pd.errors.EmptyDataError, pd.errors.ParserError, pd.errors.DataError)as e:
    if self.config.get("allow_missing_files"):
        msg = f"[table-reader-plugin]: The data file '{input_file_path}' is malformed or empty. Pandas EmptyDataError {e}"
        markdown_table=f"[table-reader-plugin]: The data file '{input_file_path}' is malformed or empty."
        logger.warning(msg)
    else:
        raise e
timvink commented 2 months ago

allow_missing_files is meant to raise a warning instead of an error when a file path is not present.

Parsing errors are a different situation. I didn't forsee that use case. What about a new, additional option allow_parsing_errors? Or is allow_loading_errors clearer for users?

Would you be open to creating a PR ?

heavyneptune commented 2 months ago

I guess allow_parsing_errors or a warn_on_parsing_failure, but the second might drive all_missing_files to move to warn_on_missing_files. Your choice.

timvink commented 2 months ago

Thought about this more. Before we create a swiss army knife full of options, I'd like to understand the use case here. Why not just disable the plugin if you don't want to deal with errors? Or remove the jinja2 tag?

Are you generating the tables? If you, try changing the order of the plugins, or using a different (earlier) hook.

bobneuman commented 2 months ago

I didn't ask for another switch. The current switch handles an error as a warning (missing file), I'd like to apply the same rule for malformed files. You could rename it to warn_on_error and include some handling to look for the old switch name if present.

timvink commented 1 month ago

allow_missing_files missing files is something else than allow_errors or alllow_malformed_files. I am not convinced this is a an option that many people might need, and I want to keep the plugin as simple as possible.

If you temporarily want to disable the plugin, I suggest you set the enabled option to false instead (see https://www.mkdocs.org/user-guide/configuration/#enabled-option). If that doesn't fit your exact use-case, you can fork the repo, I added an MIT-license.

Thanks for discussing the option though!