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


Talk Python's Mastodon Michael Kennedy's Mastodon