-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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] List of numpy arrays does not print correctly #1876
Comments
It seems to be seeing it as markup. import numpy as np
from rich import print
from rich.console import Console
console = Console()
list_of_arrays = [np.array([1, 0, 0]), np.array([0, 1, 0]), np.array([0, 0, 1])]
print(list_of_arrays)
print(f'Does something weird with f strings: {list_of_arrays}')
console.print(f'Does something weird with f strings: {list_of_arrays}', markup=False) output [array([1, 0, 0]), array([0, 1, 0]), array([0, 0, 1])]
Does something weird with f strings: ), array([0, 1, 0]), array([0, 0, 1])]
Does something weird with f strings: [array([1, 0, 0]), array([0, 1, 0]), array([0, 0, 1])] |
I tracked it down to here. Nested lists are being matched in the regular expression so it isn't specific to NumPy. Any function, class, etc with a list matches. For example: from array import array
from rich import print
test_list = [array("l", [1, 2]), array("l", [3, 4])]
print(f'test {test_list}') output test ), array('l', [3, 4])] I can't currently think of a way to fix this without breaking the markkup. With the current regular expression an escape can be used to prevent the issue. import numpy as np
from rich import print
list_of_arrays = [np.array([1, 0, 0]), np.array([0, 1, 0]), np.array([0, 0, 1])]
print(f'Does something weird with f strings: \{list_of_arrays}') output Does something weird with f strings: [array([1, 0, 0]), array([0, 1, 0]), array([0, 0, 1])] |
@sanders41 is absolutely correct. It's because the output happens to look like a console markup tag. One option is to escape the value in the f string. Import Alternatively, if you use a Console object, you can disable markup, either globally or on the individual print. However, you're better off not converting the object to a string at all. If you just print the array, Rich can format and highlight it without escaping issues: print("Don't do anything with f string:", list_of_arrays) |
The solution above is probably best. But because this is a common, and natural, thing to do in Python we are going to look at excluding tags that contain parenthesis. This should prevent accidental markup processsing. |
Thanks for the follow-up. Excited to see what comes out of this! |
Did I solve your problem? Why not buy the devs a coffee to say thanks? |
We haven't released a new version yet... |
I'm absolutely stupid, for some reason I thought it would already be in there. I'm very sorry! |
New version landing in a few days :) |
The bug is actually rather simple. Whenever you attempt to print a list that consists of NumPy arrays, it cuts off the first element of the list when combined with a string. Below is a minimal code example that shows the bug:
output
Of course, the output should be that the second line also shows the complete list.
I provide a screenshot below to show what it looks like in my terminal.
Platform
Platform: Mac
Terminal: Oh-my-zsh
Python version: 3.7
Rich version: 11.0.0
Click to expand
The other information that was asked after executing the commands: ╭───────────────────────── ─────────────────────────╮ │ A high level console interface. │ │ │ │ ╭──────────────────────────────────────────────────────────────────────────────╮ │ │ │ │ │ │ ╰──────────────────────────────────────────────────────────────────────────────╯ │ │ │ │ color_system = '256' │ │ encoding = 'utf-8' │ │ file = <_io.TextIOWrapper name='' mode='w' encoding='UTF-8'> │ │ height = 12 │ │ is_alt_screen = False │ │ is_dumb_terminal = False │ │ is_interactive = True │ │ is_jupyter = False │ │ is_terminal = True │ │ legacy_windows = False │ │ no_color = False │ │ options = ConsoleOptions( │ │ size=ConsoleDimensions(width=197, height=12), │ │ legacy_windows=False, │ │ min_width=1, │ │ max_width=197, │ │ is_terminal=True, │ │ encoding='utf-8', │ │ max_height=12, │ │ justify=None, │ │ overflow=None, │ │ no_wrap=False, │ │ highlight=None, │ │ markup=None, │ │ height=None │ │ ) │ │ quiet = False │ │ record = False │ │ safe_box = True │ │ size = ConsoleDimensions(width=197, height=12) │ │ soft_wrap = False │ │ stderr = False │ │ style = None │ │ tab_size = 8 │ │ width = 197 │ ╰──────────────────────────────────────────────────────────────────────────────────╯platform="Darwin"
WindowsConsoleFeatures(vt=False, truecolor=False)
I would be happy to provide any additional feedback if necessary.
The text was updated successfully, but these errors were encountered: