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

Certain renderables created with rich.markdown generate AssertionError when rendered in a Widget #3006

Closed
jspv opened this issue Jul 25, 2023 · 5 comments

Comments

@jspv
Copy link
Contributor

jspv commented Jul 25, 2023

If a renderable is created by Rich.Markdown with the following string, "| Simple | Table |\n| ------ | -----" Textual will raise an AssertionError when the Widget is rendered. The following code will demonstrate the problem:

from textual.app import App
from textual.widgets import Static

from rich.markdown import Markdown


# This will work
responses = "| Simple | Table |\n| ------ | ----- |\n| cell1  | cell2 |"

# This will not work
responses = "| Simple | Table |\n| ------ | ----- "


class MDTest(App):
    def compose(self):
        yield Static(Markdown(responses))


app = MDTest()
app.run()

While the offending string is not a properly formatted Markdown table, it should still render the string and not raise an exception. This was discovered trying to render Markdown output that is coming in a stream of word tokens. I've only seen the issue so far with tables, all other Markdown has rendered well, showing the raw strings until there enough tokens arrived to represent the artifact.

textual diagnose

Textual Diagnostics

Versions

Name Value
Textual 0.29.0
Rich 13.4.2

Python

Name Value
Version 3.10.12
Implementation CPython
Compiler Clang 14.0.3 (clang-1403.0.22.14.1)
Executable /Users/justin/Library/Caches/pypoetry/virtualenvs/mchat-mvZqEniY-py3.10/bin/python

Operating System

Name Value
System Darwin
Release 22.5.0
Version Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:23 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6020

Terminal

Name Value
Terminal Application vscode (1.80.1)
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=196, height=20
legacy_windows False
min_width 1
max_width 196
is_terminal True
encoding utf-8
max_height 20
justify None
overflow None
no_wrap False
highlight None
markup None
height None
@github-actions
Copy link

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@willmcgugan
Copy link
Collaborator

Including the traceback or at least the error message can be very helpful…

@TomJGooding
Copy link
Contributor

TomJGooding commented Jul 25, 2023

The traceback was huge and not particularly useful to be fair. If you run this just with Rich however:

Example code

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

# This will work
responses = "| Simple | Table |\n| ------ | ----- |\n| cell1  | cell2 |"

# This will not work
responses = "| Simple | Table |\n| ------ | ----- "

console = Console()
md = Markdown(responses)
console.print(md)

Traceback

Traceback (most recent call last):
  File "/home/tom/Projects/textual-sandbox/repro_3006_console.py", line 12, in <module>
    console.print(md)
  File "/home/tom/Projects/textual-sandbox/.venv/lib/python3.11/site-packages/rich/console.py", line 1699, in print
    extend(render(renderable, render_options))
  File "/home/tom/Projects/textual-sandbox/.venv/lib/python3.11/site-packages/rich/console.py", line 1331, in render
    for render_output in iter_render:
  File "/home/tom/Projects/textual-sandbox/.venv/lib/python3.11/site-packages/rich/markdown.py", line 686, in __rich_console__
    yield from console.render(element, context.options)
  File "/home/tom/Projects/textual-sandbox/.venv/lib/python3.11/site-packages/rich/console.py", line 1331, in render
    for render_output in iter_render:
  File "/home/tom/Projects/textual-sandbox/.venv/lib/python3.11/site-packages/rich/markdown.py", line 262, in __rich_console__
    assert self.body is not None
           ^^^^^^^^^^^^^^^^^^^^^
AssertionError

@jspv
Copy link
Contributor Author

jspv commented Jul 25, 2023

The traceback was huge and not particularly useful to be fair. If you run this just with Rich however:

Thanks! I'll move this over to Rich.

@jspv jspv closed this as completed Jul 25, 2023
@github-actions
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

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

3 participants