Skip to content

Commit aebc115

Browse files
committed
faqs
1 parent c034c68 commit aebc115

File tree

6 files changed

+99
-49
lines changed

6 files changed

+99
-49
lines changed

FAQ.md

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,81 @@
11

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

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

12-
Certain emoji take up double space within the terminal. Unfortunately, terminals don't always agree how wide a given character should be.
15+
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.
1316

14-
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.
17+
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.
1518

16-
There are also *multiple codepoints* characters, such as country flags, and emoji modifiers, which produce wildly different results across terminal emulators.
19+
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.
1720

18-
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,
21+
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.
1922

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

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

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

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

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

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

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

38-
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.
42+
The spinner example is know to break on some terminals (Windows in particular).
3943

40-
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.
44+
Some terminals don't display emoji with the correct width, which means Rich can't always align them accurately inside a panel.
4145

42-
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.
46+
<a name="rich-is-automatically-installing-traceback-handler"></a>
47+
## Rich is automatically installing traceback handler.
4348

44-
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.
49+
Rich will never install the traceback handler automatically.
50+
51+
If you are getting Rich tracebacks and you don't want them, then some other piece of software is calling `rich.traceback.install()`.
4552

46-
<a name="strange-colors-in-console-output."></a>
53+
<a name="strange-colors-in-console-output"></a>
4754
## Strange colors in console output.
4855

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

5158
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.
5259

60+
<a name="why-does-content-in-square-brackets-disappear"></a>
61+
## Why does content in square brackets disappear?
62+
63+
Rich will treat text within square brackets as *markup tags*, for instance `"[bold]This is bold[/bold]"`.
64+
65+
If you are printing strings with literally square brackets you can either disable markup, or escape your strings.
66+
See the docs on [console markup](https://rich.readthedocs.io/en/latest/markup.html) for how to do this.
67+
68+
<a name="why-does-emoji-break-alignment-in-a-table-or-panel"></a>
69+
## Why does emoji break alignment in a Table or Panel?
70+
71+
Certain emoji take up double space within the terminal. Unfortunately, terminals don't always agree how wide a given character should be.
72+
73+
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.
74+
75+
There are also *multiple codepoints* characters, such as country flags, and emoji modifiers, which produce wildly different results across terminal emulators.
76+
77+
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,
78+
5379
<hr>
5480

5581
Generated by [FAQtory](https://github.com/willmcgugan/faqtory)

questions/ansi_escapes.question.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: "Natively inserted ANSI escape sequence characters break alignment of Panel"
3+
alt_titles:
4+
- "Escape codes break alignment."
5+
---
6+
7+
If you print ansi escape sequences for color and style you may find the output breaks your output.
8+
You may find that border characters in Panel and Table are in the wrong place, for example.
9+
10+
As a general rule, you should allow Rich to generate all ansi escape sequences, so it can correctly account for these invisible characters.
11+
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.

questions/rich_spinner.question.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "python -m rich.spinner shows extra lines"
2+
title: "python -m rich.spinner shows extra lines."
33
---
44

55
The spinner example is know to break on some terminals (Windows in particular).
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: "Rich is automatically installing traceback handler."
3+
alt_titles:
4+
- "Can you stop overriding traceback message formatting by default?"
5+
---
6+
7+
Rich will never install the traceback handler automatically.
8+
9+
If you are getting Rich tracebacks and you don't want them, then some other piece of software is calling `rich.traceback.install()`.

rich/pretty.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,19 @@ def _ipy_display_hook(
130130
if _safe_isinstance(value, ConsoleRenderable):
131131
console.line()
132132
console.print(
133-
value
134-
if _safe_isinstance(value, RichRenderable)
135-
else Pretty(
136-
value,
137-
overflow=overflow,
138-
indent_guides=indent_guides,
139-
max_length=max_length,
140-
max_string=max_string,
141-
max_depth=max_depth,
142-
expand_all=expand_all,
143-
margin=12,
133+
(
134+
value
135+
if _safe_isinstance(value, RichRenderable)
136+
else Pretty(
137+
value,
138+
overflow=overflow,
139+
indent_guides=indent_guides,
140+
max_length=max_length,
141+
max_string=max_string,
142+
max_depth=max_depth,
143+
expand_all=expand_all,
144+
margin=12,
145+
)
144146
),
145147
crop=crop,
146148
new_line_start=True,
@@ -196,16 +198,18 @@ def display_hook(value: Any) -> None:
196198
assert console is not None
197199
builtins._ = None # type: ignore[attr-defined]
198200
console.print(
199-
value
200-
if _safe_isinstance(value, RichRenderable)
201-
else Pretty(
202-
value,
203-
overflow=overflow,
204-
indent_guides=indent_guides,
205-
max_length=max_length,
206-
max_string=max_string,
207-
max_depth=max_depth,
208-
expand_all=expand_all,
201+
(
202+
value
203+
if _safe_isinstance(value, RichRenderable)
204+
else Pretty(
205+
value,
206+
overflow=overflow,
207+
indent_guides=indent_guides,
208+
max_length=max_length,
209+
max_string=max_string,
210+
max_depth=max_depth,
211+
expand_all=expand_all,
212+
)
209213
),
210214
crop=crop,
211215
)
@@ -846,7 +850,7 @@ def iter_attrs() -> (
846850
pop_visited(obj_id)
847851
else:
848852
node = Node(value_repr=to_repr(obj), last=root)
849-
node.is_tuple = _safe_isinstance(obj, tuple)
853+
node.is_tuple = type(obj) == tuple
850854
node.is_namedtuple = _is_namedtuple(obj)
851855
return node
852856

0 commit comments

Comments
 (0)