diff --git a/404.html b/404.html index 39b5ed1..dc20677 100644 --- a/404.html +++ b/404.html @@ -454,7 +454,7 @@ - Using jinja2 + Use jinja2 for automation diff --git a/assets/tables/markdown_table/index.html b/assets/tables/markdown_table/index.html index 857ba61..1ba8a3c 100644 --- a/assets/tables/markdown_table/index.html +++ b/assets/tables/markdown_table/index.html @@ -454,7 +454,7 @@ - Using jinja2 + Use jinja2 for automation diff --git a/howto/alternatives/index.html b/howto/alternatives/index.html index 972cc48..f644c30 100644 --- a/howto/alternatives/index.html +++ b/howto/alternatives/index.html @@ -468,7 +468,7 @@ - Using jinja2 + Use jinja2 for automation diff --git a/howto/customize_tables/index.html b/howto/customize_tables/index.html index 4d3d205..812177e 100644 --- a/howto/customize_tables/index.html +++ b/howto/customize_tables/index.html @@ -544,7 +544,7 @@ - Using jinja2 + Use jinja2 for automation diff --git a/howto/docker/index.html b/howto/docker/index.html index 3de8da7..d9e8607 100644 --- a/howto/docker/index.html +++ b/howto/docker/index.html @@ -468,7 +468,7 @@ - Using jinja2 + Use jinja2 for automation diff --git a/howto/preprocess_tables/index.html b/howto/preprocess_tables/index.html index e2e4de0..4edc02b 100644 --- a/howto/preprocess_tables/index.html +++ b/howto/preprocess_tables/index.html @@ -526,7 +526,7 @@ - Using jinja2 + Use jinja2 for automation @@ -735,6 +735,10 @@

Combine a dire +
+

Alternative: use jinja

+

You can also use jinja2 to display a list of tables. See how to use jinja2 for automation.

+

Download a table from an API

@@ -794,7 +798,7 @@

Download a table from an API

- 2023-01-30 + 2024-08-26 @@ -805,12 +809,13 @@

Download a table from an API

- + @@ -827,7 +832,7 @@

Download a table from an API

- Authors: Tim Vink + Authors: Tim Vink, Tim Vink
diff --git a/howto/project_structure/index.html b/howto/project_structure/index.html index f5d61a9..866f321 100644 --- a/howto/project_structure/index.html +++ b/howto/project_structure/index.html @@ -468,7 +468,7 @@ - Using jinja2 + Use jinja2 for automation diff --git a/howto/use_jinja2/index.html b/howto/use_jinja2/index.html index dce0a27..0014f0c 100644 --- a/howto/use_jinja2/index.html +++ b/howto/use_jinja2/index.html @@ -22,7 +22,7 @@ - Using jinja2 - mkdocs-table-reader-plugin + Use jinja2 for automation - mkdocs-table-reader-plugin @@ -76,7 +76,7 @@
- + Skip to content @@ -112,7 +112,7 @@
- Using jinja2 + Use jinja2 for automation
@@ -477,7 +477,7 @@ - Using jinja2 + Use jinja2 for automation @@ -488,7 +488,7 @@ - Using jinja2 + Use jinja2 for automation @@ -510,9 +510,27 @@
  • - + - Indented content like content tabs + Dynamically load a list of tables + + + +
  • + +
  • + + + Insert tables into content tabs + + + +
  • + +
  • + + + Recursively insert an entire directory of tables @@ -644,9 +662,27 @@
    • - + + + Dynamically load a list of tables + + + +
    • + +
    • + - Indented content like content tabs + Insert tables into content tabs + + + +
    • + +
    • + + + Recursively insert an entire directory of tables @@ -668,14 +704,15 @@ -

      Using jinja2

      +

      Use jinja2 for automation

      table-reader supports mkdocs-macros-plugin, which enables you to use jinja2 syntax inside markdown files (among other things).

      To enable macros, specify the plugin before table-reader in your mkdocs.yml file:

      plugins:
           - macros
           - table-reader
       
      -

      Now you can do cool things like dynamically load a list of tables:

      +

      Now you can do cool things like:

      +

      Dynamically load a list of tables

      # index.md
       
       {% set table_names = ["basic_table.csv","basic_table2.csv"] %}
      @@ -685,7 +722,7 @@ 

      Using jinja2

      {% endfor %}
      -

      Indented content like content tabs

      +

      Insert tables into content tabs

      If you inserted content has multiple lines, then indentation will be not be retained beyond the first line. This means things like content tabs will not work as expected.

      To fix that, you can use the custom filter add_indendation (a filter add to macros by table-reader plugin). For example:

      @@ -726,6 +763,39 @@

      Indented content like content tabsTo avoid the tables being inserted into the code example, we replaced {{ with { {. If you copy this example, make sure to fix.

      +

      Recursively insert an entire directory of tables

      +

      mkdocs-macros-plugin enables you to define additional functions (called macros) that you will be able to use within your markdown files. +See their documentation on how to set this up. Here's an example with some functions to interact with the filesystem:

      +
      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() 
      +
      +

      Now you could do something like:

      +
      # index.md
      +
      +{% for table_name in listdir('docs/assets/my_tables") %}
      +
      +{ { read_csv(table_name) }}
      +
      +{% endfor %}
      +
      +
      +

      Note the space in { {

      +

      To avoid the tables being inserted into the code example, we replaced {{ with { {. +If you copy this example, make sure to fix.

      +
      @@ -746,7 +816,7 @@

      Indented content like content tabs - 2024-08-15 + 2024-08-26 diff --git a/index.html b/index.html index 6b7b31e..66358ac 100644 --- a/index.html +++ b/index.html @@ -532,7 +532,7 @@ - Using jinja2 + Use jinja2 for automation diff --git a/options/index.html b/options/index.html index 7f52033..0318295 100644 --- a/options/index.html +++ b/options/index.html @@ -465,7 +465,7 @@ - Using jinja2 + Use jinja2 for automation diff --git a/readers/index.html b/readers/index.html index c0702c9..572f527 100644 --- a/readers/index.html +++ b/readers/index.html @@ -579,7 +579,7 @@ - Using jinja2 + Use jinja2 for automation diff --git a/search/search_index.json b/search/search_index.json index 5660e20..6c1c7aa 100644 --- a/search/search_index.json +++ b/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Home","text":""},{"location":"#mkdocs-table-reader-plugin","title":"mkdocs-table-reader-plugin","text":"

      MkDocs plugin that enables a markdown tag like {{ read_csv('table.csv') }} to directly insert various table formats into a page.

      For a workflow with other plugins see the blogpost building reproducible reports with MkDocs

      "},{"location":"#installation","title":"Installation","text":"

      Install the plugin using pip:

      pip install mkdocs-table-reader-plugin\n

      Next, add the following lines to your mkdocs.yml:

      plugins:\n  - search\n  - table-reader\n

      If you have no plugins entry in your config file yet, you'll likely also want to add the search plugin. MkDocs enables it by default if there is no plugins entry set.

      "},{"location":"#usage","title":"Usage","text":"

      In your markdown files you can now use:

      {{ read_csv('path_to_table.csv') }}\n

      Where the path is relative to the location of your project's mkdocs.yml file, or your project's docs/ directory, or the location of your markdown source file (all 3 possible locations will be searched, in that order).

      • There are readers for .csv, .fwf, .json, .xls, .xlsx, .yaml, .feather and .tsv files. There is also the read_raw() reader that will allow you to insert tables (or other content) already in markdown format.
      • table-reader is compatible with mkdocs-macros-plugin, which means you can dynamically insert tables using jinja2 syntax.
      "},{"location":"#documentation-and-how-to-guides","title":"Documentation and how-to guides","text":"

      See timvink.github.io/mkdocs-table-reader-plugin/

      "},{"location":"options/","title":"Options","text":"

      You can customize the plugin by setting options in mkdocs.yml. For example:

      plugins:\n  - table-reader:\n      data_path: \".\"\n      allow_missing_files: False\n      select_readers:\n        - read_csv\n        - read_json\n      enabled: True\n
      "},{"location":"options/#data_path","title":"data_path","text":"

      Default is .. Set a default path to the searched directories in order to shorten table filename specifications.

      Given a file path, table-reader will search for that file relative to your your project's mkdocs.yml and relative to your docs/ folder. If you use a folder for all your table files you can shorten the path specification by setting the data_path.

      For example, if your table is located in docs/assets/tables/basic_table.csv, you can set data_path to docs/assets/tables/. Then you will be able to use {{ read_csv(\"basic_table.csv\") }} instead of {{ read_csv(\"docs/assets/tables/basic_table.csv\") }} inside any markdown page.

      Info

      Note that by default the plugin will also search the page's directory but only when a table is not found.

      For more examples see the how to guide on project structure.

      "},{"location":"options/#allow_missing_files","title":"allow_missing_files","text":"

      Default: False. When enabled, if a filepath is not found, the plugin will raise a warning instead of an error.

      "},{"location":"options/#select_readers","title":"select_readers","text":"

      Default: Selects all available readers. Specify a list of readers to improve documentation build times for very large sites.

      "},{"location":"options/#enabled","title":"enabled","text":"

      Default is True. Enables you to deactivate this plugin. This option is supported by all plugins since mkdocs 1.6 (see docs). A possible use case is local development where you might want faster build times and/or do not have the tables ready. It's recommended to use this option with an environment variable together with a default fallback (introduced in mkdocs v1.2.1, see docs). Example:

      mkdocs.yml
      plugins:\n  - table-reader:\n      enabled: !ENV [ENABLED_TABLE_READER, True]\n

      Which enables you to disable the plugin locally using:

      export ENABLED_TABLE_READER=false\nmkdocs serve\n
      "},{"location":"readers/","title":"Readers","text":"

      The following table reader functions are available:

      "},{"location":"readers/#read_csv","title":"read_csv","text":"

      {{ read_csv() }} passed to pandas.read_csv(). Example:

      InputOutput

      {{ read_csv('tables/basic_table.csv') }}

      a b 40 73 50 52 531456.123 80 name table1"},{"location":"readers/#read_fwf","title":"read_fwf","text":"

      {{ read_fwf() }} passed to pandas.read_fwf(). Example:

      InputOutput

      {{ read_fwf('tables/fixedwidth_table.txt') }}

      Brand Price Honda Civic 22001 Toyota Corolla 25000 Ford Focus 27000 Audi A4 35000"},{"location":"readers/#read_yaml","title":"read_yaml","text":"

      {{ read_yaml() }} is parsed with yaml.safe_load() and passed to pandas.json_normalize(). Example:

      InputOutput

      {{ read_yaml('tables/yaml_table.yml') }}

      a b 40 73 50 52 531456 80 name table1"},{"location":"readers/#read_table","title":"read_table","text":"

      {{ read_table() }} passed to pandas.read_table(). Example:

      InputOutput

      {{ read_table('tables/basic_table.csv', sep = ',') }}

      a b 40 73 50 52 531456.123 80 name table1"},{"location":"readers/#read_json","title":"read_json","text":"

      {{ read_json() }} passed to pandas.read_json(). Example:

      InputOutput

      {{ read_json('tables/data.json', orient='split') }}

      col 1 col 2 a b c d"},{"location":"readers/#read_feather","title":"read_feather","text":"

      {{ read_feather() }} passed to pandas.read_feather(). Example:

      InputOutput

      {{ read_json('tables/data.feather') }}

      col 1 col 2 a b c d"},{"location":"readers/#read_excel","title":"read_excel","text":"

      {{ read_excel() }} passed to pandas.read_excel(). Example:

      InputOutput

      {{ read_excel('tables/excel_table.xlsx', engine='openpyxl') }}

      a b 40 73 50 52 531456 8

      Reading xlsx files

      You might get a XLRDError('Excel xlsx file; not supported',) error when trying to read modern excel files. That's because xlrd does not support .xlsx files (stackoverflow post). Instead, install openpyxl and use:

      {{ read_excel('tables/excel_table.xlsx', engine='openpyxl') }}

      "},{"location":"readers/#read_raw","title":"read_raw","text":"

      {{ read_raw() }} inserts contents from a file directly. This is great if you have a file with a table already in markdown format. It could also replace a workflow where you use the snippets extension to embed external files.

      Example:

      InputOutput

      {{ read_raw('tables/markdown_table.md') }}

      Tables Are Cool col 1 is left-aligned $1600 col 2 is centered $12 col 3 is right-aligned $1"},{"location":"assets/tables/markdown_table/","title":"Markdown table","text":"Tables Are Cool col 1 is left-aligned $1600 col 2 is centered $12 col 3 is right-aligned $1"},{"location":"howto/alternatives/","title":"Consider alternatives","text":"

      This plugin is built to be able to quickly insert table files anywhere in a markdown file.

      You could also consider alternative approaches that might fit your use-case better.

      "},{"location":"howto/alternatives/#write-tables-to-markdown-files","title":"Write tables to markdown files","text":"

      You could write a script (maybe triggered by a mkdocs hook) that writes the tables you need into markdown files. It could look something like this:

      # write markdown tables\nimport pandas as pd\n\nmd = pd.read_csv(\"your_file.csv\").to_markdown()\nwith open(\"docs/assets/tables/my_file.md\", \"w\") as f:\n    f.write(md)\n

      You can then use the snippets extension to insert the tables into your markdown pages:

      # some_page.md\n\nMy table:\n\n--8<-- \"assets/tables/my_file.md\"\n

      Upsides:

      • Easy, fast, low on dependencies
      • You can see changes of your tables in version control

      Downsides:

      • You need to generate/update the markdown files on every build
      • if you move the page you have to update the path (if you used a relative path for the snippet)
      "},{"location":"howto/alternatives/#execute-python-during-build","title":"Execute python during build","text":"

      You could also choose to insert the markdown for tables dynamically, using packages like markdown-exec or mkdocs-macros-plugin.

      For example:

      ```python exec=\"true\"\nimport pandas as pd\n\nfile_path = \"path/to/file/from/project/root\"\nprint(pd.read_csv(file_path).to_markdown(index=False, disable_numparse=True))\n```\n
      "},{"location":"howto/customize_tables/","title":"Customize markdown tables","text":"

      You can customize the resulting markdown tables!

      "},{"location":"howto/customize_tables/#theory","title":"Theory","text":"

      Under the hood mkdocs-table-reader-plugin is basically doing:

      import pandas as pd\ndf = pd.read_csv('path_to_table.csv')\ndf.to_markdown(index=False, tablefmt='pipe')\n

      Any keyword arguments you give to {{ read_csv('path_to_your_table.csv') }} will be matched and passed the corresponding pandas.read_csv() and/or .to_markdown() functions.

      Pandas's .to_markdown() uses the tabulate package and any keyword arguments that are passed to it. Tabulate in turn offers many customization options, see library usage.

      "},{"location":"howto/customize_tables/#aligning-columns","title":"Aligning columns","text":"

      Text columns will be aligned to the left by default, whilst columns which contain only numbers will be aligned to the right. You can override this behaviour using tabulate's custom column alignment. Example:

      left center right

      {{ read_csv('tables/basic_table.csv', colalign=(\"left\",)) }}

      a b 40 73 50 52 531456.123 80 name table1

      {{ read_csv('tables/basic_table.csv', colalign=(\"center\",)) }}

      a b 40 73 50 52 531456.123 80 name table1

      {{ read_csv('tables/basic_table.csv', colalign=(\"right\",)) }}

      a b 40 73 50 52 531456.123 80 name table1"},{"location":"howto/customize_tables/#sortable-tables","title":"Sortable tables","text":"

      If you use mkdocs-material, you can configure sortable tables.

      "},{"location":"howto/customize_tables/#number-formatting","title":"Number formatting","text":"

      You can use tabulate's number formatting. Example:

      {{ read_fwf('tables/fixedwidth_table.txt', floatfmt=\".0f\") }}

      Brand Price Honda Civic 22001 Toyota Corolla 25000 Ford Focus 27000 Audi A4 35000

      {{ read_fwf('tables/fixedwidth_table.txt', floatfmt=\".1f\") }}

      Brand Price Honda Civic 22001.0 Toyota Corolla 25000.0 Ford Focus 27000.0 Audi A4 35000.0

      {{ read_fwf('tables/fixedwidth_table.txt', floatfmt=\".2f\") }}

      Brand Price Honda Civic 22001.00 Toyota Corolla 25000.00 Ford Focus 27000.00 Audi A4 35000.00"},{"location":"howto/docker/","title":"Use with docker","text":"

      If you follow the mkdocs-material tutorial on installation with docker, you will run into an error if you try to add this plugin saying:

      \u00d7 Building wheel for numpy (pyproject.toml) did not run successfully.

      The reason is that the mkdocs-material docker image uses the alpine image. mkdocs-table-reader-plugin depends on pandas, which depends on numpy, which in turn requires C++ to install, which is not part of the alpine image.

      If you need a very small image, you can adapt the alpine image to support numpy, but you are better off using a more complete docker image like python-slim. So you need to build a different docker image.

      As an example, below is a Dockerfile that is adapted from the mkdocs-material dockerfile. You can use it to create a new Docker image that supports mkdocs-table-reader-plugin:

      git clone https://github.com/squidfunk/mkdocs-material.git material-git/\ncd material-git\n# Manually replace `Dockerfile` with the example below\ndocker build -t YOUR-CONTAINER-NAME .\n# Now, inside your own project, you can use:\ndocker run --rm -it -p 8000:8000 -v ${PWD}:/docs YOUR-CONTAINER-NAME\n
      Dockerfile

      FROM python:3-slim\n\n# Build-time flags\nARG WITH_PLUGINS=true\n\n# Environment variables\nENV PACKAGES=/usr/local/lib/python3.11/site-packages\nENV PYTHONDONTWRITEBYTECODE=1\n\n# Set build directory\nWORKDIR /tmp\n\n# Copy files necessary for build\nCOPY material-git/material material\nCOPY material-git/package.json package.json\nCOPY material-git/README.md README.md\nCOPY material-git/requirements.txt requirements.txt\nCOPY material-git/pyproject.toml pyproject.toml\n\n# Perform build and cleanup artifacts and caches\nRUN \\\napt update \\\n&& \\\napt install -y \\\n    libcairo2-dev \\\n    libfreetype6-dev \\\n    git \\\n    libturbojpeg-dev \\\n    openssh-server \\\n    zlib1g-dev \\\n&& \\\napt install -y \\\n    gcc \\\n    libffi-dev \\\n    musl-dev \\\n&& \\\npip install --no-cache-dir . \\\n&& \\\nif [ \"${WITH_PLUGINS}\" = \"true\" ]; then \\\n    pip install --no-cache-dir \\\n    \"mkdocs-minify-plugin>=0.3\" \\\n    \"mkdocs-redirects>=1.0\" \\\n    \"pillow>=9.0\" \\\n    \"cairosvg>=2.5\" \\\n    \"mkdocs-table-reader-plugin\" \\\n    ; \\\nfi \\\n&& \\\nfor theme in mkdocs readthedocs; do \\\n    rm -rf ${PACKAGES}/mkdocs/themes/$theme; \\\n    ln -s \\\n    ${PACKAGES}/material \\\n    ${PACKAGES}/mkdocs/themes/$theme; \\\ndone \\\n&& \\\nrm -rf /tmp/* /root/.cache \\\n&& \\\nfind ${PACKAGES} \\\n    -type f \\\n    -path \"*/__pycache__/*\" \\\n    -exec rm -f {} \\;\n\n# Trust directory, required for git >= 2.35.2\nRUN git config --global --add safe.directory /docs &&\\\n    git config --global --add safe.directory /site\n\n# Set working directory\nWORKDIR /docs\n\n# Expose MkDocs development server port\nEXPOSE 8000\n\n# Start development server by default\nENTRYPOINT [\"mkdocs\"]\nCMD [\"serve\", \"--dev-addr=0.0.0.0:8000\"]\n
      \"mkdocs-table-rea

      "},{"location":"howto/preprocess_tables/","title":"Preprocess input tables","text":"

      mkdocs>=1.4 supports hooks, which enable you to run python scripts on mkdocs serve or mkdocs build.

      Here are some example of workflows that use hooks and the table-reader plugin:

      "},{"location":"howto/preprocess_tables/#combine-a-directory-of-tables-into-a-single-larger-table","title":"Combine a directory of tables into a single, larger table.","text":"hooks.pyindex.mdmkdocs.ymlProject structure
      from os import listdir\nfrom os.path import isfile, join\nimport pandas as pd\n\ndef on_pre_build(config, **kwargs) -> None:\n    tables = []\n    input_files = [f for f in listdir(mypath) if isfile(join(mypath, f))]\n    for table in input_files:\n        tables.append(pd.read_csv(table))\n    df = pd.concat(tables, ignore_index=True)\n    df.to_csv(\"docs/assets/output_table.csv\")\n

      My table: {{ read_csv(\"docs/assets/output_table.csv\") }}

      # ...\nplugins:\n    - table-reader\nhooks:\n    - scripts/hooks.py\n# ...\n
      .\n\u251c\u2500\u2500 scripts/\n\u2502   \u2514\u2500\u2500\u2500 hooks.py\n\u251c\u2500\u2500 docs/\n\u2502   \u2514\u2500\u2500\u2500 index.md\n\u2502   \u2514\u2500\u2500 assets/tables/\n\u2502       \u2514\u2500\u2500 table1.csv\n\u2502       \u2514\u2500\u2500 table2.csv\n\u2514\u2500\u2500 mkdocs.yml\n
      "},{"location":"howto/preprocess_tables/#download-a-table-from-an-api","title":"Download a table from an API","text":"hooks.pyindex.mdmkdocs.ymlProject structure
      import pandas as pd\n\ndef on_pre_build(config, **kwargs) -> None:\n    df = pd.read_csv('https://data.cityofnewyork.us/resource/nu7n-tubp.csv?$limit=100')\n    df.to_csv(\"docs/assets/nyc_data.csv\")\n

      My table: {{ read_csv(\"docs/assets/nyc_data.csv\") }}

      # ...\nplugins:\n    - table-reader\nhooks:\n    - scripts/hooks.py\n# ...\n
      .\n\u251c\u2500\u2500 scripts/\n\u2502   \u2514\u2500\u2500\u2500 hooks.py\n\u251c\u2500\u2500 docs/\n\u2502   \u2514\u2500\u2500\u2500 index.md\n\u2502   \u2514\u2500\u2500 assets/\n\u2514\u2500\u2500 mkdocs.yml\n

      Note that during development when you use mkdocs serve and autoreload, you might not want to run this hook every time you make a change. You could use an environment variable inside your hook, for example something like if os.environ['disable_hook'] == 1: return None.

      "},{"location":"howto/project_structure/","title":"Choose a project structure","text":"

      You have different possible strategies to store and load your tables. This guide gives some examples.

      "},{"location":"howto/project_structure/#one-table-per-markdown-file","title":"One table per markdown file","text":"

      If you only want to include an occasional table in a specific markdown file, just store it in the same directory as the markdown file:

      project structurefolder/page.md
      .\n\u251c\u2500\u2500 docs/\n\u2502   \u251c\u2500\u2500 index.md\n\u2502   \u2514\u2500\u2500 folder/\n\u2502       \u2514\u2500\u2500 another_table.csv\n\u2502       \u2514\u2500\u2500 page.md\n\u2514\u2500\u2500 mkdocs.yml\n
      Here is the table:\n\n\\{\\{ read_csv(\"another_table.csv\") \\}\\}\n

      In page.md, to read another_table.csv, you can choose to use:

      • {{ read_csv(\"docs/folder/another_table.csv\") }} (Path relative to mkdocs.yml)
      • {{ read_csv(\"folder/another_table.csv\") }} (Path relative to docs/ directory)
      • {{ read_csv(\"another_table.csv\") }} (Path relative to page source file)
      "},{"location":"howto/project_structure/#re-using-tables-across-markdown-files","title":"Re-using tables across markdown files","text":"

      If you want to reuse tables in multiple markdown files, or have many tables, you'll want to store them in a central directory, like docs/assets/tables. That way, if you restructure your navigation, the links to the tables won't break either. It's also great if you generate tables because the output directory will be the same.

      Given the following project structure:

      .\n\u251c\u2500\u2500 docs/\n\u2502   \u251c\u2500\u2500 index.md\n\u2502   \u2514\u2500\u2500 folder/\n\u2502       \u2514\u2500\u2500 page.md\n\u2502   \u2514\u2500\u2500 assets/\n\u2502       \u2514\u2500\u2500 tables/\n\u2502           \u2514\u2500\u2500 another_table.csv\n\u2514\u2500\u2500 mkdocs.yml\n

      In page.md, to read another_table.csv, you can choose to use:

      • {{ read_csv(\"docs/assets/tables/another_table.csv\") }} (Path relative to mkdocs.yml)
      • {{ read_csv(\"assets/tables/another_table.csv\") }} (Path relative to docs/ directory)
      • {{ read_csv(\"../assets/tables/another_table.csv\") }} (Path relative to page source file (note that .. stands for \"one directory up\"))
      "},{"location":"howto/use_jinja2/","title":"Using jinja2","text":"

      table-reader supports mkdocs-macros-plugin, which enables you to use jinja2 syntax inside markdown files (among other things).

      To enable macros, specify the plugin before table-reader in your mkdocs.yml file:

      plugins:\n    - macros\n    - table-reader\n

      Now you can do cool things like dynamically load a list of tables:

      # index.md\n\n{% set table_names = [\"basic_table.csv\",\"basic_table2.csv\"] %}\n{% for table_name in table_names %}\n\n{ { read_csv(table_name) }}\n\n{% endfor %}\n
      "},{"location":"howto/use_jinja2/#indented-content-like-content-tabs","title":"Indented content like content tabs","text":"

      If you inserted content has multiple lines, then indentation will be not be retained beyond the first line. This means things like content tabs will not work as expected.

      To fix that, you can use the custom filter add_indendation (a filter add to macros by table-reader plugin). For example:

      index.mdmkdocs.yml
      {% set table_names = [\"basic_table.csv\",\"basic_table2.csv\"] %}\n{% for table_name in table_names %}\n\n=== \"{{ table_name }}\"\n\n    { { read_csv(table_name) | add_indentation(spaces=4) }}\n\n{% endfor %}\n
      site_name: test git_table_reader site\nuse_directory_urls: true\n\ntheme:\nname: material\n\nplugins:\n    - search\n    - macros\n    - table-reader\n\nmarkdown_extensions:\n    - pymdownx.superfences\n    - pymdownx.tabbed:\n        alternate_style: true\n

      Note the space in { {

      To avoid the tables being inserted into the code example, we replaced {{ with { {. If you copy this example, make sure to fix.

      "}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Home","text":""},{"location":"#mkdocs-table-reader-plugin","title":"mkdocs-table-reader-plugin","text":"

      MkDocs plugin that enables a markdown tag like {{ read_csv('table.csv') }} to directly insert various table formats into a page.

      For a workflow with other plugins see the blogpost building reproducible reports with MkDocs

      "},{"location":"#installation","title":"Installation","text":"

      Install the plugin using pip:

      pip install mkdocs-table-reader-plugin\n

      Next, add the following lines to your mkdocs.yml:

      plugins:\n  - search\n  - table-reader\n

      If you have no plugins entry in your config file yet, you'll likely also want to add the search plugin. MkDocs enables it by default if there is no plugins entry set.

      "},{"location":"#usage","title":"Usage","text":"

      In your markdown files you can now use:

      {{ read_csv('path_to_table.csv') }}\n

      Where the path is relative to the location of your project's mkdocs.yml file, or your project's docs/ directory, or the location of your markdown source file (all 3 possible locations will be searched, in that order).

      • There are readers for .csv, .fwf, .json, .xls, .xlsx, .yaml, .feather and .tsv files. There is also the read_raw() reader that will allow you to insert tables (or other content) already in markdown format.
      • table-reader is compatible with mkdocs-macros-plugin, which means you can dynamically insert tables using jinja2 syntax.
      "},{"location":"#documentation-and-how-to-guides","title":"Documentation and how-to guides","text":"

      See timvink.github.io/mkdocs-table-reader-plugin/

      "},{"location":"options/","title":"Options","text":"

      You can customize the plugin by setting options in mkdocs.yml. For example:

      plugins:\n  - table-reader:\n      data_path: \".\"\n      allow_missing_files: False\n      select_readers:\n        - read_csv\n        - read_json\n      enabled: True\n
      "},{"location":"options/#data_path","title":"data_path","text":"

      Default is .. Set a default path to the searched directories in order to shorten table filename specifications.

      Given a file path, table-reader will search for that file relative to your your project's mkdocs.yml and relative to your docs/ folder. If you use a folder for all your table files you can shorten the path specification by setting the data_path.

      For example, if your table is located in docs/assets/tables/basic_table.csv, you can set data_path to docs/assets/tables/. Then you will be able to use {{ read_csv(\"basic_table.csv\") }} instead of {{ read_csv(\"docs/assets/tables/basic_table.csv\") }} inside any markdown page.

      Info

      Note that by default the plugin will also search the page's directory but only when a table is not found.

      For more examples see the how to guide on project structure.

      "},{"location":"options/#allow_missing_files","title":"allow_missing_files","text":"

      Default: False. When enabled, if a filepath is not found, the plugin will raise a warning instead of an error.

      "},{"location":"options/#select_readers","title":"select_readers","text":"

      Default: Selects all available readers. Specify a list of readers to improve documentation build times for very large sites.

      "},{"location":"options/#enabled","title":"enabled","text":"

      Default is True. Enables you to deactivate this plugin. This option is supported by all plugins since mkdocs 1.6 (see docs). A possible use case is local development where you might want faster build times and/or do not have the tables ready. It's recommended to use this option with an environment variable together with a default fallback (introduced in mkdocs v1.2.1, see docs). Example:

      mkdocs.yml
      plugins:\n  - table-reader:\n      enabled: !ENV [ENABLED_TABLE_READER, True]\n

      Which enables you to disable the plugin locally using:

      export ENABLED_TABLE_READER=false\nmkdocs serve\n
      "},{"location":"readers/","title":"Readers","text":"

      The following table reader functions are available:

      "},{"location":"readers/#read_csv","title":"read_csv","text":"

      {{ read_csv() }} passed to pandas.read_csv(). Example:

      InputOutput

      {{ read_csv('tables/basic_table.csv') }}

      a b 40 73 50 52 531456.123 80 name table1"},{"location":"readers/#read_fwf","title":"read_fwf","text":"

      {{ read_fwf() }} passed to pandas.read_fwf(). Example:

      InputOutput

      {{ read_fwf('tables/fixedwidth_table.txt') }}

      Brand Price Honda Civic 22001 Toyota Corolla 25000 Ford Focus 27000 Audi A4 35000"},{"location":"readers/#read_yaml","title":"read_yaml","text":"

      {{ read_yaml() }} is parsed with yaml.safe_load() and passed to pandas.json_normalize(). Example:

      InputOutput

      {{ read_yaml('tables/yaml_table.yml') }}

      a b 40 73 50 52 531456 80 name table1"},{"location":"readers/#read_table","title":"read_table","text":"

      {{ read_table() }} passed to pandas.read_table(). Example:

      InputOutput

      {{ read_table('tables/basic_table.csv', sep = ',') }}

      a b 40 73 50 52 531456.123 80 name table1"},{"location":"readers/#read_json","title":"read_json","text":"

      {{ read_json() }} passed to pandas.read_json(). Example:

      InputOutput

      {{ read_json('tables/data.json', orient='split') }}

      col 1 col 2 a b c d"},{"location":"readers/#read_feather","title":"read_feather","text":"

      {{ read_feather() }} passed to pandas.read_feather(). Example:

      InputOutput

      {{ read_json('tables/data.feather') }}

      col 1 col 2 a b c d"},{"location":"readers/#read_excel","title":"read_excel","text":"

      {{ read_excel() }} passed to pandas.read_excel(). Example:

      InputOutput

      {{ read_excel('tables/excel_table.xlsx', engine='openpyxl') }}

      a b 40 73 50 52 531456 8

      Reading xlsx files

      You might get a XLRDError('Excel xlsx file; not supported',) error when trying to read modern excel files. That's because xlrd does not support .xlsx files (stackoverflow post). Instead, install openpyxl and use:

      {{ read_excel('tables/excel_table.xlsx', engine='openpyxl') }}

      "},{"location":"readers/#read_raw","title":"read_raw","text":"

      {{ read_raw() }} inserts contents from a file directly. This is great if you have a file with a table already in markdown format. It could also replace a workflow where you use the snippets extension to embed external files.

      Example:

      InputOutput

      {{ read_raw('tables/markdown_table.md') }}

      Tables Are Cool col 1 is left-aligned $1600 col 2 is centered $12 col 3 is right-aligned $1"},{"location":"assets/tables/markdown_table/","title":"Markdown table","text":"Tables Are Cool col 1 is left-aligned $1600 col 2 is centered $12 col 3 is right-aligned $1"},{"location":"howto/alternatives/","title":"Consider alternatives","text":"

      This plugin is built to be able to quickly insert table files anywhere in a markdown file.

      You could also consider alternative approaches that might fit your use-case better.

      "},{"location":"howto/alternatives/#write-tables-to-markdown-files","title":"Write tables to markdown files","text":"

      You could write a script (maybe triggered by a mkdocs hook) that writes the tables you need into markdown files. It could look something like this:

      # write markdown tables\nimport pandas as pd\n\nmd = pd.read_csv(\"your_file.csv\").to_markdown()\nwith open(\"docs/assets/tables/my_file.md\", \"w\") as f:\n    f.write(md)\n

      You can then use the snippets extension to insert the tables into your markdown pages:

      # some_page.md\n\nMy table:\n\n--8<-- \"assets/tables/my_file.md\"\n

      Upsides:

      • Easy, fast, low on dependencies
      • You can see changes of your tables in version control

      Downsides:

      • You need to generate/update the markdown files on every build
      • if you move the page you have to update the path (if you used a relative path for the snippet)
      "},{"location":"howto/alternatives/#execute-python-during-build","title":"Execute python during build","text":"

      You could also choose to insert the markdown for tables dynamically, using packages like markdown-exec or mkdocs-macros-plugin.

      For example:

      ```python exec=\"true\"\nimport pandas as pd\n\nfile_path = \"path/to/file/from/project/root\"\nprint(pd.read_csv(file_path).to_markdown(index=False, disable_numparse=True))\n```\n
      "},{"location":"howto/customize_tables/","title":"Customize markdown tables","text":"

      You can customize the resulting markdown tables!

      "},{"location":"howto/customize_tables/#theory","title":"Theory","text":"

      Under the hood mkdocs-table-reader-plugin is basically doing:

      import pandas as pd\ndf = pd.read_csv('path_to_table.csv')\ndf.to_markdown(index=False, tablefmt='pipe')\n

      Any keyword arguments you give to {{ read_csv('path_to_your_table.csv') }} will be matched and passed the corresponding pandas.read_csv() and/or .to_markdown() functions.

      Pandas's .to_markdown() uses the tabulate package and any keyword arguments that are passed to it. Tabulate in turn offers many customization options, see library usage.

      "},{"location":"howto/customize_tables/#aligning-columns","title":"Aligning columns","text":"

      Text columns will be aligned to the left by default, whilst columns which contain only numbers will be aligned to the right. You can override this behaviour using tabulate's custom column alignment. Example:

      left center right

      {{ read_csv('tables/basic_table.csv', colalign=(\"left\",)) }}

      a b 40 73 50 52 531456.123 80 name table1

      {{ read_csv('tables/basic_table.csv', colalign=(\"center\",)) }}

      a b 40 73 50 52 531456.123 80 name table1

      {{ read_csv('tables/basic_table.csv', colalign=(\"right\",)) }}

      a b 40 73 50 52 531456.123 80 name table1"},{"location":"howto/customize_tables/#sortable-tables","title":"Sortable tables","text":"

      If you use mkdocs-material, you can configure sortable tables.

      "},{"location":"howto/customize_tables/#number-formatting","title":"Number formatting","text":"

      You can use tabulate's number formatting. Example:

      {{ read_fwf('tables/fixedwidth_table.txt', floatfmt=\".0f\") }}

      Brand Price Honda Civic 22001 Toyota Corolla 25000 Ford Focus 27000 Audi A4 35000

      {{ read_fwf('tables/fixedwidth_table.txt', floatfmt=\".1f\") }}

      Brand Price Honda Civic 22001.0 Toyota Corolla 25000.0 Ford Focus 27000.0 Audi A4 35000.0

      {{ read_fwf('tables/fixedwidth_table.txt', floatfmt=\".2f\") }}

      Brand Price Honda Civic 22001.00 Toyota Corolla 25000.00 Ford Focus 27000.00 Audi A4 35000.00"},{"location":"howto/docker/","title":"Use with docker","text":"

      If you follow the mkdocs-material tutorial on installation with docker, you will run into an error if you try to add this plugin saying:

      \u00d7 Building wheel for numpy (pyproject.toml) did not run successfully.

      The reason is that the mkdocs-material docker image uses the alpine image. mkdocs-table-reader-plugin depends on pandas, which depends on numpy, which in turn requires C++ to install, which is not part of the alpine image.

      If you need a very small image, you can adapt the alpine image to support numpy, but you are better off using a more complete docker image like python-slim. So you need to build a different docker image.

      As an example, below is a Dockerfile that is adapted from the mkdocs-material dockerfile. You can use it to create a new Docker image that supports mkdocs-table-reader-plugin:

      git clone https://github.com/squidfunk/mkdocs-material.git material-git/\ncd material-git\n# Manually replace `Dockerfile` with the example below\ndocker build -t YOUR-CONTAINER-NAME .\n# Now, inside your own project, you can use:\ndocker run --rm -it -p 8000:8000 -v ${PWD}:/docs YOUR-CONTAINER-NAME\n
      Dockerfile

      FROM python:3-slim\n\n# Build-time flags\nARG WITH_PLUGINS=true\n\n# Environment variables\nENV PACKAGES=/usr/local/lib/python3.11/site-packages\nENV PYTHONDONTWRITEBYTECODE=1\n\n# Set build directory\nWORKDIR /tmp\n\n# Copy files necessary for build\nCOPY material-git/material material\nCOPY material-git/package.json package.json\nCOPY material-git/README.md README.md\nCOPY material-git/requirements.txt requirements.txt\nCOPY material-git/pyproject.toml pyproject.toml\n\n# Perform build and cleanup artifacts and caches\nRUN \\\napt update \\\n&& \\\napt install -y \\\n    libcairo2-dev \\\n    libfreetype6-dev \\\n    git \\\n    libturbojpeg-dev \\\n    openssh-server \\\n    zlib1g-dev \\\n&& \\\napt install -y \\\n    gcc \\\n    libffi-dev \\\n    musl-dev \\\n&& \\\npip install --no-cache-dir . \\\n&& \\\nif [ \"${WITH_PLUGINS}\" = \"true\" ]; then \\\n    pip install --no-cache-dir \\\n    \"mkdocs-minify-plugin>=0.3\" \\\n    \"mkdocs-redirects>=1.0\" \\\n    \"pillow>=9.0\" \\\n    \"cairosvg>=2.5\" \\\n    \"mkdocs-table-reader-plugin\" \\\n    ; \\\nfi \\\n&& \\\nfor theme in mkdocs readthedocs; do \\\n    rm -rf ${PACKAGES}/mkdocs/themes/$theme; \\\n    ln -s \\\n    ${PACKAGES}/material \\\n    ${PACKAGES}/mkdocs/themes/$theme; \\\ndone \\\n&& \\\nrm -rf /tmp/* /root/.cache \\\n&& \\\nfind ${PACKAGES} \\\n    -type f \\\n    -path \"*/__pycache__/*\" \\\n    -exec rm -f {} \\;\n\n# Trust directory, required for git >= 2.35.2\nRUN git config --global --add safe.directory /docs &&\\\n    git config --global --add safe.directory /site\n\n# Set working directory\nWORKDIR /docs\n\n# Expose MkDocs development server port\nEXPOSE 8000\n\n# Start development server by default\nENTRYPOINT [\"mkdocs\"]\nCMD [\"serve\", \"--dev-addr=0.0.0.0:8000\"]\n
      \"mkdocs-table-rea

      "},{"location":"howto/preprocess_tables/","title":"Preprocess input tables","text":"

      mkdocs>=1.4 supports hooks, which enable you to run python scripts on mkdocs serve or mkdocs build.

      Here are some example of workflows that use hooks and the table-reader plugin:

      "},{"location":"howto/preprocess_tables/#combine-a-directory-of-tables-into-a-single-larger-table","title":"Combine a directory of tables into a single, larger table.","text":"hooks.pyindex.mdmkdocs.ymlProject structure
      from os import listdir\nfrom os.path import isfile, join\nimport pandas as pd\n\ndef on_pre_build(config, **kwargs) -> None:\n    tables = []\n    input_files = [f for f in listdir(mypath) if isfile(join(mypath, f))]\n    for table in input_files:\n        tables.append(pd.read_csv(table))\n    df = pd.concat(tables, ignore_index=True)\n    df.to_csv(\"docs/assets/output_table.csv\")\n

      My table: {{ read_csv(\"docs/assets/output_table.csv\") }}

      # ...\nplugins:\n    - table-reader\nhooks:\n    - scripts/hooks.py\n# ...\n
      .\n\u251c\u2500\u2500 scripts/\n\u2502   \u2514\u2500\u2500\u2500 hooks.py\n\u251c\u2500\u2500 docs/\n\u2502   \u2514\u2500\u2500\u2500 index.md\n\u2502   \u2514\u2500\u2500 assets/tables/\n\u2502       \u2514\u2500\u2500 table1.csv\n\u2502       \u2514\u2500\u2500 table2.csv\n\u2514\u2500\u2500 mkdocs.yml\n

      Alternative: use jinja

      You can also use jinja2 to display a list of tables. See how to use jinja2 for automation.

      "},{"location":"howto/preprocess_tables/#download-a-table-from-an-api","title":"Download a table from an API","text":"hooks.pyindex.mdmkdocs.ymlProject structure
      import pandas as pd\n\ndef on_pre_build(config, **kwargs) -> None:\n    df = pd.read_csv('https://data.cityofnewyork.us/resource/nu7n-tubp.csv?$limit=100')\n    df.to_csv(\"docs/assets/nyc_data.csv\")\n

      My table: {{ read_csv(\"docs/assets/nyc_data.csv\") }}

      # ...\nplugins:\n    - table-reader\nhooks:\n    - scripts/hooks.py\n# ...\n
      .\n\u251c\u2500\u2500 scripts/\n\u2502   \u2514\u2500\u2500\u2500 hooks.py\n\u251c\u2500\u2500 docs/\n\u2502   \u2514\u2500\u2500\u2500 index.md\n\u2502   \u2514\u2500\u2500 assets/\n\u2514\u2500\u2500 mkdocs.yml\n

      Note that during development when you use mkdocs serve and autoreload, you might not want to run this hook every time you make a change. You could use an environment variable inside your hook, for example something like if os.environ['disable_hook'] == 1: return None.

      "},{"location":"howto/project_structure/","title":"Choose a project structure","text":"

      You have different possible strategies to store and load your tables. This guide gives some examples.

      "},{"location":"howto/project_structure/#one-table-per-markdown-file","title":"One table per markdown file","text":"

      If you only want to include an occasional table in a specific markdown file, just store it in the same directory as the markdown file:

      project structurefolder/page.md
      .\n\u251c\u2500\u2500 docs/\n\u2502   \u251c\u2500\u2500 index.md\n\u2502   \u2514\u2500\u2500 folder/\n\u2502       \u2514\u2500\u2500 another_table.csv\n\u2502       \u2514\u2500\u2500 page.md\n\u2514\u2500\u2500 mkdocs.yml\n
      Here is the table:\n\n\\{\\{ read_csv(\"another_table.csv\") \\}\\}\n

      In page.md, to read another_table.csv, you can choose to use:

      • {{ read_csv(\"docs/folder/another_table.csv\") }} (Path relative to mkdocs.yml)
      • {{ read_csv(\"folder/another_table.csv\") }} (Path relative to docs/ directory)
      • {{ read_csv(\"another_table.csv\") }} (Path relative to page source file)
      "},{"location":"howto/project_structure/#re-using-tables-across-markdown-files","title":"Re-using tables across markdown files","text":"

      If you want to reuse tables in multiple markdown files, or have many tables, you'll want to store them in a central directory, like docs/assets/tables. That way, if you restructure your navigation, the links to the tables won't break either. It's also great if you generate tables because the output directory will be the same.

      Given the following project structure:

      .\n\u251c\u2500\u2500 docs/\n\u2502   \u251c\u2500\u2500 index.md\n\u2502   \u2514\u2500\u2500 folder/\n\u2502       \u2514\u2500\u2500 page.md\n\u2502   \u2514\u2500\u2500 assets/\n\u2502       \u2514\u2500\u2500 tables/\n\u2502           \u2514\u2500\u2500 another_table.csv\n\u2514\u2500\u2500 mkdocs.yml\n

      In page.md, to read another_table.csv, you can choose to use:

      • {{ read_csv(\"docs/assets/tables/another_table.csv\") }} (Path relative to mkdocs.yml)
      • {{ read_csv(\"assets/tables/another_table.csv\") }} (Path relative to docs/ directory)
      • {{ read_csv(\"../assets/tables/another_table.csv\") }} (Path relative to page source file (note that .. stands for \"one directory up\"))
      "},{"location":"howto/use_jinja2/","title":"Use jinja2 for automation","text":"

      table-reader supports mkdocs-macros-plugin, which enables you to use jinja2 syntax inside markdown files (among other things).

      To enable macros, specify the plugin before table-reader in your mkdocs.yml file:

      plugins:\n    - macros\n    - table-reader\n

      Now you can do cool things like:

      "},{"location":"howto/use_jinja2/#dynamically-load-a-list-of-tables","title":"Dynamically load a list of tables","text":"
      # index.md\n\n{% set table_names = [\"basic_table.csv\",\"basic_table2.csv\"] %}\n{% for table_name in table_names %}\n\n{ { read_csv(table_name) }}\n\n{% endfor %}\n
      "},{"location":"howto/use_jinja2/#insert-tables-into-content-tabs","title":"Insert tables into content tabs","text":"

      If you inserted content has multiple lines, then indentation will be not be retained beyond the first line. This means things like content tabs will not work as expected.

      To fix that, you can use the custom filter add_indendation (a filter add to macros by table-reader plugin). For example:

      index.mdmkdocs.yml
      {% set table_names = [\"basic_table.csv\",\"basic_table2.csv\"] %}\n{% for table_name in table_names %}\n\n=== \"{{ table_name }}\"\n\n    { { read_csv(table_name) | add_indentation(spaces=4) }}\n\n{% endfor %}\n
      site_name: test git_table_reader site\nuse_directory_urls: true\n\ntheme:\nname: material\n\nplugins:\n    - search\n    - macros\n    - table-reader\n\nmarkdown_extensions:\n    - pymdownx.superfences\n    - pymdownx.tabbed:\n        alternate_style: true\n

      Note the space in { {

      To avoid the tables being inserted into the code example, we replaced {{ with { {. If you copy this example, make sure to fix.

      "},{"location":"howto/use_jinja2/#recursively-insert-an-entire-directory-of-tables","title":"Recursively insert an entire directory of tables","text":"

      mkdocs-macros-plugin enables you to define additional functions (called macros) that you will be able to use within your markdown files. See their documentation on how to set this up. Here's an example with some functions to interact with the filesystem:

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

      Now you could do something like:

      # index.md\n\n{% for table_name in listdir('docs/assets/my_tables\") %}\n\n{ { read_csv(table_name) }}\n\n{% endfor %}\n

      Note the space in { {

      To avoid the tables being inserted into the code example, we replaced {{ with { {. If you copy this example, make sure to fix.

      "}]} \ No newline at end of file diff --git a/sitemap.xml.gz b/sitemap.xml.gz index 1dec3ef..499b827 100644 Binary files a/sitemap.xml.gz and b/sitemap.xml.gz differ