- Podcasts
- Python Bytes
- #295 Flutter + Python GUI Apps?
About this episodeTopics covered in this episode: Faster routing for Flask & Quart Quarto: an open-source scientific and technical publishing system built…
- Faster routing for Flask & Quart
- Quarto: an open-source scientific and technical publishing system built on Pandoc
- Fl e t UI
- Building an authenticated Python CLI
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#295 Flutter + Python GUI Apps?
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is episode 295, recorded August 4th, 2022. And I am Brian Okken. I'm Michael Kennedy. It's good to have you back. Good to be back. For people out there listening, we kind of batched some stuff up so that we have vacation for a couple of weeks.
I feel like Flask and Cort are working much closer together these days. So I don't know exactly what the relationship is, but Flask and Cort are very closely tied together right now. Yeah. What I want to talk about is routing. Or for those UK friends, routing, if you prefer. But the idea of taking a URL and figuring out what function to call.
Right. So what you do is you set up in Flask, you say at app.get, app.post, and you give it URL pattern. Sometimes that's just like slash about. Other times it's like slash categories, bracket category name, or even more general stuff. Like where you might say it's category, maybe a category ID, and it has to be an integer.
So convert that for me. Or I want to just capture arbitrary path, arbitrary URLs slash edit, whatever that happens to be. So that's the idea of routing. And the whole article here, something the big news is that Philip Jones has worked on Werkzeug, the HTTP router that is the foundation of doing the routing in Flask and Cort.
And it's something like five times faster than it was before. I think five times the number. Wow. Much faster. So for very small little toy apps, if you have a couple of routes, whatever, it's no big deal. However, if you've got a production app like, say, TalkBiton training, where you've got hundreds, at least hundreds of routes, when a URL comes in, you can spend a decent amount of time checking.
Is it get or is it post? Is it this URL and actually this thing here? Can we convert it to an integer? Because if not, that's a 404. It's a different route. And you're like all the stuff that goes on there. Yeah. So the way that it worked previously is it just, when you would specify these routes like app.get slash API and API slash angle bracket ID, those types of things, it would just come up with a table that just says, okay, here's the get verb.
Here's the path. And here's the function that it goes to. And it was using regular expressions. So slash API or slash API and then backslash D plus, right? The regular expression for a number. Okay. And the way it works is it just says, okay, we're going to run the verb test, the regex test, all those things, one at a time, top to bottom.
Well, Brian, I know you studied a lot of computer stuff. This is not something that's good that grows with time, right? As you add more of these, it's complexity. What is it? Something like O of N or maybe a little bit O of N squared, maybe? I'm not sure. Something like that where you're, cause you're testing the verbs and you're testing the things, right?
So as you get these larger and larger, you're like running through them every single request. And the world is full of interesting data structures and algorithms that you might consider. So the idea was this is going to get rewritten into something that's not just the sort of brute force test at top to bottom in the order that it's defined here.
And so I think one thing, it's interesting, the news that routing and flask and court is five times faster. That's fantastic. But also just thinking about the algorithms. I think it's a cool problem solver, problem solving thing to go look at, right? An example. So Philip thought about different ways in which you might do this and how it works.
So the first algorithm that he looked at was a radix tree. And this is an interesting tree structure that gets defined where instead of having a table, you have all the verbs. And then under each verb, you've got the API, the path pattern. And one of the things that's interesting here is they have a path type.
Otherwise, you'll go to the next part of the tree and cruise through it. What do you think of that? That looks cool, right? Yeah. And also, I mean, yeah, it looks good and also faster because you don't look at everything. Right. Exactly. You say, is it a get or post? Boom. You've like, you're down to like one segment.
Then you're like, well, what's this next path? Then you really quickly cruise through the various possibilities. And this looks really great until you get down to this wild card thing. And it turns out with all the variations and whatnot of like the wild card matching and the sub wild card matching.
And it didn't really work that well. So, but one benefit is the performance is now described as O of N, which is pretty good. Better than N squared or something like that. Yeah. Or yeah. And N is the depth now, not the. Right. And it's, that's very important. It's the depth rather than just the number, which is even better.
Awesome. Are you a fan of state machines? Yes, I am. I love them. Yeah. Yeah. State machines are pretty wild. You know, I'm, I'm in the current editing state. Now, what are my options? Where could I go from here? Things like that. So you define the same set of routes, but what you get is a state machine that has these transitions.
And for example, state one says, if you go to API, we'll go to state two. Or if you do some wildcard slash edit, then the answer is you just do the edit or you just do the true wildcard thing. And then you do some other, other step there. Right. Pretty interesting. And then say for this API, where it says go to state two, state two says, well, if there's nothing else, you've already gone through API.
Yeah. And also like, how is this faster? But exactly. Yes, it doesn't. That's like I said, I would have never thought about it because it, it also doesn't seem faster. Yeah. However, you get to the benchmarking section and it says, I think by having 20 routes here or something, it came out to be quite a bit faster.
Let's see. Boom, boom, boom, boom. Ratio of this one says 50% better. I said five times and maybe it's not that much faster, but some way I know there's a five. There's got to be. Yeah. Here we go. A factor up to five times speed increase. And the more routes you have, the faster, the bigger the increase is.
So pretty cool. If you're using Flask Record, be sure to use the latest version because the version that's coming up with this, is this going to make it a lot faster for you? And just an interesting example of how you might have a non-obvious solution to a problem like a state machine for finding the URL matches.
Yeah. Yeah. Brandon out there on and says, I agree. I don't see all this as faster. I hear you. Yeah. But the cool thing about computers is you push the button and then it does a thing and then you know. Right. Well, it's not like you've got to have a theory and then you debate the theory and it's measurable.
One of the interesting things around this also is that you can't assume much for Flask or Court because there are frameworks that other people build up websites with. So some people are going to have big forest trees that have lots of branching and everything for their routes. And some people are going to have like, oh, let's just throw half the stuff in one directory or one bucket or something like that.
Well, speaking of court, we've got court. Oh, or corto. And actually it's funny. I have no idea if this is built on court or not. Probably not. But I don't know. So corto, this was, oh, somebody suggested it. Paul McKenzie. This is a, this is a thing to build documents and stuff, but it's, it's open source.
And it's, it's, it's, they say open source scientific and technical publishing system built on Pandoc. So we love Pandoc. I, at least I do. It's, it converts Markdown to really anything else or rest to other stuff. Like a whole bunch of stuff. You can convert things to, to like PDFs or even eBooks and HTML documents, all sorts of things.
So this is, and then Jupyter, of course, Jupyter's great for a lot of scientific Python research and data science, and even just learning Python and playing and stuff. And, and I've kind of liked to see lately some people doing presentations even with, with just right within Jupyter notebooks, just kind of fun.
And I know people are teaching that way with tutorials, but anyway, so Quarto is a system where you can do, you can have documents be either Markdown documents or Jupyter notebooks and have a combination of these things around and then build up stuff. So you can, so, you know, you've got like a Jupyter notebook and a demo and some, some Markdown and stuff, and then you can convert the whole thing to a website or a, a journal entry or, you know, a publication ready for a journal or a website or an ebook or really anything.
So this is pretty exciting. I think it's, I think it's very neat. The idea that you can take a notebook, put a little extra metadata into it, and then publish it to all these different sources. Have you seen how much you can do? You know, this is based on Pandoc. Have you seen how much you can do with Pandoc?
Yeah. Yeah. So when you say, okay, well, if I could take my notebook and then power it through Pandoc to do these things, like the output possibilities are insane. Yeah. and it even does, like one of the things that was unexpected, for me is the presentation. So you can convert one of these to, to like, PowerPoint.
yeah, even to PowerPoint or, I was excited about reveal JS. I like reveal. but, and then Beamer, I don't know what Beamer is, but. I've never heard of Beamer either. it's going to be our new favorite way to present. Oh, I see. You can create Beamer LaTeX. I see. So it's Beamer maybe is a little more like scientific mathematical where you have to, you know, here's the integral of this or like where you've got really specific.
And check this out. You've got, line height line highlighting that goes, incremental. So you could have, stages. You can, instead of step creating three slides, for instance, that have just slightly different highlighted texts. You can say what order you want things highlighted in as you step through them.
So, I'm going to try this for a presentation. I might try this as well. This is pretty neat. Actually. I was excited also about the eBooks feature. you can even publish EPUB. I was talking to Matt, Matt Harrison about this and Matt pointed out that he'd seen this, but he was, if you really care about like indexing or the front matter or the back matter, this doesn't quite get there, for generating that stuff.
And whenever I used to see this, I was like, but do Moby also? Cause I want to be able to read it on my Kindle. Yes, exactly. But, I don't have the link here, but, Kindle Amazon is doing a conversion this year. So, right now the mail to the last time I sent a Moby document to my Kindle through the email feature, it emailed me back and said, we did this, but, EPUB is preferred now.
Yes. I should do that. But, so for the website stuff, it's kind of fun too. So this will generate websites for you. and, there, and then it has pages. Yeah. It has publishing input, publishing in it too. So you can, you can hook this up to a GitHub action and just say Quattro publish and be using this to publish stuff too.
So, this is really kind of cool. the whole, the whole, the whole infrastructure around, documentation and publishing around scientific computing. So I'm pretty excited about this. Yeah. I love it. That's great. Now, before we move on, Brian. Yes. Another thing I'm excited about is, Microsoft for Microsoft for startups.
it's the Microsoft for startups founders hub. So this episode of Python bites is brought to you by Microsoft for startups. Starting a business is hard, but by some estimates, over 90% of startups will go out of business in the first year. Ouch. With this in mind, Microsoft for startups set out to understand what startups need to be successful and create a digital platform to help overcome those challenges.
Microsoft for startups founders hub. it provides all founders at any stage with free resources to help solve startup challenges. The platform provides technology benefits, access to expert guidance and skilled resources, mentorship, and networking connections, and so much more. Unlike others in the industry, Microsoft for startup founders hub doesn't require startups to be investor backed or third party validated to participate.
Founders hub is truly open to all. So what do you get speed up development with free access to GitHub and Microsoft cloud with the ability to unlock credits over time to help your startup innovate. Founders hub is partnering with innovation, innovative companies like OpenAI, a global leader in AI research and development to provide exclusive benefits and discounts through Microsoft for startups.
Founders hub become becoming a founder is no longer about who, you know, you'll have access to mentors, their mentorship network, giving you access to a pool of hundreds of mentors across a range of disciplines across areas like validation, fundraising, management, and coaching. sales and marketing as well as specific technical stress points.
The link is in your show notes. Awesome. Yeah. Thanks Microsoft for supporting the show. They're, they're big backers of Python bytes and definitely help amplify what we're doing here. I think the most awesome thing is the mentors and the advice and support you get. Yeah. Well, this is one of the things I think is awesome is this.
When I, when I read about this, I think about like the startup, the startup access that people get if they're in like Silicon Valley or walk Y Combinator or something like that. Yeah. Something like that. But this is, but that's, you only get a handful of those a year and this is open to a way, way more people.
So that's cool. Awesome. Yeah. Very cool. All right. Can I take you on a diversion to show you something pretty cool? Yeah. All right. So, Dart is a programming language that we don't usually talk about on Python bytes, right? And Dart is a language that, that came out from Google and I felt like it was trying to compete with JavaScript to a large degree and didn't really gain a lot of traction until Flutter came along and Flutter is a really cool way to design mobile and desktop native applications using Dart, right?
It's think of it as an alternative to Cordova, PhoneGap, Xamarin, Ionic, all these different sort of generic ways to build apps that run on different platforms. So with Flutter, I can build an app that runs on iOS and Android, but I can also compile it as a target to macOS, Linux and Windows, and I can even compile it to a target for the web where to run as a progressive web app.
Okay. And you get some really cool apps. Like the, by far the most well known one is the BMW car. You have a BMW. This is like the app that is your car, but there's, there's other ones as well. It's used a lot within Google, obviously. Right. Now you may be wondering. I bought a car and all I got was an app.
And it's a really cool technology that I, I'm pretty excited about. So let me introduce you to something called Flet. Have you heard of Flet? Well, just because Brandon just mentioned it. Yeah. Tell me about Flet. Yes. Yes. Yes. very timely, Brandon. So Flet comes, was, sent over to us from Mikael Honkala.
It's got a nice little animated, animated GIF showing how it works. And, you know, this looks like a proper calculator app you would see on a mobile phone or something, right? Yeah. It looks like my calculator. Yeah, exactly. You could even go see, interactive version that is running in your browser because one of the six or seven compile targets is your browser.
And I don't know if you noticed, but how quick did that load? Way faster than PyScript or any of these other things. It was like nearly instant. So if you go through and you look at how you build it, you just create a main method in Python and it's provided a page. You say Flet.app and you just give it the function to call.
And here you say, I'm just going to add some text called hello world, right? So you get your hello world here, but you don't want that. You want some controls. So I'm going to add a bunch of elevated buttons with like the buttons that are on the calculator, like one, two, three, star, plus, minus, and so on.
And you end up with this column of that. That's, that's kind of interesting, but you want these in rows and columns. So you would say I'm creating a row, which has some controls for elevated buttons, another row, right? So these are the rows of the calculators. And look at that already, how cool it is to define that UI with just that in Python.
Yeah. Right. It's pretty neat. And because it's Flutter, all of these things have native representations on their platforms, right? In macOS, it looks like a macOS button and Windows looks like a Windows button and so on. You got to put styles to make it look like, you know, the calculator app type of thing.
So yeah, that's pretty much it. You just go and you put all these controls together like this and you say go. And then somewhere in here, there's a place where it talks about handling the input. But yeah, so here you just say, I have a create a class and then on click itself dot button clicked or, or whatever it is, whatever you're interested in.
And what it's going to pass over is the actual button, the elevated button that was clicked, the event source or whatever you call it. Right. So you just say, I'm hooked into these different button click events like you would with any sort of UI reactive framework. And now you have a calculator or you've got a, what other kind of app you want to build?
Isn't that cool? It's very neat. So you can build both. So I think some people would use this for iOS or Android, right? A mobile device. But you said there's other things too, like you can test it out. Would you realistically use this to develop a, like a web app? Would you? I think developing a web app seems like it would be totally reasonable.
The mobile story is not yet finished, but that's what's on the roadmap. And I would love to see it, see it come along and make good progress. There's also the possibility of other languages. That's not super interesting to me. But because I want to write Python. Anyway, but you have like Go and C# and stuff as possible other programming languages.
Yeah. But things like having a built-in database with a simple ORM, it sounds way more, it's way more powerful than it sounds. Because if you're in the web, well, how do you do database stuff? You know, the web has local storage and it has like a SQL, a wimpy SQL thing that's embedded in like offline storage for your app.
Nice. I don't know how ready this is for producing actual finished applications. Flutter is absolutely ready to go, right? It's been around for many years and there's lots of things being put out in production for it. Flutter on top of Flutter. Don't totally know. I'll ask for later, next week, and we'll know a little bit more.
Thank you, Mikhail, for sending that over. So that's a UI thing. I'd like to switch gears to the command line. So I was... To the TUI. To the Klee? The TUI, the text user interface. Oh, yeah. Text user interface. Yeah. Anyway. So like with rich and textual and things like that. So I was really excited about this article, actually.
And you need to store the Twitter token somewhere. You're going to do that OAuth exchange where you say, we're going to connect to Twitter. And Twitter says, this app is going to interact with your account this way and whatnot, right? Right. So I want to be able to just... But I'd like to be able to have the application keep that around and not have to do that.
Not really build it into the app. I don't want to compile it into the app or copy that. Token there. I want to be able to put that somewhere else. So the idea around this article is to take that, use the Twitter API. They talk about using click and rich a little bit, but for the command line stuff and click is cool.
And we both love rich. And anyway, so the idea is to use a... Once you have... So use the OAuth and you come back and you've got a bear... What they call it? A bearer token. A bearer token. And then saving that in a file called a netRC file. And instead of telling somebody to just go put it there, they're reading, like asking the user for the client ID and the secret from the Twitter API website stuff.
So copy it and paste it here. And then they're storing that bearer token in the netRC file. And then the next time the application runs, it just reads that. And you don't have to do it every time. And then that stuff isn't stored with the client code, but it's stored within the user netRC file or something.
So at first I thought this was something that you could use for, like, okay, so I don't know whether or not this is a good idea for that even, whether you want to store your bearer stuff. But you probably don't want to ask the user for username and password and store that there because it's just a text file, I think.
But maybe there's some other way around. I was kind of hoping that... I'd rather lose an OAuth token than I would my actual username and password because at least you can revoke the tokens or expire them and stuff, you know? Okay. So for token stuff, for saving tokens, is this a reasonable thing to do to keep that in the user's directory or something?
Yeah. So actually, so I wanted to start this conversation and then ask people because either for token... I know there's other password ways to, like, store them locally safely, but is it something that... Can you do that with... Anyway, I'd love to hear people's thoughts on this, on where, like, what's the best way to store people's secret information so they don't have to enter it every time?
Yeah. But, so storing in their user directory might not be terrible, but maybe there's a better way. So it's probably operating system specific too, but I don't know. Right. Well, your user profile is protected in general, right, from other users. Right. But is it protected if you run an app that decides it's just going to go cruising through your user profile?
You know, something that you ran and you shouldn't have trusted it, but you did. I mean, SSH token, SSH keys, private keys are there, right, in the .SSH folder. Yeah, that's true. Yeah, that's probably worse than losing an OAuth token as well. So if you can guess, I'm thinking of, I'm in the design process for a command line application that has to store user credentials.
Yeah. Cool. But yeah, I would probably at least encrypt the token. I don't know. Okay, thanks. Maybe it doesn't matter. Yeah. Well. Awesome. They're done with our normal items. So the extra stuff that I had that I was going to let you go first, but I'm super excited. I'm getting a whole bunch done on the PyTist course, man.
Really quick before you go on, just Sam Morley out there says, that's why you should encrypt your secret keys. And system tool chain is probably the correct, in quotes, answer. Okay, system tool chain. Okay. I don't even know what that means, but I'll look it up. Yes, this is something to go taggy or DuckDuckGo or maybe Google.
Okay. I'm excited to hear about this PyTist course, because people have been asking, and progress is being significantly made, right? Awesome. How's it going? Yeah. Yeah, it's going great. So I've got, it's really seven chapters. It's broken up into seven sections or chapters, but it's a video course.
But I'm really excited about it, because the PyTist book has received a lot of people of it, which is great. I love that. I got great feedback. But it's detailed, right? So we jump into a whole bunch of the details. And with a book, you can kind of get to some section that you're like, yeah, I don't need that right now.
And you can skip over it. And yes, you can do that with a course, but it's a little harder. So the goal for this is really for people new to PyTist or that are just using a little piece of it and unfamiliar with some of the powers, is to introduce people to the full power of PyTist, but quickly and not overwhelm them.
So brand new course over at Talk Python training, Django getting started, which is awesome. This is put together by Chris Trudeau. And this is a six hour course on Django. And right now people hurry, hurry. There's an early bird discount to save 10% that you can get as well. So that just came out Wednesday.
Wednesday is yesterday. So it came out yesterday, I think. That or the day before, very recently. Anyway, if you've been wanting to do getting started, if you want to get into Django, this gives our Django course is really good. If you've been dabbling, you're like, I really need to see how the pieces fit together better.
Also, check it out. It's definitely good stuff. Django is one of the top two web frameworks these days still on Python. Yeah. Do you know how many courses you've got on the platform now? 43 courses and about, that platform tells me, and 233 hours of content. So there's more than that in there, but a couple are not published yet.
No. Put me on the spot. Yeah, exactly. I don't either. But the little kid that was supposed to be, the kid lion who was supposed to be being groomed to be the king, is talking to his father and they're looking over the vast kingdom. This is Luxembourg. Every language that light touches has a garbage collector.
And Symbol looks and goes, but what's that shadowy place over there? That's a C++. You must never go there. Yes. Sorry, C++, but yeah, that was fun. And Mufasa. Mufasa, yes. Thank you. Yes, you got it. Mufasa says, that's a C++. You must never go there. No, but you should. Don't take it too seriously.
It's a joke, but it was fun. As a garbage collector. Yeah, I know. What's that shadowy place over there? You must never go there. What about Rust? What about Rust? Nice. I don't even know enough about Rust to make this part of the joke. Yeah. I'm busy learning Dart and Flutter. Maybe I don't have to learn Dart though, because I can do it in Python.
