Latest / Talk Python To Me / #380: 7 lessons from building a modern TUI framework
Transcript
- 0:00Terminals seem like the very lowest common denominator for software platforms.
- 0:04They have to work over SSH, they only show text, you can't do much with them.
- 0:09Or can you?
- 0:10Will McGoogan and team have been building Textual based on Rich,
- 0:14which looks more like an animated web app than a terminal app.
- 0:18And he has learned a bunch of lessons trying to maximize terminal-based apps.
- 0:22He's here to share his seven lessons he's learned while building a modern TUI,
- 0:27that is Text User Interface Framework.
- 0:30This is Talk Python To Me, episode 380, recorded September 5th, 2022.
- 0:35Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:51This is your host, Michael Kennedy.
- 0:53Follow me on Twitter where I'm @mkennedy,
- 0:55and keep up with the show and listen to past episodes at talkpython.fm.
- 0:59And follow the show on Twitter via at Talk Python.
- 1:02We've started streaming most of our episodes live on YouTube.
- 1:06Subscribe to our YouTube channel over at talkpython.fm/youtube
- 1:10to get notified about upcoming shows and be part of that episode.
- 1:14This episode is sponsored by Microsoft for Startups Founders Hub.
- 1:18Check them out at talkpython.fm/founders hub to get early support for your startup.
- 1:24And it's brought to you by Sentry.
- 1:26Don't let those errors go unnoticed.
- 1:28Use Sentry.
- 1:29Get started at talkpython.fm/sentry.
- 1:33Transcripts for this and all of our episodes are brought to you by Assembly AI.
- 1:36Do you need a great automatic speech-to-text API?
- 1:39Get human-level accuracy in just a few lines of code.
- 1:41Visit talkpython.fm/assemblyai.
- 1:44Will, welcome back to Talk Python To Me.
- 1:47Thank you.
- 1:48It's good to be here.
- 1:48Yeah, it's fantastic to have you here.
- 1:50Really looking forward to talking about the progress you've made on Rich and Textual and your company,
- 1:56which is pretty fantastic.
- 1:58The show is not specifically about that.
- 2:00It's more about all these fantastic lessons that you've learned while building it.
- 2:04But, you know, of course, we'll get a chance to talk about it and give some updates too, I'm sure.
- 2:08Cool.
- 2:09Sounds good to me.
- 2:10Yeah.
- 2:10Well, normally I ask folks how they got into programming and people, if they want to hear that story,
- 2:17they can go back and check out your episode 336 on Talk Python way back, way back last year, about a year ago.
- 2:25It's a lifetime ago.
- 2:26So much has happened.
- 2:28In terms of what's happened with your projects and stuff, it really is kind of a lifetime ago.
- 2:32So what I want to ask you instead is, you know, what have you been up to the last year?
- 2:36Give us an update.
- 2:37Yeah.
- 2:37Okay.
- 2:38So I founded Textualize.io, a new startup.
- 2:42I've hired developers and we're very busy working on Textual and it's come on really well.
- 2:49It's amazing.
- 2:50There's more than I thought it would do, to be honest with you.
- 2:52Oh, Textualize.io.
- 2:56Yeah.
- 2:56Textualize.
- 2:57There we go.
- 2:58The other one's for sale if you want it.
- 3:00I get that all the time.
- 3:04Sorry.
- 3:04Yeah.
- 3:04So you've been working on Textualize and you actually got some investments and you're hiring
- 3:09and amazing.
- 3:10I'm so happy for you.
- 3:13Thank you.
- 3:14Yeah.
- 3:14When I first heard that, I'm like, well, what is the business model?
- 3:17What is the business model here?
- 3:18Like, what are you all working towards?
- 3:20Like really trying to just leverage the terminal, right?
- 3:23Even more.
- 3:23Yeah, that's right.
- 3:24So that's a very reasonable question that everyone asks.
- 3:27So the first part is Textual, which will be open source and distributed just like any other
- 3:33open source project.
- 3:34But we'll add on this commercial service where you can take those Textual apps and then you
- 3:41can put them in the web.
- 3:42And then when they're on the web, we can charge companies and organizations a monthly fee for
- 3:49various services such as accounts and things and maybe, you know, payment portals and things.
- 3:54But there will be a very generous free tier for hobbyists and for open source projects that
- 4:00want to do the same.
- 4:01Fantastic.
- 4:01So we have SAS for software as a service.
- 4:05We have PaaS, P-A-A-S for platform as a service.
- 4:09And the whole style of apps from Textual and to a lesser degree rich often go under the
- 4:17terminology of TUI, a text user interface, right?
- 4:20So TUI as a service, TAS, T-A-S.
- 4:24T-A-S.
- 4:24Yeah.
- 4:25Are you creating a new as a service?
- 4:27Yeah, I kind of like it.
- 4:28TAS, you get, you know, in tech, we love our acronyms.
- 4:32So if I don't invent at least one, I'll be disappointed.
- 4:35That's right.
- 4:36That's right.
- 4:36And if they can have like, you know, multiple cases, right, like a capital T-A-S, then I
- 4:42think it's going to be great.
- 4:43Yeah.
- 4:43It's an even, it's one of the better types of acronyms.
- 4:45Yeah.
- 4:46Yeah.
- 4:47Yeah.
- 4:47Fantastic.
- 4:48Now, I think this is great.
- 4:50And, you know, there's one of the things I'm really fascinated about and for a long time
- 4:54I've been trying to pay attention to and highlight is how do people go from open source projects
- 5:00to business?
- 5:01And when I first started the podcast and really started to think about these things, it seemed
- 5:06like there was not that many great answers.
- 5:08It felt like a lot of, well, here's my PayPal donation link or something.
- 5:13And it just seemed like, well, okay, that might feel good as a thanks, but you cannot make
- 5:18that your job to say, buy me a coffee most of the time.
- 5:21Yeah.
- 5:21But there is, there's a lot of progress lately, isn't there?
- 5:24Yeah.
- 5:25Yeah.
- 5:25Yeah.
- 5:25Yeah.
- 5:25Some GitHub sponsors and similar programs.
- 5:28You can get sponsorship and it can be enough to live on.
- 5:34It's not easy.
- 5:35The type of projects to get sponsored are the ones which are super critical to businesses.
- 5:40And in that case, companies don't mind donating $300 a month.
- 5:46And if you get enough of those, you could and three live on it.
- 5:49And some people certainly do.
- 5:50But I don't think it's practical for most people.
- 5:54Even if your open source project is widely used and popular, it might not bring in enough
- 6:01sponsorship to live on, unfortunately.
- 6:03Yeah, unfortunately.
- 6:05And so I do think GitHub sponsors really is kind of that done right.
- 6:11You know, it's recurring.
- 6:13It's automatic.
- 6:14Yeah.
- 6:15It has a social signal benefit, right?
- 6:19Like you can see who is sponsoring which projects.
- 6:22And so if you're an organization, right, like you could say, oh, look, our company sponsors
- 6:26that project or whatever.
- 6:28Yeah.
- 6:28It's very well done.
- 6:29Actually, I've got no problems with it.
- 6:32It's done.
- 6:33You can, like I said, you can contribute every month or you can contribute a one-off and you
- 6:37can get your name mentioned on the project.
- 6:39And the author of the project can offer various perks.
- 6:43So I think it's nicely done.
- 6:46The other one that I see really, this might be the most popular one, is take an open source
- 6:52thing and then take away, help alleviate or completely solve the operational side of things.
- 6:59Right.
- 7:00You know, for example, we have MongoDB and then they have Atlas, which lets you push a
- 7:05button and manage your cluster automatically inside your own AWS or Azure account.
- 7:10We've got Streamlit, which just got acquired, has a lot of similarities to what you're doing.
- 7:15You know, it's like it's got an open source version.
- 7:17You can create these dashboards.
- 7:19It's really cool.
- 7:20But then how do you put them on the internet?
- 7:22How do you maintain them?
- 7:23Right.
- 7:24And then, well, guess what?
- 7:25There's a paid tier that just runs it in our cloud for you.
- 7:28Right.
- 7:29So it seems like a really great, really great path to proceed down for what you're doing here.
- 7:33Yeah, that's right.
- 7:34They've got a term for it.
- 7:36It's called OpenCore.
- 7:38So you use the open source part to drive adoption.
- 7:43And then you can use that to sell some related services, which will just make life easier for
- 7:48businesses and things because there's a long history of businesses making use of open source
- 7:53projects and making a lot of money out of it.
- 7:57But they haven't really put that money in the business.
- 8:00So if the people that are building and maintaining these open source projects can also build side
- 8:06businesses around the open source project, then everyone benefits because your code gets
- 8:11maintained for the indefinite future and people can make a living from it.
- 8:16Yeah.
- 8:17If you would otherwise have to hire somebody to manage a Kubernetes cluster or whatever,
- 8:22and instead you can pay $20 a month, that's a real good deal.
- 8:26Exactly.
- 8:26And you don't want to have to solve all these problems individually for everyone that uses it.
- 8:31It's actually far better for someone to solve it once and for all, or at least reduce the
- 8:37footprint for maintenance.
- 8:39Yeah.
- 8:40Who else better to figure out how to put it in the cloud than people who are creating it,
- 8:44right?
- 8:44Yeah, exactly.
- 8:45Yeah.
- 8:45Yeah, for sure.
- 8:46Okay.
- 8:46So fantastic.
- 8:48I'm looking forward to TUI as a service to come on strong here.
- 8:52That's great.
- 8:52So maybe before we jump into the lessons learned, just tell people about what textual is and
- 8:59then textual is built upon rich.
- 9:02So give us a quick rundown on take whichever order you think is better to go first, rich
- 9:08or textual.
- 9:09I think rich.
- 9:10I'll go in chronological order.
- 9:12So I started rich.
- 9:14Gosh, it must be like three years now.
- 9:17And the idea was to be able to write colored text, the terminal in a nice, elegant way, and
- 9:23also be able to build on that with larger components.
- 9:27So we've got things like tables.
- 9:29We've got progress bars.
- 9:31We've got log messages, panels, all sorts of things.
- 9:34And they're all using the same core rendering technology, which basically takes your objects
- 9:40and then turns it into ANSI codes and text.
- 9:43And that got fairly popular, shall we say?
- 9:46I was surprised.
- 9:47I would say.
- 9:48I would say so too.
- 9:50Yeah.
- 9:50I mean, it's in pip now, which still blows me away because that means that virtually every
- 9:56Python developer is running my code, which also scares me just a little bit.
- 10:01This is nuts, right?
- 10:02Because I mean, if you have Python, you have pip.
- 10:04So you have rich.
- 10:06Is it a package with pip as a dependency or is it vendored in?
- 10:10It's vendored because you've got a chicken and egg problem with pip.
- 10:14Right.
- 10:14Because pip is how you get the things, right?
- 10:16Exactly.
- 10:17Exactly.
- 10:17So everything is vendored.
- 10:18Pip is just one project with lots of vendored projects inside of it.
- 10:23Rich, for people who haven't seen it in action, it's not just like Colorama or something,
- 10:28which I'm a big fan of Colorama.
- 10:30But that's just about how do I make this line this color or whatever.
- 10:34But you're talking about like tables with auto, you know, auto ellipsing and all sorts of
- 10:40really, really powerful content, right?
- 10:43Yeah.
- 10:43And some things which you might take for granted in the browser, like text wrapping, that wasn't
- 10:48easy to do prior to Rich.
- 10:50It's more complex than you might think.
- 10:53Things like emoji and Chinese characters, those take up two cells in the terminal.
- 10:59If you use the built-in text wrap module, that won't account for that.
- 11:03And your text wrapping will be misaligned.
- 11:06So I wanted to solve those kind of problems.
- 11:09I just want it to be effortless.
- 11:10You know, I just, you just say, print this text and it'll wrap it for you, which generally
- 11:15makes it a lot more readable.
- 11:16Yeah, absolutely.
- 11:17There's a lot, there's a lot going on there.
- 11:19I would say so.
- 11:20And I don't know how many projects are using Rich now, but it's, there constantly seems
- 11:26to be like, and now this has Rich support.
- 11:28So it has much better, you know, output or it's more understandable or whatever, right?
- 11:33A lot of cool things happening because of it.
- 11:36I'm delighted every time I read one of those.
- 11:38I do try to make it quite easy to drop things in.
- 11:42For instance, if you're printing out Python data structures, you can use a Rich method and
- 11:48you'll get pretty printing and colorizing built in.
- 11:52And things like exception handling, we've got very pretty exceptions that show you snippets
- 11:57of code and can, you know, highlight the line where the error occurred.
- 12:01And you can add those with just a few lines.
- 12:04So I think that's, that's kind of pushing adoption is the fact that there is very low barrier to
- 12:08entry.
- 12:09Yeah.
- 12:09And it's just beautiful, right?
- 12:11It's easy to make beautiful UIs.
- 12:12And if you make it easy for people to make nice looking things, they, they want to use it.
- 12:17I mean, it's not as used as much these days, but think of when Bootstrap came
- 12:22on the scene, you know, eight years ago or 10 years, whatever it was 10 years ago, every,
- 12:26everything started to look like Bootstrap because you could just apply this magic and like,
- 12:30oh, everything looks like professional, but we're not professional.
- 12:32And I feel like Rich is a little bit like that for two weeks.
- 12:35I think so.
- 12:37Cause it was, it was very difficult to add pretty formatting prior to Rich.
- 12:41I mean, there were libraries that existed, but they didn't integrate very well.
- 12:45Rich kind of combines all that functionality together.
- 12:49so it's, it's very easy to, to add a pretty content and, it's not just pretty for pretty
- 12:55sake, you know, pretty can also mean more readable.
- 12:58you know, developers, we get presented with pages and pages of content that we learned
- 13:03to decipher and pick out meaning.
- 13:05but we can do that much quicker if there's been some forethought into, into how it's presented
- 13:11and rendered in the terminal.
- 13:12And Rich does give you that capability.
- 13:16This portion of talk Python to me is brought to you by Microsoft for startups founders hub.
- 13:21Starting a business is hard by some estimates.
- 13:24Over 90% of startups will go out of business in just their first year.
- 13:28With that in mind, Microsoft for startups set out to understand what startups need to be successful
- 13:34and to create a digital platform to help them overcome those challenges.
- 13:37Microsoft for startups founders hub was born.
- 13:40Founders hub provides all founders at any stage with free resources to solve their startup
- 13:46challenges.
- 13:47The platform provides technology benefits, access to expert guidance and skilled resources, mentorship
- 13:53and networking connections, and much more.
- 13:55Unlike others in the industry, Microsoft for startups founders hub doesn't require startups
- 14:01to be investor backed or third party validated to participate.
- 14:05Founders hub is truly open to all.
- 14:08So what do you get if you join them, you speed up your development with free access to GitHub
- 14:12and Microsoft cloud computing resources and the ability to unlock more credits over time.
- 14:17Help your startup innovate.
- 14:19Founders hub is partnering with innovative companies like OpenAI, a global leader in AI research and
- 14:24development to provide exclusive benefits and discounts through Microsoft for startups founders
- 14:29hub.
- 14:30Becoming a founder is no longer about who you know, you'll have access to their mentorship network,
- 14:35giving you a pool of hundreds of mentors across a range of disciplines and areas like idea
- 14:40validation, fundraising management and coaching sales and marketing, as well as specific technical
- 14:45stress points.
- 14:46You'll be able to book a one-on-one meeting with the mentors, many of whom are former founders
- 14:51themselves.
- 14:51Make your idea a reality today with a critical support you'll get from founders hub to join the
- 14:57program.
- 14:58Just visit talkpython.fm/founders hub, all one word, no links in your show notes.
- 15:03Thank you to Microsoft for supporting the show.
- 15:05Jamie on the audience says, "I love Rich.
- 15:09Rich traceback is so helpful."
- 15:11Maybe just quickly touch on the traceback.
- 15:13And then also, Demetrius is asking about, is this for Jupyter?
- 15:17Is it only a thing for Linux terminals?
- 15:19And maybe sort of touch on those two things and we'll talk textual.
- 15:22Tracebacks, yeah, you can install a traceback handler and it will capture unhandled tracebacks
- 15:29and present them nicely in the terminal.
- 15:32What was the other question?
- 15:33Sorry.
- 15:34Where does this apply?
- 15:35Like, can I use this on Windows?
- 15:36Can I use it on Mac?
- 15:37Is it for Jupyter?
- 15:38What is this?
- 15:39Pretty much everywhere.
- 15:40So you can use it on all the major platforms, Linux, Mac and Windows.
- 15:45And yes, it does work in Jupyter.
- 15:48So it will convert the ANSI codes to HTML automatically.
- 15:53That's actually the most impressive thing to me.
- 15:56That must be a lot of work to maintain all these different output destinations and whatnot.
- 16:01The terminal is not too bad.
- 16:03So macOS and Linux are frankly the same as far as I'm concerned.
- 16:08Windows has a number of tweaks and hacks because there's two windows basically.
- 16:15Newer Windows has virtual terminal sequences which make it just like the Linux and Mac
- 16:20terminal and that's terrific.
- 16:22But we also support the older style terminal which is quite limited.
- 16:27And the review like...
- 16:28Right, right.
- 16:29CMD.exe is not nearly as powerful as the new Windows terminal.
- 16:32No, it's not.
- 16:34So we have to make some compromises and some sacrifices.
- 16:38It works.
- 16:39It's usable.
- 16:40It won't look quite as pretty.
- 16:43But there's a lot of people using that out there.
- 16:44So it's important to maintain.
- 16:46Yeah, you can't really drop it.
- 16:47Yeah.
- 16:48No, you can't drop it.
- 16:49Okay.
- 16:50And then the other thing that's more directly related to your article and to your business,
- 16:56although obviously one's a building block.
- 16:58So same as Textual.
- 17:00Yeah.
- 17:01So Rich was for writing kind of static content into your kind of terminal, your scroll back
- 17:06buffer.
- 17:07What Textual does is it uses Rich, but it completely takes over your terminal to create an application.
- 17:13You don't see a command prompt, but Textual will handle key presses and mouse movements and
- 17:19it'll render quite sophisticated applications, which look more like web apps than the previous
- 17:25generation of Tuis.
- 17:26Yeah.
- 17:27But under the hood, it's using the same code that renders tables in Rich.
- 17:32It's kind of a, I call it a rendering engine.
- 17:34It's designed to take sort of abstract data and then turn it into what I call segments, which
- 17:40are basically a piece of text with a style.
- 17:42It's pretty amazing.
- 17:43You know, if you look at the UI, it's got, it really does feel a bit like a web app.
- 17:48It has say like a title bar across the top and you know, this isn't just like text you
- 17:53printed out on the top of the output.
- 17:54No.
- 17:54It's stuck to the top, like a nav bar, I guess is the right word.
- 17:58You have these different widgets you can put in line, like code highlighting.
- 18:02You've got a text widgets with like scrolling within scrolling.
- 18:07You've got animated sidebars that pop out all these cool different aspects, right?
- 18:12Even a hotkey support for like D to toggle dark and light mode if you want.
- 18:16Yeah.
- 18:17Yeah.
- 18:17We've even got data tables now, which are pretty cool.
- 18:20They look kind of like little Excel windows type of spreadsheets.
- 18:23Oh, fantastic.
- 18:24Yeah.
- 18:24So it's, I think my background is as a web developer, basically.
- 18:29So I want to make something which anyone who knows how to use a web, which is pretty much
- 18:35everyone, they would be comfortable using one of these applications.
- 18:38You could just sit them down in front of it and they'll know exactly what to do.
- 18:42They'll know that this is a menu.
- 18:43They can click that.
- 18:44Here's a scroll bar.
- 18:45I don't know how to use a scroll bar.
- 18:46It'll work with a mouse wheel or two fingered scroll on the trackpad.
- 18:51It'll just be very, very familiar.
- 18:53But at the same time, I also want to keep it keyboard focused because one of the benefits
- 18:58of TUI is it doesn't interrupt your flow.
- 19:02You know, you can be at the terminal typing commands, enter into a TUI and you could work
- 19:08with that and use the keyboard and they can drop back straight into the command line.
- 19:11So it's kind of this marriage of the command line and more sophisticated applications which work a lot like desktop and web.
- 19:21previously when people wanted to create apps like this, well, one, it was very difficult to do.
- 19:25But if they wanted to come somewhat close to these types of things, they would use a library called Curses or something like that, right?
- 19:33Yeah.
- 19:34And Curses has been around for a long time, I think decades.
- 19:38I can't remember off the top of my head, but I think in the 90s it came around.
- 19:42And, you know, people have done some very cool things with it and have been like attempts to improve upon it.
- 19:49But I think still it does take, you have to be very well motivated to create an application with Curses because you're going to have to deal with some quite archaic issues.
- 19:59Yeah.
- 20:00I haven't done a ton with it, but it feels to me like the equivalent of saying like textual versus Curses would be like you could either use something like Pygame where you can give some sprites and they can move around the screen.
- 20:13Or you could fire up OpenGL or DirectX or Metal and you could start rendering pixels on your own.
- 20:20Like you could accomplish the same UI, but one is tremendously difficult.
- 20:25The other one kind of gives you much higher level building blocks to accomplish the same thing.
- 20:30Yeah, exactly.
- 20:31So textual is more abstract.
- 20:33You don't have to plot individual characters.
- 20:35You just say put a text box in this part of the screen and a button in this part of the screen and, you know, textual handles the rest.
- 20:43So it's less archaic aspects of terminals that go back decades that you have to think about.
- 20:51Let the framework handle it and handle the differences, right?
- 20:55Yeah, exactly.
- 20:56That's what computing should be.
- 20:57It should make your life easier.
- 20:59You shouldn't have to think about, you know, decades.
- 21:02You know, you hear in science people talking about, you know, standing on the shoulders of giants, of like building on Einstein and whatever.
- 21:10There's nothing else that stands on the shoulders of previous work like software, right?
- 21:15It's just like layers and layers and layers.
- 21:18We don't have to think about that anymore.
- 21:20We now have a different set of concepts to think in.
- 21:23Now let's go build, you know?
- 21:25Yeah, yeah, which is fine for the most part, as long as you don't ever have to look behind the curtain.
- 21:30You know, it's the same with any piece of computing.
- 21:33It presents a nice, clean interface, but there's a lot of effort to get there.
- 21:38It's like an iceberg.
- 21:39You only see the very tip of it, but two-thirds are, like, below the water level.
- 21:43Basically, if it becomes a leaky abstraction, right?
- 21:48And then you've got to deal with the underlying layer, but not exactly anyway.
- 21:52Exactly.
- 21:52All right, last question, and then we'll move off.
- 21:55Talk about the lessons.
- 21:57Luca asks, will Textual have a polyfill for blink, like text blinking in terminals, which
- 22:04don't support it, or, you know?
- 22:06But basically, I guess more interesting, does it have polyfills and, like, other ways to
- 22:10make stuff happen that's not naturally supported?
- 22:12You know, it could quite easily.
- 22:14You could set up a timer called set interval, which would toggle something to show the blink
- 22:20or hide the blink.
- 22:22I don't think I want to support in Textual because I don't want Textual apps to be blinking.
- 22:25It's a terrible user interface for the most part.
- 22:28The exception would be a cursor.
- 22:31Sometimes you want a cursor to flash so you can see where it is.
- 22:34I think I might leave that one up to developer to implement, and it probably is, like, a two-line
- 22:40job.
- 22:40I can see a future where some movie has, like, a fake hacker UI implemented in Textual, and they
- 22:49probably have a blink thing about, like, when something's going to blow up going on.
- 22:52All right, let's move on.
- 22:54Let's go through your lessons.
- 22:55So this is what you've been building and what you are building.
- 22:59And obviously you've worked on Rich for a long time.
- 23:02You've worked, you and your colleagues at the company, for a year now.
- 23:06So tell us about the lessons.
- 23:08Let's go through these.
- 23:09Yeah.
- 23:09Okay.
- 23:10So terminals are fast.
- 23:12This might surprise people.
- 23:14And I can understand why.
- 23:15It surprised me.
- 23:16Yeah.
- 23:16Because when you use a terminal, you type a few characters, and the characters appear
- 23:19on screen.
- 23:20You hit return.
- 23:21You know, half a second later, you'll get a response with some text.
- 23:25And you don't think of terminals as being fast.
- 23:27But nowadays, they're built on the same technology that runs video games.
- 23:31So it uses the GPU to update the terminal with new characters, which have a background and
- 23:38a foreground.
- 23:39And it turns out that if you can write updates at 60 frames a second from the Python code,
- 23:45the terminal will happily display it.
- 23:48I was surprised at how smooth it was.
- 23:51We had 60 frames per second animation of something moving across the screen, and it looked silky smooth.
- 23:57And that was updating the entire screen, like every frame.
- 24:00Wow.
- 24:01I'm really impressed with the fact that so many of these terminals are hardware accelerated,
- 24:07like GPU accelerated.
- 24:08And I'm just poking around.
- 24:09What am I running here right now?
- 24:12I'm running iTerm.
- 24:12And it has all these settings for GPU render redraws, and basically the maximum frame rate.
- 24:19And so things like this.
- 24:22It's just, it's, you would never expect to go find hardware acceleration features inside
- 24:29the terminals.
- 24:29But they have them.
- 24:30They do, yeah.
- 24:31And it's quite strange, because not much software takes advantage of that.
- 24:36I mean, your day-to-day work at the terminal, it doesn't need to be fast.
- 24:40It really doesn't.
- 24:41As long as it can add new data, you know, within a second, you're probably quite happy.
- 24:46So these terminals have been getting faster and faster and faster.
- 24:50Some of them, like I use Kitty and something called Westerm.
- 24:55And those are incredibly fast.
- 24:56They're really optimized at getting things on the screen.
- 25:00As in this thing?
- 25:01Most software.
- 25:02Yes.
- 25:02Kitty's excellent.
- 25:03It's super fast.
- 25:04But most software doesn't need it.
- 25:06You know, it's like you use Vim or HTOP.
- 25:11HTOP updates once a second.
- 25:12So it's like the developers of these terminals are making it faster and faster and faster.
- 25:17But there's very little software that makes use of it.
- 25:21Except for like sort of hobbyist demos where they do like video to text things.
- 25:27So you can see like your face that's made up with ASCII characters, which is fun, right?
- 25:32But you don't think that's not really productivity software.
- 25:34So I was very pleased that when I started working on Textual that it wasn't a bottleneck.
- 25:39You know, if I could write things fast enough to terminal, the terminal would happily accept it and render it.
- 25:46That opens the possibility for so many things if you can get high frame rates.
- 25:51You know, and the iTerm default was limit frames to 60 frames a second.
- 25:5660 frames a second, that's plenty fine for really smooth animations.
- 26:00Yeah.
- 26:01And, you know, I think it's probably what my LCD monitor is stuck at, right?
- 26:05So that's as fast as you're going to see it basically anyway.
- 26:08That's it.
- 26:09And it can't, there's not much use in it being any faster because in order to see greater than 60 frames a second,
- 26:15you have to have something that's moving greater than 60 frames a second.
- 26:20And generally, you don't want things flying about your terminal, unless maybe you're making a video game.
- 26:29We use animation quite sparingly for nice kind of things like when a panel slides in, sidebar pops in.
- 26:37It's quite smooth.
- 26:39We can also use it for fades.
- 26:41So we can set the opacity of a block of text and have it fade away and fade in again.
- 26:46And that's sometimes quite useful if you want to draw the user's attention to the fact that you've added a new item.
- 26:53Rather than having it appear immediately, it'll fade in.
- 26:57So we can use animation in those places.
- 27:00But again, it doesn't need to be anywhere near 60 frames a second.
- 27:05No, it doesn't have to.
- 27:06But I do agree that little bits of animation are super important for highlighting things that people need to pay attention to without much effort.
- 27:15And it doesn't have to be so bright colored or right in the way, right?
- 27:20If you just have a little thing slide down that says, you know, this job has finished while the other work is happening.
- 27:26That's really nice.
- 27:27And you have a video here on this article, the seven things article that we'll link to, obviously.
- 27:33And it shows you running this thing called BasicPi.
- 27:36Is this included with Textual or is it?
- 27:38Yeah, that's one of our sandbox apps.
- 27:41So whenever we're testing something, we just put it in Basic.py.
- 27:45So that shows off like a number of the features we've got.
- 27:48I think that video is quite old.
- 27:49We've got some more cool stuff.
- 27:50I'm sure you do.
- 27:52Yeah.
- 27:53But one of the things that's really nice is it shows a lot of the animations.
- 27:57And one of the animations is changing the theme.
- 28:00Another one is to pop out the side navigation.
- 28:03And those are really nice, right?
- 28:05The fact that that happens at a frame rate fast enough that it looks completely smooth means it doesn't just look like some terminal app where a thing clicks in and then it clicks out.
- 28:14It actually feels really, you know, really nice and polished.
- 28:18Yeah, we've kind of identified there's like a sliding scale of animation.
- 28:23At one end, you've got like the scroll bars.
- 28:26It might not be obvious, but those animate.
- 28:28So if you click below it, it will scroll smoothly downwards.
- 28:32And also it will filter when you click and drag.
- 28:34It will filter sort of frames between the motion of the mouse.
- 28:39And it makes the scrolling look a lot more smooth.
- 28:41That's not obvious, but that is a use of animation.
- 28:43And at that end of the scale, I think most people would agree that's good.
- 28:48That's a bonus.
- 28:49At the other end of the scale, we've got things which are a bit gratuitous, like the sidebar.
- 28:54I like it, but some people might think, I don't want to be distracted by this animation.
- 28:58I just want it to appear instantly.
- 29:00I want it to be more, feel more snappy rather than animated.
- 29:04So I think what we're going to do is we're going to have like a switch where you can decide as a user what kind of animation you like.
- 29:10If you just want the scroll bars, you can set it to be quite low.
- 29:15And that would make the sidebar pop in instantly and disappear instantly.
- 29:18But if you love the animation, you can just whack it up to the maximum.
- 29:23And then it will use animation wherever it can for fading and sliding, etc.
- 29:27For example, with a scroll bar, that's not actually the terminal scrolling it.
- 29:31That's textual redrawing the screen and reprocessing it.
- 29:35If it's not smooth, if it just goes clunk, clunk, clunk, it's super hard to track when I scroll down.
- 29:42Where do I continue from?
- 29:44And that kind of stuff, right?
- 29:46It's not just there to make it feel good or feel smooth.
- 29:49It really has an important effect.
- 29:52Yeah, if you press the page down, if it was to jump down instantly, you wouldn't be able to find your place in the text again.
- 30:00Your eye would just have to focus in to find where you're reading.
- 30:04But if it scrolls in over, say, 300 milliseconds, your eye tends to follow where you were reading.
- 30:11So you move your eye, you follow the animation, and then you're sort of reading again from the top.
- 30:16And that's actually beneficial.
- 30:18It's not just eye candy.
- 30:19It's not just gratuitous use of a feature, you know?
- 30:23Yeah, absolutely.
- 30:24It's very, very helpful.
- 30:25So you talked about all of this happening without flickering in the terminal.
- 30:30We already discussed the GPU accelerated, hardware accelerated aspects.
- 30:34But you said also that's not necessarily enough.
- 30:37So there's a couple of tricks that you highlight here.
- 30:40The protocol of terminals came about over many decades.
- 30:44And it wasn't designed to avoid flickering.
- 30:47I think when people built the protocol...
- 30:49They never imagined what you would be trying to do with it.
- 30:51No, they didn't think someone was going to be animating it at 60 frames a second.
- 30:56So there's no...
- 30:58Well, there's very little help to reduce flicker.
- 31:01So there's a few things that you can do.
- 31:03One I've discovered is it's better to overwrite content without clearing it.
- 31:09So if you want to animate, say, a piece of text, make it move, you might think, I'll clear this text, and I'll draw on top of it.
- 31:16But what that does is it'll introduce a potential for a frame where you see where it's cleared.
- 31:22And then you see the frame where it's updated, and that'll cause flicker.
- 31:25So if you just overwrite the content without clearing it, then that reduces the chance of flickering.
- 31:31Yeah, I've also seen sometimes some of these progress bars or the little tables updating.
- 31:37They seem to write new whole sections.
- 31:40And it's like filling your history with every frame.
- 31:44Your terminal history is full of every frame and stuff like that.
- 31:48If you start to resize it, and there's all this weird stuff that happens if you're not actually overwriting it.
- 31:53Textual goes into what's called application mode.
- 31:55This is a feature of terminals.
- 31:58So when you're in application mode, you don't have a scroll back buffer.
- 32:01The scroll back buffer has that problem.
- 32:04It'll tend to fill up with garbage if you're trying to animate things.
- 32:08And it's also quicker because the terminal doesn't have to worry about appending data and moving it, etc.
- 32:14So Textual goes into application mode.
- 32:18And it's generally much faster and able to reduce that flickering.
- 32:22Trick two?
- 32:22Trick two.
- 32:23One of the problems with the protocol is that it's not aware of flickering.
- 32:29It doesn't know when to paint the screen.
- 32:31So you're sending it data.
- 32:32It needs to know when to update the screen.
- 32:35If you write lots of small pieces of data, say you're going to update a character over here, you're going to update a progress bar here.
- 32:42It might update a few of them and then paint the screen and then update some others and paint the screen.
- 32:47And it's that brief moment where you've got half an update, which is what causes flicker.
- 32:53So what I've discovered is if you batch all your updates into one write, so you just write standard output the ones rather than several writes, that will reduce the potential flicker quite a bit.
- 33:05Interesting.
- 33:06Of course it would.
- 33:07And I've never really thought about it.
- 33:09When you're doing regular pixel-based graphics programming, OpenGL, DirectX, those types of things, they often set up what's called a double-buffered mode, where you actually draw the screen on a hidden piece and then you swap that to be in one Vsync to be the screen.
- 33:25So you don't see the pieces streaming in as it goes.
- 33:29And this is the same equivalent thing for terminals, huh?
- 33:32It's like double-buffered mode.
- 33:33You write to it like a whole thing and then you make it the screen.
- 33:36It's a very similar concept.
- 33:38The terminal doesn't give you that.
- 33:39It doesn't have an invisible buffer that you can take your time to draw to.
- 33:43So you have to implement that yourself.
- 33:45But yeah, essentially, we're implementing a double-buffer.
- 33:47And the third one has to do with synchronized output.
- 33:50Doing the one write thing, that's very helpful.
- 33:53But there's also a fairly recent addition to the protocol, which helps with this.
- 33:58Basically, you write an escape code which says, I'm beginning an update.
- 34:01And then you write your data.
- 34:03And then you write another escape code which says, I've finished the update.
- 34:07But between those two escape codes, the terminal won't update.
- 34:10It'll only update at the very last moment.
- 34:12So that's kind of accomplishing the same thing as the double-buffer.
- 34:16Yeah, similar.
- 34:17So we do both because not all terminals support these new escape codes.
- 34:23So by doing both, we can ensure that it's flicker-free on newer terminals and older terminals.
- 34:29Yeah, fantastic.
- 34:30Speaking of different terminals, Kim in the audience asks, is application mode that you talked about earlier with the buffer a universal across all the terminals?
- 34:39It is, yeah.
- 34:40It's been around for a long time.
- 34:42If you've used HTOP or anything like it or a full-screen terminal app, that'll use application mode.
- 34:50This portion of Talk Byte Under Me is brought to you by Sentry.
- 34:53You know Sentry as a longtime sponsor of this podcast.
- 34:56They offer great error monitoring software that I've told you about many times.
- 34:59It's even software that we use on our own web apps.
- 35:02But this time, I want to tell you about a fun conference they have coming up.
- 35:07Deploying new code can be a lot like making a really great sandwich, taking a bite, and then having all the contents fall out.
- 35:13Exciting, chaotic, and maddening.
- 35:16If you know the feeling, then Dex by Sentry might be for you.
- 35:20This is a free conference by developers for developers, where you sort through the madness and look for ways to improve your workflow productivity.
- 35:28Join Sentry for this event in San Francisco or virtually on September 28th and discover new ways to make your life a little easier.
- 35:36Save your seat now at talkpython.fm/Dex.
- 35:40That's talkpython.fm/D-E-X.
- 35:43The link is in your show notes.
- 35:45Thank you to Sentry for supporting Talk Python To Me.
- 35:48Final question while we're on this performance thing.
- 35:52When will we see Doom implemented?
- 35:54People keep asking that.
- 35:57I don't know.
- 35:58It might happen.
- 35:59I think it has to happen.
- 36:00I mean, Doom runs virtually everywhere.
- 36:03It does.
- 36:04Maybe the first Doom.
- 36:06Maybe not Doom Eternal.
- 36:07Yeah, yeah.
- 36:07We're thinking the original Doom that really was amazing.
- 36:11Right after Castle Wolfenstein.
- 36:12Very good stuff.
- 36:13Yeah, I love that.
- 36:14All right.
- 36:14So that was all some really interesting stuff about terminals.
- 36:18But we also have some other recommendations and discoveries that are pretty awesome, even if you're doing other types of programming.
- 36:25And one of them is Dict Views.
- 36:27Apparently, I used Dict Views a lot, but I didn't really know they had a special name.
- 36:31Tell us what are Dict Views and then why are they useful?
- 36:34So Dict Views are the object returned from a dictionary when you call items or keys.
- 36:41And I think almost all Python developers have used this, but we tend to use them to iterate over to get the keys and the items.
- 36:48What you might not know is these are special objects.
- 36:51They're not just simple iterables.
- 36:52And they act a lot like sets, which kind of makes sense.
- 36:56Because if you've got a dictionary, the keys are all unique.
- 36:59So you can't have a repeating key.
- 37:01So if you've got a key view, it's set-like, which means you can do set-like operations on it.
- 37:07Right.
- 37:08You know, ask the quick question, like, is this thing I'm about to add in here or not?
- 37:12Something like that, right?
- 37:13Yeah.
- 37:14Yeah.
- 37:14And you can take two different, you can take a set and a key view and combine them and see the intersection and see what keys are common in both or neither.
- 37:25And those type of operations turned out to be very useful for what I was using it for.
- 37:31And to be honest, I think this might be the first time I've ever used that in anger.
- 37:35It's one of the things that you read about in the notes for the latest Python.
- 37:39And you think, I can't imagine I'll ever use that.
- 37:42And I didn't for years, but then I had a problem where in Textual, when we do a new update via CSS, we've got two different data structures.
- 37:51One with the old positions of things and then one with the new positions of things.
- 37:55What we want to do is compare those two data structures and find the things which have moved or have changed size.
- 38:02And I started writing code for this and it wasn't straightforward code.
- 38:05It was a couple of hours in.
- 38:07I had written a lot of code and I think, that's a bit too slow.
- 38:09It's a bit too awkward.
- 38:11And only then did I remember the fact that you could use these key views as set operations.
- 38:17And the code turned out to be almost a one-liner.
- 38:20And, you know, I was so happy about this.
- 38:23I forgot the fact I'd spent two hours writing code that I had just deleted.
- 38:28Let me give people an example here who are listening.
- 38:32So if you check out the blog post, there's a nice code example.
- 38:34And, you know, the UI of Textual is made up of all these widgets.
- 38:38They've got names like header, footer and sidebar.
- 38:41And they have boundaries and regions, rectangles.
- 38:44And as the UI changes, you might only want to redraw the delta, not the whole thing for performance reason, right?
- 38:50And so given the old view and the new view, the question is, well, what widgets have changed?
- 38:56And so you just say render map dot items, carrot or symmetric difference with the other newer frame dot items.
- 39:04And that tells you these are the pieces that have to be redrawn, right?
- 39:08Yeah, that gets exactly the information needed.
- 39:09That tells me which things are new, which things have been removed, and then which things have changed position.
- 39:15And those three types of things are the things that I need to redraw on the screen.
- 39:20And, you know, it's a one-liner.
- 39:22And I get that information for free because it is very fast.
- 39:26It runs at the C level and produces just information that I needed.
- 39:30Honestly, this surprises me that this is possible.
- 39:34I would totally expect that with dot keys because keys have to be set like.
- 39:38But items, you could have the same item assigned to different keys all over the place and stuff.
- 39:43So it's a little interesting here.
- 39:44The item is, you know, obviously the key plus the value.
- 39:48So together, they're unique.
- 39:50The values are not unique.
- 39:51Oh, I see.
- 39:52That's right.
- 39:52Because that's a tuple of the key.
- 39:54I see.
- 39:55Got it.
- 39:55Okay.
- 39:55So the keys are guaranteed to be unique.
- 39:58That makes sense.
- 39:58That's therefore, yeah, whatever you add to it.
- 40:01It's not going to make it less unique by adding stuff.
- 40:03More possible differences.
- 40:05All right.
- 40:06So not totally related to this, but I got to ask it because it's just so meta.
- 40:10Andrew out in the audience asked, can you embed a terminal within a textual app?
- 40:14I can't.
- 40:15I've been asked this a few times.
- 40:16I'm just, I'm wondering what people want to use this for.
- 40:18But in theory, it should be possible.
- 40:21It means I'd have to write a software layer which interpreted all the escape codes and then
- 40:26translated that to like a region of the textual app.
- 40:30But in theory, yes, it's possible.
- 40:32Will we do it?
- 40:33Maybe.
- 40:34One day.
- 40:34It's not the highest on the priority, but maybe.
- 40:38Yeah, maybe.
- 40:39Okay.
- 40:39Very interesting.
- 40:40Is there something like a textual.contrib?
- 40:43You know, these extras that people put in, like there's Django.contrib and there's other
- 40:48contribs.
- 40:49Yeah.
- 40:49Not currently, but I'm really hoping that the community takes it and starts building things.
- 40:54So you could search PyPy for textual underscore and get lots of widget libraries and various
- 41:00add-ons.
- 41:00Is there like a plugin or extension aspect?
- 41:05Anything official like that?
- 41:07Yes.
- 41:07So the widgets are designed to be built that way.
- 41:10Widgets are like independent portions of the screen that can handle events and things and
- 41:16updates.
- 41:16And those are bundled up into separate libraries, third-party libraries.
- 41:21So that would be the easiest way to implement something.
- 41:23You could implement anything you wish, like a full IDE if you wanted to and just import
- 41:29it as a Python library.
- 41:30Sure.
- 41:31Related to using dictvues for speed, it's really hard to beat caching for speed, isn't
- 41:37it?
- 41:37Yeah.
- 41:38Caching is awesome and it's one of the things which allows textual to be fast.
- 41:43So LRU cache, I think a lot of people have used it, but maybe not appreciated how fast
- 41:48it is.
- 41:49Caching and generating the key is done at the C level.
- 41:51So it's super fast and you can use it for quite small functions.
- 41:55We've got a lot of calculations which are pretty quick.
- 41:59You know, they're well under milliseconds, like fractions of a millisecond.
- 42:03But we do them a lot of times.
- 42:05So we might do them like 10,000 times.
- 42:07But if we introduce the LRU cache, the time it takes to do those function calls becomes time
- 42:14it takes to essentially do a dictionary lookup.
- 42:16Then they become very fast.
- 42:18So that can, that led to quite surprising speed ups.
- 42:22Yeah.
- 42:22So you have an example here that you talk about where you're given a couple of regions
- 42:29regions and you do the intersection of those two, right?
- 42:32A region being like a rectangle like thing.
- 42:35Yeah.
- 42:36So you've got to figure out, well, which is the top left most and like all those, there's
- 42:40a bunch of comparisons to find the overlapping rectangle if it exists.
- 42:45Right.
- 42:46And then, yeah.
- 42:47It's not very complicated.
- 42:48It's not very slow.
- 42:49It's just doing sort of arithmetic and it's working on local variables.
- 42:52That's generally the fastest kind of code you can expect from Python.
- 42:56You also, you create a region object which has to reserve some memory.
- 43:01But if you put the LRU cache on it, that becomes a dick lookup.
- 43:05So it becomes very fast.
- 43:06And the type of calculations we're doing, we often do the same ones many, many times.
- 43:13So we'll use the same two values to find intersection.
- 43:15Like that the first time you update the screen, you calculate some intersections.
- 43:20And the second time, maybe one or two items has moved.
- 43:22But most of the same calculations are done again.
- 43:26So LRU cache ensures that you don't do those calculations again.
- 43:30You just pick them out of a dictionary.
- 43:31And yeah, big wins.
- 43:33And it's so easy to just copy that one line and everything works.
- 43:37Yeah.
- 43:37Also, it's kind of related to what we're going to get to here.
- 43:40But this kind of code is fantastic because in terms of caching, because it doesn't really
- 43:46depend on something that could change behind the scenes.
- 43:49It doesn't have hold of like weird pointers that other, you know, could change in other ways.
- 43:55And so it's very, very deterministic.
- 43:58It's going to give you the same answer every time.
- 44:00And, you know, it's not like the cache is going to get weirdly stale, right?
- 44:05Yeah, exactly.
- 44:06So it's an immutable object.
- 44:08I think region is actually a named tuple.
- 44:11And you get all those benefits.
- 44:13There's no side effects.
- 44:14You can write these functions that have got an input and an output.
- 44:17And it doesn't depend on any other state.
- 44:20And when you have that kind of function, I think they call it a pure function.
- 44:24It's caching works beautifully.
- 44:26There's no hidden surprises.
- 44:28And also, I think, makes your code more easy to reason about.
- 44:32You can trace it, you know, just manually.
- 44:35You can tell what's going on.
- 44:37Just by looking at one function, you can see the full story.
- 44:41Sure.
- 44:42Yeah.
- 44:43Immutable objects.
- 44:44If your code can use immutable objects, I think you should favor it.
- 44:48It doesn't always make sense.
- 44:49But immutable is definitely best.
- 44:51Yeah.
- 44:52Definitely easy to reason about.
- 44:53Kim in the audience says, presumably, the memory cost of caching many frequently called functions isn't a big issue on a reasonable machine.
- 45:02And maybe it's worth pointing out the max size parameter you passed to the LRU cache.
- 45:06That's right.
- 45:07So you set a maximum size.
- 45:09And if you add more items than that, it'll throw out the oldest one.
- 45:13So you can define the set, the maximum size that you will record.
- 45:18And it depends on how you use it.
- 45:20If you tend to have like a common set of calculations, which you're doing repeatedly, and those will kind of be in the cache most of the time.
- 45:30And then you might have some calculations which happen infrequently, you know, combinations of input and output.
- 45:35And then those get recalculated occasionally.
- 45:38But it's still a big win.
- 45:40And for 4,096 items for that, the regions name tuple is quite small.
- 45:46So that keeps the memory usage to something reasonable.
- 45:50I did want to highlight this project called Async Cache.
- 45:55Because I think, I haven't checked lately, but I think the LRU cache is only for synchronous functions.
- 46:02I think so, yeah.
- 46:03Yeah, I think so too.
- 46:04You know, maybe it's been upgraded.
- 46:06I haven't checked.
- 46:07But if it's still true, there's this project called Async Cache, which has nearly the same UI, but applies to Async functions.
- 46:18So basically, the decorator has to return a function, which is first checks the cache and then calls the function.
- 46:24For the Async version, that has to be an Async function returned out of the decorator.
- 46:29Hence the difference, right?
- 46:31And so this Async Cache has something really similar where you can have an LRU cache where you set the max size.
- 46:37Or you can also have a time to live, like I only want these results to stick around for 60 seconds.
- 46:43It also has a bunch of other interesting features that you can bring in, like works on ORM objects, request objects, a bunch of other things.
- 46:51It has sort of custom support for custom types that are one of the things that this needs here.
- 46:58I think it's hashable.
- 47:00I think you have to have hashable arguments for LRU cache.
- 47:04Yeah, that strikes me as something very useful.
- 47:06If you've got a calculation which does some of weights and you want to cache it, then yeah, that looks like a fantastic project.
- 47:13Not super popular.
- 47:14People can check it out, but it looks pretty useful.
- 47:16Okay, we kind of touched on this already, so maybe we won't go too much into it, but just one of the actual lessons you said is immutability is definitely a good thing.
- 47:26So we can get this from tuples, named tuples, which are like better tuples.
- 47:31You know, you can address them by a variable type or frozen data classes.
- 47:36I tend to think immutable first.
- 47:39So I'll prefer to make my objects immutable.
- 47:42And only when I think that's going to become a hindrance do I make them mutable.
- 47:46And that's, I think, I started doing that a few years ago, and I think that's benefited my code.
- 47:51So I'd recommend mutable objects.
- 47:53Yeah.
- 47:54And then if you're out there doing Pydantic, which many of us are these days, it has faux immutability, which is kind of like it.
- 48:02It takes a shot at making things immutable.
- 48:05And you can just say allow mutation false as the config for your Pydantic model, which is pretty fantastic.
- 48:12Yeah.
- 48:12I think Python doesn't support true immutability.
- 48:14Immutability.
- 48:16If you really wanted to, you could do some horrible hack to change an object, which should be immutable.
- 48:22But, you know, like Python's philosophy is we're all adults here, right?
- 48:25So don't do crazy things like that.
- 48:28And even for languages that have like the word const and stuff, it still doesn't necessarily mean what you think it means.
- 48:35There's a little bit of Monty Python going on.
- 48:38No, Princess Bride sort of thing going on there where, okay, so this object says it can't be mutable and it has pointers to other objects.
- 48:46And so sure, you can't reassign those pointers, but that thing points to something which points to something, which internally you call a function which changes.
- 48:52You know what I mean?
- 48:53Like to get true immutability is super hard and super restrictive.
- 48:58Super hard, yeah.
- 48:59Yeah, yeah.
- 48:59So Python isn't too bad in that respect.
- 49:02It's definitely safer than C, C++, et cetera.
- 49:06Yes, that's for sure.
- 49:07Okay, immutability is good.
- 49:09So is Unicode art.
- 49:12What the heck is this?
- 49:13In the textual code, there's quite a lot of functions like this, which are kind of geometric.
- 49:18So it might divide something into two.
- 49:20Yeah, it's always doing visual things, right?
- 49:23Quite a lot of visual things.
- 49:24So here we've got an example.
- 49:25It's a function which takes a region and splits it into four by making two, or making a horizontal and a vertical cut.
- 49:32And that's quite hard to express in English succinctly.
- 49:36I mean, you can do it, but it'll take you like a big paragraph.
- 49:39But if you create something like this, this kind of Unicode art using various box characters, you can draw a diagram to show what you're doing.
- 49:50And if you're coming to that code later, that just makes it really obvious at a glance what it's doing.
- 49:57Yeah, it's fantastic.
- 49:58I'm a big fan of that kind of Unicode art.
- 50:01And I use an application called Monodraw, which is macOS only.
- 50:05But there are similar applications for other platforms.
- 50:08And yeah, I use it wherever I can.
- 50:11Also because it's kind of fun.
- 50:13It is cool.
- 50:15A powerful ASCII art editor designed for the Mac.
- 50:18It costs money, $10.
- 50:21I love that it has educational pricing, but it only costs $10.
- 50:24That's kind of cool.
- 50:28But yeah, it's pretty neat.
- 50:30I guess you have little arrows and boxes.
- 50:34And just like you might draw with PowerPoint or Keynote or something like that is kind of your, let me put together a visual graphic.
- 50:42But then output is ASCII art.
- 50:44Yeah.
- 50:44And it's kind of like a vector graphic type thing.
- 50:47So if you draw a box, you can click on the box and drag it.
- 50:50It's not like a bitmap.
- 50:52Oh, that's fantastic.
- 50:53Yeah, it's quite powerful.
- 50:55I hope the authors made a fortune out of it, frankly, because I love it.
- 51:00So what is the output like?
- 51:01You output to clipboard?
- 51:02Yeah, you can cut and paste it.
- 51:05You output to clipboard.
- 51:06You can write it to a text file.
- 51:08I usually cut and paste it directly into Python code.
- 51:11Yeah, of course.
- 51:12So that's really cool.
- 51:13It also made me think of something that's not exactly the same purpose, but Balsamic.
- 51:18I don't know if you're familiar with Balsamic, which is really great for developing UIs.
- 51:22I don't know if they have a gallery or something.
- 51:24They need some more graphics on this thing here.
- 51:27Yeah, I've used Balsamic for a while.
- 51:29It's been around for a long time, hasn't it?
- 51:31It's been around forever.
- 51:32Yeah, but it basically lets you create UIs that look almost like as if they were created
- 51:37using ASCII art.
- 51:39Not quite, but it's an interesting goal.
- 51:42I did also want to highlight the most useful piece of ASCII art.
- 51:47And to be clear, all of this discussion is what goes in the comments, right?
- 51:51What goes in the code comments or the doc strings?
- 51:53The one for, what is it, for the object allocator in Python.
- 51:58So if you look in obmalloc.c in CPython, there's all this fairly intense looking code for allocating
- 52:08memory when it's particular CPython objects, like a py object or a py long or whatever, right?
- 52:16If you scroll down to the section here around line 777, it has a paragraph.
- 52:22It's an object allocator for Python.
- 52:24Here's an introduction to the layers of Python's memory architecture, etc.
- 52:28And instead of having an essay in here, it has this incredible graphic that is like vertically aligned and shows you,
- 52:36here's where we allocate things like integers and lists.
- 52:39And here's Python core non-object memory allocation.
- 52:43Here's how it relates down to the OS and to actual hardware.
- 52:46What do you think of this?
- 52:47That's pretty awesome.
- 52:48A lot of respect to the author because I'm fairly sure they didn't use monodraw.
- 52:52I think they did that by hand with typing the characters and spaces and everything.
- 52:57Yeah, it's pretty awesome.
- 52:59Yeah, I think this is really fantastic.
- 53:00I was trying to piece together, you know, I did a course on Python memory and I was trying to piece together like, well, how do I visually represent how it is described that the memory works?
- 53:10And I'm like, well, let me go look in the source code.
- 53:12And I was looking at this like, there's an actual picture in here.
- 53:15This is amazing.
- 53:16I can't believe it.
- 53:17So, so neat.
- 53:18Yeah.
- 53:18So anyway, if people need a concrete example of the type of stuff you're talking about, here's one that's pretty close to home.
- 53:24We all use it every day and we didn't know.
- 53:26Fractions.
- 53:27I was always amazed at the fact that 0.1 plus 0.1 plus 0.1 is not equal to 0.3.
- 53:32I learned that in the school and it was, but for some reason it's not.
- 53:35What do you think?
- 53:36Yeah, so this is a problem that goes, this is, it's not just Python.
- 53:39It's pretty much any language which uses floats and doubles, which is like almost all of them.
- 53:44And it trips up beginners and experienced developers.
- 53:48It tripped up me when I was working on Textual.
- 53:50We have lots of code which will divide the screen into various proportions.
- 53:54So you might have a third and then a two thirds and you might divide the height into sevenths.
- 53:59And what I found was that when I used floats, I got a lot of rounding error.
- 54:03Not, actually not a lot of rounding error, but occasional rounding error, which means it wouldn't draw a line or it wouldn't draw a column.
- 54:09Because this kind of issue where the rounding error is compounded and it's been rounded down to the nearest character.
- 54:17Right.
- 54:17Because you've got to, you've got to talk in like little block pieces anyway.
- 54:20And if it just misses it, right?
- 54:22If it just misses it by 0.0000001, then, then you'll be a whole character out.
- 54:28And I did come up with like various ways of solving this where I used integers rather than floats and kept track of the error, which I think is the standard approach of doing it.
- 54:38But it was quite tricky code and I get it wrong too frequently.
- 54:42But then I remembered fractions.
- 54:44So fractions is, they behave just like numbers, but they start at life as a numerator and a denominator.
- 54:51So now fraction 1, 2 equals a half.
- 54:54And the great thing about fractions in the standard library is they don't suffer from that rounding error.
- 54:59So if you have, you know, three, one tenths, it'll add up to three tenths exactly with no rounding error.
- 55:06And it makes that kind of code, which I do a lot of in textual, to be much simpler.
- 55:11So I was very happy when I re-remembered it because I must have known years ago and thought, why do I need fractions?
- 55:18Yeah, exactly.
- 55:19Yeah.
- 55:20This is not elementary school.
- 55:22I don't need this.
- 55:23Come on.
- 55:23We could just say 0.1.
- 55:25Yeah.
- 55:26So you learn that you think, well, that's probably useful to mathematicians or something or someone else, not to me.
- 55:32But if you work long enough, you'll eventually find a problem where that is actually the perfect solution for it.
- 55:38I didn't know about these either.
- 55:40I see comments in the audience as well, like, fractions, what is this madness?
- 55:43So you're telling us that you can only work in rational numbers, not, you know, irrational numbers like pi and E.
- 55:51We can't have columns that are E-wide.
- 55:54No, no, you can't.
- 55:56Or pi high.
- 55:58It's pi by E.
- 56:01No, it's cut off.
- 56:02Darn it.
- 56:03Yeah.
- 56:03The cool thing is that fractions, you can drop in replacements to float.
- 56:08So if you've written some code which was previously using floats and then you pass in fractions, everything will work as normal, except you'll not get the rounding error.
- 56:17It's really kind of beautiful.
- 56:18Okay.
- 56:19So you're saying the fraction library supports things like division, multiplication, additions, and basically would duck typing behave the same?
- 56:27Yeah.
- 56:27It's duck typing as a rational number.
- 56:29So anywhere we use a float, fractional work.
- 56:32Okay.
- 56:33I learned about that.
- 56:34Amazing.
- 56:35Let's see.
- 56:35Emojis are hard.
- 56:37You talked a little bit about things that take up different sizes and Unicode and whatnot.
- 56:41Yeah.
- 56:42Yeah.
- 56:42So when I started Textalize and I got my first employee, I thought this is the problem that I want to tackle because it has been bugging me for two whole years.
- 56:53And the problem is basically this, that some characters will take up two cells in the terminal, they'll double wide, and some characters will take up one cell.
- 57:01And if you want to do any kind of formatting, say to draw a line or a panel around it, you need to know exactly how many cells a piece of text will take up.
- 57:10And it sounds like a simple problem.
- 57:12All you need to do is know how many cells a character will take up.
- 57:16So you might have a function which takes a character and returns either one or two.
- 57:20And in essence, that's what Rich does.
- 57:23But at some characters, you can't know because they will render differently on different terminals.
- 57:30They'll render differently on iTerm and Windows and Linux.
- 57:34And it gets even more complicated.
- 57:36You can get multiple characters, so multiple code points in the characters.
- 57:41So you might have something like a flag.
- 57:44And a flag has a two-letter character code and another character code which tells you this is a flag.
- 57:49So if you iterate over that, you get three code points.
- 57:52So you have to know, first of all, how everything works together.
- 57:56And there's quite a lot of those type of characters.
- 57:58I can't imagine how tricky it is to be done inside Unicode.
- 58:01Yeah.
- 58:02Yeah.
- 58:02It's crazy.
- 58:05It's very, very complex.
- 58:06And, you know, we thought, well, we'll just do it.
- 58:09We'll just apply some engineering effort and just do it.
- 58:12But we discovered that it was impossible to know because you can't tell how the terminal is going to render these Unicode.
- 58:18It might do it correctly.
- 58:19It might actually render a single character.
- 58:21If the terminal is not aware of multi-code point emojis, then you might get three characters.
- 58:27They might render as nothing or they might render as three double width characters.
- 58:33They might not even render properly.
- 58:35So they might overlap following character.
- 58:37Some number of boxes.
- 58:39Yeah.
- 58:40It's just every terminal did it differently.
- 58:42Not only the terminal, it was platform dependent.
- 58:45So if the terminal used the Unicode database on the operating system, then you'd get different results if it shipped its own copy of the Unicode database.
- 58:54And it just turned out it was impossible to know how many characters.
- 58:58So what do you do?
- 59:00Well, there's a subset up to about Unicode 9 where things seem to be mostly sane, most terminal support.
- 59:07So if you use those, those are fine.
- 59:09But after that, it becomes unreliable.
- 59:12If you have flags and multi-code point characters, then it might not work.
- 59:18It might cause the alignment of tables and panels to be out.
- 59:23You know, the end character might be shifted out by one.
- 59:25And it just comes up so frequently.
- 59:27And I would solve it if I could.
- 59:30But as far as I can tell, there is no reasonable solution which will work across all platforms.
- 59:36Too many unknowns, right?
- 59:39Too many unknowns, yeah.
- 59:40It's a crazy thing, but it kind of makes sense because these Unicode characters came out in the last few years.
- 59:46And operating systems and terminals haven't caught up and they haven't agreed on how to render them.
- 59:52So it's a frustrating situation.
- 59:55I'm sure that it is.
- 59:56Yeah, I mean, if you look at like some of the nerd fonts, for example, when I, you know, at nerdfonts.com, you know, you look at some of these, what is possible.
- 1:00:06I think here, you know, you have these like colored arrows.
- 1:00:10And do they have, I don't know, if you go over to ohmyposh, which I don't really have time to talk about.
- 1:00:16But if you look at the themes that are in here, the different themes that you can pick and just some of the effects, like a character that looks like a git branch with an arrow in it.
- 1:00:27Like, how is that?
- 1:00:29How are you supposed to decide how big that is?
- 1:00:31Or, you know, how is some of this stuff accomplished?
- 1:00:34It's just...
- 1:00:34I don't know how they represent those characters.
- 1:00:37I wonder if they reuse existing characters.
- 1:00:41When I saw that, I'm like, okay, this is...
- 1:00:43I don't know how this is possible, right?
- 1:00:45Rounded edges and all sorts of stuff.
- 1:00:47So I'm like trying to find one where it's like a sparkly fade from one character to the next.
- 1:00:52It's these things over in the OhMyPosh.dev themes.
- 1:00:56If I thought about your job to figure out, like, what is that supposed to look like?
- 1:01:00I don't know.
- 1:01:00I would just give up.
- 1:01:01Because these are...
- 1:01:03I mean, they're really impressive and really useful.
- 1:01:05But this one, for example, the cert theme, where it's got like little dots that fade from one to the next.
- 1:01:11I just...
- 1:01:12I just don't know.
- 1:01:12Amazing, but...
- 1:01:14Those look pretty cool.
- 1:01:14How are you supposed to know, right?
- 1:01:16Right.
- 1:01:16So those are your lessons.
- 1:01:19Very, very cool stuff.
- 1:01:21I really appreciate you coming on to talk about the seven lessons.
- 1:01:24You know, terminals are fast.
- 1:01:26Dictive views are amazing.
- 1:01:27LRU cache is fast.
- 1:01:29Immutable is good.
- 1:01:30Unicode R is good.
- 1:01:31Fractions are good.
- 1:01:32Emoji is bad.
- 1:01:33Does that summarize it?
- 1:01:36That about...
- 1:01:37Yeah, in a nutshell.
- 1:01:38Oh, let's wrap it up with one more thing real quick here.
- 1:01:41Let me see if I can find it.
- 1:01:43This one?
- 1:01:44There we go.
- 1:01:44So one of the...
- 1:01:46Sort of bring it full circle back around.
- 1:01:48One of the cool things to make terminals nicer that you've talked about recently is rich-cli.
- 1:01:55You want to close out the show?
- 1:01:56Just give us...
- 1:01:57Tell us what rich-cli is.
- 1:01:58Sure.
- 1:01:58Okay.
- 1:01:59So as you know, rich is a library.
- 1:02:00And rich-cli is a CLI interface for that library.
- 1:02:04So most of what rich can do is exposed by rich-cli.
- 1:02:09So you concat most file formats, and it'll be nicely syntax highlighted.
- 1:02:13You'll have line numbers and guidelines and all sorts of things.
- 1:02:17And you can do things like panels.
- 1:02:20You can format text.
- 1:02:22What else?
- 1:02:23There's a whole bunch of other features.
- 1:02:25Yeah.
- 1:02:26So for example, if I had a JSON document on the terminal, I could type...
- 1:02:31I could open it in some terminal-based editor, you know, think SSH somewhere.
- 1:02:35Or I could just type more or cat the name of it, and it would print out just plain text of whatever's on the inside.
- 1:02:42Or now I could type rich, the file name, and I get, you know, highlighted, colorized, formatted content for like CSV and JSON and all those kinds of things, right?
- 1:02:53Yeah, yeah.
- 1:02:53So the rich, the JSON example is quite good because that will decode the JSON.
- 1:02:58So if you've got like compressed JSON with no white space, it makes it impossible to read.
- 1:03:03Rich will decode it, and it'll also format it.
- 1:03:07I see.
- 1:03:08Like pretty prints it.
- 1:03:09Pretty prints it, exactly.
- 1:03:10Yeah.
- 1:03:10And it'll do markdown.
- 1:03:12It'll do a reasonable job of rendering markdown.
- 1:03:15And it'll take CSV files, and it'll turn those into nice rich tables.
- 1:03:21And if your output is quite large, you can add hyphen, hyphen, pager.
- 1:03:24And that'll put in a nice pager where you can scroll up and down with scroll bars and do page up, page down, et cetera.
- 1:03:30So it's kind of like a toolbox for fancy, rich formatting of all sorts of different data types.
- 1:03:36Definitely a cool project.
- 1:03:38And I know you're concerned about emojis, but Paul in the audience says, fortunately, Doom does not require emojis.
- 1:03:44So it's still on the table.
- 1:03:45I suppose not, yeah.
- 1:03:49That's right.
- 1:03:50All right.
- 1:03:51Well, fantastic lessons.
- 1:03:53Thanks for sharing all of your experience.
- 1:03:55Final two questions before you get out of here.
- 1:03:57If you're going to write some Python code, work on rich, what editor are you using these days?
- 1:04:01It's not so meta that you're using Textual to write Textual yet, is it?
- 1:04:04No.
- 1:04:05Maybe one day.
- 1:04:06But no, I use VS Code.
- 1:04:07And I quite like it.
- 1:04:09I'm comfortable with it.
- 1:04:10My colleague uses, what's the editor by JetBrains?
- 1:04:14PyCharm.
- 1:04:14PyCharm.
- 1:04:16And he's very proficient at PyCharm.
- 1:04:17And to be honest with you, I am jealous of some of the features of PyCharm.
- 1:04:20It does some really cool things.
- 1:04:22So I'm kind of tempted to try PyCharm.
- 1:04:25Looking over the shoulder.
- 1:04:26Awesome.
- 1:04:27Yeah.
- 1:04:27Very cool.
- 1:04:28And then notable PyPI package.
- 1:04:30I mean, we've touched on some good ones that start or end with Rich.
- 1:04:34But anything else you run across that you're like, oh, this is fantastic.
- 1:04:37People should check this out.
- 1:04:38Oh, gosh.
- 1:04:39There's so many.
- 1:04:40I'm drawing a blank.
- 1:04:42I should have heard one in advance.
- 1:04:44How about one that you use that makes Rich work well or something?
- 1:04:48Well, there's Prompt Toolkit.
- 1:04:51So I owe Prompt Toolkit a big debt of gratitude.
- 1:04:55Because when I was figuring out the textual stuff, I looked at the Prompt Toolkit source
- 1:04:59code, which is a great thing about open source that you can look at other people's code.
- 1:05:03And it is very good.
- 1:05:04It helped me understand things.
- 1:05:06And it still does things which Textual doesn't do yet.
- 1:05:09So I think Prompt Toolkit is an excellent project.
- 1:05:11And if you haven't used it, you should definitely check it out.
- 1:05:13Yeah.
- 1:05:13Prompt Toolkit's interesting.
- 1:05:14And you'll be typing along.
- 1:05:15And all of a sudden, there's like a combo drop-down box, like a select right in the middle
- 1:05:21of your terminal.
- 1:05:22And then you just carry on.
- 1:05:23Yeah.
- 1:05:23It makes things like, I think it's used in IPython.
- 1:05:25It makes that much nicer and much more friendly.
- 1:05:28Absolutely.
- 1:05:29All right.
- 1:05:30Well, final call to action.
- 1:05:32People want to do stuff with Rich Textual, maybe take some of these lessons and run with
- 1:05:37them.
- 1:05:37What do you tell them, Will?
- 1:05:38Yeah.
- 1:05:38Check out the website.
- 1:05:40Check out my Twitter profile.
- 1:05:42And if you have any questions, feel free to ping them over to me.
- 1:05:44I'm always happy to talk to people about Python-related things.
- 1:05:48Yeah.
- 1:05:48Fantastic.
- 1:05:49And of course, I'll link to the article with your list of topics there so people can check
- 1:05:54that out.
- 1:05:54Thank you so much for being here.
- 1:05:55It's been great to catch up with you.
- 1:05:57Yeah.
- 1:05:57Thank you.
- 1:05:57You bet.
- 1:05:58Take care.
- 1:05:58Bye.
- 1:05:58Bye-bye.
- 1:05:58This has been another episode of Talk Python To Me.
- 1:06:02Thank you to our sponsors.
- 1:06:04Be sure to check out what they're offering.
- 1:06:06It really helps support the show.
- 1:06:07Starting a business is hard.
- 1:06:10Microsoft for Startups, Founders Hub provides all founders at any stage with free resources
- 1:06:15and connections to solve startup challenges.
- 1:06:18Apply for free today at talkpython.fm/founders hub.
- 1:06:22Join Sentry at their conference, Dex, Sort the Madness, the conference for every developer
- 1:06:28to join as they investigate the movement and trends for better and more reliable developer
- 1:06:32experiences.
- 1:06:33Save your seat now at talkpython.fm/Dex.
- 1:06:37Want to level up your Python?
- 1:06:39We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:06:42Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:06:48And best of all, there's not a subscription in sight.
- 1:06:50Check it out for yourself at training.talkpython.fm.
- 1:06:53Be sure to subscribe to the show.
- 1:06:55Open your favorite podcast app and search for Python.
- 1:06:58We should be right at the top.
- 1:06:59You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:07:04and the direct RSS feed at /rss on talkpython.fm.
- 1:07:09We're live streaming most of our recordings these days.
- 1:07:12If you want to be part of the show and have your comments featured on the air,
- 1:07:15be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:07:20This is your host, Michael Kennedy.
- 1:07:22Thanks so much for listening.
- 1:07:23I really appreciate it.
- 1:07:24Now get out there and write some Python code.
- 1:07:26I really appreciate it.
- 1:07:47Thank you.