Transcript #55: Flask, Flask, Flask, 3x Flask
Return to episode page view on github00: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 6th, 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 basically dark.
00:19 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:33 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:47 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,
00:59 not to slam Flask.
01:00 But it is pretty low barrier to entry.
01:03 And I knew Miguel was rewriting this mega tutorial.
01:06 So I begged and pleaded and got an early copy of the rewrite.
01:10 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:17 And his Kickstarter was very successful, I think.
01:21 His 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:32 He added a bunch of stretch goals 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.
01:57 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.
02:09 Very cool.
02:10 Very cool.
02:11 Speaking of new releases.
02:12 New releases and the web.
02:13 Amazing stuff.
02:14 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:27 This is a huge deal.
02:29 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
02:37 just crazy when I looked at Django is the fact of writing regular expressions for the routing,
02:42 which is I want to take this URL and figure out which view method that goes to.
02:46 That used to be a regular expression, which was painful.
02:49 Now it's much more like Flask and Pyramid.
02:53 You just put little identifiers, like variable names in cutout URL.
02:57 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 slash users slash user ID colon enter.
03:06 I think enter goes first.
03:07 But there's this nice routing syntax.
03:09 There's some nice responsive design changes, better querying over some of the query sets.
03:16 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.
03:32 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:42 Then a 3.1, then a 3.2 LTS.
03:45 So anytime you go into new territory past the LTS version, it's a major version increment now.
03:52 Okay.
03:52 It's interesting.
03:53 Yeah.
03:54 So I suspect that we'll see major Django version numbers coming faster because of that.
03:59 But I'm not sure.
04:00 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:15 For example, when they switched their tutorials by default to use Python 3 versus Python 2,
04:20 that dramatically changed the usage by numbers on PyPI.
04:26 And so now they've actually dropped support for Python 2.
04:30 It's the first version of Django that says, you know, Python 2, that's, you know, thanks.
04:34 But that's not for us.
04:36 It's Python 3 only going forward.
04:37 Yeah.
04:37 And because of that, I've seen a few people mention on Twitter that working with the code base is a lot easier now
04:44 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.
04:52 So I think it's great.
04:53 I think it's great as well.
04:54 And yeah, it definitely makes working on new features easier because you don't have to write them twice in some sense.
04:59 And there's a bunch of small changes.
05:01 I don't want to read them all off to you.
05:03 But just to give you like 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.
05:16 And then they used to do that 36,000 times.
05:18 Now they do that 100,000 times.
05:20 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.
05:28 But probably the biggest one people will notice is the simplified URL routing.
05:33 Yeah, that's nice.
05:33 So you've got a bunch of rules for us or something, huh?
05:35 I do.
05:36 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.
05:49 Or a lot of people follow on open source projects, which is PEP 8.
05:53 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 PEP 8 was a tool called PEP 8.
06:04 That's now been changed.
06:06 The name has changed to PICodeStyle.
06:08 But now I usually use Flake8 for my Linter.
06:12 And there's a...
06:15 So Flake8 covers PICodeStyle, which is PEP 8.
06:18 And then it covers PyFlakes, which does a lot of traditional Lint stuff to catch bugs.
06:22 And then a McCabe complexity checker.
06:26 And that one, I actually have tried to figure that out several times.
06:30 And I don't know what it does.
06:31 Nice.
06:31 Yeah, cyclomatic complexity is a pretty interesting metric for code maintainability.
06:37 So the idea is how many different decision paths are possible through that code.
06:43 All right.
06:44 So if you had a method of cyclomatic complexity 5, 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.
06:55 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.
07:06 I'm not sure what the check is for McCabe, what the complexity number is that they're flagging for.
07:12 But I usually turn it on anyway because I want to know if my code's a little too complex.
07:15 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 old 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 going to be using this all the time now.
07:41 That's really cool.
07:41 I feel like there's an opportunity.
07:43 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 of plugins, whether you're using Sublime, Visual Studio Code, or PyCharm, or whatever.
07:55 You could probably get a plugin that would turn that into a hyperlink that shows the details from this list.
08:00 And that would be awesome.
08:01 Oh, yeah.
08:01 That'd be good.
08:02 Yeah.
08:02 Yeah.
08:03 I'm using, so PyCharm does this, checks for a lot of this stuff.
08:06 And yeah, and I usually turn it on for pytest too.
08:10 I have my pytest plugin to check Flake 8.
08:13 Once you find an error trying to fix it, it's good to know what it is.
08:17 Yeah.
08:17 Especially when it's just E112.
08:19 Like, what the heck does that mean, right?
08:21 Yeah.
08:22 I mean, you may be really good and know them, but I don't know.
08:25 Awesome.
08:26 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, you know, a huge mess of a thousand features like you might get somewhere like AWS or Azure, you just want to have a server and work with it in a really nice way.
09:07 Check them out at DigitalOcean.com and let them know that Python Bytes sent you.
09:11 Nice.
09:11 Yeah.
09:12 We could probably contact them with requests as well.
09:15 We could probably like do some sort of API and talk to them.
09:18 But if you want to test it, you need to mock out your request, right?
09:21 Definitely.
09:21 One of the challenges, I think there's a few things that are really make testing sticky, tricky, whatever.
09:28 One of them is time.
09:30 The other one is the network and external services.
09:34 Some of that being requests type things, some of that being databases.
09:37 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.
09:46 And he has this thing called requests 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.
09:59 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 of 503 service unavailable.
10:10 Or I'd like when you make this request to this URL, return this JSON file as the response.
10:17 So really easy to swap out the testing behavior.
10:21 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:37 And, you know, if 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 off header.
10:50 It has to have maybe a user agent.
10:53 It has some other details, some kind of token type thing.
10:56 Who knows?
10:56 A lot of shared stuff.
10:58 Or if you're going to 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.
11:08 You can't do that with just straight requests so easily so that you create one of these sessions and it keeps a persistent connection.
11:13 It handles the cookies per, you know, across all the requests and stuff like that.
11:17 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.
11:27 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:36 It's kind of like your old school HTML directory.
11:39 That's right.
11:40 With some index.html and all that kind of stuff.
11:44 They just put it in there and it traverses that.
11:45 That's cool.
11:46 It's a nice interface for the developer as well.
11:48 It's cool.
11:49 Yep.
11:49 Well done, Anthony.
11:50 So you're going to give us a bit of a preview of Python 3.7, right?
11:55 Because 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.
12:03 But these are data classes have been approved by Guido and it's PEP557.
12:09 And these are kind of a different form of regular old classes.
12:13 But you can put a decorator on there for a data class.
12:16 And then you can sort of say what your some data elements and what type they are.
12:21 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:34 Yeah.
12:35 So you could say like class C colon and then just A colon int new line.
12:41 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.
12:48 Right.
12:49 Or you can even set default values.
12:51 That's pretty cool.
12:51 It lets you do more of the definers part of the class structure instead of the self dot attribute equals value through the dunder in it.
12:59 But like you said, it still generates that dunder in it 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.
13:07 And it's like, this isn't Python, 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.
13:17 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?
13:26 It does now.
13:26 It's cool.
13:27 And I also just found out that there is a 370A3 developer build that's out that has this in it.
13:35 So if people want to play with it, they can.
13:37 But I probably wouldn't do much production code with it because 3.7 isn't scheduled until June.
13:43 Okay.
13:43 Yeah.
13:44 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 is compared to and looks somewhat similar to is adders.
13:54 And adders gets a lot of attention as well.
13:56 What's the story between those two?
13:58 I don't know the history of like how much.
14:01 I know that, hi, Nick.
14:02 Oh, he's going to clobber me again for getting his name wrong.
14:04 But I think he was involved in talking with the core developers and talking about this data class.
14:11 But I'm not sure.
14:11 But anyway, there's a few.
14:13 Adders is still great.
14:15 And these data classes don't do everything that adders does.
14:19 And it has more validators and converters and a whole bunch more stuff that you can do.
14:25 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.
14:33 Okay.
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.
14:48 I think that's a really interesting point.
14:49 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, right?
15:00 You want to slow it down by sticking it in the standard library and going, well, you can only change very slowly now and only every year.
15:07 Yeah.
15:07 That's some of the reason why requests isn't in the standard library, right?
15:10 Exactly.
15:11 Yeah.
15:11 Same reason.
15:12 All right.
15:12 So for our final thing, I want to start with our first thing.
15:16 Flask.
15:16 Flask.
15:17 Ah.
15:17 My version of Flask I want to talk about is three times faster than your version of Flask.
15:21 So how does it do that?
15:23 So there's this thing called Court, which I haven't done much with Court.
15:27 but it's kind of 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.
15:35 But Court is this thing that you can use that has the same API as Flask, but is AsyncIO friendly.
15:44 So you can plug it into the super, super fast things like UV loop or AsyncPG for asynchronous Postgres, which is pretty awesome.
15:54 And there's some really amazing benchmarks there.
15:56 So 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 they're all using Whiskey, that's not how Whiskey works.
16:20 So you can plug in a Court, 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.
16:33 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.
16:41 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 Right?
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.
16:56 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.
17:06 I'm waiting on this.
17:07 And then pick it up when it gets back.
17:09 Right?
17:09 Put me back somewhere farther down in the loop when this returns.
17:13 So that's all cool.
17:14 But your database access has to have some sort of asynchronous component.
17:20 So when you do a query, you can wait on it.
17:21 Otherwise, it's kind of useless again.
17:23 So that's why it's both the Quart but also AsyncPG.
17:29 Right?
17:29 Which is pretty cool.
17:30 So it's not entirely easy to switch over depending on what you're doing.
17:34 Like if you're using SQLAlchemy.
17:36 SQLAlchemy, I don't believe, supports anything with Async.
17:38 So you're kind of out of luck.
17:40 It depends on what you depend upon, actually.
17:42 Okay.
17:43 It's easy to switch if it's going to work at all.
17:45 How's that?
17:45 Yeah.
17:45 And 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.
17:54 But they wanted to, like, I think it's a good idea to slow down the learning curve.
17:59 You've got to figure out the Async stuff.
18:00 But you don't really have to refigure 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 such a good observation.
18:09 And it's really right.
18:11 There's HTTP, AIO, HTTP.
18:14 I don't remember the order.
18:15 Sorry.
18:15 But there's that.
18:17 There's Jepronto.
18:18 There's Sanic.
18:19 There's all these other frameworks trying to take advantage of things like UV loop and Async and Await.
18:25 But they're like, and you start from scratch.
18:27 And you learn a totally new framework.
18:29 With this, you could probably go take Miguel's tutorial thing and then go make it faster.
18:34 It's kind of cool.
18:35 And that's what I plan on doing.
18:36 Yeah.
18:37 Perfect.
18:38 Be cool.
18:38 All right.
18:39 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.
18:47 That sounds really fun.
18:48 So are you familiar with the Pythonic staff of Enlightenment?
18:52 Yes.
18:52 I carried it around for a while at PyCon.
18:54 Yes.
18:54 So did I.
18:55 So a lot of people probably don't know about this.
18:57 There's a picture of me with Anthony Shaw, who I mentioned in the mocking bit.
19:02 And me walking around with this giant, I don't know, it's probably four feet tall, this big, heavy staff.
19:09 At the end, it has like a massive Python logo.
19:12 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 So he's like, hey, would you mind letting people know about the staff?
19:28 I'm like, yeah, this is pretty cool.
19:29 I'll let people know.
19:30 Yeah.
19:30 I haven't checked it out yet.
19:32 Any idea how much it is?
19:33 I think it's like a hundred bucks US.
19:34 Okay.
19:34 I may need one anyway.
19:37 I know.
19:37 Well, Christmas is coming.
19:38 Everyone needs a cool Python staff for Christmas.
19:41 Yeah.
19:42 Anyway, I thought that was fun.
19:44 So I thought I'd throw that in there at the end for you guys.
19:46 That's nice.
19:46 Cool.
19:47 Yeah, indeed.
19:47 All right.
19:48 Well, Brian, great to chat with you as always.
19:50 And thanks everyone for listening.
19:51 Thank you.
19:51 Thank you for listening to Python Bytes.
19:55 Follow the show on Twitter via at Python Bytes.
19:57 That's Python Bytes as in B-Y-T-E-S.
20:00 And get the full show notes at pythonbytes.fm.
20:04 If you have a news item you want featured, just visit pythonbytes.fm and send it our way.
20:08 We're always on the lookout for sharing something cool.
20:11 On behalf of myself and Brian Okken, this is Michael Kennedy.
20:14 Thank you for listening and sharing this podcast with your friends and colleagues.