Search code examples
pythonansi-colorsrich

How to disable the implicit formatting of python rich?


Consider the code below, the colored formatting of bbb is explicit by markdown tags, while the colored formatting of 123 and ccc are implicit because they were identified as a number and string respectively.

Is there a way to set the console object to disable the all the implicit formatting such that only the explicit [] tags and style= arguments will take affect?

enter image description here


Solution

  • From the help of rich.console.Console:

    highlight (bool, optional): Enable automatic highlighting. Defaults to True.
    

    So just do:

    from rich.console import Console
    console = Console(highlight=False)
    console.print("aaa [red]bbb [/red] 123 'ccc'")