Make Your Python CLI Tools Pop with Rich
hackaday.com
hackaday.com
Every one of my scripts now has quick and easy progress bars, console management (including saving a copy of console output), tables with columns, rows, and headers, "Processing..." blocks, colours, error logging, emoji, and on and on and on. It's really an amazing project that does a ton of stuff and makes it all easy to do.
If nothing else, add "from rich import print" to the top of your script (assuming you use print() statements) and see how your output looks afterwards, with highlighted numbers/floats, IP addresses, etc., formatted and syntax-highlighted JSON and repr output, and so on.
[0] https://talkpython.fm/episodes/show/336/terminal-magic-with-... [1] https://github.com/Textualize/textual
Edit: Tried to sponsor your linked Ko-fi from what's listed in the rich repo, but the link provided doesn't work and searching your name doesn't pull anything up. Sent you an email to find a better way.
Here's where it checks: https://github.com/Textualize/rich/blob/5a457bfbc662ab8701dc... (and I just tested to confirm it does indeed work)
https://medium.com/hackernoon/im-harvesting-credit-card-numb...
I've been finding myself writing more and more CLI programs in Go (used to use python, but it's a bit harder to distribute python bundles around...), and so far I've just been starting with Cobra and going from there.
I always found the dictionary entry amusing:
https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=...
Right now I have an interactive python script that wraps a bunch of config files and has a few pip dependencies. currently i batch it in a docker container which is pretty heavyweight, and realistically needs its own wrapper script for users.
If there was an unfussy way to wrap that as a single artifact I'd look into making the whole thing python.
Nuitka is another option I haven’t tried.
I'll check out pyinstaller and nuitka.
Been seeing a lot of people who think Go is just better because you can build a single binary. Go is a pretty decent language but its weird that one feature seems to trump everything to far too many people I know.
There were a ton of hoops to jump through on both sides and then someone asked why I didn't just write it in go.
Well, this was consolidating some other functionality on both sides and I was extending/migrating the apps as we moved from colo to cloud, and it just... didn't occur to me, and we don't really have anything golang based, so no expertise or infrastructure and the migrations were happening regardless.
In hindsight this might have been better done that way, but I don't know that I've got the bandwidth to rewrite it all.
Distributing a shell runscript to run a docker image works because everyone in my space uses docker, and most of the teams are single language focused and most aren't python.
Its still an issue to keep everyone's run script updated, but maybe not enough to introduce a new language and toolchain.
>This requires a one-time installation of a driver for this file system (SquashFS).
Is this a trend? How do people manage packages like Rich for use in their one-off scripts? It's been years since I've done Python, but IIRC installing packages globally was pretty brittle.
The current trick is to use pipx[0]. Pipx will create an individual virtual environment for every executable tool you have, so there is no chance of conflicts.
You do `pipx install httpie` or `pipx install black` and then both utilities will be on your PATH, installed with whatever dependencies they require. I have never run into any issues with it.
Ninja edit: it also makes upgrades trivial: `pipx upgrade-all`
I see the rise of Docker as indication that static linking won.
There are two sides of the coin :)
Do I need to manually choose what to inspect in case of exception?
If it can be done by simply turn on a switch and all the exception will automatically colorful, then I can see it being somewhat useful; but for my toy script I would just run it in debugger which is even better for debugging.
If all fails use pdb.trace()
---
[1]: https://docs.python.org/3/whatsnew/changelog.html#id325
edit: but what seems like interesting concept is to overwrite sys.breakpoint_hook to start a custom debug server allowing for remote debugger attachment.
I use this in a lot of my "quick apps" for status monitoring
I'm currently using my own implementation for style/colors and tables in my main CLI tool, including highlighting with Pygments, and I'm wondering if it would make sense to move to Rich (it would a least bring Windows and Mac support).
Regarding progress bars, Python Prompt Toolkit already implements them, thus I have the feeling that Rich is quite redundant. Any feedback from experienced user?
https://new.pythonforengineers.com/blog/cool-python-librarie...