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
120 stars 20 forks source link

How to read a directory of CSVs? #72

Closed Firelord710 closed 2 weeks ago

Firelord710 commented 3 weeks ago

Hey guys.

I have a directory filled with CSVs that are created on website compilation (using MKDocs Material + Cloudflare Pages).

I am struggling to figure out how to pass through the entire directory of CSVs to table reader.

My first thought was creating key value pairs that the value would be the dir for the csv, but this was cut short by not being able to figure out how to create a key value dictionary from two Pandas Dataframes containing the keys and values separately.

I'd appreciate any help or direction!

Thanks again.

timvink commented 3 weeks ago

I did this using the https://github.com/fralau/mkdocs-macros-plugin plugin (which this plugin supports as of v3+), basically creating a for loop that reads and inserts the tables. See the how to guide: https://timvink.github.io/mkdocs-table-reader-plugin/

You will need a function (jinja macro) that reads a directory and returns a list of the tables. In mkdocs-macros, you can read how to add those functions. Here's what I used:

def define_env(env):
    """
    Register additional mkdocs-macros-plugin functions that can be used as macros in markdown files.
    """    
    @env.macro
    def listdir(path):
        return os.listdir(path)

    @env.macro
    def path_exists(path):
        return Path(path).exists()

    @env.macro
    def is_file(path):
        return Path(path).is_file() 

Hope this helps.

Firelord710 commented 3 weeks ago

I did this using the https://github.com/fralau/mkdocs-macros-plugin plugin (which this plugin supports as of v3+), basically creating a for loop that reads and inserts the tables. See the how to guide: https://timvink.github.io/mkdocs-table-reader-plugin/

You will need a function (jinja macro) that reads a directory and returns a list of the tables. In mkdocs-macros, you can read how to add those functions. Here's what I used:


def define_env(env):

    """

    Register additional mkdocs-macros-plugin functions that can be used as macros in markdown files.

    """    

    @env.macro

    def listdir(path):

        return os.listdir(path)

    @env.macro

    def path_exists(path):

        return Path(path).exists()

    @env.macro

    def is_file(path):

        return Path(path).is_file() 

Hope this helps.

Hey Tim,

Thank you for the direction!

I'm having issues figuring out using a for-loop for inserting the tables; how do I set these returned paths as variables to be referenced by the plugin, or am I missing something entirely?

Sorry and thanks again, I appreciate your time.

Edit: I see the how to for turning the directory into one large table, I am looking for multiple tables: one per CSV if possible? 🤔

timvink commented 3 weeks ago

Did you see the Jinja how to guide ? It shows a for loop.

Let me know if it's not clear.

https://timvink.github.io/mkdocs-table-reader-plugin/howto/use_jinja2/

Firelord710 commented 3 weeks ago

Did you see the Jinja how to guide ? It shows a for loop.

Let me know if it's not clear.

https://timvink.github.io/mkdocs-table-reader-plugin/howto/use_jinja2/

Essentially I need to pass through the functions you listed above to the for loop, pointed at the directory for the CSVs, correct?

Firelord710 commented 3 weeks ago

Did you see the Jinja how to guide ? It shows a for loop.

Let me know if it's not clear.

https://timvink.github.io/mkdocs-table-reader-plugin/howto/use_jinja2/

Hey Tim, I have mostly got this working but I am wondering if there is a way to define multiple datapaths?

Ill include my working code after figuring this part out. Thank you!

timvink commented 2 weeks ago

I'm guessing you're trying to read in files from multiple folders? You could create two for loops, or something like {% for table_name in listdir(path1) + listdir(path2) %} (it's normal python so you can two lists).

timvink commented 2 weeks ago

I've also updated the docs with the example from this issue

timvink commented 2 weeks ago

Closing to close the issue, let me know if you still get stuck somewhere