Latest / Talk Python To Me / #264: 10 tips every Flask developer should know
Transcript
- 0:00Are you a web developer who uses Flask? It has become the most popular Python web framework.
- 0:04And even if you've used it for years, I bet we cover at least one thing that will surprise you
- 0:10and make your Flask code better. Join me as I speak with Miguel Grinberg about his top 10 list
- 0:15for tips and tricks in the Flask world. They're great. This is Talk Python To Me, episode 264,
- 0:22recorded May 1st, 2020.
- 0:23Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the
- 0:41ecosystem, and the personalities. This is your host, Michael Kennedy. Follow me on Twitter
- 0:46where I'm @mkennedy. Keep up with the show and listen to past episodes at talkpython.fm
- 0:51and follow the show on Twitter via at talkpython. This episode is brought to you by Sentry and
- 0:56Linode. Please check out what they're offering during their segments. It really helps support
- 1:00the show. Miguel, welcome back to Talk Python To Me.
- 1:03Thank you, Michael. Glad to be here once again.
- 1:04It's great to catch up with you. It's been a while. We used to catch up a little bit more
- 1:08when you were in Portland, but a little farther away now. So it's nice to see your face and
- 1:12hear how you're doing.
- 1:13I'm doing good. For those that don't know, I'm in Ireland, probably not forever. I think
- 1:20eventually I will return to Portland. We will go back to have lunch every once in a while.
- 1:25Perfect. Perfect. I'm looking forward to it, whatever that is.
- 1:27Yes. But for the time being, yes, I am living in Ireland. The plan was to travel a little bit,
- 1:34which of course with the current situation, not so much or actually not at all.
- 1:40It's not the best time, but you have been there for a while. And I really enjoyed my time in Europe.
- 1:46I'm super happy to be in Portland right now. Don't get me wrong. But I really enjoyed living in Germany
- 1:51for a year because it was so cool to just go, hey, it's two hours on the TGV to get over to France and
- 1:57to Paris. Or I could just drive down to Austria or whatever. It's a different kind of experience over
- 2:04there, isn't it? It's really nice. And I'm really looking forward to civilization resuming
- 2:10at some point and do that for a little bit more time before we return back to the States.
- 2:16Cool. Well, I'm glad it's going well for you, even if you are a little bit restricted in your
- 2:21day trips and whatnot, which I'm all behind, right? We need to do this to get through this. It's such an
- 2:27insane time.
- 2:28Yeah. I was going to say that I feel like I've been preparing for this situation for the last
- 2:35few years by working remotely.
- 2:38You know, that's really interesting. I totally hear what you're saying and I agree with you because
- 2:42you and I talked before you moved to Ireland and I think you were thinking, all right, well,
- 2:46where I'm moving to, I might not have a job there. So let me figure out how I can start laying the
- 2:51foundation so that I could work anywhere in the world. And I just happened to be hanging out
- 2:55outside of Dublin, just enjoying life and seeing things, but maybe still working for
- 3:01somewhere in the States or somewhere else in Europe or even doing your own thing, right?
- 3:05Yes. It's a little bit of everything actually. And the situation is luckily so far has not affected
- 3:12my work life. It affected pretty much everything else in many ways, but I'm working for Twilio right now
- 3:20and that luckily is going well and hasn't been affected at all because I was remote before. So
- 3:28everything keeps going the same way for me. A lot of the company was not remote and it is remote now.
- 3:34So there's been big changes for a big part of the company, but not specifically for me and my team,
- 3:41which were, you know, we were remote before.
- 3:43It's cool. You know, Twilio is a great company and I'm happy to hear you're working with them.
- 3:48I wonder, do you think that for people like you and me and the thousands of others out there listening
- 3:54who are already on the remote side of things, it's different for me because I don't have a,
- 4:00like a in-office counterpart of my company, right? Everyone I work with that works with me on stuff
- 4:06is all remote. So not so much for me, but I guess for folks like in your shoes where I used to be as
- 4:10well, do you think this, I guess, forced remote work experiment that we're all on, do you think
- 4:17it's made it a little bit easier for you in the sense that it's like leveled the playing field,
- 4:22right? It used to be like, maybe there was a meeting with five people in the room and then there was like
- 4:25you and one other remote person were kind of on the side. People would like pointed stuff you couldn't
- 4:30see or whatever, but now everyone has to kind of be on an equal ground. Do you think it makes remote
- 4:34workers a little bit better off in a sense? I think that will be the case for teams that are
- 4:39partially remote, partially in office. That is not my case. Yeah. Even though company,
- 4:45like your team individually, it's a hundred percent remote. It's 100% remote. Basically what I do at
- 4:50Twilio is I work on editing articles for the blog, the developer blog. My team is, you know, editors.
- 4:56I work on the Python side and we have people for different languages and that's my team.
- 5:02Right. You probably have to coordinate about like, Hey, we're going to have an article on this API
- 5:06solving this problem. Exactly. I'll do one in JavaScript. You'll do one in Python and let's
- 5:10figure out what the app is, right? We have one meeting a week to basically coordinate. And then
- 5:15the rest of the time we work with external contributors, right? Also remotely. So, you know,
- 5:23for our team specifically, nothing has changed, but I would imagine, or actually I should say that the
- 5:31one thing that changed is that we were getting asked for advice or all this new people who are forced
- 5:37to work remotely want to have, you know, tips and basically pick our brains.
- 5:43Tell us how you do it, right? Tell us how you make this work.
- 5:46How could you make this work, right? I think it's so interesting to watch the news where they have,
- 5:52you know, like multiple people on the show or things like the daily show or stuff. I'm especially
- 5:58thinking of the news shows with the news anchors where they all have had to start working from home.
- 6:03And you can see like week after week, they slowly are figuring it out, right? Yes.
- 6:08They had like super echoey laptop audio for their microphones and they're slowly starting to sound
- 6:15like ironically, like you and I do. And I think that we sound better. I mean, obviously we do
- 6:20recording and stuff, but also because we've had to live a hundred percent as with this as our professional
- 6:26interaction. And so to me, like sounding good and looking good, like we both have HD cameras,
- 6:32we both have good microphones that is kind of like getting dressed and looking good for business,
- 6:37right? Like when you show up for a meeting and you sound horrible and echoey, people don't want to
- 6:41talk to you, right? That's not a good look. There's a bunch of layers that we're all cultivating here.
- 6:45So what I'm very excited to know is what's going to happen at the end of this. I think a lot more
- 6:51people are realizing that it works. You can work from home and you get all that time, you know,
- 6:58the commute time back. So I suspect a percentage of, you know, all the people who are now forced to work
- 7:06remotely will like it and we'll decide that. Yeah. And I can really do this.
- 7:12I think we're not going all the way back to the way it was. It's going to be some of these
- 7:17advantages that people were unwilling to try are going to be seen as advantages are going to stick
- 7:21for sure. Yeah. Yeah. How about we talk about some flask, huh? Something that you've been definitely
- 7:27passionate about for, for many, many years. So we were talking before and you were on episode 48
- 7:35over four years ago, we talked about building apps with flask and then you were on episode 121,
- 7:42where we were talking about microservices and really with like a bit of a flask angle there as well.
- 7:47So you've been a fan of flask for a long time. Yeah. I was a user of flask first and probably told the
- 7:54story in the first episode, but you know, quickly I wrote my blog with flask and then not knowing what
- 8:01to blog about. I decided to blog about flask at a time where, you know, I wouldn't say that it was an
- 8:08obscure framework, but you know, certainly didn't have the following that has now. So my articles for some
- 8:16reason were the first that, you know, outside of the frameworks on documentation and it started growing
- 8:22at the same time. I decided to blog about it. Yeah. You just catch the wave at just the right time.
- 8:27Exactly. But at the same time, you know, you saw the framework, you're like, no, I'm not going to do
- 8:31it in Django or whatever else. I'm going to do it in flask. Right. So there's some, you know, picking
- 8:37the right ideas. Yes. And part of my, I'd like to think that this was a little bit of my doing. I
- 8:43showed why, you know, in many cases flask was the better choice by writing tutorials, usually in my blog
- 8:50and showing actual examples where you could do things that, you know, usually are considered hard
- 8:56and they're not so hard when you look at them through flask. Yeah. To me, flask, I'll compare it
- 9:02to Django because that's its biggest alternative, right? There's certainly all these other new things.
- 9:09There's so many new web frameworks coming. I don't know. I'd like to hear your thoughts on this actually
- 9:13is there's, there's so many new cool little frameworks. You know, we got FastAPI, we have
- 9:20API star, we have Sanic and just all these, they're not all necessarily leveraging the new asyncio stuff,
- 9:29but a lot of them seem to be like, Hey, these other frameworks didn't really solve my problem
- 9:34because they didn't support async. So we're going to create something that maybe leverages type hints
- 9:38plus async. That's kind of like flask. But what do you think about some of those? Like,
- 9:43where do you see the action there? First of all, I'm very excited that the model for all these
- 9:48frameworks is flask, right? They all do kind of like flask, as you said. Yeah, that is what was
- 9:54really surprising to me. So if you compare flask against Django or against pyramid or against the other
- 9:59frameworks and you look at their popularity, like I think flask is, we were talking neck and neck,
- 10:05but I think actually, if you look at the newer projects that haven't been around for a while,
- 10:10flask is pretty clearly ahead of Django in terms of popularity. It's like there's a ton of Django
- 10:15apps that people are still working on. So when you ask, do you work on Django or flask? It's a lot of
- 10:20times I think it means I work on a Django app that's been around a while. Not that there's anything wrong
- 10:24with Django, but just in terms of that growth. But then if you look at flask as the, like the idea of it,
- 10:30that all these other frameworks seem to think that this flask style,
- 10:36yes, slight adaptation is what they want, right? Yes. A big reason for that, I think is the,
- 10:41we are moving a lot of the logic, the business logic and applications to the client side,
- 10:47right? With all these new JavaScript based frameworks for the browser, right? So for sure,
- 10:53what's left to do in the server is really the database storage related actions and maybe
- 10:59authentication, surely authentication, and that's it. So if you look at a framework like Django,
- 11:06you can do that really well, but it has a lot more components that you really have no use for.
- 11:12And, you know, all these new frameworks modeled after it sort of give you just the API portion of
- 11:20your server side part of your project. Right. It's kind of just enough server side.
- 11:25Yes. Right. Yeah. I would say that it has a little less and then you can pick,
- 11:28you know, the right extensions to, to make it exactly what you want.
- 11:31Yeah. Right. Right. Add on. Yeah. That's a good point. I'm still a fan of having a decent amount done
- 11:38on the server side. I don't know. I just, I like the instant, you know, I don't know. It drives me
- 11:45crazy to see these pages sort of like build up as I interact with them. You know, you'll see like,
- 11:50oh, you're logged out. No way. Half a second later, I'm logged in and like, you know, just that kind of
- 11:54stuff. I'm not a huge fan of. You should find the right mix between, you know, server and client.
- 11:59I think people are too quick to go to build everything as a single page app, react to view,
- 12:05angular, you know, those types. And they, they don't think about doing it a right balance. Sometimes
- 12:12you don't expect everything to be done in a single page. It feels weird. The whole page is changing,
- 12:19but it's really done in react, for example, which is slow and weird. It messes with the back button
- 12:26in the browser. I prefer to basically use the single page app only when you see a clear benefit.
- 12:33You really need like an interactive thing. I'm building like a little dashboard I'm exploring
- 12:37or something like Gmail or something like it's perfect. Right. But it shouldn't be the one hammer
- 12:43you hit everything with in the web.
- 12:44My blog, the blog that I wrote six, seven years ago, when I started with Flask, it's still a
- 12:51traditional application server centric. And, you know, it's just fine. It has a little bit of
- 12:56JavaScript sprinkle here and there to make it a little bit nicer, but you know, it's mostly server side.
- 13:02And I think for a blog that works really well.
- 13:04Yeah, I agree. So you're talking about the front end frameworks. Like I agree, like don't overuse
- 13:09them whatnot, but sometimes they make a lot of sense. What ones do you like right now?
- 13:14My preference, and this is going to generate, generate a little bit of disappointment in your
- 13:20audience. I think it's a vanilla JavaScript. That is the framework or the no framework that fits my
- 13:27brain the best. So I can do whatever you want in vanilla JavaScript. A few years ago, I would say
- 13:34jQuery. These days you don't really need that. The jQuery was a layer that will make all the browsers
- 13:41sort of uniform. And these days the browser, the browsers are pretty good at being uniform with each
- 13:47other. So that's my favorite. Out of the real frameworks, React is the one that I've used the most,
- 13:53but only for simple apps. What I've seen is that all these dependencies that are generated
- 13:59between all the parts of the page, it's very easy to get them completely out of control as the project
- 14:06grows. At least I personally find having a handle, like for example, when writing vanilla JavaScript,
- 14:12having a handle of what part of the page it's related to what other part makes it for a much faster and
- 14:20dynamic application. For small examples, I think React is a good model. And that is what I use.
- 14:26Nice. If I had to throw my vote in for one, I'd put it on Vue. I like VJS a lot. Yeah. I like it just
- 14:32quickly, just bring it in, just include a JavaScript file, you know, pull out an ID and say,
- 14:37this little sub part of the page is now an app. I really like that.
- 14:41Yeah. Vue will be my second choice actually. Yeah.
- 14:44This portion of Talk Python To Me is brought to you by Sentry. How would you like to remove a little
- 14:50stress from your life? Do you worry that users may be having difficulties or are encountering errors
- 14:56with your app right now? Would you even know it until they send that support email? How much better
- 15:01would it be to have the error details immediately sent to you, including the call stack and values of
- 15:06local variables as well as the active user stored in the report? With Sentry, this is not only possible,
- 15:12it's simple and free. In fact, we use Sentry on all the Talk Python web properties. We've actually
- 15:18fixed a bug triggered by our user and had the upgrade ready to roll out as we got the support email.
- 15:24That was a great email to write back. We saw your error and have already rolled out the fix.
- 15:28Imagine their surprise. Surprise and delight your users today. Create your free account at
- 15:34talkpython.fm/sentry and track up to 5,000 errors a month across multiple projects for free.
- 15:40Flask is on the rise. I think it's on the rise in its own right. And I think it's
- 15:46on the rise in the sense of its API as we talked about. So let's dive into the 10 tips that we're
- 15:54going to talk about. Yes. 10 tips. Yeah. What do you got for number one here? Okay. Number one,
- 15:59it's actually very specific to APIs. Okay. You do not need to use the JSONify function anymore. So this is
- 16:06in recent Flask releases. For many years, if you needed to return a JSON response, you had to
- 16:14call the JSONify function, which would take a dictionary or list and convert it into the actual
- 16:21payload that goes out to clients. Right. So if I've got a view method and I wanted to return JSON,
- 16:27I can't just say return a dictionary or return a list and have it internally serialized to JSON.
- 16:33Right. That'll like not work. Not until now. So I've always done JSONify as well. Now what can I do
- 16:39instead? So basically now you return a dictionary and Flask will itself say, okay, this, this goes out as
- 16:48JSON. So it will JSONify the response, set the correct content type and all of that.
- 16:53So when I think about this, what else has to be done to make this work? Do I need to add anything
- 17:00like to the route decorator to say the response is JSON? No. Or is it enough if the client connecting
- 17:06says, except type is application slash JSON, or what do I need to do? You don't need to do anything.
- 17:10So this is always Flask looked at the type of your response that you're returning your view function.
- 17:18There were always different behaviors depending on the type. So if it was a string, it would send it as
- 17:24text. If it was a response object, it will send it as basically whatever you said in that response object
- 17:31and so on. And now there's one more type. If you return a dictionary, then Flask says, okay,
- 17:38this is JSON. It basically sets everything up for the response to be all done for you. So it's really
- 17:44great. I'm not importing JSONify anymore. There's no need at all to import that function and then have
- 17:51it in every single last line of all your view functions.
- 17:55Yeah, that's really clean. I definitely like that. That's something I've really liked about the
- 17:59Pyramid Web Framework is you've been able to do that the whole time. You just return some data type
- 18:03and it'll serialize it. Yeah. So really glad to see this is nice and clean. And yeah, I think when I
- 18:09first started doing Flask, I was like, oh, I'm going to try to return a dictionary. Oh, it doesn't like
- 18:13that. Now I have to go hunt down. So ran across JSONify and I didn't realize that that had been
- 18:18sort of more made more general. So you don't have to. That's great.
- 18:21Yeah, it's really nice. For many years, I used the decorator specifically for that. It was
- 18:25an at JSON that I even taught in many classes how to create a decorator that will take that response,
- 18:32a dictionary. Absolutely. And I've created a decorator exactly like that that takes in the
- 18:38decorator, you specify the Jinja template and you return a dictionary and then it'll like send that,
- 18:42it'll render template with that dictionary as well. I'd love to see those kinds of things built more
- 18:47into Flask. I mean, I know this one's no longer relevant, but there's a couple of things like that
- 18:51that would be kind of cool, I think. Yeah, very nice. Okay. So no more JSONify
- 18:55API is a little bit less coderad. It just makes it cleaner. I love it. All right. Number two.
- 19:00Number two is do not store sensitive information in your user session. I see this a lot. It's so
- 19:07convenient. You have, you know, from Flask import session and then a session works like a dictionary
- 19:13and it's so tempting to write any kind of information there that, you know, in the next request from the
- 19:20same client, you can recover. It's preserved per client, right? So...
- 19:25Right. Like if you log in, you could put like the user ID in there. And so then the next request comes in,
- 19:29you could pull them back.
- 19:30So you certainly put the user ID. So it's very important to remember that unless you configure
- 19:37Flask in a non-default way, those sessions are not encrypted and they are sent in cookies to clients.
- 19:46So the client is storing all that information that you put in the session dictionary. So you should
- 19:52definitely never store information that you do not want to be public.
- 19:58at least the user could go in there and see it or they could go in and they could mess with it,
- 20:04right? They could tamper with the cookies and whatnot, right? You definitely don't want that.
- 20:08So that's actually a good point. They could not, clients cannot modify it. The cookie is a secure
- 20:14cookie. It has the content. It's not clear text, but it's also not encrypted. It's in base 64 encoding,
- 20:20which is very simple to...
- 20:21It's like visually encrypted, but that's all.
- 20:24It's visually encrypted, right? So it's base 64 encoded, but there's also a crypto signature
- 20:29in the cookie.
- 20:30I see. Okay. So it's tamper proof.
- 20:32Correct. If you make any changes in the client and then try to send that cookie again to the server,
- 20:37Flask will say, no, this is not, the signature is incorrect and it'll throw it away. So that's not
- 20:43a problem. So it's safe against tampering, but it's really not safe against seeing what the
- 20:49information is. So never store cookies or, I'm sorry, secrets, passwords, you know,
- 20:55nothing of that sort.
- 20:56Yeah. So I've never used the session feature of Flask. I've always just said, I want to store a
- 21:04cookie. And it just has like one piece of identifying information to like carry on that session. And then
- 21:10I'll go back to the database to get the rest, everything else, right?
- 21:13Okay.
- 21:13Should I not be using this? Like, should I be using session? Should I look more into it?
- 21:17Session is actually very convenient and Flask has a plugin architecture. You can install different
- 21:23types of sessions. The default is the cookie or secure cookie based session that I just described,
- 21:29but there's an extension called Flask session, which provides sessions that are stored server side.
- 21:36So this extension provides a number of storage mechanisms. You can store them in files,
- 21:41pretty much in the style of PHP. If you've seen how those are stored, you can store them in a database,
- 21:47right?
- 21:47Through SQL Alchemist, for example.
- 21:49Probably Redis. Yeah.
- 21:50There's a Redis backend as well. So Flask session will be, you will have to do that if,
- 21:56if you want to store sensitive information in your session and then you will be safe,
- 22:00right? Because the only thing that will go on the session cookie in that setup will be the session
- 22:05ID, but all the information will be safely stored in the server.
- 22:09Okay. Yeah. That sounds like a pretty good thing. And the server side bit is nice, right? So you can
- 22:15store those things, but not actually send them. And even if it's not because it's sensitive,
- 22:19maybe it's not reasonably serializable or it's not, maybe it's just a lot of data, right? You don't
- 22:26want to send like cookies are limited in how big they can be. And you don't want to exchange like
- 22:30huge cookies anyway.
- 22:31They need to be there. They are encoded to JSON before the base 64 encoding is applied. So the,
- 22:38whatever you store in the session needs to be JSON compatible. So a string, a number,
- 22:42a list of, or a dictionary. So yes, definitely there are some limitations.
- 22:47Cool. All right. What's number three?
- 22:49Kind of related.
- 22:50Oh, before we move off this one though, I want to do say one thing really quick,
- 22:53maybe a 2.5, 2.7, something like that. You're talking about exchanging these cookies. And I was
- 23:02kind of thinking, well, okay. So the user really needs to be careful about like, it'll be on their
- 23:06file system somewhere as a stored cookie. They can go to their dev tools and look at their cookies and
- 23:11whatnot, but it's kind of safe to the world. That's making a really big assumption that that
- 23:15connection is encrypted.
- 23:16Right. Yes. And actually these 10, maybe we should have an 11th tip, which is always use HTTPS.
- 23:24Yeah. Yeah. That was going to be my 2.5 is like, let's encrypt, right?
- 23:28Yeah. You always have to have encrypted servers when you deploy for production. Even if you think that
- 23:34you have nothing that's sensitive in this day and age, there's really no reason to risk it because
- 23:42you can get a free SSL certificate from Let's Encrypt.
- 23:45It's incredibly easy to set it up for Let's Encrypt.
- 23:47It's very easy to set it up.
- 23:49Yeah. I had resisted it because I was like, I don't really want to learn how, I already have this other
- 23:52SSL certificate I bought. It's like good for three years, but I don't want, and then I finally just
- 23:57said, all right, I'm going to find out, like, I'll just get Let's Encrypt working just so I don't think
- 24:01about renewing this other one. And it was like 10 minutes later. I'm like, oh, that was really easy.
- 24:05Yes.
- 24:06That was learning. You know, the second time it's even quicker.
- 24:08Yes. And the certificates last, you know, they're short-lived. They last for three months,
- 24:12but you set it up so that they automatically renew. And it's something that you never need to think
- 24:18about. They basically, they just work. So yes, absolutely. You always have to have SSL encryption
- 24:23on your sites deployed to production.
- 24:27Yeah. Two more aspects to that. Obviously this, we think of privacy when they think encryption,
- 24:31but SEO, Google is taking into account whether a page is secure or not as well these days. And
- 24:40they're also taking into account performance. One of the really nice ways to get performance these
- 24:45days is to just do HTTP2, right? And I'm pretty sure that only runs over SSL.
- 24:50I'm not so sure about that. Maybe.
- 24:51You know? Okay. I can't remember. I feel like I tried to set it up before. I don't know. I think
- 24:56I'll mess around while we're talking and figure it out.
- 24:58We should definitely figure it out. And in general, I've found that you can add all the HTTP2
- 25:04solutions, you know, outside of your web application. I don't find that I need to worry
- 25:10too much about that within Flask, for example. You put a reverse proxy in front that will take care of
- 25:17that. That's actually the same idea that I'll apply to the encryption. In general, I prefer to
- 25:22leave that to tools that, you know, that do that well, do it in native language, not in Python.
- 25:28It's way faster and more efficient. So all those things are good to have, but luckily they haven't
- 25:34changed, at least for me, how I write my web applications in Python and Flask.
- 25:39Yeah. Same. Yeah. I'm doing HTTP2 for my stuff. And it's like, most of that is around serving the
- 25:45CSS and the images and the JavaScript through Nginx. And like, that's even before you talk Python,
- 25:49right? Okay. So let me read something really quick and you interpret it for me. This is from
- 25:53SSL.com. It says, browsers distinguish between clear text HTTP2 and HTTP2 over encrypted TLS as two
- 26:01different protocols. As of this writing, none of the major browsers support H2C, which is a clear text
- 26:07version, which means TLS encryption is mandatory. So the protocol specifies a non-encrypted version,
- 26:13but apparently the browsers... None of the browsers do it. Right. At least as of 2000, November,
- 26:192018. Right. So maybe they have, but I'm surprised if they'd be... I don't see why they would. Right.
- 26:25Exactly. So I think theoretically you don't need SSL, but in practice you need SSL. So one more reason,
- 26:31I guess there. Cool. All right. More secrets. Number three. Okay. Another secret related one.
- 26:37You should use a .env or .env file to store your secrets. I'm sure if I go search GitHub for the
- 26:47standard names that people assign to AWS secrets or even Twilio passwords, you know, all those things.
- 26:55All these things.
- 26:56I'm sure I can collect a bunch of them, right? That people put in source code and then they forget.
- 27:01Yeah. And that's fine until it's not. It's fine because you're like, oh, we're never going to open
- 27:06source this. And then somebody does and they don't realize it. And then bad stuff happens.
- 27:09S3 buckets are exposed and so on. Right. So you should get used to never write a password or a
- 27:18secret on an API key directly in the code, even if you're doing it for a quick test, because it happened
- 27:24to me many times. I think that I'm going to do a quick test, but then that evolves. And eventually
- 27:29I said, oh, this is a good thing to show in a gist. And then off it goes to GitHub. So never do it.
- 27:35And then you'll be safe. What you need to do instead is to replace in the place where you are
- 27:42going to write the password or secret, just read an environment variable and then get used to always
- 27:47have the secrets that you use in your applications in environment variables.
- 27:51Right. That's helpful for things like Docker as well. It helped you create these more isolated
- 27:57reproducible elements. So that totally makes sense. And I've heard put them in environment variables,
- 28:02but what's the story with the .env file? How's that relate?
- 28:05The problem is that people find it annoying to have to set environment variables because when you set an
- 28:11environment variable, the life of that environment variable is the session, right? If you close your
- 28:16terminal window, let's say we're talking about development, right? So at the end of the day,
- 28:21you turn off your machine. The next day, all those environment variables are gone, right? So my
- 28:26recommendation and something that works really well with Flask is that you put all your environment
- 28:32variables in the .env file and then Flask in recent releases, if the package python.env is installed,
- 28:41we'll just import all those variables when you run the application.
- 28:45And it basically sets them as if into the environment. Like if you ask for them from the
- 28:50environment dictionary, they'll be there.
- 28:52They'll be there in the same way as if you had set them by hand on your terminal before running
- 28:57Flask run.
- 28:58Okay. What's the name of this package?
- 29:00Python-.env. D-O-T-E-M-V.
- 29:02Yeah. Yeah.
- 29:03There you go.
- 29:03Cool. I'll put it in the show notes.
- 29:05So even if you don't use Flask, this package makes it so simple to import a .env file. You just
- 29:12need to call a function load.env. That's it.
- 29:15And then after you call that, all the variables that you stored in the .env file will be in your
- 29:21environment.
- 29:21So I totally agree with this, but I always wonder what people use to persist and version and keep
- 29:29those types of things.
- 29:31Where do you store the stuff?
- 29:33So if you got a new computer or you got a new team member, how do you get them this information?
- 29:37I'm going to tell you what I do. Some people may like it. Some I'm guessing won't. But what I do is I
- 29:42create a .env template. I usually call it .env-template or something very clearly that you can see that it is an
- 29:51example of how your .env file should be structured. And in that template file, I write the variables,
- 29:59the equal sign, and then I leave it empty. So when you're installing your application in a new machine,
- 30:06you copy the .env-template to .env, and then you fill out your secrets. And yes, it is annoying,
- 30:13but it's only the first time when you're installing a new application. That's the only time you have to do
- 30:18it. And then it works. Second to that, you have to put the .env file in your gitignore file,
- 30:25because you, of course, don't want to commit that on purpose or by mistake to source control.
- 30:31Right. That kind of defeats the purpose, right?
- 30:33Of course.
- 30:33I think that might be a default in GitHub if you pick the Python ignore template. I can't remember,
- 30:39but I think so.
- 30:40No, actually, I wouldn't say that. That was my complaint. I don't believe it is.
- 30:44Oh, it's just .env, not .env?
- 30:46Right. I don't think they have .env, even though it's actually fairly standard. Many technologies and
- 30:53many languages and frameworks use it, but I don't believe it is. I'd like to see it there by default.
- 30:58Yeah. Well, you know that these gitignore templates are like projects on GitHub, right? So like
- 31:06github.com/github slash gitignore is the project where those are kept. We could do a PR,
- 31:12see what they think.
- 31:12We should do a PR, definitely.
- 31:14We should do a PR, yeah.
- 31:15This portion of Talk Python To Me is brought to you by Linode. Whether you're working on a personal
- 31:21project or managing your enterprise's infrastructure, Linode has the pricing, support, and scale that
- 31:27you need to take your project to the next level. With 11 data centers worldwide, including their
- 31:32newest data center in Sydney, Australia, enterprise-grade hardware, S3-compatible storage, and the
- 31:38next-generation network, Linode delivers the performance that you expect at a price that
- 31:43you don't. Get started on Linode today with a $20 credit, and you get access to native SSD
- 31:48storage, a 40 gigabit network, industry-leading processors, their revamped cloud manager at
- 31:54cloud.linode.com, root access to your server, along with their newest API and a Python CLI.
- 32:00Just visit talkpython.fm/Linode when creating a new Linode account, and you'll automatically
- 32:06get $20 credit for your next project. Oh, and one last thing, they're hiring. Go to
- 32:10linode.com slash careers to find out more. Let them know that we sent you.
- 32:14Good advice. Another thing that I've seen, this is not the same, it's the same vein, but it's not the
- 32:21same thing, is, and this honestly sounds better, but also pretty good, is usually we're using virtual
- 32:27environments for web apps and any meaningful app, right? So the virtual environment you create has an
- 32:34activate script, you can set environment variables there. So anytime you activate the virtual environment,
- 32:40it's always got its environment variables set. It's kind of the same, you create a new one,
- 32:44you got to reset them, but similar.
- 32:46Yes, it's a little bit more difficult to remember to do it when you do a new installation.
- 32:50Yeah.
- 32:51But yes, definitely a good readme pointing you to do it should help and you achieve the same result.
- 32:56I think the one difference is like the .env template explicitly kind of reminds you that
- 33:02there's some kind of thing I've got to do, whereas the virtual environment is like magic. Like it's good
- 33:07when it's set and it's working, but there's no indicator that this is a step you need to do.
- 33:11Yes, that is a good point because when you call the load.env function from this Python.env package,
- 33:17if there's no .env file in the file system, it'll print a warning. So you will see a warning in the
- 33:23console.
- 33:24Nice. That's good. Perfect. All right. What's number four?
- 33:27So this is not specific to Flask, but for many years, people who use Windows, they're sort of
- 33:33treated like second class citizens, right? Because most tutorials I've written for Bash and, you know,
- 33:40Unix based.
- 33:41Dot space VMV bin activate. Like, wait a minute, that doesn't work.
- 33:45The dot doesn't work. The bin doesn't work. What is all this?
- 33:48Right. And then the forward slashes versus the backward slashes that you use on Windows.
- 33:52So in recent years, there's also Chromebooks, which are another group of people, the users of Chromebooks,
- 33:58who sort of felt at a disadvantage because you have a perfectly fine machine where you should be able to
- 34:04write, develop code, write Python. But for many years, it was not possible or it required hacks on the
- 34:12device itself. So these days, both Windows and Chromebooks both have Linux emulation solutions.
- 34:20They're not the same, but the end result is that you get a Linux prompt where you get Bash or C shell
- 34:27or your favorite shell that you'd like. And then basically you can run Ubuntu Linux in your Windows or
- 34:34Chromebook machine.
- 34:35Yeah. Nice.
- 34:36So I don't use Windows so much now, but I have a Chromebook. I can do pretty much anything I do on my
- 34:42Mac laptop on the officially supported Linux emulation on the Chromebook.
- 34:48So definitely take a look at the WSL, the Windows subsystem for Linux, if you're on Windows or the
- 34:55Linux files support, if you're on Chrome OS, because on both, you can run the Ubuntu or other
- 35:03distributions. That's the default, but you can use a different one if you like. And you can run Python,
- 35:08you know, recent versions of Python, Flask, and all of that works exactly like it would on a
- 35:14Unix or POSIX type OS. So you will be able to follow tutorials using the standard instructions
- 35:22that you see for Unix.
- 35:24Yeah, that's cool. And the Windows subsystem for Linux 2 was just released not long ago,
- 35:29which is a big improvement, I understand.
- 35:30It's actually an improvement in performance and it's actually more directly, it's less of a VM
- 35:36or virtual machine solution.
- 35:38Right. It's kind of more linked between the different two environments and apps and whatnot. Yeah.
- 35:43Like you can open Explorer from your Linux and to get your file system, stuff like that. But I think
- 35:48actually the Chromebook story is bigger because one of the important places people might want to use
- 35:54both Chromebooks or rephrase that they have a Chromebook as their only option and they want to learn Python
- 35:59or programming is in the educational space.
- 36:03Oh, yes.
- 36:03So many kids have Chromebooks. My daughter has a Chromebook from her school and if they wanted to do Python,
- 36:09they could maybe do some online thing, maybe? Or the answer was no, you don't do Python, right?
- 36:14It's actually hard to even an SSH connection to a remote server. You can, but it's tricky. It's not
- 36:21immediately obvious how to do it. But now you can run your own local Ubuntu distribution in the Chromebook.
- 36:28Nice. Cool. That's a big deal.
- 36:30And it's all official. It doesn't require any hacks. It's all sanctioned by Google and developed by Google as well.
- 36:36Cool. And shout out to the folks over at Canonical for releasing Ubuntu 20.04 LCS.
- 36:42Yes.
- 36:43The first LCS release.
- 36:44Yeah. In two years. So it was super awesome. And it comes with Python 3.8 as the default and Python 2 is not even installed.
- 36:51So the Python story is better there. And obviously just the new Ubuntu is nice. Cool. All right. Number five.
- 36:56Number five. So this is the thorny one. A lot of people to this day don't understand the contexts in Flask.
- 37:04Most people don't see this, but they only learn about it when they get an error. And there's actually two different errors.
- 37:11And they're so similar that people think there's only one. And the two errors are you're working outside of the application context. That's number one. And then the second one is you're working outside of the request context.
- 37:24And I suspect this is where people are going just statically saying Flask.request.property. But that's not set.
- 37:32Right.
- 37:32Because they're outside the context. Yeah.
- 37:34This comes all from the philosophy of Flask, which is to make all these variables, let's call them variables, that are global in nature to be magically available.
- 37:44So you import, for example, current app or your import request, and then you just use it as if it was a global variable. It's actually, there's a little bit of magic underneath these imports, but you use them as global variables.
- 37:57And if you use them in the wrong place, then you get the error.
- 38:01Right. So once the context is created, these are what, thread local storage, basically?
- 38:06Yes. The implementation is based on thread local storage. So they basically belong to a thread.
- 38:12Right. And that's the request coming in through like uWSGI or something like that. Sets that and then the rest of your app just has it.
- 38:17Correct. So the interesting thing is that people don't realize that these are very different.
- 38:23If you get the, you're working outside of the application context error, that means that some piece of code wants to know what the application is.
- 38:34And all you need to do is set the application context yourself.
- 38:39If you look in the Flask official documentation, it shows how to do it. It's a single line.
- 38:44There's even a context manager to do this.
- 38:46So you set the context and then your code will work and you solve the problem. That's it.
- 38:52Yeah.
- 38:52The issue is when you get the other one and people confuse the two.
- 38:55But if you get, you're working outside of the request context, that typically means that you have a bug in your application.
- 39:01You're trying to get information about a request and there's no request, which means that you're running this code.
- 39:07Right. Maybe you kicked off a thread or something weird thing like that.
- 39:11Yeah.
- 39:11You're doing it in a place where there's no information about the client.
- 39:15So you know nothing about a client or a request.
- 39:18I see.
- 39:19That indicates that you have a bug and people find crazy ways to fix that basically to avoid the error.
- 39:26And one that I see a lot is that Flask has this way to create a test request for unit tests.
- 39:33Right.
- 39:34If you create a test request, then you can invoke request dot something and it will work.
- 39:40But the information that you get is all fake.
- 39:43So, I mean, the fact remains, you're probably doing something in a place where you shouldn't be doing it.
- 39:49So, application context, if you get the error, you set the context and you're good to go, it's perfectly fine.
- 39:55Request context, you need to look at your code, you probably have a bug.
- 39:59You're doing it wrong.
- 40:00You're doing it wrong.
- 40:01Cool.
- 40:01All right.
- 40:01What's number six?
- 40:02Number six is still related to this topic.
- 40:06I've seen many, many applications that have, for example, they have Celery workers or any other type of auxiliary processes, maybe cron jobs, you know, all those things.
- 40:18So, let's say you have a web application written to Flask and you're using Flask SQLAlchemy for your database.
- 40:27So, when you go write the Celery side or the cron job, what do you do?
- 40:33You create all the models using Flask SQLAlchemy.
- 40:37And that extension has a very nice way of managing your connection.
- 40:42You don't have to worry about it, actually.
- 40:43Right.
- 40:44Create the engine, create the connection, create the table, all that.
- 40:46Yeah, right.
- 40:47All of that is done.
- 40:48You don't do any of that, right?
- 40:50When people start coding their Celery workers or cron jobs, they say, well, okay, I'm going to have to do all of this myself.
- 40:57They create their SQLAlchemy engine and they have to figure out crazy ways to get the models which are written to inherit from db.model, which is a Flask SQLAlchemy class, you know, to inherit from the base, you know, the declarative base from SQLAlchemy.
- 41:13And they invent a lot of crazy ways to make that work.
- 41:16And the fact is that you don't have to.
- 41:19You can create a Flask instance and instantiate Flask SQLAlchemy and then not start a server.
- 41:26And you can use the database just fine.
- 41:29Right.
- 41:29If you just don't call Flask.run, you're kind of everything set up anyway, huh?
- 41:32Don't run the server, but create a Flask instance in the Celery worker, in the cron job, it doesn't matter.
- 41:38Just create, call the create app function to make it an application, configure it exactly like you configure it for the server, but then don't start the server.
- 41:48Yeah.
- 41:49And then that gives you access to using all the extensions.
- 41:52You can use the database through Flask SQLAlchemy just fine.
- 41:55There's absolutely no problem.
- 41:57Another big one is sending emails.
- 41:59So if you use the Flask mail extension, it's exactly the same thing.
- 42:03You can send an email from a Celery worker.
- 42:05You don't have to look for a different library.
- 42:08You can just create a Flask application instance, configure it, and then Flask mail will work just fine.
- 42:13Yeah.
- 42:14And well, sending email is exactly why you would do this on another thread or in a background.
- 42:19Because if you're sending one email, fine.
- 42:22It's probably okay.
- 42:23It's not ideal, but it's fine.
- 42:25But I ran into this thing where I had thousands of people sign up to get notified when I posted a new office hour.
- 42:33Because I have free office hours for people to take my courses.
- 42:36They can drop in on Zoom and chat and whatnot.
- 42:39When that started to grow, I remember hitting it.
- 42:41And it literally timed out the request, trying to send the emails.
- 42:45And then it was halfway through sending the emails or some percent through.
- 42:49But I didn't know how far.
- 42:51So I didn't know how to go back and send the rest of the emails.
- 42:53I'm like, well, the other rest of the people are just not getting sent because I'm going to email the first half again.
- 42:58Right?
- 42:58It's just like, it's not a thing you do as part of a request.
- 43:01It's really sad that similar stories happened to me.
- 43:05And yes, that's actually a good reason to send the emails to a background job.
- 43:11And then it can take as long as it needs.
- 43:13But yes, you would need to create the Flask application instance.
- 43:17You will need to set the application context.
- 43:20Because in a particular case of both Flask SQLAlchemy and Flask Mail, they need an application context because they need to get the configuration.
- 43:30That's all they need.
- 43:31That's actually, they don't really care about Flask itself.
- 43:34But they have the configuration variables in app.config.
- 43:40So you need to set the application context.
- 43:42So they need the first part.
- 43:43They need the app part.
- 43:44Yeah.
- 43:44Right.
- 43:45They just need to know where the app is.
- 43:46So only so that they can get to the configuration.
- 43:49And then they can know what the database is or what the email server is.
- 43:52So definitely do it that way.
- 43:54And then you can have a consistent way of working with database or emails between the server and your auxiliary processes.
- 44:01Yeah.
- 44:02That's great, great advice.
- 44:03Now when I'm doing something like email, even if I'm just sending one, it's on a background job.
- 44:07It's not part of the request.
- 44:09It should always be.
- 44:09And people don't realize this, but sending an email is actually very slow.
- 44:13It takes a few seconds if you're lucky.
- 44:15Right.
- 44:15Well, and it also depends on things you don't control, right?
- 44:18Like you probably control your database somewhat.
- 44:20But this is like some external mail server that's then talking to some other thing.
- 44:24Yeah.
- 44:24Yes.
- 44:24And, you know, many servers introduce artificial delays for security purposes.
- 44:29There's a lot of tricks that many servers implement.
- 44:32The end story is that sending an email takes two, three seconds at least.
- 44:36Yeah.
- 44:36Just one.
- 44:37At least.
- 44:37Yeah.
- 44:38So it should never be done in the server, you know, in the foreground of a server route.
- 44:42Right.
- 44:43Absolutely.
- 44:43All right.
- 44:44Number seven.
- 44:45This one I kind of nudged you to cover.
- 44:47Yeah.
- 44:47This is a good.
- 44:49Because I'm a fan of it.
- 44:49But yeah, tell us about this one.
- 44:51I'll jump in as well a bit if you want.
- 44:52You surely know this one better than me because I found out a few days ago when you pointed this out.
- 44:58But secure.py is a project that provides a lot of security settings around how you configure your cookies, your HTTP headers, in your requests, actually in your responses.
- 45:14Not only for Flask, but for a lot of frameworks.
- 45:16A really long list of frameworks.
- 45:19Yeah.
- 45:19This is one of the things I really like about this project.
- 45:21It's because if I go learn how to do secure headers in Pyramid, then I go write my Flask API.
- 45:28I've got to like rethink, okay, well, what does this default to?
- 45:31And then how do I do it?
- 45:32And what's cool about this is it's like literally one line, one piece of middleware or callback.
- 45:37And you've got all these things added.
- 45:40And it works for Flask, Pyramid, Django.
- 45:43Like Starlette and...
- 45:45The newer frameworks, right?
- 45:46The AsyncIO frameworks.
- 45:47Right.
- 45:48All the core, which we'll talk about, Responder.
- 45:51Like it has support for all.
- 45:52So you just do the one thing for that framework, the one line, and then you're good.
- 45:56All right.
- 45:57So tell us about like why do we care about these things?
- 45:58What is this?
- 45:59There is a number of things that the default configuration for Flask and for all the other frameworks, they don't do.
- 46:06And the reason why many of these things are not set by default is that they don't make sense during development.
- 46:12I mean, that's one of the reasons.
- 46:14Yeah.
- 46:15Yeah.
- 46:15Like strict transport security, for example.
- 46:17Right.
- 46:18Like you don't want to run SSL to just run localhost, right?
- 46:20Exactly.
- 46:21That would be an unnecessary complication.
- 46:24Or setting the secure bit on your session cookie.
- 46:28That's another thing that you will not do when you're working on your application, but that you definitely need to do when you deploy the application for production.
- 46:37Right.
- 46:37If I have that right, that means I'm only allowing the server and the browser to exchange that cookie if it's an SSL request.
- 46:45So maybe I type the domain name, but I don't put HTTPS at the beginning and it does a HTTP and then an HTTP redirect to the S.
- 46:52It's a very clever hack that some people exploit.
- 46:55The browser will send your cookies unless told otherwise, regardless of you going to the site via HTTP or HTTPS.
- 47:05The browser doesn't care.
- 47:07Unless you tell the browser that the cookie should only be sent on the secure connection.
- 47:12So the default is not set.
- 47:14So cookies in Flask are not secure.
- 47:16What people do is they call your site with HTTP and that basically forces the cookie to go from the client to the server on an encrypted connection.
- 47:26Right.
- 47:27And it can be intercepted.
- 47:29Right.
- 47:29And then probably your server will say, well, I don't have HTTP.
- 47:33It'll do a redirect to the same URL.
- 47:36But it's too late.
- 47:37It's too late.
- 47:38The cookie's been exchanged.
- 47:39Yeah.
- 47:39Time that's too late.
- 47:40Exactly.
- 47:40The cookie has been, you know, traveling the network, you know, unencrypted.
- 47:44Yeah.
- 47:45Yeah.
- 47:45Definitely.
- 47:45This is a good way if you don't want to think about security.
- 47:48And, you know, ideally most people, they're not security experts.
- 47:53So this is a good way to make sure that you have the baseline of your security protections all in place.
- 48:00Yeah.
- 48:00So some other things that it does is it prevents your site from being embedded in an iframe in another site automatically.
- 48:08Right.
- 48:09So people can't spoof yours, like wrap it with something else.
- 48:12Yeah.
- 48:12It turns on strict transport security, things like that.
- 48:15Yeah.
- 48:15The list of things that they do is actually way more than what I would have done myself or what I do myself by hand.
- 48:22It's definitely worth checking.
- 48:23It comes from the OWASP, like, which is a web security group.
- 48:27It comes from their recommendations and it just automatically adds it.
- 48:30And I imagine if there's a new recommendation, it'll just pick it up when you pip install upgrade this and you'll get the newer ones, which I think is nice, like kind of like versions your security.
- 48:39So, yeah, it's really easy to use and I dig it.
- 48:41So even if you're a do-it-yourselfer, it's a good place to learn all these things.
- 48:47As I mentioned, there are many in that list of things that they do that, you know, they were not on my radar.
- 48:52At all.
- 48:52Yeah.
- 48:53Yeah.
- 48:53Like the iframe one was not on my radar.
- 48:55Yeah.
- 48:56Me neither.
- 48:56Right.
- 48:57Yeah.
- 48:57So very good project.
- 48:58Yeah.
- 48:59Cool.
- 48:59All right.
- 48:59Number eight.
- 49:00Number eight.
- 49:00So HTTPY is a very nice project.
- 49:04So we're used to see when we look at tutorials, people use curl to show how to send a request on the command line.
- 49:13And curl has a somewhat unfriendly structure, especially when you need to send API requests that have JSON.
- 49:22JSON payload.
- 49:23So what I've been using for the last few years is this Python project called HTTPY, HTTPIE.
- 49:32And that is a much friendlier command line HTTP client for mostly for API use.
- 49:39Yeah.
- 49:39I love this package so much.
- 49:42It's one of the first things I install in a new server.
- 49:44It is so much better than curl.
- 49:47I almost never install packages in the global Python.
- 49:52I always create virtual environments.
- 49:54But HTTPY is in my global Python because I always use it even if I'm not working in a Python project.
- 50:02So I always want to have a copy of it readily available on my command line.
- 50:07So, for example, if you need to send headers, there's a syntax where you just use header name, colon, and then the value after you put the URL.
- 50:15If you want to send JSON variables instead of the colon, you use an equal sign.
- 50:20And there's a number of shortcuts that you can use to create an HTTP request very easily.
- 50:27And not only get requests.
- 50:29You post and put and all the harder ones as well.
- 50:32Yeah. So once you install this, probably in your semi-global namespace with a --user type of thing, then you have two commands on your terminal.
- 50:40You have HTTP and HTTPS.
- 50:42And then you just give it a URL or whatever.
- 50:45It's really nice.
- 50:46Right. It's actually the method and then the URL.
- 50:48So you can say post and then the URL.
- 50:50And then you add your headers and your variables and off you go.
- 50:53Yeah. And one of the things I really love about this is the response is color coded like a code editor.
- 51:00All right. So you get like syntax highlighting on your responses and on your headers, cookie values and everything.
- 51:06It's great.
- 51:06It's very nice. And I rarely use code anymore now.
- 51:09Yeah. Same. Love it.
- 51:11All right. Number nine.
- 51:12I talked about these new frameworks coming along because Flask and Django and Pyramid and so on don't inherently support AsyncIO.
- 51:21And a lot of them are growing to do that.
- 51:22But a lot of people want to use AsyncIO with Flask.
- 51:27I think you had Philip Jones on your podcast at some point to talk about this.
- 51:33Yeah, absolutely.
- 51:34He went and created from scratch a clone of all the Flask classes and methods within those classes.
- 51:43So basically he created a full clone of all the Flask APIs.
- 51:47And that project is called Quart.
- 51:50Nice.
- 51:50The difference with Flask is that it runs on AsyncIO.
- 51:53So you can create Async view functions.
- 51:56I believe at this point he has even added some support for some Flask extensions.
- 52:02It's almost entirely compatible with the extension system as well, he said.
- 52:06So I don't think it's 100%, but most of the common extensions will still work with Quart, which is pretty cool.
- 52:12The common extensions that don't do any blocking work, then yeah, they will work on Quart.
- 52:19So it's definitely something interesting.
- 52:21He's actually involved in this effort that's starting to add AsyncIO support to Flask as well.
- 52:29There's a pull request, a first pull request.
- 52:31I don't recall if it was merged already, but it's close to be merged to add the first shy implementation of AsyncIO support within Flask.
- 52:42He's involved with that effort.
- 52:44Otherwise, if you want to have full support for AsyncIO today using a very familiar Flask and all the Flask features,
- 52:52then Quart is actually a very good choice.
- 52:56Yeah, it's a super good one.
- 52:57All you have to do is replace the word Flask with Quart.
- 53:00If it's lowercase Flask, it's lowercase Quart.
- 53:02If it's uppercase Flask, it's uppercase Quart.
- 53:04And then you have the API.
- 53:05You do have to get the zen of AsyncIO and Async and await and all that, which is a different way of thinking,
- 53:13but it's super awesome once you get it.
- 53:15Yeah, very, very nice stuff.
- 53:17I guess that'll just kick you down the path to go,
- 53:20okay, well, now I need to figure out how to do Async SQLAlchemy or Async Redis or Async FileIO.
- 53:26Right.
- 53:26I mean, it's like a, it's a layers of exploration to take full advantage of it, but it's very,
- 53:31it's a very cool framework.
- 53:31And I'd love to see it part of Flask just proper, not have these two projects.
- 53:35It's going to take some time to redesign Flask to be fully AsyncIO compatible.
- 53:42But yeah, the first steps are there and Quart for now is a great option.
- 53:47If you really, this is interesting.
- 53:49It used to be that you would think from the side of standard Python and AsyncIO will have
- 53:56very few libraries that you could work with.
- 53:58Now there are starting to be libraries that you want to use that only support AsyncIO.
- 54:04You have a Flask application and you want to use some AsyncIO library.
- 54:09So going to Quart will be a good option in that case.
- 54:13And you talked about building kind of microservice-y type of things as well.
- 54:17And in that world, you're waiting a lot on external systems.
- 54:20And when you're mostly waiting on other things, AsyncIO is like, it's like pure magic, right?
- 54:25It really shines.
- 54:25Yes.
- 54:26That is actually the use case.
- 54:27The only one, if you ask me, that really makes sense because you can scale your little service,
- 54:34tens of thousands of clients.
- 54:36And since most of them are, you know, most of the time waiting, it can do that like nothing else.
- 54:42It's really, really nice.
- 54:44Yeah.
- 54:44Super cool.
- 54:45All right.
- 54:46You have the last one, semi-last one.
- 54:49Number 10 here is something in the same vein, but not the same.
- 54:53Tell us about it.
- 54:53So number 10 is, so let's say you like asynchronous programming.
- 54:59You're not ready to go full-blown AsyncIO.
- 55:02And you have a Flask application.
- 55:05You want to take advantage of asynchronous programming.
- 55:08What can you do?
- 55:09What a lot of people seem to have forgotten is that there were many Async frameworks that existed from before AsyncIO.
- 55:18And there are a couple of them that continue to be supported.
- 55:23And they still run really well.
- 55:25And Flask supports them, always supported them.
- 55:28And the two that I have experience with are G-event and event-let.
- 55:34And these frameworks are, they have a different philosophy compared to AsyncIO.
- 55:41AsyncIO wants to make it very explicit that you are writing asynchronous code.
- 55:47You even have new language keywords, Async await.
- 55:52So both G-event and event-let look at this from a different side.
- 55:57They try to make normal code that you're familiar with become asynchronous under the hood.
- 56:03So you write the code in the way you've always done it.
- 56:06And they implement or re-implement all the blocking functions in the Python standard library using asynchronous functions.
- 56:15They use the same interfaces.
- 56:17So these frameworks are intentionally supported by Flask.
- 56:21So Flask recognizes if it's running under these frameworks.
- 56:25Both are based on a package called Greenlet, which is the core technology that makes this possible.
- 56:31It's a native code extension or plugin for Python that you install with pip.
- 56:36And if you're running under Greenlets, then Flask makes everything work the same as if you were working with normal threads.
- 56:44I personally have been using both G-event and event-let for many years on Flask applications with very little problem.
- 56:52Cool. All right.
- 56:53Well, that's one I didn't know about.
- 56:54So that's very nice.
- 56:55That's another option.
- 56:55I bet it'll work great with Pyramid as well.
- 56:58Yeah, probably.
- 56:59You get a new implementation of sockets and threads and all those blocking functions in the standard library.
- 57:06that are asynchronous.
- 57:07And you call them in the same way.
- 57:09Yeah, so it's way below the layer.
- 57:11Yeah, like the framework wouldn't even know, right?
- 57:13Right.
- 57:14So you can use Flask.
- 57:15You can use requests.
- 57:16You know, all those libraries that do threading, networking, all those automatically become asynchronous by installing this library and using it as a server.
- 57:25Very cool.
- 57:26All right.
- 57:26Well, that's the awesome one.
- 57:27I'm going to look into that.
- 57:28Now, that's our 10.
- 57:30I wanted to throw just really quick and let you just riff on this one because I feel like this one is so in the wheelhouse of Flask.
- 57:36And yet, I don't nearly see it spoken about enough.
- 57:39Blueprints.
- 57:40Blueprints.
- 57:41That is a good point.
- 57:42When you're building an API, you probably, if you're doing microservices, you probably use a full application for each service.
- 57:52When you're building something a little bigger than that, than a microservice, you would like to structure your application in a way that's modular.
- 58:01Right.
- 58:01You don't want like a 3,000 line app.py file.
- 58:05Right.
- 58:06Probably.
- 58:06Definitely don't want that.
- 58:07I don't want one.
- 58:08So, Flask has this concept of blueprints, which a lot of people think there's something very complex or magical or sophisticated.
- 58:19And it's really a very simple idea, which is to partition the application in different modules or packages.
- 58:26And you create a blueprint, which is like a mini application that then you plug into the Flask application.
- 58:34So, you create all these application subsets, which can be a collection of routes, templates, and static files.
- 58:41You build these bundles.
- 58:43You call them blueprints.
- 58:45Each one can be a module or a package.
- 58:47And then those by themselves, they do nothing.
- 58:51But at some point, when you create your Flask application, you can plug them into the application.
- 58:58And then at that point, they become active.
- 59:01And then you can get all of those working together to form the application.
- 59:06That's pretty cool.
- 59:06I really like it.
- 59:07So, if you had like a site and you had, let's say, seven or eight view methods that had to do with account management, right?
- 59:14You would have maybe have an account module.
- 59:16And there you'd have your view methods.
- 59:17And instead of trying to somehow reverse past the app itself that you've created, but then also bring that back into the app.py.
- 59:26It's sort of circular.
- 59:27You create the blueprint.
- 59:28And then you put, say, at blueprint.route instead of app.route.
- 59:33And otherwise, it's basically the same.
- 59:35And then you've got a much nicer participant.
- 59:37It's like an application.
- 59:38Yeah.
- 59:39Definitely, most applications that I build, they have at least two blueprints.
- 59:43One that I call main, which is the core logic.
- 59:46And then one that's called auth that performs all the authentication, right?
- 59:50And one nice thing about working in this modular way is that if you wrote an auth blueprint, very likely you can take it from one project to the next.
- 1:00:01Oh, yeah.
- 1:00:01That's a good point.
- 1:00:02You can make it into a Python package that you can install with pip.
- 1:00:04Some extensions, some Flask extensions are actually blueprints.
- 1:00:08I hadn't really thought about it that way.
- 1:00:09That's cool.
- 1:00:10Because then you just go from your package.blueprint.
- 1:00:13You just go app.registerroute, the package.blueprint, and boom.
- 1:00:18It's good.
- 1:00:18That's a really interesting way to modulize it.
- 1:00:21Yeah.
- 1:00:21The one that I've used does this is Flask Bootstrap, which gives you access to some nice helpers to deal with the markup and the JavaScript required to do and the forms for the bootstrap framework for the client side.
- 1:00:34And that's a blueprint.
- 1:00:37So when you register the extension, the extension, the only thing it does is it registers the blueprint with your application.
- 1:00:42So it plugs into your application and then off it goes.
- 1:00:45So it's actually a nice way to develop extensions for Flask or your own reusable packages that you want to use in your own solutions.
- 1:00:55So many uses for blueprints.
- 1:00:56Very cool.
- 1:00:57All right, Miguel.
- 1:00:58We're pretty much out of time, I think, at this point.
- 1:01:00So those were really, really interesting.
- 1:01:03And I think people will get a lot out of those ideas.
- 1:01:06I'm sure most people, at least one of those is new to them, right?
- 1:01:09Hopefully one or two.
- 1:01:10Yes.
- 1:01:11Yeah.
- 1:01:11Very cool.
- 1:01:12I know there was one new to me.
- 1:01:13Secure the Pi was new to me.
- 1:01:15So I got something out of it, too.
- 1:01:17Me as well.
- 1:01:18Awesome.
- 1:01:19All right.
- 1:01:19So really quick, last two questions.
- 1:01:21If you're going to write some Python code, what editor are you using these days?
- 1:01:25I use three editors.
- 1:01:26So I use PyCharm sometimes.
- 1:01:28I use Visual Studio Code sometimes.
- 1:01:30And I use Vim sometimes.
- 1:01:32You have to pick one.
- 1:01:33You have to pick one.
- 1:01:35And this is also, I'm sure it's going to disappoint a lot of people.
- 1:01:38I think my favorite is Vim.
- 1:01:41Okay.
- 1:01:42And the reason is that I work a lot on remote servers.
- 1:01:46And it's really inconvenient to set up one of the larger IDEs to work remotely.
- 1:01:52It's sort of an annoyance.
- 1:01:53I just want to SSH into a box and then drop my config file for Vim.
- 1:01:58And then it works exactly like it works locally.
- 1:02:01And Vim is our learning curve.
- 1:02:04But once you learn it, it's actually very efficient.
- 1:02:07And it has Python plugins that give you some of the things that you get on the more elaborate IDEs.
- 1:02:15So I have to think that Vim is my favorite.
- 1:02:17Also, a lot of people...
- 1:02:19Somebody made you choose.
- 1:02:20Yeah.
- 1:02:20Yeah.
- 1:02:20A lot of people don't like that when I do my tutorials, my tutorial videos, I use Vim.
- 1:02:27The reason why I use Vim there is not because I like it.
- 1:02:30It's because I know that's the least likely editor that people following my tutorials will be using.
- 1:02:37And I want to force them to think about how to translate what I do instead of just copying every keystroke that I make.
- 1:02:44So I use the editor that I think it's the least likely to be known by my students.
- 1:02:51Well, interesting philosophy.
- 1:02:53So since you brought up Vim, I have a Vim joke for you.
- 1:02:55Okay.
- 1:02:56How do you generate a random string?
- 1:02:59You get a first-year computer science student to open Vim, and then you ask them to quit.
- 1:03:04To quit.
- 1:03:04Yeah.
- 1:03:07It's funny.
- 1:03:09So I did two videos that are actually very popular, showing how to set up a Flask application with PyCharm.
- 1:03:16And then I did another one with a VS Code.
- 1:03:18I'm going to do the same with Vim.
- 1:03:21Yeah, you should.
- 1:03:22It's going to be very fun.
- 1:03:24It's probably going to generate a lot of hate as well, but I'll take it.
- 1:03:28Yeah.
- 1:03:28Why are the comments disabled on YouTube for this one?
- 1:03:30I don't know.
- 1:03:30I think people will be surprised because, of course, the very first thing that I'm going to show is how to quit, of course.
- 1:03:38But, you know, you don't have to learn a lot of things to be productive with Vim.
- 1:03:43Yeah, very cool.
- 1:03:43All right.
- 1:03:44And the notable PyPI package?
- 1:03:45This isn't a PYPI.
- 1:03:47Tmux is a package that I use a lot with Python and, in general, when I work on the command line.
- 1:03:53Okay.
- 1:03:54What does it do?
- 1:03:54It's a terminal multiplexer.
- 1:03:56A lot of people ask me when they see my videos.
- 1:03:58This is another one that I get very often.
- 1:04:01They see me, you know, happily punching keystrokes.
- 1:04:04And suddenly, the screen divides into two.
- 1:04:07And I get two terminals instead of one.
- 1:04:09People don't understand what happened.
- 1:04:11And this is Tmux.
- 1:04:12Tmux is a very nice package.
- 1:04:14There are actually packages on PYPI that allow you to talk to Tmux and make it even more integrated into your Python workflows.
- 1:04:24But the package is a native utility that you install with, you know, it comes with Ubuntu.
- 1:04:29There are Ubuntu packages or on the Mac with Brew.
- 1:04:32On Windows and Chromebooks, you can use.
- 1:04:34It runs perfectly well on the Linux emulation systems.
- 1:04:37And basically allows you to create multiple terminals in the physical space of one.
- 1:04:44And one thing that is very nice, if you have it installed in a remote server, is that when you exit the server, those terminals remain.
- 1:04:52When you SSH back into the server the next day, all the terminals are there exactly how you left them.
- 1:04:59I see.
- 1:04:59So whatever you're up to is still around.
- 1:05:01It saves all the terminals.
- 1:05:02Yes.
- 1:05:03Yeah.
- 1:05:03Very cool.
- 1:05:04So that will be my nice package to mention.
- 1:05:06Yeah.
- 1:05:06That's a great one.
- 1:05:08All right.
- 1:05:09Final call to action.
- 1:05:09People may have been working on Flask for a while.
- 1:05:11They want to apply some of these tips.
- 1:05:13What do you tell them?
- 1:05:14Something that's very important to me.
- 1:05:16I had a lot of great stories that were a result of me sharing, you know, when I started on my blog and then conferences.
- 1:05:25Who knows when we're going to do conferences again.
- 1:05:27So sharing is, I think, what helped me get to where I am.
- 1:05:32Having a job that I love.
- 1:05:34My recommendation would be that using these tips or all the tips that you get from other sources.
- 1:05:40And when you come up with a solution, share it.
- 1:05:43You may think that it's something that everybody else will, you know, could easily come up with.
- 1:05:50All right.
- 1:05:50They already know it.
- 1:05:51I don't need to talk about this.
- 1:05:52I don't need to talk about it.
- 1:05:53Well, you actually do.
- 1:05:55There's always going to be someone who's, you know, on the same path you are, but it's, you know, a few steps behind that will benefit.
- 1:06:04And you will help them progress in the same way that you are making progress.
- 1:06:08So you should definitely start writing a blog or videos or tweets, you know, however, in whichever way you are comfortable.
- 1:06:16But start sharing.
- 1:06:18And what's going to happen is that all that value of work that you're sharing over time is going to speak for yourself.
- 1:06:25So you will not have to take so much effort in showing yourself to prospective employers or contracting user groups, conferences when you apply to a conference.
- 1:06:38All of that thing that you produce a little bit at a time is going to speak for you.
- 1:06:44And it's going to be your best advocate.
- 1:06:46That's what worked for me.
- 1:06:47So I think everyone should.
- 1:06:49Yeah, definitely.
- 1:06:49And I think also it's a great way to learn, even if you don't totally know, because people will say, why are you doing that?
- 1:06:53Why don't you use this package?
- 1:06:54Or you could actually do this other extension or like, well, the reason I didn't do that, I didn't know.
- 1:06:59But now I know.
- 1:07:00Right.
- 1:07:00Thanks for telling me.
- 1:07:01Right.
- 1:07:01Next time I'll do something different.
- 1:07:02Right.
- 1:07:03So, yeah, it's great.
- 1:07:04All right.
- 1:07:05Really great to catch up with you.
- 1:07:06Thanks for coming on and sharing all these tips.
- 1:07:08I think people will find them very useful.
- 1:07:10Thank you so much for having me.
- 1:07:11Bye.
- 1:07:11Bye.
- 1:07:12Bye-bye.
- 1:07:12This has been another episode of Talk Python To Me.
- 1:07:16Our guest on this episode was Miguel Grinberg, and it's been brought to you by Sentry and Linode.
- 1:07:20Take some stress out of your life.
- 1:07:23Get notified immediately about errors in your web applications with Sentry.
- 1:07:27Just visit talkpython.fm/sentry and get started for free.
- 1:07:32Start your next Python project on Linode's state-of-the-art cloud service.
- 1:07:36Just visit talkpython.fm/Linode, L-I-N-O-D-E.
- 1:07:41You'll automatically get a $20 credit when you create a new account.
- 1:07:44Want to level up your Python?
- 1:07:46If you're just getting started, try my Python Jumpstart by Building 10 Apps course.
- 1:07:51Or, if you're looking for something more advanced, check out our new async course that digs into all the different types of async programming you can do in Python.
- 1:07:59And, of course, if you're interested in more than one of these, be sure to check out our Everything Bundle.
- 1:08:04It's like a subscription that never expires.
- 1:08:06Be sure to subscribe to the show.
- 1:08:08Open your favorite podcatcher and search for Python.
- 1:08:10We should be right at the top.
- 1:08:11You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
- 1:08:21This is your host, Michael Kennedy.
- 1:08:23Thanks so much for listening.
- 1:08:24I really appreciate it.
- 1:08:25Now get out there and write some Python code.
- 1:08:27I'll see you next time.