Latest / Talk Python To Me / #336: Terminal magic with Rich and Textual
Transcript
- 0:00Have you heard of the package rich?
- 0:01This library allows you to create, well, rich terminal-based UIs in Python.
- 0:06When you think of what you can typically build with basic print statements,
- 0:09they may seem quite limited.
- 0:11But with rich, imagine justified tables, progress bars,
- 0:15rendering of markdown, and way more.
- 0:17This is one of the fastest-growing projects in the Python space these days,
- 0:21and the creator, Will McGoogan, is here to give us the whole history
- 0:24and even a peek at the future of rich and a follow-on library called Textual.
- 0:29This is Talk Python To Me, episode 336, recorded October 13th, 2021.
- 0:34Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:50This is your host, Michael Kennedy.
- 0:52Follow me on Twitter, where I'm @mkennedy,
- 0:54and keep up with the show and listen to past episodes at talkpython.fm.
- 0:58And follow the show on Twitter via at Talk Python.
- 1:01We've started streaming most of our episodes live on YouTube.
- 1:05Subscribe to our YouTube channel over at talkpython.fm/youtube
- 1:08to get notified about upcoming shows and be part of that episode.
- 1:12This episode is brought to you by Shortcut, formerly known as clubhouse.io,
- 1:17and us over at Talk Python Training.
- 1:19And the transcripts are brought to you by Assembly AI.
- 1:24Will, welcome to Talk Python To Me.
- 1:25Thank you.
- 1:26It's fantastic to finally have you on the show.
- 1:28I feel like we've talked a lot about the work that you've been doing many times.
- 1:32Not so much on Talk Python because we're more focused on a single topic.
- 1:36But even so, I believe, you know, at the end, I always ask for some project that needs attention,
- 1:41needs some sort of shout out.
- 1:43And, you know, I believe Rich has come up more than once.
- 1:45I think Textual has come up at least once there.
- 1:48So the prior guests of the show have been fans.
- 1:51And I know the audience is a big fan.
- 1:53So, yeah, congrats on all the progress there.
- 1:56Great. Thanks.
- 1:57Yeah, I really appreciate the coverage.
- 1:58You probably increased my star count by a few thousand.
- 2:02Well, I do want to talk about that because this project is super popular.
- 2:06And as we get into it, I think it's going to be fun to explore some of the things that you felt were key to that.
- 2:12And if this is the first time people are hearing about Rich, you definitely want to check out some of the screenshots.
- 2:18Maybe I'll do something fun, like make part of the show, have the show notes or the podcast player have some screenshots from the various sections.
- 2:26Like as I said, I'll see if I can make that happen.
- 2:28But before we get to all that, before we dive into Rich and Textual and all the other things, let's talk about you.
- 2:33How did you get into programming and how did you find yourself doing all this open source Python?
- 2:37How did I get into programming?
- 2:39Oh, OK.
- 2:39So as a kid in the 80s, I guess, I had a Spectrum 48K computer.
- 2:44It was a little plastic thing you plugged into your TV and you could create very simple animations and little games.
- 2:51And I think from there I was hooked.
- 2:52Oh, fantastic.
- 2:53That's just something about me that connected with programming, I guess.
- 2:57What's interesting is those games were so basic, right?
- 3:00They weren't like 3D, VR, oh my gosh, I'm there.
- 3:03Or some of the flashy, even the flashy mobile apps, mobile games these days.
- 3:08But something about those early, early games really captured the imagination, didn't they?
- 3:13Yeah.
- 3:14And the fact that they were so limiting had to make you a little bit creative.
- 3:18So it encouraged creativity because you couldn't do much of anything.
- 3:21So you had to make the best of what you got.
- 3:24So it encouraged you to experiment.
- 3:26And I think it was a great way to get people into programming again, I think.
- 3:29Yeah.
- 3:30Until recently, we haven't had that.
- 3:32And I think the Raspberry Pi does that to some extent, which is a great thing.
- 3:35Yeah.
- 3:35It sort of reaches out to the real world in a more simplistic way, kind of repeating that cycle, right?
- 3:41Yeah.
- 3:41Yeah.
- 3:42And it just allows people access to programming in a very kind of accessible form for children, I guess.
- 3:48So I think the next generation in 20 years will be citing Raspberry Pi as how they got into programming.
- 3:55Interesting.
- 3:55Yeah.
- 3:56I built a robot the first time around or something like that, right?
- 3:59Yeah.
- 3:59Instead of wanting to play a game or script a game out or something like that.
- 4:04Interesting.
- 4:04And, you know, a lot of times I ask people, okay, well, what are you doing now?
- 4:08They're like, oh, I'm, you know, head of data science at a company such and such.
- 4:13You've taken a very interesting, and I suspect a lot of people will be quite jealous of what you're up to these days, right?
- 4:20What are you doing now?
- 4:21Yeah.
- 4:21So up until recently, I was contracting.
- 4:24But I ended my contract.
- 4:26I'm going to take a year out.
- 4:28Well, possibly a year.
- 4:30It depends how much.
- 4:31It depends how things go.
- 4:32But the idea is to work on open source, specifically rich and textual.
- 4:38There are other projects that take my fancy as well.
- 4:40Anything that I can contribute to might try my hand at it.
- 4:44It's not entirely selfless because I do think there might be commercial applications for textual down the line.
- 4:51But certainly for this first six months, it'll be just focusing on just making it the best thing you can.
- 4:57Making it a super solid foundation.
- 4:59Yeah, absolutely.
- 5:00Well, I don't think it's going to take that much to get some things in place to make this long term for you.
- 5:07You know, people can go, if their company or individually they're finding huge value, they could go to GitHub and sponsor you.
- 5:14There's enterprise stuff that can be set up.
- 5:16We'll dive a little bit into that more possibly later as well.
- 5:19But I wish you a lot of luck.
- 5:21But I think with the traction that you're getting and the new things like I really find GitHub's sponsor feature to be something of a game changer.
- 5:30Yeah.
- 5:31I remember looking back, you'd see, oh, here's a popular project.
- 5:34Maybe it's not even an open source library, but it's like an app.
- 5:37And it's like, click here to thank the developer on PayPal.
- 5:40Right.
- 5:40And just.
- 5:41There was a little bit of a barrier to entry.
- 5:43Yeah.
- 5:44And maybe you do it once.
- 5:45Right.
- 5:45But with GitHub, you can say, I just kind of want to say, I want this to keep going.
- 5:49So here's two dollars a month.
- 5:51And if, you know, not that many people who find it valuable send in a couple of bucks a month, all of a sudden it starts to be a foundation that you can really build from.
- 5:58Yeah.
- 5:58It could build up and be something which is sustainable and sustain open source because so many people benefit from open source, including big companies, big corporations.
- 6:08But a lot of these developers are doing it in their spare time for the love of it.
- 6:13And they haven't asked for funding before, but a lot of them deserve funding.
- 6:18Certainly lots of projects which could really use funding to make sure they keep going, to make sure that the software that we all use is still available in a year, two years and five years down the line.
- 6:30Yeah.
- 6:30Otherwise, we're going to end up in a place with like open SSL where there's one person who maintains it and a quarter of the world seems to be built directly upon it.
- 6:39Right.
- 6:39Remember that?
- 6:39Yeah.
- 6:39It was a huge problem.
- 6:41Heartbleed, was it called?
- 6:42Which is a Heartbleed?
- 6:44Yeah.
- 6:44Heartbleed, that's right.
- 6:45Yeah.
- 6:46Which is a great name for a bug.
- 6:47It really is.
- 6:49Why wasn't this fixed?
- 6:50Well, there's one person who does it in their spare time, but everything depends on it.
- 6:54Yeah, but there's still one person who does it in their spare time.
- 6:57Yeah.
- 6:57And yeah, it's just really hard to put that kind of energy and responsiveness into it.
- 7:01All right.
- 7:02Fantastic.
- 7:02So let's start with textual, which I had on the screen, but let's start with rich.
- 7:07Rich is where things got started amongst rich and textual, right?
- 7:11If I remember the history.
- 7:12Yeah.
- 7:13Rich was first.
- 7:14That's about two years ago that I started that.
- 7:16Yeah.
- 7:16Cool.
- 7:17So tell people, you know, a lot of people have heard of rich, but maybe tell folks out there,
- 7:22how would you describe it?
- 7:23You know, it's, we've had ways to sort of print stuff nicer.
- 7:27We've got pretty print Python.
- 7:29We've got colorama where you can put color into your, your terminal, but this takes it
- 7:33to an absolutely new level.
- 7:35So tell us, tell us about rich.
- 7:36Yeah.
- 7:37that makes it difficult to describe sometimes when people ask me what it does, because it
- 7:40does quite a lot of things.
- 7:42but it's all under the umbrella of writing more sophisticated outputs to the terminal.
- 7:47At the basic level is you can set colors and you can set styles like bold and italic.
- 7:53the next level up, it'll do word wrap and it'll also word wrap the style.
- 7:58So you can apply bold and then, then word wrap it.
- 8:00And then we have things like tables.
- 8:03There's quite sophisticated table support, which are quite close to HTML tables.
- 8:08You can, you know, put things in cells.
- 8:10You've got a header row, you've got a little divider, and then you've got the data.
- 8:13Yeah.
- 8:14Yeah.
- 8:14And then you can draw lines around it and change the styles.
- 8:17And, it's, it's all, you even have like alternating rows, right?
- 8:22So it kind of helps you line across, which is pretty neat.
- 8:25Exactly.
- 8:25Yeah.
- 8:26So it's, it's quite sophisticated.
- 8:27It's all composable.
- 8:29So if I've got a table, I can obviously put text inside it, but I can put another
- 8:33table inside it, or I could put a progress bar inside it or syntax highlighting inside
- 8:37it.
- 8:38So the idea is that, rather than like lots of separate libraries, which don't work
- 8:41well together, which I think was a situation that we had previously.
- 8:45Now they all work together.
- 8:46They fit inside each other and they integrate quite well.
- 8:49Right.
- 8:50So you could take your formatting and put it in your word wrap and put it inside of a
- 8:53table cell or something like that.
- 8:54Exactly.
- 8:54Yeah.
- 8:55Yeah.
- 8:55Yeah.
- 8:56Yeah.
- 8:56Yeah.
- 8:56So one of the things that struck me, well, there's a couple of things, but one of them
- 9:00is just how popular Rich is.
- 9:03Right.
- 9:03It's almost 30,000 GitHub stars.
- 9:05That's, that's close to FastAPI level of popularity and not that far behind Flask and
- 9:11Django.
- 9:11That's a really, really popular.
- 9:13When did you create this?
- 9:14Two years ago.
- 9:15Yeah.
- 9:16Yeah.
- 9:16So I guess on the timeline, similar age to FastAPI, but much younger than Flask and Django,
- 9:24if I'm comparing them to those.
- 9:25And over here, it says on your page, you have 2 million downloads a month.
- 9:30That's pretty incredible.
- 9:31Yeah.
- 9:31That's, that's, that's pretty crazy.
- 9:33I think quite a few of those are automated.
- 9:35They're from CI systems.
- 9:37Yeah.
- 9:37Yeah.
- 9:37But I do see that, that rising quite, quite steadily.
- 9:41Yeah.
- 9:41I wonder how many of the CI systems just in general out there do caching at some level where it
- 9:47wouldn't register.
- 9:48Right.
- 9:48you know, I pip install a thing I've already installed and it's a certain version.
- 9:52And it'll just say using cached version.
- 9:53Mm-hmm.
- 9:54Mm-hmm.
- 9:54Right.
- 9:55Right.
- 9:55versus if you create a brand new Docker image and then the next thing you do is install
- 10:00your, you know, pip install your dependencies inside of your Docker.
- 10:03That's a true download, right?
- 10:04Because that machine is totally fresh.
- 10:05Yeah.
- 10:06Yeah.
- 10:07So it's, it's hard to tell.
- 10:08Yeah.
- 10:08I don't know.
- 10:09Do you have any feel for what that breakdown is?
- 10:11To be honest, I'm not sure.
- 10:12That site doesn't give you the breakdown.
- 10:15To be honest, what I would be interested in is how many developers typed pip install rich
- 10:20in that month.
- 10:22You know, how many human beings played with it.
- 10:24That would interest me more.
- 10:26But suffice to say, quite a lot of people have used it.
- 10:29It is quite a lot.
- 10:30Downloading it.
- 10:30Yeah.
- 10:31Yeah.
- 10:31I'm sure it's changed the way that you think about working on the library and whatnot.
- 10:35Right.
- 10:36You know, it's, oh, maybe this might, might destable it.
- 10:39This might cause a problem or this might cause confusion.
- 10:41It's one thing to do that for a thousand people.
- 10:43It's another to do that for two million.
- 10:45That's right.
- 10:47Right.
- 10:47It's, I am.
- 10:48You're going to hear about it, I bet.
- 10:49Absolutely.
- 10:50I didn't follow Semver very strictly originally.
- 10:54I always plan to use Semver and people are starting to use it.
- 10:58And I made, I made a breaking change.
- 11:00And I didn't think anyone was using this particular feature.
- 11:03So I didn't increase the major version number.
- 11:06And then a couple of days later, I got an issue, someone telling me off.
- 11:10Quite rightly so.
- 11:12Oh man.
- 11:13For not warning them about a breaking feature.
- 11:15So since then, I've been very, very strict.
- 11:17It's a version 10.
- 11:19And that's because I've made 10 changes, breaking changes to the API.
- 11:24They're actually quite small.
- 11:25You know, it might just be one signature and one method, but that requires a major version
- 11:30change.
- 11:31Right.
- 11:31But at that scale, obviously, that's still going to affect it.
- 11:34That people, you're going to hear about it and whatnot, right?
- 11:37Exactly.
- 11:37I don't want to break anyone's code.
- 11:39I don't want to give them a bad day.
- 11:40So I'm very strict about that kind of thing.
- 11:42Yeah.
- 11:43Fantastic.
- 11:43And also people should pin their versions, right?
- 11:46So on the flip side, they can also make sure that what they're working on is nice and stable,
- 11:53right?
- 11:54Yeah.
- 11:54A lot of people don't.
- 11:55I do search GitHub sometimes for rich and I look at their PyTummel.
- 11:59What is it?
- 12:00PyProjectTummel.
- 12:01Yeah.
- 12:02PyProjectTummel.
- 12:02Yeah.
- 12:03And a lot of people don't pin their rich version.
- 12:06They'll just be rich and it shouldn't break too much.
- 12:10And often it's a hobby project, so it's not the biggest deal.
- 12:13Right.
- 12:13It really depends.
- 12:14Like it's one of the things I struggle with.
- 12:16So I do a lot of course development, right?
- 12:18And I don't necessarily want to pin people to the oldest version.
- 12:23I'd rather let them have the newest stuff so it exactly matches the documentation.
- 12:27If they go check it these days and stuff, if they go back, you know, six months and watch
- 12:30the video or check out the demo app.
- 12:33But at the same time, there's a chance of that instability.
- 12:35There's always this tension, right?
- 12:37And I guess it depends on what the use case of that app or that library is.
- 12:41Yeah.
- 12:41Yeah.
- 12:42It's tricky to say.
- 12:43The less critical it is for your business or your project, the more you can relax.
- 12:48If it's a tutorial, maybe it doesn't matter quite so much.
- 12:51But if it's critical infrastructure, then you want to pin.
- 12:55Yeah.
- 12:57Yeah.
- 12:58For my web apps, versions are pinned.
- 13:00Super strict.
- 13:01Even the dependencies of the dependencies, like the transitive closure of the dependencies
- 13:05are all pinned.
- 13:05Yeah.
- 13:06On like little demo apps and stuff.
- 13:07Like it's just wide open.
- 13:09So I think it depends.
- 13:10All right.
- 13:11So out in the audience, we have Hybotics says, Will, this looks really good to me.
- 13:15I'm looking at repo now.
- 13:16So not everyone has previously heard of Rich, which is awesome.
- 13:19Oh, it's good to know there's a few people left.
- 13:25That's right.
- 13:25Yeah.
- 13:25I suspect there's actually a lot of people.
- 13:28So who haven't heard it before.
- 13:30Again, check out the screenshots because if you think, oh, here's something that sort of
- 13:36enhances terminal output that completely undersells the level of what you've pulled off here.
- 13:41And that's only before we even talk about textual, right?
- 13:44Let's talk about compatibility because one of the things I find with these sort of nicer
- 13:50terminal output things is like, this works fantastic on POSIX systems.
- 13:54Oh, but you better not be on Windows.
- 13:55Or if you're a data scientist, you like Jupyter Notebooks, you can forget about it.
- 14:00But if you really want to run this thing, like, so what's the story?
- 14:02Like, where can I use this?
- 14:03Just about everywhere.
- 14:04Linux, OSX, Windows, and Jupyter.
- 14:07It started out, it was Linux OSX because that is the easiest platform to develop this kind
- 14:13of stuff for.
- 14:13Windows is a bit of a black sheep.
- 14:15It didn't quite work as is.
- 14:18Windows is getting better though, right?
- 14:20I mean, when it was cmd.exe, it was like, oh boy, this is really different.
- 14:24But the new Windows terminal, I'm really digging it.
- 14:27You know, the new PowerShell, the things like, oh, my Posh extensions.
- 14:30I can feel much more at home on Windows on the terminal than I used to.
- 14:34Yeah, yeah.
- 14:34So the new Windows terminal is much better.
- 14:36Rich doesn't have to do quite so many, doesn't have to jump so many hoops to get Windows support.
- 14:43In fact, it just runs kind of as is.
- 14:46It still supports the older Windows terminal, which does have a few issues.
- 14:51It doesn't, it has very limited colors.
- 14:53That's brave.
- 14:54Yeah.
- 14:54But I guess maybe you want to, right?
- 14:56If you're going to give the app to somebody, you can't really package up the terminal they're going to run it in.
- 15:01So you probably want to have your best possible experience on.
- 15:04To be honest, most people are still going to be running cmd.exe, even if they shouldn't.
- 15:08Yeah, exactly.
- 15:09Yeah.
- 15:09I mean, I could just tell them to install Windows terminal, but that kind of goes against the ethos of this library.
- 15:15I just want it to work so that people don't have to think about what it runs on.
- 15:19Yeah.
- 15:19Put a lot of work initially into.
- 15:21Yeah, so Linux, macOS, and Windows with good support for the new Windows terminal, very limited support for the old command prompt.
- 15:30That's still pretty good.
- 15:31That's still pretty good.
- 15:32I think it's fantastic, actually.
- 15:34If people are really passionate about their terminal and they're on Windows, they probably know about Windows terminal anyway.
- 15:39So they're probably good.
- 15:40That's true.
- 15:40Yeah.
- 15:41The one that I thought was interesting and nice is Jupyter Notebooks.
- 15:44What's the support there?
- 15:46So it works quite, quite well.
- 15:49So I wasn't a big Jupyter user at the time.
- 15:52I was obviously aware of it, but I didn't use it myself.
- 15:55And people asked me for Jupyter support.
- 15:58And I thought, it doesn't do that.
- 16:00It just works in the terminal.
- 16:00But then I looked into it.
- 16:01And it wasn't too bad because I already had functionality to export terminal content to HTML.
- 16:08So I could put a little wrapper around that, export it to HTML, and then insert it into Jupyter.
- 16:14They've got an API which allows you to write content into a notebook.
- 16:18And so I got Jupyter support up quite quickly, quite easily.
- 16:22And it works quite nicely, which people appreciate.
- 16:25It means that you can write code which writes to terminal mostly.
- 16:29But if you do happen to run it in a Jupyter notebook, then it'll write the same thing there as well.
- 16:34It just knows, it detects that it's running in the Jupyter environment.
- 16:38And then it just, all right, output is not print.
- 16:40Output is generate HTML.
- 16:41Exactly, yeah.
- 16:42And Jupyter does have support for that.
- 16:45It will capture standard output and it will convert the colors and everything.
- 16:49But the problem is it wrapped the lines.
- 16:52So if you expanded the window, it would break any kind of neat formatting.
- 16:56If you've got like a grid or a table, it would break that.
- 16:59So I had to do the HTML export within Rich as well.
- 17:03Yeah.
- 17:04Basically, most anywhere people do Python with a UI of some sort, this works is the takeaway.
- 17:11Yeah.
- 17:11This portion of Talk Python To Me is brought to you by Shortcut, formerly known as Clubhouse.io.
- 17:18Happy with your project management tool?
- 17:20Most tools are either too simple for a growing engineering team to manage everything,
- 17:24or way too complex for anyone to want to use them without constant prodding.
- 17:29Shortcut is different though, because it's worse.
- 17:31No, wait, no, I mean it's better.
- 17:33Shortcut is project management built specifically for software teams.
- 17:37It's fast, intuitive, flexible, powerful, and many other nice, positive adjectives.
- 17:41Key features include team-based workflows.
- 17:44Individual teams can use default workflows or customize them to match the
- 17:48the way they work.
- 17:49Org-wide goals and roadmaps.
- 17:51The work in these workflows is automatically tied into larger company goals.
- 17:55It takes one click to move from a roadmap to a team's work to individual updates and back.
- 18:01Tight version control integration.
- 18:03Whether you use GitHub, GitLab, or Bitbucket, Clubhouse ties directly into them,
- 18:07so you can update progress from the command line.
- 18:10Keyboard-friendly interface.
- 18:11The rest of Shortcut is just as friendly as their power bar, allowing you to do virtually
- 18:16anything without touching your mouse.
- 18:18Throw that thing in the trash.
- 18:20Iteration planning.
- 18:21Set weekly priorities and let Shortcut run the schedule for you with accompanying burndown
- 18:26charts and other reporting.
- 18:27Give it a try over at talkpython.fm/shortcut.
- 18:32Again, that's talkpython.fm/shortcut.
- 18:36Choose shortcut because you shouldn't have to project manage your project management.
- 18:42Just going back to the pip-pinning version stuff, Wayland on the livestream says pip-compile.
- 18:47Pip-tools is a game changer for pin dependencies and pip-compile specifically for managing pin
- 18:52dependencies.
- 18:53That's what I've switched to as well.
- 18:54So I just run a script, checks for all the new versions, regenerates all the pip-compiled
- 18:59stuff.
- 18:59And I'm really enjoying that.
- 19:01I think that's fantastic.
- 19:01Okay.
- 19:02I've not used pip-compile.
- 19:03I'll have to check that out.
- 19:03All right.
- 19:04You basically define a requirements file that has what you actually would have pip-typed,
- 19:11pip install, and then it will generate a requirements.txt that is the transitive closure of all of
- 19:17those dependencies, which are pinned.
- 19:18And then you can ask any time for it to update the versions, the pinned versions of that.
- 19:22Okay.
- 19:22Is that like Poetry's log files?
- 19:25I think it's similar.
- 19:27Yeah.
- 19:27I'm not 100% sure, but I think so.
- 19:29All right.
- 19:29So let's talk about various features here.
- 19:32I think just going through, I mean, we touched on them, but let's dive into it a little bit,
- 19:36maybe talk a little bit about the code you write.
- 19:38Sure.
- 19:38So Kim Van Wake is here to kick us off on the first one.
- 19:41Try something as simple as from rich import print in your next project, and you will be
- 19:46amazed.
- 19:46So Will, tell us why we'll be amazed.
- 19:50Like what's this alternate print?
- 19:52When I first wrote this as a console class, you have to construct a class and that's got
- 19:57a print method.
- 19:59But I figured I could just overwrite the existing, the built-in print because it's a, it's a function
- 20:04in Python three.
- 20:05I can just replace it with my own version.
- 20:07So that's what I've done here.
- 20:09I'll give, there's a version of print that you can import from rich, which has the same
- 20:13signature as a built-in print, but it supports the console markup.
- 20:18So you can insert these little square brackets with a style like, here we've got bold magenta
- 20:24and it'll do emojis, colon, vampire, colon.
- 20:28And these styles, like the square bracket, bold magenta slash bold magenta, this is specific
- 20:34to rich.
- 20:35This is something that you came up with.
- 20:37Yeah, that's right.
- 20:38It's called, I call it console markup and the syntax is very BB code like.
- 20:42I don't know if you ever used BB code.
- 20:44Yeah.
- 20:45It's quite simple.
- 20:46It's just like a markup where the tags have square brackets.
- 20:50Yeah.
- 20:51I like this a lot because one of the things that I'll use a lot still is, which maybe I
- 20:56need to start switching to what you're doing here, is Colorama.
- 20:58But for Colorama, you'll do things like you'll import the foreground settings or styles and then
- 21:06you can say foreground dot green plus the text.
- 21:10Yeah.
- 21:11Foreground reset to go back to normal, but you don't have the bold.
- 21:15And then all of that stuff has to happen in code, right?
- 21:18If I wanted to say import some text and then show it on the screen, that text could have
- 21:22these styles in it, right?
- 21:24That's right.
- 21:24Yeah.
- 21:25So you can embed it in code easier or read it from a file, et cetera.
- 21:29I think it's a bit easier to read rather than doing lots of string concatenations.
- 21:34And also the benefit over the Colorama approach.
- 21:36Colorama is a very good bit of software.
- 21:38I've relied on it for years.
- 21:40But the problem is when you concatenate strings like that, you insert these ANSI codes.
- 21:45Once you've built that string, you can't do anything with it really.
- 21:47You can't word wrap it.
- 21:49You can't format it.
- 21:50So with console markup, you can do.
- 21:52You can markup bits of text with color and style, et cetera.
- 21:55And then you can further do operations on them like word wrap and centering text and putting
- 22:02it inside the table, et cetera.
- 22:03Right.
- 22:03Two other things that jump out here that are interesting is you have emoji support.
- 22:07So you can say colon vampire colon, which is pretty awesome.
- 22:12You can technically, if the file format supports it, you could actually put a vampire emoji
- 22:19in the string.
- 22:19Yeah.
- 22:20But it's still kind of nice that you have this sort of emoji lookup, right?
- 22:23Exactly.
- 22:24Because if you want to insert the Unicode character, you'd have to go and find it and then cut and
- 22:28paste it.
- 22:29Yeah.
- 22:29You can just do colon.
- 22:30You can set that into console markup, just colon vampire colon or colon smiley colon.
- 22:36I think there's a couple of thousand emojis you can use there now.
- 22:38Fantastic.
- 22:39Then another thing that jumps out is you're printing hello, bold magenta world.
- 22:45So that's the word world, bold and magenta.
- 22:48And then the vampire.
- 22:49But then you're also printing out a dictionary.
- 22:51And the dictionary is like pretty printed, but also syntax highlighted.
- 22:57So if you print a container, like a dictionary, a list, or like an atomic Python type, it'll
- 23:04run the pretty printer over it.
- 23:05So it will format it in kind of the style that people like.
- 23:09In code, you'd probably format.
- 23:11This is how black would format it.
- 23:13So it looks much the same.
- 23:15Then it runs the syntax highlighting over it.
- 23:18There's a few regular expressions.
- 23:19So in which you can say anything between two quotes is a string and therefore it's green.
- 23:25Anything in angle of brackets is a tag-like thing.
- 23:29So I'll bold the brackets and change the tag name to bright red or whatever it is.
- 23:35And so that the output you get is quite readable and looks like something that came out of VS Code or your editor.
- 23:42Yeah.
- 23:43The more I look at this, the more I think maybe just every project, I'm going to follow Kim's advice and just from rich import print.
- 23:49Because why not?
- 23:50This looks, it has all this cool auto formatting.
- 23:53Does it look actually at the type that it's printing to make any determination?
- 23:58Or does it just look and see if it's source code and then try to format it?
- 24:01It looks at the type.
- 24:03Like if it gets a dictionary or it gets an object versus getting it like a true string.
- 24:07It'll do both.
- 24:08It'll syntax highlight a string.
- 24:10But if it's a container, if it knows that the type, it'll do some syntax highlighting there.
- 24:15There's also a simple protocol you can add to your own objects if you want them pretty printed and formatted.
- 24:23Oh, okay.
- 24:23But not dunder stir, dunder repper, but something else.
- 24:27Dunder rich.
- 24:28It is dunder rich repper.
- 24:30Right.
- 24:30You can specify the arguments and parameters and the indentation.
- 24:36It'll render something that's very much like a pretty printed dict.
- 24:39That sounds like something that would be fantastic to add to some intermediate library that people use.
- 24:44So I, sure, I could create a class and add it to mine.
- 24:47But so often what I want to do is print out a MongoEngine model or a SQLAlchemy model or a Pydantic model.
- 24:53Yeah.
- 24:53Pydantic could add that.
- 24:54Or, you know what I mean?
- 24:55Like these like intermediate SQLAlchemy could add something like that.
- 24:58Oh, this is how you describe.
- 25:00Like this one has an index and whatnot.
- 25:02I think that'd be fantastic.
- 25:03Yeah.
- 25:03So I've added to Attrs.
- 25:05So it'll pretty print objects from the Attrs library.
- 25:08I have a PR for Pydantic as well.
- 25:11So hopefully in the future, you print a Pydantic object and it will format it quite similar to the built-in data structures.
- 25:18Yeah.
- 25:18Okay.
- 25:19Fantastic.
- 25:19I love it.
- 25:20Before we move on to this, I do want to talk about some other things because we're just scratching the surface here.
- 25:25But one of the things that I think has both impressed me and, you know, Brian and I over on Python Bytes on our podcast we do there,
- 25:31we've been continuously impressed at how fast you're adding new features and still kind of keeping the ethos of this library together.
- 25:40So how, maybe give people a little hint on just the velocity here.
- 25:44Like how's that work?
- 25:44Well, I'm not convinced that it's been that fast.
- 25:47I mean, bear in mind Rich is...
- 25:49It felt like a lot of work.
- 25:50It didn't feel that fast.
- 25:51When I add new stuff to Rich, I'm not starting from scratch.
- 25:55There's several layers which are already built and well tested.
- 26:00The bit that I add might not be as large as it, maybe it looks.
- 26:04I see.
- 26:05So you've already got a lot of structure and architecture that makes adding a new feature...
- 26:09Yeah.
- 26:10...from scratch sort of thing, right?
- 26:11So a good design basically.
- 26:12I hope so, yeah.
- 26:13And it seems to be working quite well because, you know, I did build a core feature set and then I added some things to it.
- 26:20And admittedly, those things came quite fast because it wasn't that hard to implement.
- 26:24And I've got to a point now where Rich is quite large.
- 26:29I'd be resistant to adding any more stuff to it unless it is very useful for like a broad selection of users.
- 26:36Sure.
- 26:36Do you have a sense of how many lines of code it is?
- 26:38I know you don't mean large in that sense.
- 26:40You mean large in sort of feature set.
- 26:41But do you have a sense of how many lines of code?
- 26:43You know what?
- 26:43I've never checked.
- 26:44I couldn't guess.
- 26:46By the time we're done with this recording, someone out in the audience will have like already downloaded and checked for us.
- 26:52Who knows?
- 26:53Probably.
- 26:53All right.
- 26:53So the next thing let's talk about is the REPL.
- 26:56So I can create a REPL redevelop print loop by typing the word Python on the terminal.
- 27:02And that opens it up.
- 27:04But it looks just...
- 27:05It's probably the least possibly good experiences you can have in Python, right?
- 27:09There's no color.
- 27:11There's no feedback on sort of what's happening, right?
- 27:14But then I can say from rich import pretty, pretty.install.
- 27:18And then all of a sudden, basically the output of the REPL, like if I'd set a variable name, it'll print it out.
- 27:26Like that becomes rich printed, right?
- 27:28That's right.
- 27:30Yeah.
- 27:30Yeah.
- 27:30So yeah, you call pretty.install.
- 27:33And then everything you put into the...
- 27:36After the prompt will be pretty printed.
- 27:38So previously, if you print a dict without rich, it'll just smash it onto a few lines.
- 27:45It's quite hard to visually parse.
- 27:47It's all one line except for the word wrapping, which doesn't even break on words.
- 27:51And there's zero color, right?
- 27:53Yeah.
- 27:54Exactly.
- 27:55So it's quite difficult to read.
- 27:57I'm quite a visual person, so I always had difficulty with this.
- 28:00If it was more than two lines, it'd be quite difficult for me to figure out where the keys and the values are.
- 28:05But if you do it with rich, it'll pretty print it onto lines and it'll indent it like you would code and then it'll highlight it.
- 28:12So it makes things just much more readable.
- 28:14Yeah.
- 28:14A lot of people will put it in their startup file, so they just get this ripple by default.
- 28:18Oh, interesting.
- 28:19Yeah, that's a good idea.
- 28:20Have you tried this on the more advanced repls like PT Python or BT Python or those where you kind of get an Emacs E or Vim experience?
- 28:31I haven't.
- 28:31No, I've tried it on IPython and it works quite nicely on IPython, but I haven't tried it on other repls.
- 28:37Yeah, it probably works on PT Python, but I haven't tried it.
- 28:40Cool.
- 28:40All right.
- 28:41Now, another thing that you can do a lot with is sort of taking it up to the next level is the console.
- 28:47Tell us about this.
- 28:48Yeah, so the console class gives you more kind of advanced features.
- 28:53There's more options, more things to specify.
- 28:56Typically, you'd have a single console per project.
- 28:59You'd keep it in your top level object or as a global.
- 29:03It has a print method and there's also some other methods like there's a log method and there's a whole bunch of features you can do when you construct the console.
- 29:12Things like exporting the output to HTML.
- 29:16Okay.
- 29:16Nice.
- 29:17That's fantastic.
- 29:18Yeah.
- 29:18So one of the things you can do with the console, for example, is you can set a style on console.print and set some styles.
- 29:25And then it'll come out in that style as opposed to embedding this console markup into the text itself, right?
- 29:31Yeah.
- 29:32So sometimes you might not want the console markup, especially if there's going to be square brackets in the output.
- 29:39You don't want them to be confused with.
- 29:40Right.
- 29:41Or even if you're receiving a string and you just need to put it on the screen, but you haven't generated the string or it was generated by some other part of the app.
- 29:51Like here's a message I need to log this that I got.
- 29:53Exactly.
- 29:53You don't want to parse the string to try to put text into it or more text, right?
- 29:57Yeah.
- 29:57Exactly.
- 29:58So you can disable the highlighting and you can still set a style globally for that string if you want it in red or cyan, whatever.
- 30:05You can still get that, but you can disable the console markup.
- 30:09Nice.
- 30:09And then we have the inspect, rich inspect.
- 30:13What is this one?
- 30:14This is my favorite function in rich and it came quite late.
- 30:19But what it does is you call it with any object and it'll inspect the object and it'll pull out doc strings and it'll pull out methods.
- 30:30And then it'll render it in quite a nice little table that's quite easy to read.
- 30:35And I find this terrific for exploring APIs.
- 30:39Sometimes it's better than documentation.
- 30:42You know, if you get an object back from an API, you don't quite know what methods it supports.
- 30:46You just call rich.inspect.
- 30:48I could have typed something like dir, my, you know, print dir, my object.
- 30:53And I get a list of dictionary objects which are representing, you know, fields and methods and whatnot.
- 31:01Exactly.
- 31:01But they're all jammed together.
- 31:02There's no like help.
- 31:04This is fantastic.
- 31:05So it's like almost a table version of that with one line of help next to it, right?
- 31:11Yeah.
- 31:11So it does the same kind of thing as dir or help.
- 31:14But way, way nicer, yeah.
- 31:16It makes it easier, easier to read.
- 31:17There's also two things.
- 31:18I see there's a block of stuff that has, it's like a list of, I guess those are field names.
- 31:24And then it has the methods.
- 31:26It's sort of called out separately as well.
- 31:28So you're like, these are the fields or properties or these are the fields.
- 31:31And then here's the probably methods and properties, right?
- 31:33That's right.
- 31:33Yeah.
- 31:34So it basically shows you the signature of all the methods and the first line of the doc string.
- 31:40There's an option to show you the full details.
- 31:42But I find just that abbreviated information is generally as much as I need.
- 31:47Oh, this is fantastic.
- 31:48So it says things like copy equals def, copy, bracket, bracket.
- 31:52Would it say async def if it was an async method?
- 31:55Or what's the alternative of def there?
- 31:58Is it just to show it's a method?
- 31:59That's just to show it's a method.
- 32:01That's a good point about async.
- 32:02I don't think it does do async def.
- 32:04And that's a good idea.
- 32:06It'd be pretty dope to throw in an async def or maybe a property if it's a getter method, right?
- 32:11You're right.
- 32:11Yeah.
- 32:12I think they probably should do that.
- 32:13They should inspect the method and see whether it's async and then emphasize that.
- 32:17Yeah.
- 32:18It's a good idea.
- 32:18Yeah, sure.
- 32:19We'll open a PR on that podcast here.
- 32:21No problem.
- 32:22All right.
- 32:23So those are ones that you've got like graphics calling them out as some of the really main
- 32:28things.
- 32:28There's so much happening here.
- 32:30That's that's amazing.
- 32:31But like Waylon out in the live stream points out like mention rich tracebacks.
- 32:36They're so good.
- 32:37I have my ipython automatically start up with that.
- 32:40And yeah, you've got a whole section down here under the library of things like logging,
- 32:45log handlers, progress bars, status, tree views, like crazy.
- 32:50You have tree views in the terminal that can expand and collapse with the mouse.
- 32:54You know, there's there's more going on here than just the stuff we've touched on.
- 32:57Right.
- 32:57There's a bunch of cool features.
- 32:58There's a lot going on.
- 32:59Yeah.
- 33:00Well, Rich will render the tree view.
- 33:02It's textual, which provides the collapsing and navigation features.
- 33:06Yeah.
- 33:07Ah, got it.
- 33:07Got it.
- 33:08Got it.
- 33:08Okay.
- 33:08So we'll get to the interactive bits.
- 33:10But yeah, so I can still draw a tree view even with like little your example here.
- 33:14You've got emoji icons for say folders and files.
- 33:17And then even in the file, you've got an embedded syntax highlighted bit of code that comes out
- 33:22of one of the files and a markdown with some of the markdown rendered as a rich markdown.
- 33:27Yeah.
- 33:27Just a markdown.
- 33:28Like not, not rich the library, but like just colorized and formatted.
- 33:31Yeah.
- 33:32So it goes back to the composability of rich, rich objects.
- 33:36I call them renderables, but you can use them in various contexts.
- 33:40So in here, you can set a renderable per node on the tree.
- 33:43So you can like do what we've done here, add a table next to a tree item or some syntax highlighting
- 33:50or render some markdown.
- 33:51It doesn't really matter to rich what you ask it to render.
- 33:55You can just do it in various contexts.
- 33:58Yeah.
- 33:58Very cool.
- 33:58Okay.
- 33:59So we've got the tree, which is amazing.
- 34:00Let's, you know, since Waylon mentioned it, let's talk tracebacks real quick.
- 34:03I mean, one of the things that really is tricky with the tracebacks is, you know, a lot of
- 34:10times you've got to go to like one end of them to see sort of the error.
- 34:13And then like, there's no color.
- 34:15There's just a lot of stuff dropping in there.
- 34:18Maybe sometimes it'll show the variable values, but they're not really got to kind of pull
- 34:22them out.
- 34:23Right.
- 34:23Things like that.
- 34:24And what you get here is, is ridiculous.
- 34:27First of all, what do I do to make this happen with the beautiful tracebacks?
- 34:30You can do from rich import traceback, traceback.install.
- 34:34Oh, good.
- 34:34Yeah.
- 34:35From then on, if you don't, if you don't handle an exception, it'll be printed with rich
- 34:39or you can explicitly.
- 34:40All right.
- 34:41There's the second thing I got.
- 34:42I just put on all my apps.
- 34:43Yeah.
- 34:44It's a piece of cake to add.
- 34:46So yeah, it's easy to do.
- 34:47Yeah.
- 34:47So tell me what people who are not seeing this necessarily on the screen, like what is this
- 34:51alternative traceback style look like here?
- 34:54Okay.
- 34:54So it actually follows much the same format as a regular Python traceback.
- 34:59It's just underneath the file, you'll see some syntax highlighted code showing you that
- 35:05the line where the exception happened for each frame and underneath each block of code,
- 35:11it'll show you the locals at that point in the frame.
- 35:14So you can see the local variables.
- 35:16Right.
- 35:16And these are called out in a nice table with a nice formatting that we've already talked
- 35:20about.
- 35:20So kind of as if you had done print, you know, from rich import print and then printed out
- 35:25the locals into a table.
- 35:26Yeah.
- 35:26And it's all, it's all pretty printed.
- 35:28So it's quite easy to read.
- 35:29I find with regular Python tracebacks is it, it takes quite a bit of skill to read them.
- 35:35Yeah.
- 35:36Particularly for beginners and even for intermediates.
- 35:38You've got to sit down and analyze the tracebacks, but I'm hoping, but this, this just kind of
- 35:44presents the information in a more readable way and you can like get more of the context
- 35:48of the error.
- 35:49Yeah.
- 35:49I think this is fantastic.
- 35:50This definitely super interesting.
- 35:52I guess one more thing here to really dive into, maybe two, two.
- 35:57I think the log handler is really nice.
- 35:59People should check that out, but maybe tables.
- 36:01I know it's, it doesn't sound as appealing and amazing as necessarily as what we've been
- 36:06talking about.
- 36:07But if I want to have a nice formatted table in a text output, I basically just don't do
- 36:13that.
- 36:13I'm like, that is way too much work.
- 36:15Yeah.
- 36:15Yeah.
- 36:16to worry about this.
- 36:17Right.
- 36:18But with using rich here, you can have almost HTML level formatting styles, you know, borders
- 36:25on border off, just header content divider, like alternating rows.
- 36:29Like I said, a right align, left align.
- 36:31There's all sorts of amazing stuff here.
- 36:33Tell us about the tables.
- 36:34I didn't realize how hard tables would be to implement when I started it or I might
- 36:38not have.
- 36:39You might not have done it, huh?
- 36:41Tables are quite complicated because you've got to calculate optimal column widths and that
- 36:47gets really complicated when you've got text, which can, can wrap and other like renderables
- 36:53that can go in those cells.
- 36:54It does work quite nicely now and it can handle just about anything you can throw at it and
- 37:00it will scale the table nicely and elegantly if, if it doesn't fit into the, you know,
- 37:06the width of the terminal.
- 37:07And it's also quite a good layout tool.
- 37:09You can switch the borders off entirely and then use it to lay out other things.
- 37:15You know, one thing that comes to mind right away is I think of some of the nice progress
- 37:19bar type things for the terminal, like TQDM and stuff.
- 37:22And they're great, but I'm always like the stuff on the right, it'll have like what it's
- 37:26doing and then it'll have a progress bar and then it'll have maybe, you know, how fast
- 37:30is it operating or how much time has it got to go or something.
- 37:32And those are always kind of like doing like a little pulsing, like, cause the thing on
- 37:35the right is always changing and they don't never quite line up.
- 37:38You could just, you could do that here, but have a table and put the progress bar and one
- 37:42of the center fill bits, right?
- 37:44Yeah.
- 37:44So you can do lots of things regarding alignment to fit everything together.
- 37:48And like you said, stop that effect where bits of content will flicker because they're
- 37:53using less characters because it goes from a hundred and into 99 and then.
- 37:56Yeah, exactly.
- 37:56Yeah.
- 37:57Yeah.
- 37:57It's a very good layout tool and also just a good way of presenting tabular information,
- 38:02which is kind of what it was designed for.
- 38:04Yeah.
- 38:04It is a table, right?
- 38:05All right.
- 38:07So last one, I've got some content and probably a most common way that it's in a lightweight
- 38:14format, but you want to turn it into something full featured in terms of text is Markdown,
- 38:19right?
- 38:19So for example, the Python bytes website is almost like entire, like the vast majority
- 38:23of the content there is Markdown, talk by on training, like all the stuff we've got, the
- 38:27CMS we built in the back end, it's all Markdown.
- 38:29Rich has Markdown support too, right?
- 38:31That's right.
- 38:31Yeah.
- 38:31There's a Markdown class.
- 38:33And basically I took common Mark library, which parses the Markdown.
- 38:37Okay.
- 38:37I substituted the bits which were generating HTML with something which generates rich output.
- 38:43And it turns out there's a reasonable job of things like headers and does the style just
- 38:49fine.
- 38:49And there's also syntax highlighting.
- 38:51It'll actually call out to the syntax highlighting code.
- 38:54So if you've got a Python code block, it'll actually highlight that Python code block.
- 39:00Yeah.
- 39:00You have support for inline code with the backtick thing, backtick.
- 39:03Yeah.
- 39:03And the blocks of code, which are the triple backslashes or triple backticks or four spaces
- 39:09or whatever.
- 39:10Yeah.
- 39:10So it supports much of the basic common Mark syntax and does a reasonable job of rendering.
- 39:16It won't look quite as good as a web browser, but I find it quite readable.
- 39:20But it's in the terminal and I didn't have to do anything to get it there, right?
- 39:24So that's pretty fantastic.
- 39:26Yeah.
- 39:27Really, really nice.
- 39:28All right.
- 39:29So I think this is probably as much of the details of rich we want to dive into in terms
- 39:35of like the feature set and stuff, but there's still more to go.
- 39:38There's a lot to love out of this library, which is great.
- 39:41Maybe just give us a sense of the internals.
- 39:44Like, how did you make this happen?
- 39:45How do you make it work?
- 39:46Is this curses to the nth degree or what's happening?
- 39:50There's no curses.
- 39:51There's a layer, which what makes most of it work, where I render everything into, and that's
- 39:57a list of what I call segments.
- 39:59The segment consists of a bit of text plus a style.
- 40:02And the thing about having that intermediate layer before you actually render it to the
- 40:06terminal is you can manipulate it afterwards.
- 40:10So I can apply color and style and then do word wrapping and then I can render it onto the terminal.
- 40:17So everything is built on that and a protocol.
- 40:22So objects can add a couple of methods.
- 40:24They can add a Dunder rich or a Dunder rich console method.
- 40:28Then they can themselves be renderable.
- 40:31So you can print your own custom objects and that will use that intermediate layer of segments
- 40:35to render everything onto the terminal.
- 40:38Nice.
- 40:38Okay.
- 40:38Yeah.
- 40:39It sounds like a really good separation.
- 40:40And you could probably also, if you need to do something specific for one platform versus
- 40:44another, that layer, you can make a decision on how to do that without.
- 40:48That's exactly it.
- 40:49Yeah.
- 40:49Having to put it all over the place, right?
- 40:52Yeah.
- 40:52So I render onto the segments and that's platform independent.
- 40:55But there's another bit of code which will convert those segments onto the appropriate format
- 41:01of the platform.
- 41:02And the platform might be the number of colors that's supported by terminals because some will
- 41:07support 16.7 million colors.
- 41:09Some will support 256 and then some will support 16.
- 41:13Yeah.
- 41:13But because of that intermediate layer, I can make sure that no matter what you write, we'll
- 41:18work on the terminal on the given platform.
- 41:21Yeah.
- 41:21Fantastic.
- 41:22One of the things I do want to circle back to is this idea of you're taking a year off
- 41:27to continue to work on this project, to grow it even further and already has and also do
- 41:32other things in this general realm.
- 41:34And so there's a couple of ways which people can support you, right?
- 41:38If you're a large bank that depends on this kind of stuff, one of the good options is Tidelift,
- 41:44right?
- 41:45People can get a Tidelift subscription for Rich and that gives, what do they get with that?
- 41:49So I don't think you get a subscription for Rich per se, but you can get a Tidelift subscription.
- 41:54I see.
- 41:54And that means that that money is divided amongst all the open source projects that you use that
- 42:00are signed up to Tidelift.
- 42:02In return, you get more responsive developers and developers which will handle security issues,
- 42:10et cetera.
- 42:11It takes the risk of open source code for big organizations because there is a little bit of
- 42:16risk in that if you're relying on someone's hobby project, are they going to be around in
- 42:21six months to your time?
- 42:22Yeah.
- 42:23So Tidelift ensures that developers will be around in the future to support your code
- 42:29going forward.
- 42:30Absolutely.
- 42:30The other one is right up at the top here.
- 42:33I could click sponsor and then come over here.
- 42:36Also, that pulls up a link to the external funding for Tidelift.
- 42:39But I could hit sponsor.
- 42:41Do you have like plans or anything like that?
- 42:43I know some projects have, you know, there's like a gold sponsor and here's just a sort
- 42:49of keep it going sponsor.
- 42:50Yeah.
- 42:51Do you have anything like that or is it just, you know, what people want?
- 42:53GitHub sponsors supports tiers.
- 42:55So depending on how much you want to sponsor, I will help you with your projects.
- 43:00I'm always happy to help people actually open source projects.
- 43:03Always happy to do that.
- 43:05But for the larger tiers, I will do code reviews or I will help you with your project on a more
- 43:12more formal basis.
- 43:13I might even write code for you.
- 43:15It's up to what you want to sponsor.
- 43:17If you just want to say thanks, that's very much appreciated.
- 43:19You know, if I've solved, if I fixed a bug for you and just want to say thanks, then that's
- 43:24fantastic.
- 43:24But if you're a company which is benefiting from the work that I do or the work that other
- 43:29open source developers do, you can sponsor a bit more to ensure that it keeps the work
- 43:34going.
- 43:34Right on.
- 43:35Yeah.
- 43:35And I encourage people if they're depending heavily upon this, you know, help you keep going
- 43:39strong, especially as you're transitioning to just working on this.
- 43:43So yeah.
- 43:44Also, notice someone's forked it since we even pulled it up here.
- 43:47How cool.
- 43:48So the other, the next step that maybe you would, would take this, you talked about not
- 43:54wanting to add too many insane features to rich to keep growing that, right?
- 43:58Keep it focused on target is you also have this project called textual.
- 44:03Textual is a TUI.
- 44:04We've all heard of a GUI, but a TUI is a text user interface instead of a graphical user
- 44:10interface, right?
- 44:10Yeah.
- 44:10I think it's a bit of a misnomer.
- 44:12I mean, because the interface is constructed with text, granted, but it's still a graphical
- 44:16thing you're looking at.
- 44:17Yeah.
- 44:17Yeah.
- 44:17I think of it as a GUI, but with kind of like a fairly retro aesthetic.
- 44:22Yeah.
- 44:22It does have a bit of a retro aesthetic.
- 44:25I would say maybe if you built something with like Colorama or something, that would be maybe
- 44:28more TUI-esque, right?
- 44:30Where there's, where you look at what you build with textual, it's got scroll bars, it's got
- 44:35banners, it's got icons.
- 44:37It's, it's closer by far.
- 44:41Looks a bit more, a bit more graphical.
- 44:43Yeah.
- 44:43Yeah.
- 44:44So tell us about textual and why not just more features on Rich.
- 44:47Rich has some kind of dynamic features.
- 44:50There are progress bars and they're updating live dashboards.
- 44:55And I've been asked quite, quite a lot if I could add keyboard support and mouse support.
- 45:01And I've resisted for quite a while because I want to keep the focus of Rich onto just generating
- 45:08mostly static output.
- 45:09But then, then I saw a project called GH top, which is kind of like H top, but it would take
- 45:16information from the GitHub API and it would show you like real-time events.
- 45:21And they used, oh, there we go.
- 45:23And they used Rich for that.
- 45:25And when I saw that, I realized, oh, I've got to do this.
- 45:28There's a lot of potential there.
- 45:30And I put it off a little bit, but then, then I started on it and I kind of realized that,
- 45:36yeah, there's quite a lot you can do with a terminal these days.
- 45:39This is one of the things that blew my mind.
- 45:40Maybe, you know, give us a sense of, I'm trying to pull a particular picture.
- 45:45Maybe this is just one I could sort of leave on the screen.
- 45:47But one of the things I remember from my early days of GUI type development is how do you
- 45:54resize stuff on the screen, right?
- 45:55So I want to put something where like the main window is here, but then I want the status
- 46:00bar thing, but I want it to stick to the bottom.
- 46:03And then I want some other stuff on the left.
- 46:04And it sounds like that would be pretty tricky to just dynamically try to generate with Rich.
- 46:08But with Textual, you can say like this thing docks to the left and this docks to the bottom.
- 46:13And here, this fills the main content.
- 46:14And then those bits in the middle, are those basically rendered either more of these containers
- 46:19and these widgets?
- 46:20Or is that Rich directly?
- 46:22Or give people a sense of like what they build with this.
- 46:25So Rich does the rendering, which is responsible for getting stuff onto the screen, but Textual
- 46:31handles the dynamic stuff.
- 46:33At the most atomic layer, there's something called a widget.
- 46:37And a widget is almost like a software component in itself.
- 46:41It's built on AsyncIO.
- 46:43So each widget has its own AsyncIO task and it's constantly processing events.
- 46:48And Textual will change the size of that and change the layout in response to resizing the terminal.
- 46:55And you can tell it how the widgets fit together within the given dimensions of the terminal.
- 47:02Right.
- 47:03So you've got like the layout elements that handle docking and whatnot.
- 47:07Yeah.
- 47:08Yeah.
- 47:08Yeah.
- 47:08Very cool.
- 47:09So one of the examples you have on the Textual readme is building one of these widgets.
- 47:14So you just create a class that drives from widget.
- 47:16Here you have a hover example and it has a way to render itself.
- 47:21And then it very much like traditional, you know, I'm thinking of building like VB6 apps
- 47:27or Windows Forms apps, like these traditional ones where you have drag and drop widgets.
- 47:31They have these events, right?
- 47:32And one of the events here, you wouldn't think of this as a terminal thing.
- 47:37You've got like, you know, on mouse, you know, on enter, on leave and, you know, mouse over
- 47:43and things like that.
- 47:45Right.
- 47:45Like those are regular UI types of interactions that you would not expect to see in a text-based
- 47:52app.
- 47:53Right.
- 47:53Yeah.
- 47:53That's fantastic.
- 47:54It's based partially on my knowledge of writing desktop applications, which is quite old now.
- 48:00Oh yeah.
- 48:00What did you write them in?
- 48:01WX widgets mostly.
- 48:03Yeah.
- 48:03It's a C++ framework.
- 48:05I think it's got a Python layer.
- 48:07Yeah.
- 48:08Yeah.
- 48:08Yeah.
- 48:08There's wxPython.
- 48:09I think that might be the next, the Python wrapper.
- 48:13That's right.
- 48:13Yeah.
- 48:13But in the last 10 years, I haven't done any desktop applications.
- 48:16I've been working mostly in the web.
- 48:18So it's mostly influenced by web development with modern frameworks, particularly Vue, which
- 48:24I've used a few times.
- 48:25Vue's nice.
- 48:26Yeah.
- 48:26Vue's very nice.
- 48:27So I'm trying to replicate some of the best features, I think, of you into the terminal.
- 48:32I'm surprised how well some of these features translate.
- 48:35Yeah.
- 48:36This is incredible.
- 48:37It really does build these interactive things.
- 48:39Now, another thing to talk about is how do you control the look and feel, right?
- 48:43Like the way you might do it in Rich is you might have one of these console markups or markdown,
- 48:48or you could use a console and set a style.
- 48:50But if you're inspired by the web, you know, like dot main container hash thing I want to
- 48:58style, right?
- 48:59Like it's some sort of CSS selector, right?
- 49:01Yeah.
- 49:01So you've read my mind.
- 49:02I've been last few days working on CSS.
- 49:06Okay.
- 49:06It's going to work very much like...
- 49:09Is it actual CSS or CSS-like stuff?
- 49:11Like what do you have in mind here?
- 49:12It's probably not actual CSS.
- 49:14A lot of the stuff just wouldn't apply to terminal.
- 49:16Yeah, it makes sense.
- 49:17Sure.
- 49:17But essentially, it's the selectors.
- 49:19So I will have selectors where you can select an ID and then a child with a class name,
- 49:26et cetera.
- 49:26I mean, that's basically what I was thinking when I was saying real CSS, right?
- 49:29Like is it...
- 49:30Right.
- 49:30Or will I say like hash container dot children type and I would write that or is it like...
- 49:37It's exactly like that, but it's...
- 49:38Exactly like that.
- 49:39Yeah.
- 49:39Oh, beautiful.
- 49:39And the only thing that differs is the...
- 49:41I don't know what you call them.
- 49:42The bit that goes inside the curly brackets, the actual rules, they will be different.
- 49:46Sure.
- 49:46Yeah, yeah.
- 49:47Yeah.
- 49:47They render different things, but very much like CSS.
- 49:51If you come from the web, you see this, you'd be very much at home.
- 49:54Nice.
- 49:54I could probably even use less and transpile that down to CSS and put the odds...
- 50:00Yeah, maybe.
- 50:00Who knows?
- 50:01Yeah, maybe.
- 50:02I was thinking, should I just do CSS, which is hard enough in itself?
- 50:06Or should I try to implement less or SAS or one of these things?
- 50:10Well, I'm going to try CSS first of all.
- 50:13If you get it working with CSS, then you probably can get the less compiler to generate the right CSS out of it somehow.
- 50:19Yeah, maybe.
- 50:20But actually, one of the worst things I think of JavaScript and web app development is all those pre-processors.
- 50:26I know.
- 50:27I'm 100% with you.
- 50:29Yeah.
- 50:29When you've got to run all these tasks and all these CLI things just to get it so you can start using your app, it's like, there's something kind of broken about this.
- 50:38Can I just include a file here and go?
- 50:41It's gotten so complicated that JavaScript is one of the more complicated ways to write code rather than one of the simplest, I think.
- 50:47Exactly.
- 50:48Yeah.
- 50:49There was a time where front-end development was seen as kind of like the baby brother of real development.
- 50:56You and your QJ query.
- 50:58That's not true anymore.
- 51:00I don't think it's been true for a while.
- 51:02I think front-end development requires just the same type of thinking as back-ends.
- 51:08You've got to organize all these different processes together and mental models.
- 51:13And it's actually more complicated because there's so much going on.
- 51:17There's so many little things you've got to remember.
- 51:19Yeah.
- 51:20You're taking so much of modern software development and squishing it down to this narrow little bit that is like run it in JavaScript on the browser.
- 51:28Yeah.
- 51:29It's kind of got to fit into this historically what used to be like a narrow-focused environment, and now it's definitely not.
- 51:34Yeah.
- 51:35So I'm trying to take what I think are the good things about front-end development and apply them to the terminal and hopefully leave out the things which I don't like so much.
- 51:46Yeah.
- 51:46That sounds great.
- 51:48So maybe for this one, I think what people should probably do is they should check out the examples, right?
- 51:54The examples from Textual.
- 51:55They can clone the repo and just run these, and it's super easy.
- 51:59There's also a way to see them, I guess, on the developer video log here.
- 52:02Are these you doing these videos here?
- 52:04That's me, yeah.
- 52:05It's just using a short demo of each.
- 52:08Nice.
- 52:08This is my ad apparently I'm getting.
- 52:10No.
- 52:10All right.
- 52:11We'll not play that.
- 52:12Go YouTube.
- 52:15All right.
- 52:15So, yeah, but people can go and check these out because I think seeing it in action is really what you need to appreciate Textual.
- 52:22And it can demonstrate the features which Textual can do, which I don't see in other TUI frameworks.
- 52:31I'm thinking particularly of animation.
- 52:33Yeah.
- 52:33I was just thinking like the CSS easing functions and those types of animations.
- 52:37Yeah.
- 52:38Yeah.
- 52:38I was surprised how well that worked.
- 52:41I was animating things at 60 frames a second, and it can go up to 120 frames per second.
- 52:46Terminals these days, they're built on the same technology as video games.
- 52:50They use hardware accelerated graphics under the hood, so they can actually render terminal updates very, very quickly.
- 52:56I don't think people have taken advantage of that.
- 52:59How long until someone re-implements Doom on Rich or on Textual?
- 53:04I'm sure it's possible.
- 53:05You could render it and then, you know, render it onto text.
- 53:08I don't know how you do it, but various ways of rendering images.
- 53:11So, in theory, you could put Doom in the middle of a Textual application.
- 53:15I would say start with really, really small fonts in a big terminal window so you get higher resolution.
- 53:20Yeah.
- 53:20Yeah.
- 53:21But, yeah, you've got the color.
- 53:22You've got the emojis.
- 53:23I bet you could make it happen.
- 53:24Awesome.
- 53:25All right.
- 53:25Well, we're getting pretty short on time here, Will.
- 53:28Anything else you want to sort of throw in about Textual or even Rich before we wrap it up?
- 53:34Just to say that I like getting feedback and input.
- 53:38So, if you have any suggestions, jump onto Textual discussion board.
- 53:42Or if you find any bugs, let me know.
- 53:44Or connect with me on Twitter and I'm happy to talk about these things.
- 53:48Yeah.
- 53:48Fantastic.
- 53:49Also, I want to give a shout out to one of your other projects real quick.
- 53:52Great.
- 53:53Yeah.
- 53:53So, this is PAL file system.
- 53:55I've been working on that for well over 10 years now.
- 53:57I've handed it over to some very talented developers.
- 54:00But, essentially, the idea is that there's an abstraction there for file systems.
- 54:05So, the same code can write to your disk drive or an FTP server or a zip file.
- 54:13And it just all works exactly the same way.
- 54:15Nice.
- 54:15Do you have, like, cloud format support, like S3 and things like that?
- 54:19Yeah.
- 54:19Exactly.
- 54:19Yeah.
- 54:20So, there's an S3 version and there's a Google Cloud version and there's dozens of other implementations.
- 54:26Any database stuff?
- 54:28Can I, like, treat a table as a directory or something like that?
- 54:31I wouldn't be surprised if there is.
- 54:33I don't know of any off the top of my head.
- 54:35But some people have done some quite creative things where they've made something which is not a file system look like a file system.
- 54:42Oh, look at this.
- 54:42Dropbox FS.
- 54:44Dropbox as a file system.
- 54:46Okay.
- 54:47That's pretty amazing.
- 54:49Okay.
- 54:49Here's the index of file systems.
- 54:50Let's see.
- 54:51We've got application data, FTP in memory.
- 54:54Oh, that's pretty slick.
- 54:55So, you can read and write files, like, say, for tests and not care.
- 54:58Or temp files, maybe.
- 54:59Tem files would be fantastic, right?
- 55:01Yeah.
- 55:01So, you can use temp files and, like you said, for testing.
- 55:04So, you can write it into memory without bothering to write it onto your hard drive.
- 55:08Okay.
- 55:08The multi-file system.
- 55:10So, you could multiplex reads and writes.
- 55:12That's pretty killer.
- 55:13Yeah.
- 55:13That's more, I remember correctly, you can layer several file systems.
- 55:18So, you could have one to write and then several to read.
- 55:22And depending on where the file is, it'll just make it appear like a single file system.
- 55:26Yeah.
- 55:27A lot of neat stuff here.
- 55:27So, you know, people, they got a lot of file reading and writing to do.
- 55:30They can check that out.
- 55:31Also, I want to give a quick shout out.
- 55:33I saw Paul Everitt on the live stream out there.
- 55:35So, Paul and you dove inside in a more visual way into textual, right?
- 55:41So, I'll link to a live stream you all did over there together.
- 55:44Great.
- 55:44Cool.
- 55:44Yeah, that was fun.
- 55:45Yeah.
- 55:46Awesome.
- 55:47All right.
- 55:47Well, I think that pretty much wraps it up.
- 55:50So, I'll give you the final two questions before we get out of here, though.
- 55:54So, Will, if you're going to write some Python code, what editor do you use these days?
- 55:59I use VS Code.
- 56:00I've used that for quite a while.
- 56:02Quite happy with it.
- 56:03But I do try other editors from time to time.
- 56:06Nice.
- 56:06And then, in addition to pip install rich and pip install textual, any other packages out
- 56:12there you want to give a shout out to that you think you use some eyeballs and some attention?
- 56:15Anything that's impressed you lately?
- 56:16I'm drawing a blank.
- 56:17There's so many.
- 56:18It is hard to choose, isn't it?
- 56:20There's a project I saw.
- 56:21Can I mention one that uses rich?
- 56:23It was quite cool.
- 56:23Of course.
- 56:24Yeah, absolutely.
- 56:24It's called Object Explorer.
- 56:27It's a terminal user interface, but it's not.
- 56:29I think it's OBJ Explorer, which is quite nice.
- 56:33You could create an, you explore a Python object and you can navigate into it in a visual way
- 56:39and it will show you the attributes, et cetera.
- 56:41Oh, fantastic.
- 56:42Yeah, that sounds really fun.
- 56:43Is this for like in-memory Python objects or database objects or what is it?
- 56:47Oh, in-memory Python objects.
- 56:49So, I think it's like a debugging tool.
- 56:51It's kind of like, it's a bit like rich.inspect, but it's more visual.
- 56:54Okay.
- 56:55Yeah, fantastic.
- 56:56We'll have to link to that one.
- 56:57All right, Will.
- 56:58So, thank you for being here.
- 57:00This has been really great.
- 57:01Congratulations on both of these projects and all the momentum you've gotten.
- 57:07Thank you.
- 57:07Final call to action.
- 57:08People want to check out Rich or Textual.
- 57:11Maybe want to support you.
- 57:12Whatever else you want to give a final shout out for or call to action before we call it a show.
- 57:17Just connect with me on Twitter.
- 57:19My handle is at Will McGugan.
- 57:21Say hi.
- 57:22I'm happy to talk on Twitter.
- 57:24Right on.
- 57:25Be sure to put the link in the show notes.
- 57:27Thanks for being here, Will.
- 57:28Thank you.
- 57:28It's been great.
- 57:29Bye.
- 57:29Bye-bye.
- 57:30This has been another episode of Talk Python To Me.
- 57:33Our guest on this episode was Will McGugan.
- 57:36It's been brought to you by Shortcut.
- 57:38And us over at Talk Python Training.
- 57:40And the transcripts are brought to you by Assembly AI.
- 57:42Choose Shortcut, formerly Clubhouse.io, for tracking all of your project's work.
- 57:47Because you shouldn't have to project manage your project management.
- 57:50Visit talkpython.fm/shortcut.
- 57:53Do you need a great automatic speech-to-text API?
- 57:56Get human-level accuracy in just a few lines of code.
- 57:58Visit talkpython.fm/assembly AI.
- 58:02Want to level up your Python?
- 58:03We have one of the largest catalogs of Python video courses over at Talk Python.
- 58:07Our content ranges from true beginners to deeply advanced topics like memory and async.
- 58:12And best of all, there's not a subscription in sight.
- 58:15Check it out for yourself at training.talkpython.fm.
- 58:18Be sure to subscribe to the show.
- 58:20Open your favorite podcast app and search for Python.
- 58:22We should be right at the top.
- 58:24You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 58:29and the direct RSS feed at /rss on talkpython.fm.
- 58:33We're live streaming most of our recordings these days.
- 58:37If you want to be part of the show and have your comments featured on the air,
- 58:40be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 58:45This is your host, Michael Kennedy.
- 58:46Thanks so much for listening.
- 58:47I really appreciate it.
- 58:49Now get out there and write some Python code.
- 58:51I'll see you next time.