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

Table layout and carriage return #2

Closed ncarboni closed 4 years ago

ncarboni commented 4 years ago

I am testing with this plugin and it is already fantastic, but I was wondering if there is a way to take into account carriage return within a cell. I would like a cell to display

→ P108i → E12 → P32 → E55 
→ P108i → E12 → P2 → E55

But if I tried to load a CSV with a carriage return I obtain this:

csv_return

fair enough, it is a CSV. Using an excel file (using {{ read_excel('file'.xlsx) }} instead I obtain this result:

excel_return

While nicer the result coming from excel do present a new line instead of a carriage return within the same cell. I was wondering if it would be possible to have a real carriage return, something like this (obtained using <br>):

br_xls
timvink commented 4 years ago

Thanks for the feedback, glad to hear you like it! And a different use case than I imaged when I created it, nice!

Some technical context

This plugin uses df.to_markdown() to create markdown from a table. That's included in pandas>=1.03, but actually uses tabulate under the hood.

**kwargs These parameters will be passed to tabulate. docs for pandas.DataFrame.to_markdown

Currently, this is what the call looks like in this plugin:

https://github.com/timvink/mkdocs-table-reader-plugin/blob/1d75fbfc8cdb07c21b196065965a1e8e7845baff/mkdocs_table_reader_plugin/plugin.py#L10

There are other parameters supported by tabulate, see python-tabulate. Specifically the section on line-breaks is very relevant: "Note that some output formats (e.g. simple, or plain) do not represent row delimiters, so that the representation of multiline cells in such formats may be ambiguous to the reader" . The default is tablefmt="simple" so it might be useful to try a different format.

MkDocs uses python-markdown which implements this reference syntax. That one doesn't include tables, which is why python-markdown has a tables extension. That extension in turn uses the PHP Extra Markdown syntax for tables, which is supported by tabulate via tablefmt="pipe", which is included in the list of output formats that support multiline cells!

That was a fun bit of detective work :) With this knowledge, I think "pipe" should be the default tablefmt for this plugin. I created a new merge request with the change in #3 that we can test.

Possible solutions

  1. The new default tablefmt="pipe" output setting. Can you test (by installing #3) if this solves your issue before I release a new version?
  2. If you are on windows, make sure your line endings are in unix format (\n instead of \r\n). context
ncarboni commented 4 years ago

Thanks for the explanation, it is quite useful to understand the possibilities. I quickly tried yesterday using tablefmt="pipe", but I still did not get any multiline. I will do some other testing today (I will try other tablefmt options and see if something change) and come back with more info. In the meanwhile, thank you very much!

ncarboni commented 4 years ago

A bit of recap after some tests. I added tablefmt="pipe" (return df.to_markdown(showindex=False, tablefmt="pipe") to read_csv and read_excel, and re-installed the package. However, I do not see any differences in the creation of the table. I tried with other table format from python-tabulate, but they did not seems to work either. I tried to inserti a \n in order to signal a <br> in the cell of the CSV or excel file, but also there I did not obtained a real carriage return but only a new row. I am maybe doing something wrong from my side? Did you by any chance make it work?

timvink commented 4 years ago

Did some experiments (and found + fixed a different bug in #4).

This csv:

image

Renders as:

image

However inserting <br/> directly works as intended, and might already be the solution for your specific use-case:

image

image

It seems that the csv standard does not support escape characters like \n (source). Alternatively, specifying the newline differently in the CSV creates a new row but the alignment is proper:

image

image


I'm not sure what the proper behaviour should be, and where that should be addressed. Most likely it's in python-markdown tables extension. But the root cause is not in mkdocs-table-reader-plugin, so I suggest to close this issue. I might investigate further and open a new issue elsewhere.

timvink commented 4 years ago

FYI, I opened https://github.com/Python-Markdown/markdown/issues/975

ncarboni commented 4 years ago

Thanks for the testing! If will use
for now and let's see if something will change from python-markdown side. Feel free to close :-)

timvink commented 4 years ago

Long story short, for multiline cells the solution is to use <br/>. (see also the python-markdown issue). If you need anything more complex, you can always fall back to raw HTML tables.