Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Creating the Flask Python web app
Lecture: Modeling the posted data with Pydantic
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now it's time to actually receive the data.
0:02
We got it as a python dictionary out of JSON,
0:05
but I want to do more.
0:06
I want to validate it. I want to put it into strongly type classes in
0:09
Python, so that brings us to a library that's fairly new.
0:13
And it's not often used or not naturally used with flask,
0:16
but certainly should be going forward.
0:18
And that's pydantic. Let's just look an example real quick.
0:22
So pydantic is a way to very simply define classes using python types.
0:27
This id is an integer. This is a string with a default value of John
0:31
Doe, because it's set to a string.
0:33
This is a daytime, but it could be none or nullable.
0:38
Then we have a friends, which is a list of integers,
0:41
and then we have some, JSON Data is actually a dictionary,
0:45
but right it came from JSON being posted over and check this out.
0:48
If we just go to this class way to find and say,
0:51
load that data, it's going to validate and potentially even convert it.
0:55
Notice that the ID. Here submitted as a string,
0:57
but it can be parsed to an integer,
1:00
and it is, notice we have a list of integers,
1:04
but one of the strings. One of the things in the list is a string
1:09
in the result it actually converts
1:11
it and validates everything through a list of integers,
1:14
even though they're not. Not all of them were and so on, really,
1:17
really need. So what we're gonna do is we're going to use pydantic for our
1:22
application, and we're going to define sum
1:24
pydantic models go along with it.
1:26
So it's time for a new requirement.
1:28
A second one, I guess pydantic,
1:31
And let PyCharm install that.
1:32
If not pip install -ourrequirements.txt is the way to go.
1:37
And what we're gonna do is we're gonna create a separate section where these classes that
1:42
model the state exchange go. We're gonna have a couple of them throughout the course
1:45
We'll call these models that's typically the way they're referred to within pydantic.
1:51
And I'm going to add something here or order.
1:54
We're going to get an order model and within here,
1:58
we're going to have a customer,
2:04
and we're going to have a cake.
2:07
But let's see about finding a couple of classes here.
2:11
So have a class customer and I'll just put it empty for a second.
2:16
And I have a class cake,
2:18
which is also empty for a second, in our order is going to be a class
2:22
I'll have a cake order and it's going to have a couple of things.
2:28
It's going to have a customer,
2:30
which of type, the mer, import that which is going to be cake.
2:36
So it's also going to have the price that is just going to be a float
2:41
that how we specified it.
2:43
Let's go double check, our price is part of our cake.
2:52
Let's change this. Let's make it actually, separate like this match, what we're doing, perfect
3:02
there and published that now this is pretty neat.
3:08
But in order for us to use pydantic,
3:10
you can see there's nothing about pydantic yet.
3:12
These are just all types in python.
3:14
Does this have to derive from base model,
3:17
which is a pydantic type? There has to be true here,
3:21
but also for the nested items like customer and cake.
3:24
So let's go over here and make this a base model and this a base model
3:32
just like I did with the order.
3:36
Let me go and put the types here, or the fields that we're going to need
3:40
along with their type. We look back for our order,
3:46
we're gonna need a topping, and I'll just copy this over bar cake we're going
3:52
to have is a topping. And all of these are going to be strings like
3:56
so by sounds like it could be an integer.
4:04
But remember, that's small, medium,
4:06
large and so on. So here we've got these types,
4:09
they don't have default values and they're not optional,
4:11
so they must be supplied for this to validate.
4:15
I want to do something similar for a customer.
4:17
And let's go double check what we're passing over here, again
4:26
These are all strings like that.
4:30
And as simple as defining these little classes here and then putting together like this,
4:36
we're gonna get automatic data parsing and conversion,
4:39
and it'll even reverse the hierarchy.
4:41
Right? So it knows there's a customer and the customer is itself one of these
4:44
models and so on. So it's going to build this all up,
4:47
and this is gonna be really easy for us to do.
4:49
Let's just go over here and use our model.
4:53
So we'll go over here today.
4:55
Let's call it order. We're going to use our cake order.
5:00
And all we have to do is take the dictionary pass it as keyword arguments.
5:04
The way to do that is our store data.
5:07
And now let's just return our order.
5:12
And I guess we could set a little break
5:13
point this while this cake order because it's conflicting with the name of the function also
5:20
called Order. Here we go.
5:23
All right, so let's put a little break point here,
5:26
I guess. And have it run again.
5:29
How about we order some more cake?
5:33
Has more cake. Feel like some rainbow cake with vanilla coming on?
5:55
Alright, here we go. This is gonna call back through ngrok into our
5:58
service and let's see what happens here.
6:03
Hit a breakpoint and we've got our data,
6:05
but we have not yet converted it notice,
6:07
Here's our customer info. If we take one more step in debugger,
6:13
we've now converted our cake order over and check this out.
6:16
If we expand the cake order,
6:17
we've got a cake and we've got a customer.
6:20
We can expand the cake and it's got all the data flavor frosting and so on
6:24
And the price is an integer.
6:28
How did the price come over in the data?
6:29
Let's have a look. Look at the price came over as a string.
6:34
Well, 24. That's not actionable.
6:37
We can't use that. And so in order for this to actually be a number
6:40
we would have to do that conversion our own,
6:43
our ourselves and make sure that that worked not with pydantic automatically is converted.
6:48
And all the hierarchical things are parsed that we're going to use pydantic for all the
6:52
data exchange or our entire app.
6:55
Really, really nice. It's a great extra layer to add into our API to
6:59
make it super easy to exchange JSON data with Twilio studio and any other external API's
7:05
that we work with. One quick thing.
7:08
I did hear that maybe we should just throw on it doesn't really matter because we're
7:11
not actually using this response or anything.
7:14
But in order to return one of these as part of this dictionary,
7:18
it can't directly be returned. We have to do a really simple dictionary like this
7:22
okay, that will convert it over to a dictionary that then I thought can
7:27
take and turn that into your response. Now this should be perfect.