Rock Solid Python with Python Typing Transcripts
Chapter: Frameworks Built on Typing
Lecture: Web frameworks using Type Hints
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Continuing our exploration of frameworks and tools and libraries built on Python typing,
0:07
I want to look at three different web frameworks, two of them built on Pydantic, one that has optional Pydantic support.
0:15
So first of all, we have FastAPI. This is an incredibly popular framework. Notice the 63,000 GitHub stars at the moment.
0:24
That puts it right on par with Flask and Django, even though it's only a fifth as old as those
0:29
frameworks, something along those lines. So really, really popular framework. This is
0:33
based on pedantic. It makes deep use of type information or also a gold sponsor of them.
0:41
So that's awesome. Now, if you look down here, you'll see a quick example. So for example,
0:46
we could create something that responds to a get request to forward slash and it just
0:51
returns this Hello World. But we can come over here and say you could also get an item passing the item ID which goes here and it's an integer.
1:01
So this is pretty awesome. And when you say slash item slash something, just like with Pydantic, if everything on the web is a string,
1:10
so it looks and tries to parse it to an integer. And if it can't be, it's gonna not even let this function run. It'll say there's an error
1:17
because it's deeply based on this typing. Again, we have a query string. So question mark Q equals something,
1:26
And that could either be a string or none a union here. So I'd rather again, say optional string, but you do you. And here we get that data passed in.
1:34
See if there's a slightly more complicated example, you get awesome documentation. We're gonna look this in more details.
1:40
Yeah, here's the final interesting thing is, we could have a Pydantic model that has rich information here, this item.
1:47
And we can say this actually takes an item right there to be updated. And so this is gonna be either a put or a post,
1:58
something where a rich body of data is posted to it, like a JSON post, and it'll actually do that Pydantic parsing before it even lets your code run,
2:07
all the validation, everything we saw about Pydantic. Lots of fun stuff about FastAPI. We'll look an example at that, like I said.
2:15
If you don't want to pick a totally new framework, but you love Django, check out Django Ninja. Yeah!
2:23
Fast to learn, fast to code, fast to run, very awesome. Based on Pydantic and both FastAPI and Django Ninja have async support as well.
2:32
So this is kind of the Django equivalent. And here you have another example. Gonna API get int int, convert some to ints automatically.
2:43
Again, here's an item for a rich type. And here's some operation that's being passed in, automatically parsed. This is a schema,
2:52
but I'm pretty sure that schema is going to derive from a pedantic type as well. So quite popular, not as popular as FastAPI
3:00
'cause that's kind of meteoric, but here you go. Final one to look at, this one's pretty new, coming on strong, it has, let's see,
3:08
3000 GitHub stars, but it's quite new, called Litestar, Litestar is pretty excellent. We come down here, we'll see some examples.
3:18
Let's go further down. You can have method-based views or class-based views. Here's one that's a class just for the heck of it.
3:28
And notice this one has some data pass here and it's called a DTO, kind of like the model in FastAPI.
3:36
And this data is going to be some kind of rich type, very much similar to a pedantic, the item thing we just saw, and quite neat.
3:45
Again, supports async like you would expect. that are all about the optional return type as well. Somewhere they have a user, yeah, here we go.
3:54
This post to create a user, the data for the user is passed in, and then the one that's created with, say, the primary key and created date set
4:03
is returned to the user as well. So, awesome, awesome framework. Definitely interested in checking this one out more.
4:10
I haven't done too much with the second two, but FastAPI I've done a lot with. Recommend pretty much all three of them, okay?
4:18
Very cool and you can see they each have their own slight variation. For example, if we go over here and search for Pydantic.
4:28
This has support for data classes, typedex and both versions of Pydantic, adders and message spec, which is actually a really cool
4:37
alternative to Pydantic, I suppose. Maybe a good way to put it, but yeah, so they have their own way of working with typing, but they all have
4:47
typing at the core of how they work. So give these a look. You can use your type knowledge that you have now to really build some awesome web APIs
4:55
in surprisingly little amount of code.