News for hackers. Stuff that matters.
Re:Make Your Python CLI Tools Pop with Rich by macintux on Thursday January 20, 2022 @08:22PM (#30017950)
Rich was discussed 5 days ago: https://news.ycombinator.com/item?id=29945848
Re:Make Your Python CLI Tools Pop with Rich by danudey on Thursday January 20, 2022 @07:53PM (#30017725)
Rich is probably the biggest lifestyle change I've ever had because of a single library.

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.

Re:Make Your Python CLI Tools Pop with Rich by TylerE on Thursday January 20, 2022 @09:04PM (#30018301)
Question: How horribly does the world break if you pipe the output to a file instead of stdout?
Re:Make Your Python CLI Tools Pop with Rich by varenc on Thursday January 20, 2022 @10:31PM (#30018801)
Not at all! Like many CLI tools, it turns off the formatting when it detects you're not outputting to a terminal.

Here's where it checks: https://github.com/Textualize/rich/blob/5a457bfbc662ab8701dc... (and I just tested to confirm it does indeed work)

Re:Make Your Python CLI Tools Pop with Rich by windexh8er on Thursday January 20, 2022 @09:32PM (#30018483)
Rich McGugan (author of Rich) talks about a lot of different design aspects of Rich on an episode of Talk Python [0] last October. I've used it in a few projects since then and for what I've been using it for the output is still easily consumable. Rich is a game changer for output in Python and it pairs nicely with CLI libs like Click. McGugan also is putting a lot of effort into Textual [1] which is fantastic for TUI apps.

[0] https://talkpython.fm/episodes/show/336/terminal-magic-with-... [1] https://github.com/Textualize/textual

Re:Make Your Python CLI Tools Pop with Rich by willm on Friday January 21, 2022 @02:41AM (#30020395)
*Will McGugan :)
Re:Make Your Python CLI Tools Pop with Rich by windexh8er on Friday January 21, 2022 @10:34AM (#30024920)
Oh man, fail on my part. Apologies Will! I think now's a great time for me to donate. ¯\_(ツ)_/¯

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.

Re:Make Your Python CLI Tools Pop with Rich by eyegor on Thursday January 20, 2022 @09:09PM (#30018335)
I would assume the lib authors accounted for this, it's pretty easy to check with sys.stdout.isatty(). If not you could alias it yourself.
Re:Make Your Python CLI Tools Pop with Rich by willm on Friday January 21, 2022 @02:40AM (#30020387)
Glad you find it useful!
Re:Make Your Python CLI Tools Pop with Rich by bikingbismuth on Thursday January 20, 2022 @08:44PM (#30018135)
Thanks for the suggestion. I occasionally add progress % to some of my scripts, but anything more advanced I can’t be bothered. Excited to check out the library, thanks for the print() suggestion.
Re:Make Your Python CLI Tools Pop with Rich by jszymborski on Thursday January 20, 2022 @08:44PM (#30018137)
I so appreciate the little things, like `console.log` printing not only discretely naming the filename and line, but also providing a link to it.
Re:Make Your Python CLI Tools Pop with Rich by danudey on Friday January 21, 2022 @01:12PM (#30027532)
Or the logging output only outputting timestamps when they're different so you can clearly see what happens and when.
Re:Make Your Python CLI Tools Pop with Rich by n8ta on Thursday January 20, 2022 @10:27PM (#30018777)
Anyone else immediately thinking of that post about harvesting credit card numbers with js packages? That used some fun color package as an example for an entrypoint into your codebase.

https://medium.com/hackernoon/im-harvesting-credit-card-numb...

Re:Make Your Python CLI Tools Pop with Rich by jlnazario on Friday January 21, 2022 @03:56AM (#30020925)
Rich is the shit. Go show https://github.com/willmcgugan some love.
Re:Make Your Python CLI Tools Pop with Rich by kelvie on Thursday January 20, 2022 @10:10PM (#30018700)
Curious to know if anything exists like this for Go?

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.

Re:Make Your Python CLI Tools Pop with Rich by jaxxstorm on Thursday January 20, 2022 @10:24PM (#30018764)
Re:Make Your Python CLI Tools Pop with Rich by kelvie on Thursday January 20, 2022 @10:26PM (#30018774)
Oh awesome. And it's called bubble tea and not boba, too (for some reason calling it boba bothers me).
Re:Make Your Python CLI Tools Pop with Rich by thaumasiotes on Friday January 21, 2022 @01:21PM (#30027712)
> (for some reason calling it boba bothers me)

I always found the dictionary entry amusing:

https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=...

Re:Make Your Python CLI Tools Pop with Rich by theshrike79 on Friday January 21, 2022 @05:51AM (#30021828)
Saying "Boba" brings the expectation that it should be followed with "Fett" :)
Re:Make Your Python CLI Tools Pop with Rich by artificial on Friday January 21, 2022 @02:30AM (#30020318)
These are nifty! Thanks for sharing!
Re:Make Your Python CLI Tools Pop with Rich by scrame on Friday January 21, 2022 @01:23AM (#30019881)
So what's the best way to pack up a python cli app in a single artifact?

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.

Re:Make Your Python CLI Tools Pop with Rich by nsomaru on Friday January 21, 2022 @01:26AM (#30019899)
I’ve used pyinstaller with some success to do what you’ve described. It does end up bundling an entire Python runtime but it worked for me.

Nuitka is another option I haven’t tried.

Re:Make Your Python CLI Tools Pop with Rich by scrame on Friday January 21, 2022 @01:47AM (#30020047)
Thanks for the response. I understand pythons popularity especially with the "batteries included" philosophy, but when it runs out of batteries the headaches can multiply quickly.

I'll check out pyinstaller and nuitka.

Re:Make Your Python CLI Tools Pop with Rich by VectorLock on Friday January 21, 2022 @01:32AM (#30019949)
I've seen .EXE builders for Python that produce binaries for Windows, it'd be cool to have something similar in the *nix world.

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.

Re:Make Your Python CLI Tools Pop with Rich by scrame on Friday January 21, 2022 @01:45AM (#30020030)
Thanks for the response. I was thinking something similar. This particular cli is a client to an app i wrote in kotlin/java that tracks particular kubernetes pods.

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.

Re:Make Your Python CLI Tools Pop with Rich by GOATS- on Friday January 21, 2022 @08:51AM (#30023428)
You can produce binaries for macOS and Linux with PyInstaller.
Re:Make Your Python CLI Tools Pop with Rich by cricalix on Friday January 21, 2022 @03:45AM (#30020854)
There’s https://github.com/facebookincubator/xar - but cross platform could be a pain. Basically static link “binary” for Python (or other).
Re:Make Your Python CLI Tools Pop with Rich by VectorLock on Friday January 21, 2022 @09:48AM (#30024185)
Looks cool despite being a facebook thing but hit this mammoth caveat:

>This requires a one-time installation of a driver for this file system (SquashFS).

Re:Make Your Python CLI Tools Pop with Rich by cricalix on Friday January 21, 2022 @03:06PM (#30029249)
Yeah. Depends on your use case I suppose. If you’re after cross platform or non-Linux, then it’s not an answer.
Re:Make Your Python CLI Tools Pop with Rich by cglong on Thursday January 20, 2022 @10:41PM (#30018867)
> It seems as though more and more of the simple command-line tools and small scripts that used to be bash or small c programs are slowly turning into python programs.

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.

Re:Make Your Python CLI Tools Pop with Rich by 41b696ef1113 on Friday January 21, 2022 @12:19AM (#30019476)
>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`

[0] https://pypa.github.io/pipx/

Re:Make Your Python CLI Tools Pop with Rich by MereInterest on Friday January 21, 2022 @12:31AM (#30019545)
This might be an old man yelling at clouds, but I feel like virtual environments should be used for development and testing, but not for deployment. I'm development and testing, virtual environments let you test the package against many different versions of the dependencies. Using it for deployment, though, means that after a security hotfix, the admin needs to hunt down each utility's copy of a dependency, rather than just updating the one shared copy of the dependencies.
Re:Make Your Python CLI Tools Pop with Rich by 41b696ef1113 on Friday January 21, 2022 @12:46AM (#30019653)
While everything about Python packaging is still a mess (but we now have a walrus operator, win?), I view virtual environment distribution as nothing more than a clunky static vs dynamic linking situation. Trust the system or bring your own.

I see the rise of Docker as indication that static linking won.

Re:Make Your Python CLI Tools Pop with Rich by 0x008 on Friday January 21, 2022 @01:32AM (#30019946)
But dependencies should be pinned for every application anyway to not break compatibility when sysadmin goes and updates all dependencies.

There are two sides of the coin :)

Re:Make Your Python CLI Tools Pop with Rich by renewiltord on Friday January 21, 2022 @01:39AM (#30019997)
For a language like Python, the pipx venv is similar to a Go static link. Same model of problem.
Re:Make Your Python CLI Tools Pop with Rich by thrdbndndn on Thursday January 20, 2022 @10:30PM (#30018793)
What's the use case for `inspect`?

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.

Re:Make Your Python CLI Tools Pop with Rich by VTimofeenko on Friday January 21, 2022 @01:05AM (#30019782)
I have been using it in an environment where I could not hook in IDE debugger but could sprinkle in breakpoint()s and rich.inspect away to my heart's content.
Re:Make Your Python CLI Tools Pop with Rich by 0x008 on Friday January 21, 2022 @01:33AM (#30019954)
Which environment is that? So far I have been able to use a debugger everywhere.

If all fails use pdb.trace()

Re:Make Your Python CLI Tools Pop with Rich by VTimofeenko on Friday January 21, 2022 @02:59AM (#30020519)
I had no way to punch holes through a bunch of nested ssh sessions on a tightened down production environment
Re:Make Your Python CLI Tools Pop with Rich by usrme on Friday January 21, 2022 @03:06AM (#30020576)
If you're fortunate enough to use Python 3.7 or newer, then you can forego the importing of 'pdb' and using 'pdb.set_trace()' (I think you had a small omission there) in favor of just writing 'breakpoint()'[1] directly.

---

[1]: https://docs.python.org/3/whatsnew/changelog.html#id325

Re:Make Your Python CLI Tools Pop with Rich by 0x008 on Sunday January 23, 2022 @11:49AM (#30047668)
What is the difference? Set trace will basically hold the code at that line (breakpoint) and drop you into the interactive debugger session. It seems like syntactic sugar.

edit: but what seems like interesting concept is to overwrite sys.breakpoint_hook to start a custom debug server allowing for remote debugger attachment.

Re:Make Your Python CLI Tools Pop with Rich by airstrike on Thursday January 20, 2022 @08:49PM (#30018179)
For an amazing example of how far one can take this tool, take a look at Textual, by the same guy behind Rich: https://www.youtube.com/watch?v=J-dzzD6NQJ4
Re:Make Your Python CLI Tools Pop with Rich by waf on Friday January 21, 2022 @02:56AM (#30020500)
For C#, there's https://github.com/spectreconsole/spectre.console

I use this in a lot of my "quick apps" for status monitoring

Re:Make Your Python CLI Tools Pop with Rich by goffi on Friday January 21, 2022 @04:06AM (#30021005)
How does Textual compares to Urwid? And Rich to Blessed?

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?

Re:Make Your Python CLI Tools Pop with Rich by slapslash on Friday January 21, 2022 @01:10AM (#30019814)
Dammit, I'm programming in python since a while and only just realized, that there are coroutines (asyc/await)!
Re:Make Your Python CLI Tools Pop with Rich by chaps on Thursday January 20, 2022 @07:33PM (#30017570)
That `locals` block is really freaking neat.
Re:Make Your Python CLI Tools Pop with Rich by shantnutiwari on Friday January 21, 2022 @04:18AM (#30021088)
rich an Textual look awesome, they are on my list of cool Python libraries to try in 2022

https://new.pythonforengineers.com/blog/cool-python-librarie...