I'm using the click module.
pip install click
This gives me red text
import click
click.secho('Error: This error is ...xx', fg='red')
Now I want that only 'Error:' is shown in red. How can I do this using click.secho
?
Use click.echo
with click.style
click.echo(click.style("Error", fg="red") + ": This error is...")
You can use the built in secho
command with the nl
(new line) flag.
In your specific use case it would look like
click.secho('Error', fg='red', nl=False) # This will prevent the secho statement from starting a new line
click.echo(': This error is ...xx')
This will give you the output you wanted
From click
documentation for the echo method
In addition to that, if colorama is installed, the echo function will also support clever handling of ANSI codes.
From colorama
documentation
print('\033[31m' + 'some red text')
print('\033[30m') # and reset to default color
Thus, combining, you should have something like the following
click.echo('\033[31m' + 'Error:' + '\033[30m' + ' This error ... ')
to get what you were looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With