Change the title color in python rich panel

175 views Asked by At

Is it possible to change the title color in python rich panel? I have tried several ways, and I have not succeeded.

This is my code:

from rich.console import Console
from rich.markdown import Markdown
from rich.panel import Panel
import configuracion
from rich import print

def imprime_comandos (comandos: list, consola: Console, ancho_panel: int) -> None:
    formato_comandos = '\n\n'.join([f""" `{c}`""" for c in comandos])

    salida_cmd = Markdown(
        formato_comandos,
        inline_code_lexer = 'bash',
    )

    consola.print (
        Panel(
            salida_cmd,
            title = "[red]Comando/s[/red]",
            title_align = 'left',
            width = ancho_panel,
        )
    )
def imprime_extras (extras: list, consola: Console, ancho_panel: int) -> None:
    formato_extra = '\n'.join([f"""* {ex}""" for ex in extras])

    salida_extras = Markdown(formato_extra)
    salida_extras_str = str(salida_extras)
    salida_extras_str = re.sub(r"|", "", salida_extras_str)

    consola.print (
        Panel(
            salida_extras,
            title = '[red]Ten en cuenta...[/red]',
            title_align = 'left',
            width = ancho_panel,
        )
    )

I was hoping to print the title in red color

1

There are 1 answers

0
August1328 On

In your function definition, the assignment of consola: Console is not correct.

I made an excerpt from your code, to show how this works. Either:

from rich.panel import Panel
from rich import print

print(Panel(renderable="test",title ="[red]Comando[/red]", title_align="left",width=30))

Or:

from rich.panel import Panel
from rich.console import Console

console = Console()
console.print(Panel(renderable="test",title ="[red]Comando[/red]",title_align="left",width=30))

In the second example, there will be an AttributeError if you just code console = Console without the paranthesis.