Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code-generated Markdown tables do not render correctly #4

Open
tsalo opened this issue Apr 6, 2022 · 12 comments
Open

Code-generated Markdown tables do not render correctly #4

tsalo opened this issue Apr 6, 2022 · 12 comments

Comments

@tsalo
Copy link
Member

tsalo commented Apr 6, 2022

This is a known issue in MyST-NB. I want to figure out a workaround.

@tsalo tsalo changed the title Code-generate Markdown tables do not render correctly Code-generated Markdown tables do not render correctly Apr 6, 2022
@tsalo
Copy link
Member Author

tsalo commented Apr 6, 2022

@effigies
Copy link
Contributor

effigies commented Apr 6, 2022

If you copy-paste the generated markdown in for one of the tables, does it render correctly? Is it possible that we're not generating the correct markdown?

@tsalo
Copy link
Member Author

tsalo commented Apr 6, 2022

If I copy the to_markdown() output to the file directly, it will render correctly. The problem is when I try to display the code-cell output. The relevant MyST-NB issue is executablebooks/MyST-NB#322.

@effigies
Copy link
Contributor

effigies commented Apr 6, 2022

Can we render to RST or HTML?

@tsalo
Copy link
Member Author

tsalo commented Apr 6, 2022

I was planning to try HTML. Hadn't thought of trying RST.

@tsalo
Copy link
Member Author

tsalo commented Apr 6, 2022

I think one reasonable solution might be to use pandas Styling, like here. We'll need to figure out the optimal formatting for our tables though...

@tsalo
Copy link
Member Author

tsalo commented Apr 6, 2022

One idea of @effigies' that seems to work is

display(HTML(markdown(table.to_markdown(), extensions=['tables'])))

The thing I'm stuck on now is including glossary links in the table. For example, having the string {term}`Name (metadata)` ends up looking like
image
in the rendered table.

@effigies
Copy link
Contributor

effigies commented Apr 6, 2022

Here's a nuts idea:

display(Markdown(markdown(table.to_markdown(), extensions=['tables'])))

Probably won't work since the outer markdown will interpret the HTML as hands-off...

@tsalo
Copy link
Member Author

tsalo commented Apr 6, 2022

Just tried it out, and the result was the same as the HTML version.

@effigies
Copy link
Contributor

effigies commented Apr 6, 2022

I don't know the Markdown object well enough, but if we could invert this like markdown(Markdown(...), extensions=['tables']), that might work.

@tsalo
Copy link
Member Author

tsalo commented Apr 6, 2022

I tried writing a function that would replace {term}<code>...</code> with {term}`...`. The HTML string does end up having the {term}`...` I want, but it looks like the glossary links must be rendered before the table is displayed?

Even having display(Markdown("{term}Manufacturer (metadata)")) in a code cell didn't work.

EDIT: I've opened #8 about this problem.

I don't know the Markdown object well enough, but if we could invert this like markdown(Markdown(...), extensions=['tables']), that might work.

This didn't work. markdown() requires a string.

@effigies
Copy link
Contributor

effigies commented Apr 7, 2022

It looks like we can use .to_markdown(tablefmt=...) for values listed in https://github.com/astanin/python-tabulate#table-format, which includes 'rst'. So might be worth seeing if converting to RST would be easier than hacking myst to render markdown tables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants