typer icon indicating copy to clipboard operation
typer copied to clipboard

New setting to increase the width of the rich (exception) output

Open brot opened this issue 3 years ago • 2 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the Typer documentation, with the integrated search.
  • [X] I already searched in Google "How to X in Typer" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to Typer but to Click.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

import typer

app = typer.Typer()


@app.command()
def main(name: str = "morty"):
    deep_dict_or_json = {
        "this_is_a_long_key": {
            "this_is_the_next_long_key": {
                "this_is_the_next_long_key": {
                    "this_is_the_next_long_key": {
                        "this_is_the_next_long_key": {
                            "this_is_the_next_long_key": {
                                "this_is_the_next_long_key": {
                                    "this_is_the_next_long_key": {
                                        "this_is_the_next_long_key": {
                                            "this_is_the_next_long_key": {
                                                "and_once_again_a_very_long_key": {
                                                    "but_this_is_not_the_end": {"end": True}
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    print(name + 3)


if __name__ == "__main__":
    app()

Description

If I want to include the locals in the nice rich output and such a local includes a very big and deep dictionary (e.g: Kubernetes pod description) the output is very long, although I have a lot of horizontal space in my terminal.

Wanted Solution

So I want to be able to increase the width of the yellow and red rich exception border to make the output more useful and readable

Wanted Code

app = typer.Typer(pretty_exceptions_with=120)

Alternatives

No response

Operating System

macOS

Operating System Details

No response

Typer Version

0.7.0

Python Version

3.11.1

Additional Context

No response

brot avatar Jan 03 '23 12:01 brot

hi, would it be okay if I made a PR for it?

indiVar0508 avatar Jan 07 '23 19:01 indiVar0508

Currently it fits this block in terminal width by default:

python main.py
╭──────────────────────────────────────────────────────────────────────────────────── Traceback (most recent call last) ─────────────────────────────────────────────────────────────────────────────────────╮
│ /home/yurii/code/typer/motov/helloworld/main.py:33 in main                                                                                                                                                 │
│                                                                                                                                                                                                            │
│   30 │   │   │   }                                                                                                                                                                                         │
│   31 │   │   }                                                                                                                                                                                             │
│   32 │   }                                                                                                                                                                                                 │
│ ❱ 33 │   print(name + 3)                                                                                                                                                                                   │
│   34                                                                                                                                                                                                       │
│   35                                                                                                                                                                                                       │
│   36 if __name__ == "__main__":                                                                                                                                                                            │
│                                                                                                                                                                                                            │
│ ╭────────────────────────────────────────────────────────────────────────────────────────────── locals ──────────────────────────────────────────────────────────────────────────────────────────────╮     │
│ │ deep_dict_or_json = {                                                                                                                                                                              │     │
│ │                     │   'this_is_a_long_key': {                                                                                                                                                    │     │
│ │                     │   │   'this_is_the_next_long_key': {                                                                                                                                         │     │
│ │                     │   │   │   'this_is_the_next_long_key': {                                                                                                                                     │     │
│ │                     │   │   │   │   'this_is_the_next_long_key': {                                                                                                                                 │     │
│ │                     │   │   │   │   │   'this_is_the_next_long_key': {                                                                                                                             │     │
│ │                     │   │   │   │   │   │   'this_is_the_next_long_key': {                                                                                                                         │     │
│ │                     │   │   │   │   │   │   │   'this_is_the_next_long_key': {                                                                                                                     │     │
│ │                     │   │   │   │   │   │   │   │   'this_is_the_next_long_key': {                                                                                                                 │     │
│ │                     │   │   │   │   │   │   │   │   │   'this_is_the_next_long_key': {                                                                                                             │     │
│ │                     │   │   │   │   │   │   │   │   │   │   'this_is_the_next_long_key': {'and_once_again_a_very_long_key': {'but_this_is_not_the_end': {'end': True}}}                            │     │
│ │                     │   │   │   │   │   │   │   │   │   }                                                                                                                                          │     │
│ │                     │   │   │   │   │   │   │   │   }                                                                                                                                              │     │
│ │                     │   │   │   │   │   │   │   }                                                                                                                                                  │     │
│ │                     │   │   │   │   │   │   }                                                                                                                                                      │     │
│ │                     │   │   │   │   │   }                                                                                                                                                          │     │
│ │                     │   │   │   │   }                                                                                                                                                              │     │
│ │                     │   │   │   }                                                                                                                                                                  │     │
│ │                     │   │   }                                                                                                                                                                      │     │
│ │                     │   }                                                                                                                                                                          │     │
│ │                     }                                                                                                                                                                              │     │
│ │              name = 'morty'                                                                                                                                                                        │     │
│ ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯     │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
TypeError: can only concatenate str (not "int") to str

So, I think the issue is solved.

If, for some reasons, it fails to determine the width of the terminal screen correctly, you can specify it using TERMINAL_WIDTH ENV variable.

TERMINAL_WIDTH=70 python main.py`

or

export TERMINAL_WIDTH=70
python main.py`

YuriiMotov avatar Sep 19 '25 10:09 YuriiMotov