6 ms·
Make Your Python CLI Tools Pop with Rich
- chaps 5y agoThat `locals` block is really freaking neat.
- danudey 5y agoRich 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.
- bikingbismuth 5y agoThanks 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.
- jszymborski 5y agoI so appreciate the little things, like `console.log` printing not only discretely naming the filename and line, but also providing a link to it.
- danudey 5y agoOr the logging output only outputting timestamps when they're different so you can clearly see what happens and when.
- TylerE 5y agoQuestion: How horribly does the world break if you pipe the output to a file instead of stdout?
- eyegor 5y agoI 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.
- deleted 5y ago[deleted]
- windexh8er 5y agoRich 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-rich-and-textual https://talkpython.fm/episodes/show/336/terminal-magic-with-... [1] https://github.com/Textualize/textual https://github.com/Textualize/textual
- willm 5y ago*Will McGugan :)
- windexh8er 5y agoOh 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.
- varenc 5y ago
- willm 5y agoGlad you find it useful!
- macintux 5y agoRich was discussed 5 days ago: https://news.ycombinator.com/item?id=29945848 https://news.ycombinator.com/item?id=29945848
- airstrike 5y agoFor 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 https://www.youtube.com/watch?v=J-dzzD6NQJ4
- kelvie 5y agoCurious 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.
- jaxxstorm 5y agohttps://github.com/charmbracelet/bubbletea https://github.com/charmbracelet/bubbletea https://github.com/charmbracelet/lipgloss https://github.com/charmbracelet/lipgloss
- kelvie 5y agoOh awesome. And it's called bubble tea and not boba, too (for some reason calling it boba bothers me).
- theshrike79 5y agoSaying "Boba" brings the expectation that it should be followed with "Fett" :)
- thaumasiotes 5y ago> (for some reason calling it boba bothers me) I always found the dictionary entry amusing: https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=0&wdqb=%E6%B3%A2%E9%9C%B8 https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=...
- artificial 5y agoThese are nifty! Thanks for sharing!
- n8ta 5y agoAnyone 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-numbers-and-passwords-from-your-site-here-s-how-9a8cb347c5b5 https://medium.com/hackernoon/im-harvesting-credit-card-numb...
- thrdbndndn 5y agoWhat'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.
- VTimofeenko 5y agoI 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.
- 0x008 5y agoWhich environment is that? So far I have been able to use a debugger everywhere. If all fails use pdb.trace()
- VTimofeenko 5y agoI had no way to punch holes through a bunch of nested ssh sessions on a tightened down production environment
- usrme 5y agoIf 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 https://docs.python.org/3/whatsnew/changelog.html#id325
- 0x008 5y agoWhat 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.
- cglong 5y ago> 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.
- 41b696ef1113 5y ago>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/ https://pypa.github.io/pipx/
- MereInterest 5y agoThis 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.
- 41b696ef1113 5y agoWhile 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.
- 0x008 5y agoBut 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 :)
- slapslash 5y agoDammit, I'm programming in python since a while and only just realized, that there are coroutines (asyc/await)!
- scrame 5y agoSo 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.
- nsomaru 5y agoI’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.
- scrame 5y agoThanks 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.
- VectorLock 5y agoI'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.
- scrame 5y agoThanks 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.
- waf 5y agoFor C#, there's https://github.com/spectreconsole/spectre.console https://github.com/spectreconsole/spectre.console I use this in a lot of my "quick apps" for status monitoring
- jlnazario 5y agoRich is the shit. Go show https://github.com/willmcgugan https://github.com/willmcgugan some love.
- deleted 5y ago[deleted]
- goffi 5y agoHow 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?
- shantnutiwari 5y agorich an Textual look awesome, they are on my list of cool Python libraries to try in 2022 https://new.pythonforengineers.com/blog/cool-python-libraries-i-want-to-try-in-2022/ https://new.pythonforengineers.com/blog/cool-python-librarie...
- hackerboyap 5y ago
- hackerboyap 5y ago