Skip to content

Commit

Permalink
faqs
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Apr 30, 2024
1 parent c034c68 commit aebc115
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 49 deletions.
78 changes: 52 additions & 26 deletions FAQ.md
@@ -1,55 +1,81 @@

# Frequently Asked Questions
- [Why does emoji break alignment in a Table or Panel?](#why-does-emoji-break-alignment-in-a-table-or-panel)
- [Why does content in square brackets disappear?](#why-does-content-in-square-brackets-disappear)
- [python -m rich.spinner shows extra lines](#python--m-rich.spinner-shows-extra-lines)
- [How do I log a renderable?](#how-do-i-log-a-renderable)
- [Strange colors in console output.](#strange-colors-in-console-output.)
- [How do I render console markup in RichHandler?](#how-do-i-render-console-markup-in-richhandler)
- [Natively inserted ANSI escape sequence characters break alignment of Panel](#natively-inserted-ansi-escape-sequence-characters-break-alignment-of-panel)
- [python -m rich.spinner shows extra lines.](#python--m-richspinner-shows-extra-lines)
- [Rich is automatically installing traceback handler.](#rich-is-automatically-installing-traceback-handler)
- [Strange colors in console output.](#strange-colors-in-console-output)
- [Why does content in square brackets disappear?](#why-does-content-in-square-brackets-disappear)
- [Why does emoji break alignment in a Table or Panel?](#why-does-emoji-break-alignment-in-a-table-or-panel)

<a name="why-does-emoji-break-alignment-in-a-table-or-panel"></a>
## Why does emoji break alignment in a Table or Panel?
<a name="how-do-i-log-a-renderable"></a>
## How do I log a renderable?

Certain emoji take up double space within the terminal. Unfortunately, terminals don't always agree how wide a given character should be.
Python's logging module is designed to work with strings. Consequently you won't be able to log Rich renderables (Table, Tree, etc) by calling `logger.debug` or other similar method.

Rich has no way of knowing how wide a character will be on any given terminal. This can break alignment in containers like Table and Panel, where Rich needs to know the width of the content.
You could use the [capture](https://rich.readthedocs.io/en/latest/console.html#capturing-output) API to convert the renderable to a string and log that. However I would advise against it.

There are also *multiple codepoints* characters, such as country flags, and emoji modifiers, which produce wildly different results across terminal emulators.
Logging supports configurable back-ends, which means that a log message could go somewhere other than the terminal -- which may not correctly render the formatting and style produced by Rich.

Fortunately, most characters will work just fine. But you may have to avoid using the emojis that break alignment. You will get good results if you stick to emoji released on or before version 9 of the Unicode database,
If you are only logging with a file-handler to stdout, then you probably don't need to use the logging module at all. Consider using [Console.log](https://rich.readthedocs.io/en/latest/reference/console.html#rich.console.Console.log) which will render anything that you can print with Rich, with a timestamp.

<a name="why-does-content-in-square-brackets-disappear"></a>
## Why does content in square brackets disappear?
<a name="how-do-i-render-console-markup-in-richhandler"></a>
## How do I render console markup in RichHandler?

Rich will treat text within square brackets as *markup tags*, for instance `"[bold]This is bold[/bold]"`.
Console markup won't work anywhere else, other than `RichHandler` -- which is why they are disabled by default.

If you are printing strings with literally square brackets you can either disable markup, or escape your strings.
See the docs on [console markup](https://rich.readthedocs.io/en/latest/markup.html) for how to do this.
See the docs if you want to [enable console markup](https://rich.readthedocs.io/en/latest/logging.html#logging-handler) in the logging handler.

<a name="python--m-rich.spinner-shows-extra-lines"></a>
## python -m rich.spinner shows extra lines
<a name="natively-inserted-ansi-escape-sequence-characters-break-alignment-of-panel"></a>
## Natively inserted ANSI escape sequence characters break alignment of Panel

The spinner example is know to break on some terminals (Windows in particular).
If you print ansi escape sequences for color and style you may find the output breaks your output.
You may find that border characters in Panel and Table are in the wrong place, for example.

Some terminals don't display emoji with the correct width, which means Rich can't always align them accurately inside a panel.
As a general rule, you should allow Rich to generate all ansi escape sequences, so it can correctly account for these invisible characters.
If you can't avoid a string with escape codes, you can convert it to an equivalent `Text` instance with `Text.from_ansi`.

<a name="how-do-i-log-a-renderable"></a>
## How do I log a renderable?
<a name="python--m-richspinner-shows-extra-lines"></a>
## python -m rich.spinner shows extra lines.

Python's logging module is designed to work with strings. Consequently you won't be able to log Rich renderables (Table, Tree, etc) by calling `logger.debug` or other similar method.
The spinner example is know to break on some terminals (Windows in particular).

You could use the [capture](https://rich.readthedocs.io/en/latest/console.html#capturing-output) API to convert the renderable to a string and log that. However I would advise against it.
Some terminals don't display emoji with the correct width, which means Rich can't always align them accurately inside a panel.

Logging supports configurable back-ends, which means that a log message could go somewhere other than the terminal -- which may not correctly render the formatting and style produced by Rich.
<a name="rich-is-automatically-installing-traceback-handler"></a>
## Rich is automatically installing traceback handler.

If you are only logging with a file-handler to stdout, then you probably don't need to use the logging module at all. Consider using [Console.log](https://rich.readthedocs.io/en/latest/reference/console.html#rich.console.Console.log) which will render anything that you can print with Rich, with a timestamp.
Rich will never install the traceback handler automatically.

If you are getting Rich tracebacks and you don't want them, then some other piece of software is calling `rich.traceback.install()`.

<a name="strange-colors-in-console-output."></a>
<a name="strange-colors-in-console-output"></a>
## Strange colors in console output.

Rich will highlight certain patterns in your output such as numbers, strings, and other objects like IP addresses.

Occasionally this may also highlight parts of your output you didn't intend. See the [docs on highlighting](https://rich.readthedocs.io/en/latest/highlighting.html) for how to disable highlighting.

<a name="why-does-content-in-square-brackets-disappear"></a>
## Why does content in square brackets disappear?

Rich will treat text within square brackets as *markup tags*, for instance `"[bold]This is bold[/bold]"`.

If you are printing strings with literally square brackets you can either disable markup, or escape your strings.
See the docs on [console markup](https://rich.readthedocs.io/en/latest/markup.html) for how to do this.

<a name="why-does-emoji-break-alignment-in-a-table-or-panel"></a>
## Why does emoji break alignment in a Table or Panel?

Certain emoji take up double space within the terminal. Unfortunately, terminals don't always agree how wide a given character should be.

Rich has no way of knowing how wide a character will be on any given terminal. This can break alignment in containers like Table and Panel, where Rich needs to know the width of the content.

There are also *multiple codepoints* characters, such as country flags, and emoji modifiers, which produce wildly different results across terminal emulators.

Fortunately, most characters will work just fine. But you may have to avoid using the emojis that break alignment. You will get good results if you stick to emoji released on or before version 9 of the Unicode database,

<hr>

Generated by [FAQtory](https://github.com/willmcgugan/faqtory)
11 changes: 11 additions & 0 deletions questions/ansi_escapes.question.md
@@ -0,0 +1,11 @@
---
title: "Natively inserted ANSI escape sequence characters break alignment of Panel"
alt_titles:
- "Escape codes break alignment."
---

If you print ansi escape sequences for color and style you may find the output breaks your output.
You may find that border characters in Panel and Table are in the wrong place, for example.

As a general rule, you should allow Rich to generate all ansi escape sequences, so it can correctly account for these invisible characters.
If you can't avoid a string with escape codes, you can convert it to an equivalent `Text` instance with `Text.from_ansi`.
File renamed without changes.
2 changes: 1 addition & 1 deletion questions/rich_spinner.question.md
@@ -1,5 +1,5 @@
---
title: "python -m rich.spinner shows extra lines"
title: "python -m rich.spinner shows extra lines."
---

The spinner example is know to break on some terminals (Windows in particular).
Expand Down
9 changes: 9 additions & 0 deletions questions/tracebacks_installed.question.md
@@ -0,0 +1,9 @@
---
title: "Rich is automatically installing traceback handler."
alt_titles:
- "Can you stop overriding traceback message formatting by default?"
---

Rich will never install the traceback handler automatically.

If you are getting Rich tracebacks and you don't want them, then some other piece of software is calling `rich.traceback.install()`.
48 changes: 26 additions & 22 deletions rich/pretty.py
Expand Up @@ -130,17 +130,19 @@ def _ipy_display_hook(
if _safe_isinstance(value, ConsoleRenderable):
console.line()
console.print(
value
if _safe_isinstance(value, RichRenderable)
else Pretty(
value,
overflow=overflow,
indent_guides=indent_guides,
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
expand_all=expand_all,
margin=12,
(
value
if _safe_isinstance(value, RichRenderable)
else Pretty(
value,
overflow=overflow,
indent_guides=indent_guides,
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
expand_all=expand_all,
margin=12,
)
),
crop=crop,
new_line_start=True,
Expand Down Expand Up @@ -196,16 +198,18 @@ def display_hook(value: Any) -> None:
assert console is not None
builtins._ = None # type: ignore[attr-defined]
console.print(
value
if _safe_isinstance(value, RichRenderable)
else Pretty(
value,
overflow=overflow,
indent_guides=indent_guides,
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
expand_all=expand_all,
(
value
if _safe_isinstance(value, RichRenderable)
else Pretty(
value,
overflow=overflow,
indent_guides=indent_guides,
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
expand_all=expand_all,
)
),
crop=crop,
)
Expand Down Expand Up @@ -846,7 +850,7 @@ def iter_attrs() -> (
pop_visited(obj_id)
else:
node = Node(value_repr=to_repr(obj), last=root)
node.is_tuple = _safe_isinstance(obj, tuple)
node.is_tuple = type(obj) == tuple
node.is_namedtuple = _is_namedtuple(obj)
return node

Expand Down

0 comments on commit aebc115

Please sign in to comment.