For the simplest cases, you can just import print from rich and use it instead of the standard print:
importtyperfromrichimportprintdata={"name":"Rick","age":42,"items":[{"name":"Portal Gun"},{"name":"Plumbus"}],"active":True,"affiliation":None,}defmain():print("Here's the data")print(data)if__name__=="__main__":typer.run(main)
Just with that, Rich will be able to print your data with nice colors and structure:
fast →python main.py Here's the data { 'name': 'Rick', 'age': 42, 'items': [ {'name': 'Portal Gun'}, {'name': 'Plumbus'} ], 'active': True, 'affiliation': None }
If you are wondering what tool should be used for what, Typer is useful for structuring the command line application, with options, arguments, subcommands, data validation, etc.
In general, Typer tends to be the entry point to your program, taking the first input from the user.
Rich is useful for the parts that need to display information. Showing beautiful content on the screen.
The best results for your command line application would be achieved combining both Typer and Rich.
The way printing works underneath is that the operating system (Linux, Windows, macOS) treats what we print as if our CLI program was writing text to a "virtual file" called "standard output".
When our code "prints" things it is actually "writing" to this "virtual file" of "standard output".
This might seem strange, but that's how the CLI program and the operating system interact with each other.
And then the operating system shows on the screen whatever our CLI program "wrote" to that "virtual file" called "standard output".
You can print to "standard error" creating a Rich Console with stderr=True.
Tip
stderr is short for "standard error".
Using stderr=True tells Rich that the output should be shown in "standard error".
importtyperfromrich.consoleimportConsoleerr_console=Console(stderr=True)defmain():err_console.print("Here is something written to standard error")if__name__=="__main__":typer.run(main)
When you try it in the terminal, it will probably just look the same:
fast →python main.py Here is something written to standard error
As a final detail, when you type text in your keyboard to your terminal, the operating system also considers it another "virtual file" that you are writing text to.
typer.echo() (which is actually just click.echo()) applies some checks to try and convert binary data to strings, and other similar things.
But in most of the cases you wouldn't need it, as in modern Python strings (str) already support and use Unicode, and you would rarely deal with pure bytes that you want to print on the screen.
If you have some bytes objects, you would probably want to decode them intentionally and directly before trying to print them.
And if you want to print data with colors and other features, you are much better off with the more advanced tools in Rich.
Info
typer.echo() comes directly from Click, you can read more about it in Click's docs.
The way color works in terminals is by using some codes (ANSI escape sequences) as part of the text.
So, a colored text is still just a str.
Tip
Again, you are much better off using Rich for this. 😎
You can create colored strings to output to the terminal with typer.style(), that gives you strs that you can then pass to typer.echo():
importtyperdefmain(good:bool=True):message_start="everything is "ifgood:ending=typer.style("good",fg=typer.colors.GREEN,bold=True)else:ending=typer.style("bad",fg=typer.colors.WHITE,bg=typer.colors.RED)message=message_start+endingtyper.echo(message)if__name__=="__main__":typer.run(main)
Tip
The parameters fg and bg receive strings with the color names for the "**f**ore**g**round" and "**b**ack**g**round" colors. You could simply pass fg="green" and bg="red".
But Typer provides them all as variables like typer.colors.GREEN just so you can use autocompletion while selecting them.
You can pass these function arguments to typer.style():
fg: the foreground color.
bg: the background color.
bold: enable or disable bold mode.
dim: enable or disable dim mode. This is badly supported.
underline: enable or disable underline.
blink: enable or disable blinking.
reverse: enable or disable inverse rendering (foreground becomes background and the other way round).
reset: by default a reset-all code is added at the end of the string which means that styles do not carry over. This can be disabled to compose styles.