Set different background color using Rich
Asked Answered
U

1

6

I found useful package rich. And want to use it in the next way: drawing in different colors words in sentence and at different background colors. I use next code:

from rich.console import Console
console = Console()
console.print('[green]some[/green] [#F47983]text[/#F47983]')

This way I can set any color for any word:

enter image description here

But I don't know, how to set different background colors. I tried background= , bg: and backgrounp-color - this doesn't work :(

Urbanna answered 16/7, 2020 at 10:17 Comment(0)
I
10

Use the word "on" to introduce the background color. For example:

print("[bold green on blue]Fear is the mind-killer[/]")

Also if you want to use hex codes:

print('[green on #F47983]some[/] [#F47983]text[/]')

See https://rich.readthedocs.io/en/latest/style.html for how to define styles.

Insurgency answered 16/7, 2020 at 22:2 Comment(3)
It works! Thank you! Furthermore, it works next way too: console.print('[green on #F47983]some[/green on #F47983] [#F47983]text[/#F47983]') I added info to your answer :)Urbanna
but how would you change the color of whole console in python.this only changes the background colour of the text we print.Petterson
Only way would be to set your terminal theme. The color codes will only have an effect on text that is subsequently written.Insurgency

© 2022 - 2024 — McMap. All rights reserved.