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

[BUG] Rich markdown printed to console only shows a single link in a column/ table cell #3115

Closed
2 tasks done
cwerner opened this issue Aug 31, 2023 · 13 comments · Fixed by #3130
Closed
2 tasks done
Assignees
Labels
accepted Task was accepted bug Something isn't working

Comments

@cwerner
Copy link

cwerner commented Aug 31, 2023

Describe the bug

I use Rich to format a pandas data frame with multiple links in some column to markdown and show it in the terminal. However, when there are more than a single link in a column rich only shows the last one?

from rich.console import Console
from rich.markdown import Markdown

import pandas as pd

md_table = pd.DataFrame(
    {
        "links": ["[page1](https://www.example.com/page1), [page2](https://www.example.com/page2)"]
    }
).to_markdown()

console = Console()
console.print(Markdown(md_table))

Output:

      links  
 ━━━━━━━━━━━ 
  0   page2  

Expected

      links  
 ━━━━━━━━━━━ 
  0   page1, page2  

If I write the markdown to a file I get the expected table with multiple links...

Platform

platform="Darwin"
rich==13.5.2

@github-actions
Copy link

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@willmcgugan
Copy link
Collaborator

Could you post the Markdown it generates?

@cwerner
Copy link
Author

cwerner commented Aug 31, 2023

|    | links                                                                          |
|---:|:-------------------------------------------------------------------------------|
|  0 | [page1](https://www.example.com/page1), [page2](https://www.example.com/page2) |

@cwerner
Copy link
Author

cwerner commented Aug 31, 2023

I actually discovered that the problem is that if there is a link in a table, only the link (and in case of multiple links the last one) is rendered.

For instance:

from rich.console import Console
from rich.markdown import Markdown

import pandas as pd

md_table = pd.DataFrame(
    {
        "links": ["some text [page1](https://www.example.com/page1 )"]
    }
).to_markdown()

console = Console()
console.print(Markdown(md_table)) 

will result in:

      links  
 ━━━━━━━━━━━ 
  0   page1  

Thus, swallowing "some text" that was also present in this table cell. Not sure if this gives a clue about what might be wrong?

@willmcgugan
Copy link
Collaborator

This may also be present in Textual.

@davep
Copy link
Contributor

davep commented Aug 31, 2023

This may also be present in Textual.

If I'm reading the issue correctly, it seems to be working fine in Textual (as seen via Frogmouth):

Screenshot 2023-08-31 at 13 13 57

viewing this Markdown:

| One                                                        | Two       | Three                                 |
|------------------------------------------------------------|-----------|---------------------------------------|
| Some test [and then a link](https://textualize.io/)        | Just Text | [Just a link](https://textualize.io/) |
| Text [then a link](https://blog.davep.org/) then more text | Just Text | Some final text                       |

@willmcgugan
Copy link
Collaborator

Good to know. We must be doing something different in Rich.

@cwerner
Copy link
Author

cwerner commented Aug 31, 2023

Oh nice. Wondering: Can I use textual to render my Markdown before I hand it over to console.print()until this bug is fixed? I haven't used textual before but don't mind this extra dependency for the moment...

@willmcgugan
Copy link
Collaborator

You can certainly use Textual, but it works differently from console.print. It creates an interactive window inside the terminal.

@rodrigogiraoserrao
Copy link
Contributor

Consider this markdown file:

[page 1](https://mathspp.com)

| links |
| - |
| asdfasdf  [page 1](https://mathspp.com) |

Running python -m rich.markdown file.md will show the first link just fine but the one on the table doesn't:

page 1 (https://mathspp.com)


  links
 ━━━━━━━
  )

Putting text on the right of the table link makes the link go away and only displays that text:

file.md with text on the right.
[page 1](https://mathspp.com)

| links |
| - |
| asdfasdf  [page 1](https://mathspp.com) right |
Output of python -m rich.markdown file.md
page 1 (https://mathspp.com)


  links
 ━━━━━━━━
   right

Running with --hyperlinks does a slightly better job, but it still drops things.

With text on the right

file.md with text on the right.
[page 1](https://mathspp.com)

| links |
| - |
| asdfasdf  [page 1](https://mathspp.com) some text |
Output of python -m rich.markdown file.md -y (The “page 1” has the proper link in the terminal)
page 1


  links
 ━━━━━━━━━━━━
   some text

Without text on the right

file.md without text on the right.
[page 1](https://mathspp.com)

| links |
| - |
| asdfasdf  [page 1](https://mathspp.com) |
Output of python -m rich.markdown file.md -y (The output has the proper links in the terminal)
page 1


  links
 ━━━━━━━━
  page 1

@rodrigogiraoserrao
Copy link
Contributor

Turns out we're seeing this interesting interaction with a bunch of inline styles in markdown.

The markdown below gets rendered into the output shown after that:

| stuff |
| - |
| asdfasdf  [page 1](https://mathspp.com) some text |
| This is `some code` here |
| This is `some code` |
| Can I set this to _italics_? |
  stuff
 ━━━━━━━━━━━━
   some text
   here
  some code
  ?

@rodrigogiraoserrao rodrigogiraoserrao self-assigned this Sep 12, 2023
@rodrigogiraoserrao
Copy link
Contributor

I've tracked the issue down to rich/markdown.py::TableDataElement.on_text, which should append to its content instead of setting it directly.

@github-actions
Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Task was accepted bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants