Making tables from files (and more) #3315
Replies: 6 comments 12 replies
-
You could do this with a custom fence with SuperFences: https://facelessuser.github.io/pymdown-extensions/extensions/superfences/#custom-fences. The one thing to note is that when using SuperFence's custom fences, whatever is returned by the custom fence logic is stored away and replaced with a placeholder such that further Markdown parsing is not run on it. Placeholders are replaced with the actual content at the end. You can do almost anything with them. We use it in the pymdown-extension documentation to create special blocks that spawn a new Markdown process such that we can display Markdown rendered with different options (specified with YAML frontmatter) than what the documents use: https://github.com/facelessuser/pymdown-extensions/blob/main/tools/pymdownx_md_render.py. Then we just provide our blocks: ```md-render
---
extensions:
- pymdownx.magiclink
- pymdownx.saneheaders
- markdown.extensions.attr_list
extension_configs:
pymdownx.magiclink:
repo_url_shortener: true
repo_url_shorthand: true
social_url_shorthand: true
social_url_shortener: true
user: facelessuser
repo: pymdown-extensions
---
- Just paste links directly in the document like this: https://google.com.
- Or even an email address: fake.email@email.com{.magiclink-ignore}.
``` So, you could simply create your own that simply parses the code block as a CSV and return it as an HTML table. |
Beta Was this translation helpful? Give feedback.
-
Thanks for suggesting. Material for MkDocs is a theme, so what it does is mainly rendering. All Markdown processing is done by upstream projects, mainly Python Markdown and Python Markdown Extensions by @facelessuser. Preprocessing tabular data into Markdown tables is definitely Markdown processing, which is beyond the scope of this project. Material for MkDocs tries to make the most functional and beautiful website of the Markdown you're throwing at it, but that's basically it. Furthermore, preprocessing is theme-agnostic, since you're taking a format and converting it into Markdown, so it could probably be implemented as a Markdown extension. I would love to improve data tables, but the Markdown table syntax doesn't allow for much flexibility. The DataTables project depends on jQuery, which we don't have as a dependency for several reasons. Do you know of any other good data tables libraries, ideally without any external dependencies? I could take a look at whether we can add native support, similar to TableSort, which is also a third-party library. |
Beta Was this translation helpful? Give feedback.
-
Moving this into a discussion |
Beta Was this translation helpful? Give feedback.
-
Thanks for the replies! @squidfunk that of course makes sense, I keep forgetting that its a theme. Maybe it would be worth considering adding this case to the docs (once we perhaps end up at a good solution). As for alternatives, I especially like Simple DataTables - it's also lightweight, has no external dependencies and seems to be be well-maintained so that might be worth considering. (examples here) @facelessuser that seems a good approach. I've not previously worked with PyMdown or its extensions so I'm note quite sure how to go about this: Say I have a |
Beta Was this translation helpful? Give feedback.
-
Understood. I like to set expectations up front as sometimes I am asked for more than I am willing to give 🙂. |
Beta Was this translation helpful? Give feedback.
-
I've found a really good solution for this. You can read more on this blog: The best MkDocs plugins and customizations. All you need to do is: pip install mkdocs-table-reader-plugin Then extend the plugins:
- search
- table-reader And then add a CSV to your page like: {{ read_csv('path_to_table.csv') }} Or an Excel like:
This plugin uses the Pandas library, so this function should comply with that syntax. No need to roll your own Super Fences or custom Java Script processes. |
Beta Was this translation helpful? Give feedback.
-
Contribution guidelines
I want to suggest an idea and checked that ...
Description
First of all, thanks for all the brilliant work you're putting into this project - it's my go-to for everything documentation now!
This is a multi-feature request, which I'd be happy to discuss further. I'm no JS wizard so I'm not sure if I can help much myself on that side.
I'm proposing enabling the following workflow:
Tables from files
Being able to parse and query a data file (e.g. csv) to Markdown. There might be inspiration to get from the Obsidian obsidian-csv-table plugin. They take the approach where you can read the file by:
More js options
The above point is the important one! However, although being able to sort the table is good, it could easily be more. I'd think this should be relatively easy to implement (though I haven't tried and might be very wrong) e.g. with DataTables which is open source and can be further customized with extensions.
Use Cases
The data used in tables most often lives in data files (be it .csv, .txt, .xlsx etc.). And currently we have to type out the content of these files to render them as a table. If we'd be able to call a data file and render it directly, when something changes in the data, we wouldn't have to scour the pages to find the entries and replace them, they'd be changed automatically. I think this would be useful to almost everyone who has the need to make tables.
Personally, I'm currently working on an open hardware project where these have proved vital. We have a collection of parts in a file and want to showcase subsets of parts needed to make each build.
Screenshots / Mockups
No response
Beta Was this translation helpful? Give feedback.
All reactions