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

Ability to choose specific columns while reading csv #61

Closed rajdevworks closed 2 months ago

rajdevworks commented 2 months ago

Hello, so I have 10 columns in my csv file and want to read only columns 3 to 7. Using pandas read_csv, I can pass parameter usecols which allows to pick columns to read from a given csv.

How can I do the same with this reader plugin read_csv? Here is what I have and it is failing. I'm using mkdocs material theme.

{{ read_csv('docs/assets/csv/error_codes.csv, usecols=["X", "EE"]') }}

timvink commented 2 months ago

Hi!

You're missing a quotation mark after .csv. And you have an extra quotation at the end.

Try

{{ read_csv('docs/assets/csv/error_codes.csv', usecols=["X", "EE"]) }}

rajdevworks commented 2 months ago

Thanks @timvink . After fixing the missing quotation mark, here is what I have

{{ read_csv('docs/assets/file.csv', usecols = ["A", "B"]) }}

Error I get during mkdocs serve

File "/opt/homebrew/lib/python3.11/site-packages/mkdocs/plugins.py", line 507, in run_event result = method(item, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/lib/python3.11/site-packages/mkdocs_table_reader_plugin/plugin.py", line 94, in on_page_markdown pd_args, pd_kwargs = parse_argkwarg(result[1]) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/lib/python3.11/site-packages/mkdocs_table_reader_plugin/safe_eval.py", line 83, in parse_argkwarg raise AssertionError( AssertionError: [table-reader-plugin] Make sure the python in your reader tag is correct: Positional arguments follow keyword arguments in ''docs/assets/file.csv', usecols = ["A", "B"]'

timvink commented 2 months ago

Check that it works in python. Try not to mix the quotation mark types (single or double)

rajdevworks commented 2 months ago

I did a test and the quotation mark type did work

$ python3 py-test.py

A   B
0  10  23
1  34  56
2  93  67

$ cat py-test.py

import pandas as pd

# Read the CSV file into a pandas DataFrame
#df = pd.read_csv('file.csv')

df = pd.read_csv('file.csv', usecols=["A", "B"])
# Print the first few rows of the DataFrame
print(df.head())

$ cat file.csv

A,B,C,D,E
10,23,45,78,99
34,56,87,12,37
93,67,34,23,11%
timvink commented 2 months ago

Thanks for the example. This is actually a bug I can reproduce. It was a bug in the way I parsed args and kwargs from the string; you were passing a list with an additional ,.

Fixing now..

timvink commented 2 months ago

Fixed in https://github.com/timvink/mkdocs-table-reader-plugin/commit/170e82c854a00aa74250e4114c6ab1b5fbadf292

Will make a new release now

timvink commented 2 months ago

Release: https://github.com/timvink/mkdocs-table-reader-plugin/releases/tag/v2.2.0

Pypy: https://pypi.org/project/mkdocs-table-reader-plugin/