Latest / Talk Python To Me / #452: Top Quart (async Flask) Extensions
Transcript
- 0:00Have you heard of Quart? It's the fully async version of Flask created by Philip Jones,
- 0:05who's working closely with the Flask team on these parallel projects. The TLDR version is
- 0:10that if you want to take advantage of async and await and you're using Flask, you want to give
- 0:15Quart a solid look. We've spoken to Philip previously about Quart. This time around,
- 0:20he's here to share his top Quart extensions and libraries that you can adopt today.
- 0:25This is Talk Python To Me, episode 452, recorded January 10th, 2024.
- 0:31Welcome to Talk Python To Me, a weekly podcast on Python. This is your host, Michael Kennedy.
- 0:50Follow me on Mastodon, where I'm @mkennedy and follow the podcast using @talkpython.
- 0:55both on fosstodon.org. Keep up with the show and listen to over seven years of past episodes
- 1:01at talkpython.fm. We've started streaming most of our episodes live on YouTube. Subscribe to our
- 1:07YouTube channel over at talkpython.fm/youtube to get notified about upcoming shows and be part of
- 1:13that episode. This episode is sponsored by Posit Connect from the makers of Shiny. Publish, share,
- 1:19and deploy all of your data projects that you're creating using Python. Streamlit, Dash, Shiny,
- 1:25OKFastAPI, Flask, Quattro, Reports, Dashboards, and APIs. Posit Connect supports all of them. Try
- 1:32Posit Connect for free by going to talkpython.fm/Posit, P-O-S-I-T. And it's also brought to you
- 1:39by us over at Talk Python Training. Did you know that we have over 250 hours of Python courses? Yeah,
- 1:47that's right. Check them out at talkpython.fm/courses. Philip, welcome back to Talk Python.
- 1:52Tommy. Great to have you here. Thank you. Thank you for the invite. We're going to talk about web
- 1:55things. I love some web things. We're going to talk about async. I love async. And we're going to talk
- 2:01about a lot of little cool things you can plug into your web apps that make them more awesome.
- 2:06And we give a lot of people a lot of things to think about. So yeah, let's do it. So I guess
- 2:13quickly before we get into talking about Quart, maybe a little catch up on where it's been since the
- 2:18last time we spoke about that. Give folks a bit of background on yourself who don't know you.
- 2:23Sure. Yeah. I guess probably in this context and maybe the people listening are probably known
- 2:29for Quart and Hypercorn and helping maintain the pallets libraries like Flask and Virksuk.
- 2:35But well, outside of that, I guess in my professional career, I work as a software engineer in London.
- 2:41I work for a company called Curaleaf International. And quite interestingly, because it's not very well
- 2:47known, we're one of the few companies that do medical cannabis in the UK. Not many people know
- 2:52it's legal. Yeah. Put that aside. So I've worked there for about a year and a bit now. Before that,
- 2:58worked at various other software firms. And then before that, I was actually a particle physicist
- 3:02working on an experiment out in Canada in a deep mine, which was quite a good fund.
- 3:05So you like your variety of your jobs, I can tell.
- 3:09Yeah. Had a definite career shift.
- 3:10Indeed. Just a funny sidebar, you know, cannabis was legalized here three or four years ago in Oregon.
- 3:17There are more cannabis shops than there are Starbucks, than there are McDonald's. Like
- 3:23I could walk to four from my house in five minutes. It's nuts. Anyway, it's very different than it was a
- 3:30few years ago, for sure. It's all fine, but just different. All right. So court, like Flask,
- 3:35kind of, but very interesting. It's got some other capabilities. Tell people about court. How's it
- 3:41relate to Flask? There's a lot of crossover. You and David Lord are working pretty closely,
- 3:46it seems like, on things these days.
- 3:48I started court about, I guess, seven, maybe a bit more years ago now. And the original idea was
- 3:54make Flask async. At the time, I couldn't figure out how to adapt Flask at all in that direction. So I
- 4:00thought, okay, I'll start by just re-implementing the Flask API using async await. And that's what
- 4:05court started out to be. Over the years, David and I have worked to try and merge the two together.
- 4:11So the very latest change that's happened a few months ago is we actually based court on Flask
- 4:17now. So there's a lot of shared code now. And for a long time, court was based on VirgSug. And if
- 4:23you're familiar with the palettes and Flask ecosystem, Flask is built on VirgSug. And court was,
- 4:28but now court is built on Flask, which is built on VirgSug. And they're very, very similar.
- 4:31Yeah. Oh, that's fantastic. Because the more that you can share,
- 4:34like the more you all can just focus on features and making it better rather than duplicating that
- 4:40effort.
- 4:40The other aim is that the APIs are the same as they can be other than async await. So you
- 4:45can take your Flask code base and move to court just by adding async await. That's the dream,
- 4:49even closer than it's ever been to that now.
- 4:51You're pretty close to be able to rename the word Flask, keeping the case sensitivity
- 4:56intact from Flask to court. And it works, right?
- 4:59Yeah. In simple projects, yeah. The more complicated ones, it comes down to the extensions,
- 5:03which we're going to talk about.
- 5:05Yeah. That's the whole topic of today, right?
- 5:08Yes, exactly. But yeah, that's the aim. Our long-term aim is to actually merge the two
- 5:13and be able to say async Flask. Well, it's kind of the case now where you can say async Flask is court
- 5:18and we can't quite do what Django has done and put async Django as it is in Django. Ours,
- 5:24you have to make a naming choice from the start. But yeah, I think it's getting as close as we can get.
- 5:29Maybe we'll figure out how to merge them in the future.
- 5:31Yeah. It'd be great if you could choose to use async def or just regular functions for your views
- 5:38and not really think about it.
- 5:39You can't do that.
- 5:40Okay.
- 5:40You've been able to do that for about two years, I think, trying to figure out when we merge that.
- 5:44So you could use either. It's just not as performant. If your code base is going to be
- 5:48mostly async, you probably want to switch to court fully because it'd be more performant
- 5:52and more reliable and robust.
- 5:54Sure. Why is that?
- 5:55The Flask implementation will run your async code on a separate thread.
- 5:59So you get all that overhead of switching between the threads, sending the information over and back,
- 6:04et cetera.
- 6:04Right. Does it run like an asyncio event loop on the other thread, but then
- 6:09basically curry the results back and forth?
- 6:12Yep.
- 6:13And with court, it's running right on top of an ASGI server straight, right?
- 6:18Yeah.
- 6:19Yeah. That's super cool. And you might be responsible for one of the more prominent ones out there.
- 6:24Tell people about, well, what is an ASGI server for people who don't know?
- 6:28Why does it exist? And then, you know, the one that you got.
- 6:30Yeah, sure. So historically there was the whiskey standard, which separated servers from
- 6:35frameworks applications and kind of gave you a choice between how do you want to serve
- 6:40and deal with concurrency and stuff? And then how do you want your API to build your apps,
- 6:44the framework to be? I think it was a great choice because it made the ecosystem really,
- 6:48really nice for developers. And ASCII is the same thing for async. And so court is a,
- 6:54is an example of an ASCII framework and Hypercon is an ASGI server.
- 6:58They originally together, much like Sanic started out as well, but then I split that out Hypercon
- 7:04into its own thing. And then later on now, Hypercon is also a wsgi server. So yeah,
- 7:08in some respects, Hypercon is just a general purpose server for Python.
- 7:12And so Hypercon, it lives in the same general usage base as Gunicorn and MicroWSGI UWSGI,
- 7:22right. So I could run it in production in a kind of like a overseer mode where you can fan out
- 7:27process, worker processes and system daemon, system D, all that, that, that where it's at.
- 7:32Yeah, it's exactly like that. Yeah. Yeah. And it also in the ASCII space, it's similar to Uvicorn or
- 7:38Daphne. That's it. Right.
- 7:39I think there's another, but I've forgotten the name just now.
- 7:42Yeah. I was just messing with some Docker stuff and I was running the web app, a FastAPI app,
- 7:48NGunicorn using uvicorn workers. So handling the ASGI stuff that way, is it better to use Hypercorn?
- 7:57Is it rather than something along those lines, a little more direct maybe, or what do you think?
- 8:02I think it's a bit of a choice. So how it differs from uv, well, Uvicorn in general is quicker because
- 8:06it's HTTP parser. HP1 parser is quicker. And there's a few other choices that tend to make it a bit
- 8:12quicker. However, this last time I checked, Uvicorn doesn't support as many extensions as Hypercorn and
- 8:19it doesn't support HTTP2 or HTTP3. Now, most people don't actually serve that directly. They tend to have
- 8:23a load balancer in front, but I tend to do that because it's quite exciting. So you're getting there
- 8:28with Hypercorn. And then after that, it's a bit of a choice between, you know, what features do they
- 8:32both have? What do you want to use? So they're fairly similar, I would say.
- 8:35Sure. Okay. Somewhat. Up to you, what do you want to choose? But for people who have mental models,
- 8:40maybe using uvicorn, they can check out Hypercorn, right?
- 8:43Yeah. Yeah. So you wouldn't need Gunicorn because Hypercorn has the process model that Gunicorn adds to
- 8:48uvicorn. And yeah, you can just switch over. It should be a one-liner because most of the time it's just
- 8:54that one command line where you set this up, right? So yeah.
- 8:57Yeah. Very exciting. I'm going to give it a solid look because I'm deep in that world right now,
- 9:02playing with things, which is very exciting, but not the topic for today. So the topic for today is
- 9:07people who are interested in doing Flask and Quart, especially Quart, what are ways you can do more,
- 9:15right? So the frameworks are pretty simple, right? That's of all the different APIs out there,
- 9:20the Flask API has definitely won. You know, even though there's many different web frameworks,
- 9:26right? If you look at the way programming works, you look at FastAPI, you look at a Litestar,
- 9:33many of these things, it's like you've got to create a thing called app and then you say app.get and so
- 9:38on, right? So this style of programming is really popular. And I think it's because it's just
- 9:43straightforward and simple. Opposite of Django, maybe. Not so many batteries included, but you can
- 9:48add the batteries through extensions. Yeah.
- 9:50Yeah, exactly. I think that's always been the trade-off between Django and Flask and perhaps
- 9:54even Django and Quart if you were to go Quart is Django makes the choice of your batteries to a large
- 9:59extent, whereas Quart and Flask means you have to choose basically. Yeah.
- 10:02Yeah. Which is really nice if you got like, you know, like I would really rather use this graph
- 10:07database for this thing. And I would really rather, you know, like you get a lot of choice, but you
- 10:11have to, you're going to have to build a little bit more yourself and you've got to decide more
- 10:15yourself. So, but with all these extensions, it starts to become easier to click the Lego blocks
- 10:20back together. Yeah.
- 10:21Yeah, exactly. Yeah.
- 10:22All right. So if you go, people listening, go to court.palettesproject.com and go in there and find
- 10:30the extensions. There's a great long list of extensions that we've got here that they can
- 10:35choose to run, right? And how to do that and so on. But you picked out a couple that you really want
- 10:40to highlight and we'll dive into them. And if there's extra time, maybe we can, you know, pick some
- 10:45more out of the list and just see what we can make of them.
- 10:48I think the best one to start with is the bit of discussion around an extension called
- 10:52court flask patch, which is a bit of a odd name and maybe need to think of a better name
- 10:57for it. But the idea behind this extension is it patches court at import time to look very
- 11:04much like flask. So you can use some flask extensions directly. So this is again, the idea
- 11:10to make it easier. If you are so willing to transfer from flask to court, this would allow you to
- 11:15an easier step because you could add this one line and court flask patch and some of your flask
- 11:21extensions would just work. Now you've changed the court.
- 11:24That's awesome because there's a bunch out there. So what happens when I run this? Does it basically
- 11:30say like, okay, there's a bunch of module namespaces called court this court that let's create proxies
- 11:36called flask that just delegate into the court elements and expose that back. So code written
- 11:41against flask dot whatever think it still exists. Yeah, exactly that. Yeah, it does slightly more because it
- 11:47what it also needs to do is because a lot of the court APIs are async. It needs to wrap them in a wrapper
- 11:53that allows it to be called synchronously, but it needs to do so in a nested sense. And I don't know whether
- 11:59you've looked much into like nesting event loops in one another that you're getting back to the same event loop.
- 12:05It's a bit of a, I think it's fair maybe to call it a bit of a hack, but it does work well enough to give you to allow you to buy that time to develop on. But that's what it has to do. Yeah.
- 12:17Yeah. I really wish that Python had an ambient event loop. That was the default. This like,
- 12:23get me an event loop. Oh, it doesn't exist in exception. Oh, create an event loop. Sorry, exception. It already
- 12:29exists. Like, you know, there's like, it's just really tricky to figure out kind of deep down in
- 12:34some library, like, well, am I in charge of this? Is it already going? And I got to be part of something
- 12:38bigger. If you could just say, just async run this, you know, and just you do that. You need to make an
- 12:43event loop, make it. If you don't need to make one, just put it in the one that's already there,
- 12:47right? Like I really wish there was a little bit cleaner story there, but you know, we're not going to
- 12:52solve that with court extensions. No, no. I mean, if they had have done that, it would have made making
- 12:56Flask async a lot easier. You've probably come across the library. I think it's Greenback. Greenback. No, this is new.
- 13:03So Greenback is a green like stack switcher based library that allows you to get back to the event loop when you're deep in some
- 13:14sync code, which is quite nice in the sense that, you know, it allows you to do all the stuff that's
- 13:19difficult, but it's not so nice in that it's kind of hidden in a way, monkey patching type stuff going on
- 13:25in the background. Yeah. Yeah. Yeah. I think this is what SQLAlchemy uses to get their async await
- 13:31supporting. So it's certainly, yeah, I guess popular. Yeah. Sure. Reenter asyncio or trio event loops from
- 13:38synchronous code. Very nice. Cool. Okay. So Flask patch, that's pretty good if you just have some
- 13:45Flask code and you're like, oh, why don't you switch to court? But hopefully nothing, nothing changes and
- 13:50I'll just run this at the beginning and everything will work. How often does that, how often does it
- 13:54work out to be easy like that? So it depends on what the extension does. So if the extension does its own
- 14:01IO or calls certain Flask functions that we can't, this extension can't patch, then yeah, it just fails.
- 14:08And yeah, so not all of them. If you go into tests, you can see an example of some of them that we test
- 14:14against or I test against, sorry, just to make sure they work. Like login potentially? Yeah. So login is
- 14:19probably the most popular, I think. So I don't think it works for SQLAlchemy, for example. There's a
- 14:25technical example, anywhere that uses app context as a, in a contact manager in a synchronous location,
- 14:32basically that's too hard to patch. So they tend to all fail, but yeah, a few do. And I hope it makes
- 14:37it easier for people. I'm pretty sure it is used because I get bug reports. So some people find it
- 14:41useful at least. Yeah, absolutely. It looks, looks excellent, right? So once you, once you do all the,
- 14:48the magic, you can just work with the login manager and off it goes, right? Yeah, exactly that. Yeah.
- 14:52Super cool. This portion of Talk Python To Me is brought to you by Posit, the makers of Shiny,
- 14:58formerly RStudio, and especially Shiny for Python. Let me ask you a question. Are you building awesome
- 15:05things? Of course you are. You're a developer or data scientist. That's what we do. And you should
- 15:10check out Posit Connect. Posit Connect is a way for you to publish, share, and deploy all the data
- 15:16products that you're building using Python. People ask me the same question all the time. Michael,
- 15:21I have some cool data science project or notebook that I built. How do I share it with my users,
- 15:26stakeholders, teammates? Do I need to learn FastAPI or Flask or maybe Vue or React.js? Hold on now.
- 15:34Those are cool technologies, and I'm sure you'd benefit from them, but maybe stay focused on the
- 15:38data project. Let Posit Connect handle that side of things. With Posit Connect, you can rapidly and
- 15:43securely deploy the things you build in Python. Streamlit, Dash, Shiny, Bokeh, FastAPI, Flask,
- 15:49Quarto, ports, dashboards, and APIs. Posit Connect supports all of them. And Posit Connect comes with
- 15:56all the bells and whistles to satisfy IT and other enterprise requirements. Make deployment the easiest
- 16:02step in your workflow with Posit Connect. For a limited time, you can try Posit Connect for free
- 16:07for three months by going to talkpython.fm/Posit. That's talkpython.fm/P-O-S-I-T. The link is in
- 16:15your podcast player show notes. Thank you to the team at Posit for supporting Talk Python.
- 16:21Cores. Everybody loves their cross-site origin scripting restrictions. I guess we do love it in
- 16:29the sense that we're not getting our bank account stolen when we browse the web. But as a web developer,
- 16:35you're like, oh, Cores again. I need to allow this thing to talk to that, but it can be a hassle.
- 16:41And so I'm guessing Cores allows you to control those settings and expose them and so on?
- 16:47Yeah, exactly. I think it's become more prevalent, right? Since the switch to kind of front-end
- 16:52frameworks has been so popular, right? Because you end up serving them, well, typically you end up
- 16:56serving them separately. But yeah, it's exactly as you described. You can put decorators on roots,
- 17:02or you can put it on a blueprint or on the entire app, and you just say, this is a Cores policy for
- 17:06everything. Yeah. Very similar to Flask Cores, but different API.
- 17:10So you can put it on even like a blueprint and say, this whole section of code is where the APIs live.
- 17:15Things can call that, but nothing else. That'd be really, that's really nice.
- 17:19Yeah. Okay.
- 17:19So it also has a slight web, because obviously Quart does WebSockets where it's Flask,
- 17:24at least natively can't. But yeah, there's some WebSocket considerations for Quart as well,
- 17:29which mostly come around to checking the origin header. But yeah, it does that as well.
- 17:32Okay. So you have a route, and you just put the at route underscore Cores on it, or like you said,
- 17:38a blueprint or on the whole app or however you want. But, and then what kind of arguments does it take?
- 17:43What can I ask it, make it do?
- 17:45Yeah. So I went for the kind of, I guess, functional kind of API for it. So you can put in like the
- 17:50allow headers or what's the other one, the credentials, I forget what it's called now,
- 17:54but they're all listed and all keyword arguments effectively to it. There was in the issues,
- 17:59someone a couple of, maybe last year, put out a new, a suggestion for a new API for Cores libraries
- 18:05that would be more intuitive, but wouldn't match the actual headers that came out. So at some point,
- 18:09I hope to like look into that and offer that as well. But at the moment that you basically say
- 18:13the header and then the values you want. So it's very, you have to really kind of know calls to use it.
- 18:17Okay. It just basically juggles the headers and there's not a lot of abstractions in between.
- 18:22No, no. So I think on the, in the readme, it shows you them all actually. So these are all the headers
- 18:27you can control and it's basically the same name.
- 18:29Sure. So access control, allow origin, access control, allow credentials, expose headers, max age,
- 18:36all this, all these things that you might, you might want to use, right? Yeah. Very cool.
- 18:40Yeah. Okay. Oh, you have types as well. So that's lovely. Are you a fan of types, type hints?
- 18:45Since it came out, I've tried not to write untyped code. I think, especially in a team,
- 18:50I think it makes a huge difference to the understanding of the code. So yeah, I'm a very big fan.
- 18:54Yeah. I also think it makes your editors help you more. You know, if it knows what type that is
- 18:59and you say dot, it'll, is this what you meant? Like, yes, actually, I didn't know that existed,
- 19:03but that's what I want. Give that to me. Without types, you're kind of like, well, you know,
- 19:07you could object or weird, useless stuff that you can't do anything with. All right. So cores,
- 19:12if you need to do cores, there's, I guess, let me know how to do this, but I think pretty much
- 19:17most of these that exist, there's probably a flask variant. So if you use flask and you're like,
- 19:22oh, I need cores, but I don't use court, just flask-cores instead of court-cores.
- 19:27Yeah. Yeah. I think it's quite well maintained, the flask one. So yeah, the flask cores,
- 19:31it's slightly different API, but the concepts are all the same, obviously. So yeah, not very much.
- 19:35Another one is auth means so many different things. Is it a session type based auth? Is it OAuth? Is it,
- 19:43who knows what, YubiKeys? What are we talking here?
- 19:46Yeah. Session management via a secure cookie. So it's a similar usage, if you're familiar with
- 19:52flask login, but I didn't want to call it court login because it didn't really do anything with
- 19:56the login. It didn't check the password or the username or an MFA or anything to do with login,
- 20:00in my opinion. So I called it auth instead. But yeah, so with this extension, you tell it to log in the
- 20:06user and give it some kind of identifier and it saves that information securely in a cookie for you.
- 20:12And whenever the user comes back and presents the cookie, it then pulls it out and turns it into
- 20:17what is called the current user. So I think it's called the current user in flask login as well. So
- 20:22very similar to that. All right. So you get this auth user back. What is this auth user? It's not like
- 20:27out of your database, right? It has properties. By default, it's just as a method to say whether the user is
- 20:33authenticated and to return their ID. But if you customize it, if you extend it. So what I typically do in my
- 20:40codebases, I extend it and I add methods to return a model from the database, for example.
- 20:46Yeah, very cool. Nice. And, you know, there's a lot of fancy login off stuff, but nice session cookie.
- 20:53You know, it just, this works these days. It still works really well.
- 20:57It does. Yeah. Much simpler than JWTs and less likely to go wrong. I think.
- 21:02It's not exactly related, but I've had a lot of websites now I've given up on passwords, but they
- 21:08don't use something new or they just decided like every time you want to get to the site, you just,
- 21:13we're going to email you a code and you enter it. When did that become the way we're going to log into
- 21:17everything? It's driving me crazy. So some good old username password. We're going to store your ID in a
- 21:22cookie. It warms, warms the heart.
- 21:24I've been playing with the WebOrph and little USB keys and such recently. I quite like it. I think
- 21:32it might be a bit complex still. It's a little tricky to get working, but yeah, I think that's
- 21:37probably the future to be honest with you. The site just sees like, yeah, you got the right
- 21:40hardware thing. Off we go. Peter knows you're there. Something like that, right? Yeah. Nice. Okay. So
- 21:45people need to deal with users. This is not about storing them in the database, encrypting, hashing
- 21:52their passwords or those types of algorithms. It's really just about the web browser exchange that
- 21:57involves setting a cookie when you're logged in and getting it back and make sure it's not tampered with,
- 22:01right? Yeah. Yeah. So what it does is it stores the value, like the here, the number two is the ID
- 22:08for the user. So it stores that in the cookie in plain text, but then cryptographically signs it. So you
- 22:14don't want to store anything sensitive in the cookie if you're worried about it being read,
- 22:18but you can store identifiers, et cetera, and know that it hasn't been tampered with.
- 22:22Yeah. That's the thing. It's not going to harm you to know that your user ID is two or 75117 or
- 22:29whatever. But if it's two was to stop somebody from going, let me change that to a three and see what
- 22:35happens. Oh, look, I'm an admin. How cool is that? Right. That's the problem that you're trying to
- 22:41solve with secure, right? Exactly. Yeah.
- 22:43Well, from my side, I baked that thing in myself, but having an extension that just does it,
- 22:47that sounds better. Yeah. So the nice thing, well, the bit I took a bit of interest, I think it's
- 22:52changed now with Flask Login, but a couple of the areas where I wanted to improve is it does same site
- 22:57settings on the cookie by default. So it will start with the strict and then you can play around and
- 23:02go down to lax if you so need. But it also has the special, it allows the special host, you know,
- 23:09the double underscore host prefixes on the name and stuff like that. So although I don't think in
- 23:14reality that adds that much to your security, but it's a kind of a safety net in some respects.
- 23:19Yeah. Sometimes also it becomes a hassle when you're doing development because your host is local host
- 23:24and you're saying it has to be on mysite.com. You're like, well, I can't log in anymore. So then you've
- 23:29got to have local hosts. And then I don't know, it doesn't feel super hamper resistant to me either.
- 23:34Are you familiar with SecurePy? This is an interesting project too.
- 23:39Oh, it's been archived.
- 23:40Oh, it's archived. Oh, sadly. Is there a new one? No, but I think it still works. I'm not sure what
- 23:46been deprecated. Okay. Well, I guess that's not being used as well, that are used that much,
- 23:51but it used to be a way to kind of set all those different things, I believe. Or is this different?
- 23:55This is a totally different.
- 23:56Yeah. I think this is, I thought you were going to say secure headers, I think. Is it called that?
- 24:00Oh, I think it's just called secure what it is. Yeah. That's the one. So sorry. That was some
- 24:05other random thing that we've done that was called SecurePy, but not, this thing is still alive and
- 24:10supports court, which is why I brought it up amongst like lots of other frameworks. But yeah,
- 24:16you were going to tell people about this. This is cool.
- 24:17Yeah. Yeah. So I pretty much end up adding all of these by hand. So yeah, most of the headers you want
- 24:24to add. And if you ever do a pen test, right, they're going to, if you don't have these headers by
- 24:28default, there you go, that's going to be a finding straight away. So yeah, it just adds
- 24:31all the ones you want really.
- 24:33Right. For one of the ones that you might not think of that you should probably have,
- 24:37unless you have, unless you have a really specific use case is what if somebody created a site,
- 24:42you know, you had service.com and they had service.io or, you know, whatever. And they,
- 24:48they bought that and then they could put your, whatever their URL is, your, just into your site
- 24:54as a hundred percent width and height iframe. Right. And then they just capture all the keystrokes
- 24:59like, huh, you're typing into your thing. It looks like your thing. Your data is there.
- 25:04You don't want people to do that to your site or just randomly host your site and their site. So
- 25:08for example, this secure thing will set X frame options to same origin. So you can't embed your site
- 25:14can no longer be embedded by default. You've got to choose to let it be embedded. All those kinds of
- 25:18things, right. Are really nice here. Actually got caught out by that many, many years ago. I built
- 25:24a, an image photo editor in the browser. It started to get quite a lot of juice. I was like,
- 25:29oh, this is great. And then I realized it was on somebody else's domain, basically in an iframe.
- 25:33They're like, look at our amazing editor that we're getting Google AdWord money for or whatever.
- 25:38Right. Yeah, exactly. Yeah. So a little bit of a header action will shut that stuff down,
- 25:44but I mean, I didn't know to do it until I kind of dug into it more. Right. Or you find out your
- 25:48site somewhere else. Yeah. I think it's like a lot of it seems to be, yeah, learned with experience,
- 25:54I guess, isn't it? Or on the job. It only happens once when you see your web app in somebody else's
- 25:58site embedded as an iframe, right? No, this is not going to be okay. Awesome. All right. So that was
- 26:04court auth as well as the secure thing. Next, I guess, kind of related to security, maybe not exclusively,
- 26:12but somewhat rate limiting. Yeah. Cause I mean, on a login route,
- 26:15you'd want to put a rate limiter on straight away to stop any kind of brute forcing anyway. Right.
- 26:19So. Oh, absolutely. And the ability to do really nice rate limiting in ways that punish the rate
- 26:27limit breaker is so much easier on async things like court, right? Because if you don't want to be
- 26:33overwhelmed by them, a lot of times you just say you send them some kind of response code. I don't
- 26:38know what the status code is like too many requests or something like some 400.
- 26:41429.
- 26:42429.
- 26:42Yeah, there you go. So you can just send them back straight away, but there's no
- 26:45cost for them to receive that 429. You know, they're just like super quick going. If you wanted to say
- 26:52like, because maybe they're trying to guess passwords or something, you're like, let's not let them guess
- 26:56so quickly. So you can like sleep and then respond to them in 10 seconds later. So it's like,
- 27:02oh, well, I don't know. Is the password good or not? Why don't you wait around for a while
- 27:06instead of going somewhere else? With a asyncio based website, that's await asyncio.sleep,
- 27:11nearly zero cost to you, other maybe an open socket. And you can just like send them to sort of time
- 27:17black holes, super easy. Whereas like a WSGI app, you're blocking a request and you can only handle
- 27:22so many and all that kind of stuff. Right?
- 27:24Yeah. So this one sadly doesn't do any of that fun.
- 27:27It doesn't have punishment built into it. No, but tell us how it works, what it does.
- 27:33I was going to say, I think there was an article today suggesting you should serve zip bonds or
- 27:36something to like unpleasant requesters, but yeah, it doesn't do any of that.
- 27:40Send them like a hundred gigs back.
- 27:42Yeah. That would be good. That would be good actually.
- 27:45Yeah. So this one implements rate limiting as it's defined, like a user would be allowed to do so
- 27:51many requests in a certain time and also implements the RFC for the like headers that you're supposed to
- 27:58send back when they over, when they go over the rate limits. So they know when they can, or when the client
- 28:03can then request again. I see like you've made too many, rather than just saying too many requests,
- 28:08try again later. You're like, and you'll be reset in five minutes or like there's a proper way to communicate that.
- 28:13The thing I find really interesting about this, which I don't know if everyone does, but I'll say anyway, but
- 28:18most people implement rate limiting like a leaky bucket or something like that. These are the standard algorithms.
- 28:24And typically that requires you to store two variables per key per rate limit you're looking at.
- 28:30But there's a, I think it's not a very well known algorithm called the generic cell rate algorithm. I
- 28:35think that's right. And this allows you to store one algorithm at one variable per rate limit. And it's
- 28:42really quite nice. The algorithm is very, when you, when you get it, it's really intuitive and clean. You
- 28:47basically, what you store is when you expect the next request to arrive. And if the request arrives too early,
- 28:53then you just rate limit it. And yeah, it's really clever, I think. So I'd like more people to know
- 28:57about it basically, because it's a really nice solution.
- 29:00It's a great solution. Yeah. So this, it doesn't slow them down. It just tells them that they've
- 29:05gone over the limit in this particular one, right? It's not like if you expect it to be in five seconds,
- 29:10you don't just sleep for four and then let it in.
- 29:13No, no. So the idea is that this decorator is much less costly in terms of your compute than
- 29:19your function, your handler. So this decorator will just send back a 429 very quickly and never
- 29:24touch your root instead. Okay. Interesting. I'm starting to think about maybe if you have
- 29:29a sink and a wait available and you, your algorithm told you when the next one was coming, you could just
- 29:34sleep them until the next one's allowed potentially, if it wasn't 10 minutes, right?
- 29:39That would be the client. I mean, it depends on your rate limits, but there's a good chance
- 29:42the client would time out, right? So, right. It'd have to be like five seconds or, or something
- 29:47real short, right? Like I don't want more than one request a second type of rate limit, not like
- 29:51five per hour that then they would definitely time out. But yeah, there's a lot of, a lot of things you
- 29:55can do with this. So basically the way it works for people listening is you can set it up on the app.
- 30:02So the entire app itself, all the requests to it are limited, right? Or you can go and put kind of like
- 30:08tenacity, you can put the decorator exactly on a particular endpoint and then set the details for
- 30:14that one, right? Yep. So a bit like cause, you can do it per app, per blueprint or per route,
- 30:18depending on what you need. Okay. And here you say rate limit one, and then a time delta. What's the one?
- 30:23It's the count. So you might want, so for a rate of one per second, you might want to express that as
- 30:2910 per 10 seconds. So it would be instead of the one, the 10, and that allows you a bit of bursting,
- 30:34if you will. So, you know, that 10 can come in one second or it can be spread over. So yeah,
- 30:39it allows you to express the rate limits in different ways, really.
- 30:42Yeah. I think this probably bypasses the static files. Is that true?
- 30:47If you do the default limits, I think that applies to static files as well. I can't remember now,
- 30:52but I think that's what it does.
- 30:53Yeah. Because you might have, you know, six CSS files, 20 images and so on, right? And like
- 30:58one page request might blast those all back super quick.
- 31:01Yeah. Yeah. You certainly want a higher limit.
- 31:03Yeah. But presumably you're using something like Nginx or whatever, or maybe even a CDN. And then
- 31:09all those other things I described don't actually go through the Python app, ideally.
- 31:14Yeah. Yeah. I mean, it does work if you do it for the Python app as well. So up to you, I think.
- 31:18Yeah.
- 31:18Indeed. All right. WT forms. Next up, court WTF. What is this?
- 31:24So this was, I'm tempted to say it's been a bit less popular recently with the front end frameworks
- 31:30and stuff. But when you did template rendering, you'd render the form, then you do the validation
- 31:36in your Python framework for that form and you pass back the errors. And basically it would,
- 31:40the WTF framework would do a lot of that for you. So you could just put in like in your template,
- 31:47an input box and it would do the errors and do the, like you can see here, you could do the CSRF tokens
- 31:53and render all the HTML for the labels and the actual input boxes and get it all hooked up very
- 31:59easily. And that's what it does. Yeah. It's a, it does all that. And the validation when it comes
- 32:03to the backend as well, I think you can just do form dot validate on submit or something like that.
- 32:07Yeah, there we are. And that's it. That's your whole validation. So it saves a lot of effort, really.
- 32:12Yeah, that's pretty handy. And I'm guessing you're getting generally generic HTML out on the client
- 32:18side so you can CSS style it up all you like.
- 32:21Yeah, I think it's, I can't
- 32:23remember exactly. I think WTF might have hooks so you can put styling directly in, but yeah,
- 32:28it's, it's pretty, yeah, pretty generic as I remember it.
- 32:31Yeah. Some frameworks require explicit stuff on say the input box, like form dash control would
- 32:37be bootstrap and things like that. Right. There's probably some way to set a class.
- 32:41Yeah. I think there's a class name argument. I'm trying to, I may have,
- 32:44may have misremembered that. I think it was called class name.
- 32:47Yeah, sure. Nice. Yeah. I haven't used this really, but I've used it kind of like it in
- 32:52framework, other frameworks. In the end, I ended up just going back to writing my own
- 32:56HTML because I felt like I was getting 60% help, but maybe 30% struggle to fight the system. You
- 33:04know what I mean? And then like, all right, you know what? I just know this stuff well enough.
- 33:07I'm just going to do it myself. And, and, but if you don't want to mess with it, you want to some
- 33:11sort of quick development type of thing, or maybe you're not super familiar with HTML. This will
- 33:15definitely help. Right. Absolutely. Yeah. I think you can also write your own HTML instead and just let
- 33:19it do the Python validation part. All right. Long as it lines up. Yeah, sure.
- 33:23Yeah. Yeah. I think it's just the name that matters. Like your input has to have the right name.
- 33:27Yeah. Because otherwise how would it know, right? It couldn't, it couldn't know.
- 33:30Yeah, exactly. The CSRF is quite easy and nice. That just puts a hidden input in with the token in,
- 33:35and it's all taken care of for you. So that can be quite nice.
- 33:38Yeah. Excellent. All right. Schema.
- 33:40So schema.
- 33:41For APIs, right?
- 33:43Yeah. So it's possibly the kind of more, I'll say modern, but I'm not sure if that's the word I mean,
- 33:48but yeah. So we, as an industry, we seem to have moved away from the forms of old and form data to
- 33:53send in JSON all the time. Right. And a court schema is about saying,
- 33:57I expect this structure to arrive, validate it for me. So it's a very similar,
- 34:03it's very similar to FastAPI, which I'm sure a lot of people know.
- 34:06Okay. That's pretty interesting. So like you say, very much like FastAPI, it kind of brings in
- 34:12some frameworks called model binding, where you can say there's an argument and it's of this type,
- 34:17and then, you know, like a class, pedantic class or something along those lines. And it'll,
- 34:21here's a data class and it'll take all the inputs, right? Query strings or route primers,
- 34:27whatever, and parse them and map them in there. And then just give you an object and go here,
- 34:31it's ready. Yeah. So for this library, it supports, I've used all the data documentation as data class,
- 34:36but it's pedantic based at the moment. And because of the, it's not actually the typing. So there's no
- 34:41dependency injection or anything like that with FastAPI, but it uses the decorator validate request,
- 34:46and you pass it the model. And if the request that arrives can be turned into that structure and
- 34:52validated as such, then your function will be called and that'd be passed in as data. If it can't,
- 34:57then the client will get a 400 response.
- 34:59Bad request, bad data.
- 35:00Exactly. Yeah.
- 35:01Okay. What's the, so validate request makes sense to me. It means like, here's your structured object
- 35:07that comes in, passed in as data. What's validate response?
- 35:11So it's the same concept, but going back. So if you return like a to-do class instance itself,
- 35:18then there's no validation to do, right? You've already said it's the right structure,
- 35:21but often you'd return a dictionary or something like that, right? And expect it to go back as
- 35:26JSON. What this will do is it will say, I'll check that dictionary is of the right structure. And if it
- 35:32isn't, it throws a 500 response to the client and an error that you can go and view in whatever
- 35:37error monitoring tool you've got. Sure. Okay. That's excellent. So I see you have data classes
- 35:42here and you said it also works with Pydantic. Yeah. So, well, actually a shout out to one of
- 35:46your previous recent streams is I'm working on msgspec support at the moment, because I
- 35:51actually quite like that. So msgspec is interesting. So it will support Pydantic and message
- 35:56spec. So you'll be able to use Pydantic data classes, atas, msgspec stuff, or the message
- 36:01spec struct. I think that's probably all of it, but yeah, quite a bit of choice.
- 36:04The struct classes really, they've done some super interesting things here to make these
- 36:10data classes almost better for performance than even regular and for memory and stuff
- 36:14than regular Python classes. It's quite impressive. Yes, absolutely. Yeah.
- 36:18Yeah. Just for people who don't know, maybe just tell them real quick what msgspec is.
- 36:23They probably know data classes and Pydantic. Yeah. So much like data classes, and you can see here,
- 36:27it's a nice, simple way of creating a class with various things created for you. But it also,
- 36:33much like Pydantic, will do validation when you try and, if you use one of its functions to convert
- 36:39raw data like a dictionary into this type. And much like Pydantic as well, it also gives the ability
- 36:45to take this model and turn it into a open API JSON schema definition. And that's the final thing
- 36:51Quartz schema does is once you've decorated all your routes, it will auto-generate a open API definition
- 36:58for you as well. Oh, it will. Okay. Yeah. Using Swaggers/docs. I think that's really nice. A lot of APIs
- 37:05just don't. They're like, we're not going to do that. Yeah. If they don't have something automated to do it.
- 37:09And having typed classes representing all your inbound and outbound data, pretty easy, right?
- 37:15Yeah. I mean, it really helps with myPy, right? Because instead of having like your request.data or
- 37:19something like that, which is some dict that as far as myPy concerns can be anything, it must be a to-do
- 37:25instance in this function now. So yeah, really helps with your type checking.
- 37:29Yeah, absolutely. All right. What do we got next? Sock from Miguel Grimberg. Flask WebSockets.
- 37:36This one, there is an extension for because this is one of the areas that Quart being natively
- 37:40async can just do itself. So the Quart, I think I described sometimes as a superset of Flask.
- 37:45Because it can do everything Flask can do.
- 37:47The C++ of Flask.
- 37:49Yeah, exactly.
- 37:51Awesome. So WebSockets, a problem that websites generally have not been able to do. Like once
- 37:58you make a request, usually there's the response and that's it. But what if the server, you want to
- 38:02send more stuff to the server, everyone wants to send stuff out to you later. Like I started this job,
- 38:07three seconds later, now it's done. Or this bidirectional exchange is really cool. But a lot
- 38:12of times I feel like it's a little over the top. Like a lot of times I think people maybe just want push
- 38:18notifications from the server and not true. Like let's have a conversation. I just need to know
- 38:23when this happens. Just let me know as soon as it's done. Right. So for that, yeah, go ahead.
- 38:27Server sent events, sorry, seem to be not that well known, but yeah, probably described your usage
- 38:32really well. That's exactly what I was getting at. So does Quart support SSE? Does it support server sent
- 38:37events? Yeah, yeah. I think there's an extension for it, but in reality, it's a very small bit of code
- 38:44you need. So it's yeah. I mean, cause you, you literally return a response with a certain header
- 38:51and then you just return a, well, here we go. Here's the, there's an example in the documentation
- 38:56for Quart, but you just return a certain data structure and then you, you've got it. Yeah.
- 39:00Yeah. So I think that's when people say they want web sockets, I think most of the time,
- 39:04this is what they want. They want the client, the server to call the client sometimes, not just the
- 39:09other way around. Yeah. So I'm going to go slightly off topic because it's interesting,
- 39:13but it's related to this, but one of the things I implemented in Hypercorn, because I find it
- 39:17really interesting is that I think two, no, it must be more like four years ago, they introduced web
- 39:23sockets for HTTP two. And of course, one of the reasons you want web sockets because the cost of
- 39:28opening the connection for HTTP one is, is fairly expensive. So you can keep it open and do the bi-directional.
- 39:32But with HTTP two, you don't even need an extra connection anymore. You can have your web socket stream on the
- 39:38same connection as all your requests as well. So it's really nice and efficient. So yeah,
- 39:42I'd love to have an opportunity to make more use of it. So I find it really interesting, but
- 39:46the code I write professionally at the moment doesn't quite need that level of real time interaction.
- 39:50I know, I know all the stuff I built, I'm like, that would be so fun, but you know,
- 39:53I just don't need that. Yeah, exactly.
- 39:55I have no use case. I mean, there are apps I'm sure that do it, right? Like really complicated
- 40:02single page apps like StreamYard that we're using right now. For example, this has probably got some
- 40:07crazy interchange going on, but I don't write this kind of apps either. All right, let's see. So
- 40:13the socket stuff, which is cool. That's like you said, built into court. That's one of the advantages
- 40:18of it. But you know, you can use Miguel Grimberg's stuff to add it to Flask. SQLAlchemy, you mentioned
- 40:24them before, probably outside. If you're not in the Django space, it's not the only option for sure,
- 40:30but it's probably the de facto choice people when they think, I want to know our ORM for a relational
- 40:35database. You probably start here, right? Yeah, I think you would. Yeah. And this is a port,
- 40:41as I understand it, of the Flask, SQLAlchemy extension to work with or natively work with court. So
- 40:47should match all your expectations, I think. And if you're used to it, you just use this and it works
- 40:54with court and there you go. Let's see an example here. You can set up your database with a pretty
- 41:00interesting nested type of construct here, like just, you know, passing rich arguments that take
- 41:06more. And if you look at the code on there, just the shape of the code is kind of unique here, but
- 41:12you end up with this constructed database thing and then it has the base model class and off you go,
- 41:19right? Just get a session from it, do all the things. I've always had mixed feelings about
- 41:23these things that you set up here that are built like into the web frameworks,
- 41:28essentially that make them part of the request. Because if you're going to go and write little
- 41:31scripts that also do data stuff, you want to want to have, be able to get access to the data and the
- 41:36models over there as well. All those looks like it does take the app there, but I don't know if it
- 41:40really needs a meaningful app. Like, I don't think that it would use it probably, but it just adds it.
- 41:45No, I don't think you need to be within an app context to use the DB. I may be wrong there,
- 41:51but yeah, I think you could probably use it in a script. Yeah.
- 41:54Yeah. That's the only thing that concerns me about these like database helpers is like,
- 41:57you know, I need to do database stuff outside of a request as well. Even potentially some of the stuff
- 42:02that we're going to talk about in a minute, like it's just a background task that's not part of an
- 42:06actual request, but it could even still be in the web server process, right?
- 42:10I use, there's an extension called court DB, which is my preference because I prefer to write the SQL
- 42:16and go down the OIM route. And yeah, that works as you'd expect. You'd still set it up and pass it
- 42:23the app, which I think is true of this, but then you can just use it without an app context. It just
- 42:28basically uses the app to get the configuration values. So it knows where the database is.
- 42:32Interesting. And now we're on to probably the biggest one that you want to talk out,
- 42:37talk about, which is quite exciting, court tasks. Yeah. So this is a very recent release that I
- 42:43haven't announced anywhere else yet. This is something I think I read a post recently that said,
- 42:49of free web framework, you need like five things. Like you need a database connection. You need some
- 42:55kind of validation, input, output, rate limiting, authentication, and you need array to run periodic tasks.
- 43:01And in the past, my way to do this was extra infrastructure. So it was cron running somewhere,
- 43:08or some kind of, I think cloud watch does some kind of event generator based on a cron schedule.
- 43:13And for me, that's just annoying because now I have to build extra infrastructure,
- 43:18which I don't need. I'm not at that scale. Yeah. Maybe you got celery or rabbit MQ or something
- 43:23like that, right? Yeah, exactly. But again, with those, you need that extra infrastructure for it to work.
- 43:27Yeah. It's another server. It's another thing that could get hacked. You got to patch it.
- 43:31You got to secure it. You got to know how to run it. It could go down and then
- 43:34none of your background stuff works anymore. It's all, it's all a hassle, even though it has a benefit.
- 43:39Yeah. So what I really want to do is make use of the async event loop and just run a task in
- 43:45the background periodically. And obviously at some point in this one, normally on scale,
- 43:49because I have so many background tasks that dominate the server and stop it doing requests.
- 43:53But while I'm small and starting out, this is just makes life so much easier.
- 43:56Yeah, absolutely. It totally does. And because everything's async and await, you can just set up
- 44:00the task and just await the task. And it just blends in hopefully well with all the web requests, right?
- 44:07Yeah. Yeah. So for court tasks, if you write your task as an async def, then it will run it in the event
- 44:13loop. If you write it as a def, it will run it on a thread. So if you have synchronous code that's going
- 44:19to block the event loop, like you're using traditional IO to want for one of the better phrase, then if you just
- 44:25use def, they will run it on a task, won't block your event loop and you'll be fine. So yes, it's nice in that respect.
- 44:30Yeah. And as long as your async pieces are fine grained enough, you probably won't know.
- 44:37Yeah. Yeah. It's just if you run a whole bunch of synchronous code or something like that, you might
- 44:42start to clog things up, but don't do that. Honestly, like what you find, what I find anyway on the web
- 44:48most of the time is you're talking to other things, talking to a database, talking to a cache, talking to
- 44:52an API, and all those things are like perfectly suited for await because it's most of the time you're
- 44:59almost outside the web app. Yeah. I think the perfect one for this that I keep thinking of is
- 45:03when you want to send a periodic email, like a reminder or a summary of the day, just write this
- 45:09decorated task run and you have your function that just sends the emails. It's yeah, that's why it
- 45:14exists for me. It makes that so much easier. Yeah, absolutely. Right. Yeah. Like you say,
- 45:18just send out a quick email like, "Hey, I need to reset my password." All right, here, we're going to send
- 45:22you a call on API, like SendGrid or something like that. We're going to send you, but you don't want to block
- 45:27block things up. You just kick that onto the background work and off it goes.
- 45:31So one of the things that people would say, all right, you guys, I know you're excited about
- 45:36async, but we need durability. What about, what if the server goes down or what if another potential
- 45:43issue is what if you scale, like at least the cron stuff, like what if you ban out your web app into
- 45:49like 10 worker processes and you say, "Run in five seconds," and they all run in five seconds,
- 45:5410 times or, you know, those kinds of issues here. So.
- 45:59So this extension supports a couple of extensions to it. So you can pass it a custom store, which allows
- 46:07you to store when the task last ran. And so that means if the server goes down and it misses an invocation,
- 46:13then when it comes back up, it will recognize that and run the task straight away, rather than wait till the next time.
- 46:18And the other thing it supports is you can decide what should happen before the task starts and what should happen after.
- 46:25So this allows you to put some locking in for your second case. In both cases, the reason it's not built in to court tasks is you'll need to decide
- 46:33where you store this information, where you store the lock, where do you store the. So usually a database works for that.
- 46:38So yeah, so you can add it all, but you've got to choose how yourself.
- 46:41That's cool that it has placeholders and ways to plug that in.
- 46:45That's what I was thinking. I was right. Like instead of just storing it in memory and throwing it on the queue, put it into the database and have the task just check the database and go, okay, I'm working on it.
- 46:53No one else do this. We don't want to send 10 emails to the person. I got it. Right. All that kind of stuff.
- 47:00It's not a job queue, although I've been thinking about that recently. So yeah, it can't be like, it doesn't work in the sense that job will run once it will do jobs periodically.
- 47:10Got it. Okay. So more of a scheduler type of thing. So I need to see if anyone's uploaded files to this location and then convert it from PNG to WebP or like safe space or
- 47:22you could be like every morning, I want to send out an email to the users and say, imagine it's a to do app because everyone uses that as an example.
- 47:29And say, these are your tasks for the day or something like that.
- 47:31Sure. There are people who've signed up for the daily digest, send them their digest. Right.
- 47:36Yeah.
- 47:37Yeah.
- 47:37Yeah.
- 47:37Very cool. And because it's on a background thread, it's not going to harm anything. Excellent. Okay. Well, it looks pretty simple. Again, very decorator driven. Right.
- 47:46Yeah. So I mean, with all of these, the extensions at least I've written, I've tried to follow the kind of flask conventions for better for worse. So yeah. Same with court. It follows flask.
- 47:56Yeah. It feels like if you're already doing flask or court, you just know what to do. It's just similar. Yeah. Excellent. Well, I think we've covered all the ones we explicitly said, let's make sure we get a chance to cover those. I definitely want to encourage people to check out the court extensions page because there's quite a few more. I'll see if maybe some jump out at me here that are kind of cool. Court B trip.
- 48:17We briefly mentioned court DB, which is one I maintain. So I can say a bit more about that if you're so interested.
- 48:23Yeah, sure. And it's hanging out right here on your list.
- 48:25So this is much like the SQLAlchemy one. It's a way to connect to a database, but unlike the SQLAlchemy way, it doesn't promote an ORM rather it just gives you connection that you can run SQL on. I don't know you can do that with SQLAlchemy. I just, I mean, typically that's not what people do.
- 48:43So in this case, like the example here is you, you pass it the URL you want to connect to and it supports Postgres and SQL light at the moment. And then in the roots, you have this G dot connection object that you can make use of.
- 48:56And you can make use of that in background tasks. And it's quite easy to make use of it actually in court tasks as well. It's just a little snippet you need to do. But yeah, and that's it. You get a connection that you can do whatever you want with.
- 49:07Yeah. Okay. Very nice way to just have everything set up right. Like you don't have to worry about passing things around and create a transaction. Go do your things. Right. Excellent. We got court events for broadcasting server sent events and web sockets. That's pretty cool.
- 49:22There is one. Yeah. Yeah. Another one that's cool is, minifying all of your elements, right? CSS JavaScript.
- 49:28I've been doing that at the build stage a lot recently. There's a little snippet I can share if people want for court where you change the static function. So it will serve a G zipped version of the static file over the un-G zipped one if the client will accept it. And yeah, then if you do it build time, cause I think this one might be a mistake, but I suspect it works at runtime. So it compresses at runtime.
- 49:51Sounds like it does. Yeah. It talks about doing it as a response, right? So yeah. Oh, it does have a cache though.
- 49:58Ah, okay. Yeah. Well, you get similar. Yeah. Very similar to what I'm suggesting then.
- 50:01Excellent. Now those are really, really handy. If people go and they want to make their website better, check out, I guess, page speed.
- 50:10Lighthouse is what I was going to call it. Yeah. But they renamed it to page speed. So you can go put in and talk Python and see how we're doing, but it'll give you things like how is your site fast enough? Let's see how the desktop one's doing right. It'll give you a score. And you know, are we going to index you well, or are you going to be punished for having some kind of crappy experience in this minification stuff that we're talking about can help there. Right?
- 50:34Absolutely. Yeah. Similar to your DB, your QuartDB, we have QuartMotor, which is the async database access library for MongoDB. So I suspect it's super similar. I haven't used it because I use Beanie, which is like ORM style. So kind of on top of those things.
- 50:50KeyCloak is interesting. Redis also is pretty interesting. Uploads. Let's finish it out with uploads. We'll call it after that. So I haven't used this, but uploading files can always be a bit of a hassle, right?
- 51:02It looks very similar to the Flask uploads one. So it's a, it's kind of a nice wrapper around how you get access to the files you've uploaded and what you do with them kind of thing. Yeah.
- 51:12Yeah. That can be a little bit of a hassle, right? You got your request.files and all that. Don't know how to use this thing, but I know that people kind of struggle uploading files a lot. So that can be a thing.
- 51:23I don't tend to actually write it that much come to think of it, but yeah, it's just makes it easier.
- 51:28Yeah, exactly. You know, profile, profile pictures, maybe it's like that kind of stuff I'd be doing. Anyway, I guess I've wrote some code that lets you upload MP3s at one point for podcasts, but I stopped using it. So it's not top of mind anymore.
- 51:43Anyway, excellent stuff here. I love just going through and seeing all these little pieces, these little building blocks you can bring together and just quickly build an app.
- 51:51So thanks for being on the show and telling everyone about it.
- 51:54Oh, thank you. Thank you. Yeah. Great. Hopefully opportunity to introduce some stuff that I hope people will find useful.
- 52:00Yeah. And just more broadly, like thanks for working on Quart and pushing that forward.
- 52:03Yeah. I mean, like I said, our aim is hopefully we'll, well, we're kind of going around in different ways of how we'd actually might merge Flask and Quart, but that's still the ultimate aim.
- 52:13I kind of think because of the limitations we were talking about earlier about running async code when you're deep in async code base.
- 52:21I think you'll always have to make a choice with Flask where if you're going to be mostly sync, you go Flask. If you're mostly async, you go an async Flask or whatever it's called, which at the moment it's called Quart.
- 52:30We might be able to put them in the same code base perhaps. So then you can, you know, maybe mix and match because you can, you can certainly run Flask and Quart together with Hypercorn as the server and just have a, what's called the dispatcher middleware layer in between.
- 52:44That says for these routes, it goes to Flask and for those routes, it goes to Quart. So yeah, there are lots of ways to do it, but it depends what you want to do really.
- 52:52Yeah. That's interesting. Maybe you could even create a different app instead of app equals Flask. It's app equals async Flask. Who knows?
- 52:58That's effectively what Quart is. I mean, I could, if I called it async Flask at the start.
- 53:03A Flask. Yeah. It ranks higher in the alphabetical sortings.
- 53:08Yeah. All right. So people are excited with Quart. Maybe it's back on their radar now that we've been talking about all these extensions. What do you tell them? How do they get started? What do they do?
- 53:17Oh, I mean, you can just pip install Quart and a quick start is as simple and familiar as it's ever been for Flask. You just do app equals Quart and then app.get or app.whatever and then your function below to handle it.
- 53:30Yeah. The selling point is basically if you know Flask, you know Quart, you just use the word async some of the time.
- 53:35Yeah. Yeah. Yeah. I mean, even the WebSocket support, I've tried to make it as familiar as it would be if you knew Flask. So instead of using the global request, you now use the global WebSocket.
- 53:46So there's WebSocket.headers and you can do WebSocket.send, send a message, that kind of stuff. So hopefully it just feels familiar.
- 53:52Yeah. Fantastic. I'm sure it does. And thanks for being on the show and nice to have you back.
- 53:57Thank you.
- 53:57Catch you next time.
- 53:58Yeah, you bet.
- 53:59This has been another episode of Talk Python To Me. Thank you to our sponsors. Be sure to check out what they're offering. It really helps support the show.
- 54:07This episode is sponsored by Posit Connect from the makers of Shiny. Publish, share and deploy all of your data projects that you're creating using Python.
- 54:16Streamlit, Dash, Shiny, Bokeh, FastAPI, Flask, Quarto, Reports, Dashboards and APIs.
- 54:23Posit Connect supports all of them. Try Posit Connect for free by going to talkpython.fm/posit, P-O-S-I-T.
- 54:30Want to level up your Python? We have one of the largest catalogs of Python video courses over at Talk Python.
- 54:36Our content ranges from true beginners to deeply advanced topics like memory and async.
- 54:41And best of all, there's not a subscription in sight. Check it out for yourself at training.talkpython.fm.
- 54:47Be sure to subscribe to the show, open your favorite podcast app and search for Python.
- 54:52We should be right at the top. You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
- 55:02We're live streaming most of our recordings these days.
- 55:05If you want to be part of the show and have your comments featured on the air, be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 55:14This is your host, Michael Kennedy. Thanks so much for listening. I really appreciate it.
- 55:18Now get out there and write some Python code.
- 55:20Thank you.
- 55:20Thank you.
- 55:26Thank you.
- 55:27Thank you.
- 55:28Thank you.
- 55:34Thank you.
- 55:34Thank you.