-
-
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] Writing JSON to STDOUT #1591
Comments
Works fine here. The broken pipe suggests it is something to do with the mechanics of piping the data in to JQ. Are you using a custom shell? What happens if you don't pipe it in to jq? |
I am using zsh but I have had issues in bash too. |
Since I can't reproduce it, can you help me narrow down what the trigger is? What happens if you output a just 10 objects? Does it break if you don't pipe it in to jq? Does your three line example break for you? I can see that the traceback has some additional code. |
@thehappydinoa Any more information. Will close in a week if I can't reproduce this. |
Hi @willmcgugan I have been unable to reproduce. My apologies but the best thing for my use case was to be able to revert to standard print statements. Thank you for all the help. |
Did I solve your problem? Consider sponsoring the ongoing work on Rich and Textual. Or buy me a coffee to say thanks. |
Hi @willmcgugan, this issue still persists and I think I have a minimal reproduction. # file: foo.py
from rich.console import Console
console = Console()
console.print("foo")
console.print("bar")
console.print("baz") Running the following bash command reproduces the error: A few points to note:
Traceback:
EDIT: added traceback |
Hmm yes just saw this regress after I switched to rich, but it can be remedied in the interim for integration in library code for example (who would otherwise see the same error if using regular print statements) with the following context manager. import os
import sys
__all__ = ("pipe_cleanup",)
def pipe_cleanup() -> None:
"""Handle BrokenPipeError: redirect output to devnull"""
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
sys.exit(1) # Python exits with error code 1 on EPIPE
from contextlib import AbstractContextManager
from types import TracebackType
from .pipes import pipe_cleanup
__all__ = ("SuppressBrokenPipeError",)
class SuppressBrokenPipeError(AbstractContextManager):
def __exit__(
self,
__exc_type: type[BaseException] | None,
__exc_value: BaseException | None,
__traceback: TracebackType | None,
) -> bool | None:
if __exc_type is BrokenPipeError:
return pipe_cleanup()
else:
return super().__exit__(__exc_type, __exc_value, __traceback) I incorporated this into my library like so: from contextlib import contextmanager
from rich.console import Console
from .error_handlers import SuppressBrokenPipeError
__all__ = ("FugitConsole", "fugit_console")
class FugitConsole:
console: Console
use_pager: bool
page_with_styles: bool
def __init__(self, use_pager: bool = False, page_with_styles: bool = True):
self.console: Console = Console()
self.use_pager: bool = use_pager
self.page_with_styles: bool = page_with_styles
@contextmanager
def pager_available(self):
"""Uses console pagination if `DisplayConfig` switched this setting on."""
if self.use_pager:
with self.console.pager(styles=self.page_with_styles):
yield self
else:
yield self
def print(self, output: str, end="", style=None) -> None:
"""
Report output through the rich console, but don't style at all if rich was set to
no_color (so no bold, italics, etc. either), and avoid broken pipe errors when
piping to `head` etc.
"""
with_style = style if fugit_console.no_color else None
with SuppressBrokenPipeError():
fugit_console.console.print(output, end=end, style=with_style)
"""
Global `rich.console.Console` instance modified by a model validator upon initialisation
of `fugit.interfaces.display.DisplayConfig` or its subclass, the main `DiffConfig` model.
"""
fugit_console = FugitConsole() I can then test that piping output of my package's CLI entrypoint to |
Opened a PR that fixes it: |
I hope we solved your problem. If you like using Rich, you might also enjoy Textual |
Describe the bug
A clear and concise description of what the bug is.
I have been attempting to use rich to output a very large JSON into STDOUT.
To Reproduce
A minimal code example that reproduces the problem would be a big help if you can provide it. If the issue is visual in nature, consider posting a screenshot.
test_rich.py
Platform
What platform (Win/Linux/Mac) are you running on? What terminal software are you using?
macOS, I have tried the normal Terminal, iTerm 2, and VSCode Integrated Terminal.
Diagnose
The text was updated successfully, but these errors were encountered: