Brought to you by Michael and Brian - take a Talk Python course or get Brian's pytest book


Transcript #55: Flask, Flask, Flask, 3x Flask

Return to episode page view on github
Recorded on Wednesday, Dec 6, 2017.

00:00 Hello and welcome to Python Bytes where we deliver Python news and headlines directly to your earbuds.

00:05 This is episode 55, recorded December 6, 2017.

00:10 I'm Michael Kennedy.

00:11 And I'm Brian Okken.

00:12 And Brian, can you believe it's December?

00:13 Yeah, it's getting cold out.

00:15 It's getting cold. I look outside, it's the middle of the day and it's still like basically dark.

00:20 So I guess we're getting there.

00:21 Yeah.

00:21 So before we get into our picks for the week though, let's just say thanks to DigitalOcean.

00:26 They have a ton of awesome servers for you.

00:29 The websites I run run on DigitalOcean.

00:32 So, we'll tell you more about that later.

00:34 However, one option, I guess one of the servers I have actually runs Flask.

00:39 And Brian, I hear you're kind of digging Flask these days.

00:42 - Yeah, I am actually going through Miguel Grimberg's Flask mega tutorial.

00:48 So, I'm pretty excited about that.

00:50 And I got actually from the, I think I took the advice from you to try something simple like Flask at first, not to slam Flask, but it is pretty low barrier to entry.

01:03 And I knew Miguel was rewriting this mega tutorial, so I begged and pleaded and got an early copy of the rewrite.

01:11 So I'm partway through it right now.

01:12 But he did a Kickstarter to try to rewrite it.

01:16 The first one was in 2012.

01:18 And his Kickstarter was very successful, I think.

01:22 'cause part one of the rewrite is available right now today.

01:25 - Yeah, that's awesome.

01:26 And I know Miguel's been putting a ton of work into the rewrite.

01:29 I was so excited to see his Kickstarter be successful.

01:33 He added a bunch of stretch scrolls to do additional sections.

01:38 He has an ebook version and a video version coming out of it, coming out as rewards from it.

01:43 He hasn't done the videos yet.

01:44 He and I were actually just talking today about the videos.

01:46 So that'll be fun.

01:48 But yeah, so if you want to learn how to get going Flask, his work is really great.

01:53 And so definitely check it out.

01:54 - He does have, what he's going to do is he's going to release one part every week, but if you can't wait that long, you can buy his ebook.

02:01 I think it's just like 10 bucks or something.

02:03 - Yeah, totally affordable.

02:04 - And that's what I'm reading right now.

02:05 And yeah, his video, he says he's planning on January for the video version.

02:09 - Yep. Very cool. Very cool.

02:11 - Speaking of new releases.

02:12 - New releases and the web, amazing stuff.

02:15 Django 2.0 is released.

02:17 And this is a huge, huge change.

02:19 It has been many, many moons since major point release of Django has come out.

02:26 I mean, after all, it's only version two, right?

02:28 This is a huge deal, and it's a lot of cool new features.

02:32 One of the things that they added that I really like, and I don't know, it's always made me just crazy when I looked at Django, is the fact of writing regular expressions for the routing, which is I want to take this URL and figure out which view method that goes to.

02:47 That used to be a regular expression, which was painful.

02:50 Now it's much more like Flask and Pyramid.

02:54 You just put little identifiers, like variable names in cutout URL, and then that's how it maps over.

02:59 And you even have types.

03:00 You can say it has to map to /users/userid, colon, int, or I think int goes first.

03:07 But there's this nice routing syntax.

03:10 There's some nice responsive design changes, better querying over some of the query sets.

03:17 These are all cool.

03:18 They have a new versioning, what they're calling loose form of semantic versioning.

03:25 So if you look at the possible versions, we have two, maybe it'll be a 2.1, and then a 2.2, and then that 2.2, maybe that's something they're calling stable, long-term support, LTS.

03:37 So it might be 2.2 LTS.

03:39 And then if they go anything beyond the LTS, that's a three.

03:43 then a 3.1, then a 3.2 LTS.

03:45 So like anytime you go into like new territory past the LTS version, it sort of is a major version increment now.

03:52 - Okay, it's interesting.

03:54 - Yeah, so I suspect that we'll see major Django version numbers coming faster because of that, but I'm not sure, I guess we'll have to see.

04:01 - And then there is some exciting thing about Python 3.

04:05 - Yeah, it's very exciting.

04:06 The legacy Python is dealt yet another blow.

04:09 So Django has had a significant disproportionate influence on the adoption of Python 3.

04:16 For example, when they switched their tutorials by default to use Python 3 versus Python 2, that dramatically changed the usage by numbers on PyPI.

04:26 And so now, they've actually dropped support for Python 2.

04:31 It's the first version of Django that says, you know what, Python 2, thanks, but that's not for us.

04:36 It's Python 3 only going forward.

04:37 - Yeah, and because of that, I've seen a few people mention on Twitter that working with the code base is a lot easier now because there aren't a lot of backwards compatible things in there.

04:49 They were able to clean up the code base quite a bit for this, so I think it's great.

04:53 - I think it's great as well, and yeah, it definitely makes working on new features easier 'cause you don't have to write them twice in some sense.

05:00 And there's a bunch of small changes.

05:01 I don't wanna read 'em all off to you, but just to give you a sense, down in django-contrib.auth, luckily they're doing password hashing and folding.

05:11 So not just hashing with salt, but then you take that and you hash that, and you take that and you hash that, and then they used to do that 36,000 times.

05:18 Now they do that 100,000 times, so it's more computationally expensive to guess the password if somehow the database were to leak.

05:25 And so there's just tons of little cool changes like that throughout there as well, but probably the biggest one people will notice is the simplified URL routing.

05:33 - Yeah, that's nice.

05:34 So you got a bunch of rules for us or something, huh?

05:36 - I do.

05:37 - What's up with that?

05:37 - I'm usually somebody that doesn't follow a lot of rules.

05:41 But one of the things I embraced when coming into Python is the notion of that there's kind of a coding style that everyone follows, or a lot of people follow on open source projects, which is PEP 8, and then it's extended.

05:55 So there's, when I started using type checkers like Lint, or at the time I started it, The way to check for PEP8 was a tool called PEP8.

06:04 That's now been changed, the name has changed to PyCodeStyle.

06:08 But now I'll usually use Flake 8 for my linter.

06:12 And there's a, so Flake 8 covers PyCodeStyle, which is PEP8, and then it covers PyFlakes, which does a lot of traditional lint stuff to catch bugs.

06:23 And then a McCabe complexity checker.

06:26 And that one, I actually have tried to figure that out several times and I don't know what it does.

06:31 - Nice, yeah, cyclomatic complexity is a pretty interesting metric for code maintainability.

06:38 So the idea is how many different decision paths are possible through that code, right?

06:44 So if you had a method of cyclomatic complexity five, there's five separate execution paths that could go through there.

06:51 There could be one if case that does an early return, another that's an if, elif, elif, and taking all the possible ways in which you could go through those conditionals and loops and whatnot, there would be five possibilities.

07:02 So meaning basically you need five tests minimum to cover that.

07:05 - Okay, I'm not sure what the check is for McKay, what the complexity number is that they're flagging for, but I usually turn it on anyway, 'cause I wanna know if my code's a little too complex.

07:16 The issue with it is a lot of these spit out an error message with a one-liner explaining what it is.

07:23 And so what I have for us today is called the Big Ol' List of Rules, which translates all of those errors and warning numbers into very nice one-page descriptions of what they are with links to more information.

07:38 And I really like it.

07:39 I'm gonna be using this all the time now.

07:41 - That's really cool.

07:42 I feel like there's an opportunity, first of all, well done Grant for writing this and putting this all out for everyone.

07:48 But I think there's an opportunity for editor plugins, whether you're using Sublime, Visual Studio Code, or PyJarn or whatever, you could probably get a plugin that would turn that into a hyperlink that shows the details from this list, and that would be awesome.

08:01 Oh yeah, that'd be good.

08:02 Yeah, I'm using... so PyCharm does this, checks for all this stuff, and I usually turn it on for pytest too, I have my pytest plugin to check FlateGate.

08:13 Once you find an error trying to fix it, it's good to know what it is.

08:17 Yeah, especially when it's just E112, like what the heck does that mean, right?

08:21 Yeah.

08:22 I mean, you may be really good at knowing, but I don't know.

08:25 - Awesome, so before we get on to the next item, just want to let everyone know that this podcast and really all the sites that I run are coming to you through DigitalOcean.

08:35 I have, gosh, it's just a growing list.

08:37 I think I probably have eight servers over there now doing all sorts of hard work and working together on various services and database connectivity and whatnot.

08:45 So super excited about working with DigitalOcean and talking about their stuff because it's really, really been great to work with.

08:53 So if you're looking for cheap, reliable, fast servers that are simple and not a huge mess of a thousand features like you might get at somewhere like AWS or Azure, you just wanna have a server and work with it in a really nice way, check them out at digitalocean.com and let them know that Python Bytes sent you.

09:11 - Nice.

09:12 - Yeah, we could probably contact them with requests as well.

09:15 We could probably do some sort of API and talk to them, but if you wanna test it, you need to mock out your request, right?

09:21 - Definitely.

09:22 One of the challenges I think, there's a few things that are really make testing sticky, tricky, whatever.

09:29 One of them is time.

09:31 The other one is the network and external services.

09:34 Some of that being requests type things, some of that being databases.

09:38 So any chance you get to cleanly sort of mock that out is really nice.

09:43 And so this one actually comes from a friend of the show, Anthony Shaw, and he has this thing called request static mock.

09:50 And I think we were recently talking about something with mocking requests.

09:54 And he's like, you should check out request static mock.

09:58 And so I did, and it's pretty cool.

10:00 So I decided to make it one of the things we're talking about this week.

10:03 And the idea is you can create a request session, and then mock that out like, hey, I want that return a 503 service unavailable.

10:11 Or I'd like when you make this request to this URL, return this JSON file as the response.

10:18 So really easy to swap out the testing behavior, like if your code somewhere deep down calls into requests, but you can do it without monkey patching.

10:26 - Yeah, that's the neat part is it's without monkey patching or doing a lot of these test-based mocks.

10:32 It's pretty cool.

10:33 - Yeah, it definitely is.

10:34 Yeah, you don't really mock stuff as much.

10:36 You kind of just plug in the session.

10:38 You know, for people who don't know, the session object is a thing that comes from requests, which is actually pretty interesting.

10:43 So suppose you're going to start talking to a service and every single request has to have an auth header, it has to have maybe a user agent, it has some other details, some kind of token type thing, who knows, a lot of shared stuff.

10:58 Or if you're gonna try to submit a form and then you need to take a session on the server, like a cookie-based session, and then go and do other things, you can't do that with just straight requests so easily.

11:10 So you create one of these sessions and it keeps a persistent connection, it handles the cookies per, you know, across all the requests and stuff like that.

11:18 So that's really handy.

11:19 And what Anthony's thing does is create a sort of testing session variant of that.

11:25 So it's pretty cool.

11:26 So you can mock that thing out.

11:27 - Yeah, and the way you put it together too, is the data that's coming back is just in like a, you can just set it up as like a tree structure in your file system.

11:37 It's kind of like your old school HTML directory.

11:40 - That's right, with some index.html and yeah, all that kind of stuff, they just put it in there and it traverses that, that's cool.

11:46 - It's a nice interface for the developer as well.

11:49 It's cool.

11:50 - Yep, well done, Anthony.

11:51 So you're gonna give us a bit of a preview of Python 3.7, right?

11:55 'Cause there's some pretty awesome stuff that just got approved or finalized.

11:59 - Data classes, which I didn't know it was on the fence for a while, but these are data classes that have been approved by Guido and it's PEP 557.

12:10 And these are kind of a different form of regular old classes, but you can put a decorator on there for a data class.

12:16 And then you can sort of say what your--

12:19 some data elements and what type they are, and you can assign defaults.

12:23 And the cool thing about that is you don't have to write your own init statement.

12:27 It kind of generates one for you.

12:29 So the first time I saw these, I'm like, wait, that's not valid Python.

12:32 What is this?

12:33 What language is this?

12:35 Yeah, so you could say like class C colon, and then just A.

12:40 colon int new line B colon int new line.

12:43 And you just start out with a class when you create it that has an A and a B, and those are both none, right?

12:49 Or you can even set default values.

12:51 It's pretty cool.

12:51 It lets you do more of the define as part of the class structure instead of the self.attribute equals value through the Dunder init.

12:59 But like you said, it still generates that Dunder init and then moves over the default values and all that.

13:03 >> I kind of like the syntax.

13:04 The first time I saw it, like you said, it's bracing and it's like this isn't Python.

13:09 But it's kind of nice that you can just put that in one place and not worry about it too much.

13:15 It's pretty clean.

13:16 Definitely like it, yeah.

13:17 I find myself doing this sometimes, and I'll just have to set everything to none, or to zero, or something like that, because it won't work otherwise.

13:25 But guess what? It does now.

13:26 It's cool.

13:27 And I also just found out that there is a 3.7.0.A3 developer build that's out that has this in it.

13:35 if people want to play with it, they can, but I probably wouldn't do much production code with it because 3.7 isn't scheduled until June.

13:43 - Okay, yeah, so it's a little ways out, but still exciting to see this coming.

13:46 I think this is pretty nice.

13:48 So one of the things that this feels like, I think it's compared to and looks somewhat similar to, is Adders, and Adders gets a lot of attention as well.

13:57 What's the story between those two?

13:58 - I don't know the history of how much, I know that, hi Nick, oh, he's gonna clobber me again for getting his name wrong, But I think he was involved in talking with the core developers when talking about this data class, but I'm not sure.

14:12 But anyway, there's a few--

14:14 Adders is still great, and these data classes don't do everything that Adders does.

14:20 And it has more validators and converters and a whole bunch more stuff that you can do.

14:26 So it doesn't completely take the place of Adders.

14:29 But for simple cases, I think it's a simpler interface.

14:32 Yeah, OK.

14:33 That sounds good.

14:35 The best example that I heard of why people wanted it in there is because the core developers wanted to use it on Python itself.

14:42 And you can't use non-standard library stuff within the core of Python.

14:48 Yeah, I think that's a really interesting point.

14:50 And Adders is changing fast.

14:51 It's still getting a lot done to it.

14:53 And you don't want to hamper it and cover it in quicksand or some sort of tar.

15:00 You want to slow it down by sticking it in the standard library and going, well, you change very slowly now and only every year.

15:07 >> Yeah, that's some reason why requests isn't in the standard library, right?

15:10 >> Exactly. Yeah, same reason.

15:12 All right. For our final thing, I wanted to start with our first thing.

15:16 >> Flask.

15:16 >> Flask. My version of Flask I want to talk about is three times faster than your version of Flask.

15:22 How does it do that? There's a thing called Court, which I haven't done much with Court, but it's like a wrapper around some of the asyncio stuff, but also an API that can run Flask apps.

15:34 Like I said, I haven't done a ton with it, but Court is this thing that you can use that has the same API as Flask, but is asyncio friendly.

15:44 You can plug it into the super, super fast things like UV loop or async PG for asynchronous Postgres, which is pretty awesome.

15:54 There's some really amazing benchmarks there.

15:56 Flask, along with Django and along with Pyramid and all the others, they don't support any async and IO stuff.

16:04 And they can't take advantage of basically releasing the thread to go do other work when it's say waiting on a database or on a call over request or something like that.

16:15 Just because it's they're all using WSGI, that's not how WSGI works.

16:20 So you can plug in a quart, which basically has the same API as Flask, and you just have to make a few minor changes to get your code to go much faster.

16:31 So here's an article with a demo application, and they've got benchmarks and stuff saying we're getting roughly three times the speed by just switching a few things around in the app.

16:40 >> Yeah, I think that's cool. I definitely need to try this.

16:43 >> Yeah. So the things you have to do, obviously, if you want to take advantage of AsyncIO is you have to make your functions Async.

16:51 Otherwise, they're just regular functions.

16:53 They go just the same speed.

16:54 So you would put Async in front of your view methods, And then when you call into things like databases or web services via request say, you have to await those to basically tell Python, give up my thread, I'm waiting on this and then pick it up when it gets back, right?

17:09 Put me back somewhere further down in the loop when this returns.

17:13 So that's all cool, but your database access has to have some sort of asynchronous component. So when you do a query, you can wait on it, otherwise, it's kind of useless again. So that's why It's both the court but also async PG, which is pretty cool.

17:31 It's not entirely easy to switch over depending on what you're doing.

17:34 If you're using SQLAlchemy, SQLAlchemy I don't believe supports anything with async, so you're out of luck.

17:40 It depends on what you depend upon actually.

17:43 It's easy to switch if it's going to work at all. How's that?

17:45 >> Yeah. One of the things I think is neat about this and it's a clever idea, is instead of inventing a completely new framework, It is a completely new framework, but they wanted to, like, I think it's a good idea to slow down the learning curve.

17:59 You gotta figure out the async stuff, but you don't really have to re-figure out how the framework works, because they've said--

18:05 - Yeah, that's cool.

18:06 - The framework's just like Flask.

18:07 - That is so, that is such a good observation, and it's really right.

18:11 There's HTTP, I, AIO, HTTP, I don't remember the order, sorry, but there's that, there's Gepronto, there's Sanic, there's all these other frameworks trying to take advantage of things like UV loop and async and await, but they're like, "And you start from scratch, and you learn a totally new framework." With this, you could probably go take Miguel's tutorial thing and then go make it faster. It's cool.

18:35 >> That's what I plan on doing.

18:36 >> Yeah.

18:37 >> Be cool.

18:38 >> All right. Well, that's our news for this week, Brian.

18:41 Anything you got going on over there?

18:44 >> No, I'm just trying to learn Flask, man.

18:46 >> Awesome. That sounds really fun.

18:48 So are you familiar with the Python staff of enlightenment?

18:52 Yes, I carried it around for a while at Python.

18:54 Yes, so did I. So a lot of people probably don't know about this. There's a picture of me with Anthony Shaw, who I mentioned in the mocking bit, and me walking around with this giant, I don't know, it's probably four feet tall, this big heavy staff at the end. It has like a massive Python logo. And so one of the guys that was involved in creating that thing originally actually decided, so many people asked for it, he's creating a store where you can buy your very own Pythonic Staff of Enlightenment.

19:24 He's like, "Hey, would you mind letting people know about the staff?" I'm like, "Yeah, this is pretty cool. I'll let people know." >> Yeah, I haven't checked it out yet. Any idea how much it is?

19:33 >> I think it's like a 100 bucks US.

19:34 >> Okay. I may need one anyway.

19:37 >> I know. Well, Christmas is coming.

19:39 Everyone needs a cool Python staff for Christmas.

19:42 >> Yeah.

19:43 >> Anyway, I thought that was fun, so I thought I'd throw that in there at the end for you guys.

19:46 That's nice. Cool. Yeah, indeed. All right. Well, Brian, great to chat with you as always.

19:50 And thanks everyone for listening. Thank you.

19:53 Thank you for listening to Python Bytes. Follow the show on Twitter via @pythonbytes. That's Python Bytes as in B-Y-T-E-S. And get the full show notes at Python Bytes.FM. If you have a news item you want featured, just visit Python Bytes.FM and send it our way. We're always on the lookout for sharing something cool. On behalf of myself and Brian Okken, This is Michael Kennedy. Thank you for listening and sharing this podcast with your friends

Back to show page