-
-
Notifications
You must be signed in to change notification settings - Fork 672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[QUESTION] using python-rich with Typer #196
Comments
Hi, I was just passing by this thread and I hadn't heard of Rich before seeing this question so thank you for the super dope introduction to that library. Anyway, the Typer For example, CliRunner catches |
Hi, the eg. above say: from rich.console import Console
console = Console()
@app.command()
def version() -> None:
"""Show project Version."""
# typer.secho(f"project Version: {__version__}", fg=typer.colors.BRIGHT_GREEN)
console.print(f"project Version: {__version__}", style="bold green") this doesn't working as we know, so to make it work we need to move from rich.console import Console
@app.command()
def version() -> None:
"""Show project Version."""
console = Console()
# typer.secho(f"project Version: {__version__}", fg=typer.colors.BRIGHT_GREEN)
console.print(f"project Version: {__version__}", style="bold green") so when i run the test it's work fine but am not sure this is the best solution. |
Hi, Author of Rich here. I think what is happening is that Click is wrapping sys.stdout to catch what you print. A Console constructed at the module level will store sys.stdout prior to it being wrapped by Click. There may be a hook in Click / Typer where you could construct a singleton Console, but I don't know enough about the API to comment. In the meantime @Mohamed-Kaizen 's solution is a reasonable one. |
The solution works for getting pytest to run with simpler Typer/Click apps but it may not work for more complex apps, for example where a Typer/Click context object may have to get passed around, or any other scenario where it might be unreasonable to create a new console object for every command (like an interactive cli tool). Ultimately the code itself works either way you write it. It's just the tests that appear to be breaking, so maybe that'll be up to the developer to figure out what works best for them. |
I made some changes to Rich recently that may impact this. Rather than store stdout when the Console is constructed, Rich will now refer to |
that actually work 👍 , Thanks :) |
Did anyone manage to better integration rich with click/typer? Mainly I tried to add some markup to descriptions of click commands hoping that it would make help me make the --help more colors but they are rendered as plain text. |
@ssbarnea If you're looking to add color to your commands, use https://github.com/click-contrib/click-help-colors. It works really well. |
@daddycocoaman I was able to to make use of click-help-color with typer. In case someone needs a hint on how to do it, check https://github.com/pycontribs/mk/pull/25/files |
Thanks for the discussion here everyone! ☕ And thanks for coming back to close the issue @Mohamed-Kaizen 🍰 For anyone coming afterwards to this, Typer now directly integrates with Rich for the help output, Rich is included and used when you install with And Rich is also the recommended tool to display data, everywhere in the Typer docs. 🤓 🎨
|
I tried import logging
import typer
from typer.testing import CliRunner
from rich.logging import RichHandler
app = typer.Typer()
runner = CliRunner()
logging.basicConfig(
level="NOTSET",
format="%(message)s",
datefmt="%X",
handlers=[RichHandler()],
)
logger = logging.getLogger(__name__)
@app.command()
def logging_command() -> None:
"""Show project Version."""
typer.echo("Typer Message")
logger.info("Rich logging message")
def test_logging_rich() -> None:
"""It exits with a status code of zero."""
result = runner.invoke(app)
assert result.exit_code == 0
assert "Typer Message" in result.stdout # True
assert "Rich logging message" in result.stdout # AssertionError Maybe I set something up wrong? UPD. It looks like I need to set the level on the logger itself. logger = logging.getLogger(__name__)
handler = RichHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG) Now it works 🎉 |
First check
Description
How can I use python-rich with Typer ?
Eg.
output:
project Version: 0.0.1
the problem that am facing is in test mode
output:
FAILED tests/test_manage.py::test_version_succeeds - AssertionError: assert 'project' in ''
So is there a way to make them work together ?
The text was updated successfully, but these errors were encountered: