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] Extra Space above Certain Markdown Tables #3027

Open
2 tasks done
dgrahn opened this issue Jul 6, 2023 · 5 comments
Open
2 tasks done

[BUG] Extra Space above Certain Markdown Tables #3027

dgrahn opened this issue Jul 6, 2023 · 5 comments

Comments

@dgrahn
Copy link

dgrahn commented Jul 6, 2023

Describe the bug
Certain markdown tables contain extra newlines above them in Rich 13.4.2.

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

MD = """
|   Temperature |        |        |        |        |           |
|--------------:|:-------|:-------|:-------|:-------|:----------|
|          0.01 | sam    | sam    | sam    | sam    | sam       |
|          0.1  | sam    | sam    | sam    | sam    | sam       |
|          0.25 | sam    | sam    | sam    | sammy  | sammy     |
|          0.5  | lilly  | sam    | sammy  | sammy  | taffy     |
|          0.75 | bambi  | lola   | snoopy | taffy  | taz       |
|          0.9  | bella  | harper | millie | molly  | sweetie   |
|          1    | Anna   | molly  | shaker | sydney | wheessie  |
|          1.25 | Finley | funny  | gertie | gladi  | road kill |
""".strip()

console = Console()
markdown = Markdown(MD)

print('--')
console.print(markdown)
print('--')

image

Platform

Click to expand

Windows 10.

┌───────────────────────── <class 'rich.console.Console'> ─────────────────────────┐
│ A high level console interface.                                                  │
│                                                                                  │
│ ┌──────────────────────────────────────────────────────────────────────────────┐ │
│ │ <console width=148 ColorSystem.WINDOWS>                                      │ │
│ └──────────────────────────────────────────────────────────────────────────────┘ │
│                                                                                  │
│     color_system = 'windows'                                                     │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 56                                                            │
│    is_alt_screen = False                                                         │
│ is_dumb_terminal = False                                                         │
│   is_interactive = True                                                          │
│       is_jupyter = False                                                         │
│      is_terminal = True                                                          │
│   legacy_windows = True                                                          │
│         no_color = False                                                         │
│          options = ConsoleOptions(                                               │
│                        size=ConsoleDimensions(width=148, height=56),             │
│                        legacy_windows=True,                                      │
│                        min_width=1,                                              │
│                        max_width=148,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=56,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=148, height=56)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 148                                                           │
└──────────────────────────────────────────────────────────────────────────────────┘
┌─── <class 'rich._windows.WindowsConsoleFeatures'> ────┐
│ Windows features available.                           │
│                                                       │
│ ┌───────────────────────────────────────────────────┐ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ └───────────────────────────────────────────────────┘ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
└───────────────────────────────────────────────────────┘
┌────── Environment Variables ───────┐
│ {                                  │
│     'TERM': None,                  │
│     'COLORTERM': None,             │
│     'CLICOLOR': None,              │
│     'NO_COLOR': None,              │
│     'TERM_PROGRAM': None,          │
│     'COLUMNS': None,               │
│     'LINES': None,                 │
│     'JUPYTER_COLUMNS': None,       │
│     'JUPYTER_LINES': None,         │
│     'JPY_PARENT_PID': None,        │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
└────────────────────────────────────┘
platform="Windows"
@github-actions
Copy link

github-actions bot commented Jul 6, 2023

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

@TomJGooding
Copy link

It looks like an extra new line is rendered for every blank cell in the table.

I'm still getting to grips with the codebase, but I think I've narrowed it down to here

rich/rich/markdown.py

Lines 692 to 699 in 720800e

should_render = (
not context.stack
or context.stack
and context.stack.top.on_child_close(context, element)
)
if should_render:
if new_line:
yield _new_line_segment

@martinpk
Copy link

@TomJGooding Thanks for contributing a PR for this. Do you know if there's a workaround in the meantime until your PR is merged?

@TomJGooding
Copy link

A simple workaround would be to replace the empty cells in the table. Here's a quick example where I found using an invisible space character seems to trick Rich!

import re

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

table_with_empty_cells = """\
| First Header  |               |
| ------------- | ------------- |
| Content Cell  | Content Cell  |
|               | Content Cell  |\
"""

empty_cell_pat = re.compile(r"(?<=\|) +(?=\|)")
new_empty_cell = "\u200B"  # invisible space
new_table = empty_cell_pat.sub(new_empty_cell, table_with_empty_cells)

console = Console()
console.print(Rule("table_with_empty_cells"))
console.print(Markdown(table_with_empty_cells))

console.print(Rule("new_table"))
console.print(Markdown(new_table))

@martinpk
Copy link

@TomJGooding Ah, good idea. Thanks for the example code.

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

Successfully merging a pull request may close this issue.

3 participants