Collier King - FastAPI: A new and exciting Python web development framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
cool all right so welcome to the austin python meetup our ninth meetup of the 2021. yeah thank you everyone for coming from austin and sometime from around the world uh to today we have two great talks uh we have a collier king who's gonna do a talk on the fast api and then we have uh gil forsyth who's gonna do a talk on marsh if i pronounce that correctly all right and uh yes again like yeah feel free to do introduce yourself and the better the best you can or the way you like and we're going to start with uh collier so clear the floor is yours all right uh can everyone see my screen uh yes yeah you sure okay all right great well thanks thank you for having me i'm here to talk about fast api an exciting new web framework in python a little bit about me uh i'm a python and automation enthusiast i love automating stuff we love python i have about 10 years working in technical roles in finance and marketing and currently i'm a software development consultant at my company fuse cloud um so what are we gonna talk about today well first we're gonna talk about what what is fast api and then we're gonna talk about web frameworks we're talking a little bit about background and history and then we're going to look more at the fast api community what traction has had so far what uses could it be used for and then why fast it why fast api what's what's so special about it what are the batteries included with fast api and then we'll do a really short high level coding demo we'll look at some a fast api app and then we'll talk about my meetup group that i organized here in austin that's the fast api developers meetup group where we only focus on fast api as the name suggests so what is fast api well it's a next generation web framework for developing and serving web apps and apis and so four kind of main things about it it uses modern quote modern python features like the async io framework and data validation with pedantic it's fast and performant it's simple but flexible and has a very strong and growing community so what's a web framework well it's a package that makes creating web apps easier by abstracting away low level details like you know http pro protocol sockets processes all the network kind of layer stuff that uh you know goes into web development and the internet this this kind of abstracts it away for us and uh you know depending on the web framework developers can follow a lax or rigid pattern for developing apps apis and then over here we have some pictures that just show some examples of popular front end frameworks some popular back-end frameworks so the front end we we know kind of react view angular the kind of javascript world on the back end you have all types different languages represented you have django flask from python and now fast api and then you have laravel from php ruby on rails for ruby and then node.js of course so fast api firmly sits in that backend uh area so what what's the history of python web frameworks well it goes back quite a while like it goes back to when django was released uh which was in around 2005 so django's been on the scene for you know over 15 years a few years later in 2010 flask came out flask was kind of an alternative solution that was more customizable django was kind of you know a lot of features built in uh not as many options the developer had but it did you know it definitely is well represented in the you know corporate corporate world with companies like instagram uh and some others using it i think netflix used it um so then what changed so december 2016 python 3.6 came out and that had a sync support so async i o came out and that was kind of a renaissance for the python web development framework world so you had stuff come along like tornado starlet sanic core which all our async web frameworks and we're gonna talk more about what async means but it was a step change forward for web frameworks uh when python started supporting async and then uh not too long ago about two and a half years ago in december 2018 this guy sebastian ramirez released fast api and so since then fast api's really caught on and some of the uh you know big features about which we'll talk about really drove its success uh and so it's a it's a new framework and it's you know still changing and growing and so that's what we're going to talk about um so just a really quick graphic to look at um you know python we all always talk about batteries included in python there's so many great things in the standard library so many great packages and libraries people have built uh it really does a lot well so when we look at that just in the web framework space you have you know batteries included and then do-it-yourself scale you have that same scale so django has a lot of batteries included it's very opinionated about what you can use what you can't use flask is on the very other side and it's pretty much do it all yourself uh now you know there's flask has been around for 10 years people have developed helper packages to help uh do different things fast api is is definitely more on the do-it-yourself side but it has a lot of cool stuff built in that takes away a lot of uh you know headache from from web development so uh it kind of sits in the middle and it has a lot of good stuff built in but it also lets you do a lot of stuff that you want uh so popularity i mean you know just in the last two and a half years you can kind of see how how much traction fast api has gotten the little red time series right there shows when fast api came out and these are the stars on github over time so we're you know just under 40 000 stars and you can really see that growth compared to flask and django which have been around for you know a decade um so i think this is just gonna continue to grow and uh you know it's it's gonna continue to gain momentum uh so the community so great community behind fast api the github repo has you know 35 000 stars there's something like four and a half thousand twitter followers there's about 100 contributors there's about 50 top reviewers and then 40 experts who are people that answer questions uh and like you know respond to issues on github so you know just and then there's some gold there's some you know pretty nice sponsors they have uh you know solid commit history that's going on the project so you know you feel pretty confident that this project is is you know here to it's here to stay it's going to be around for a while um company traction so just some quotes from people talking about fast api there's you know some people from different companies like this guy from microsoft says he's planning to use it for all of his team's ml services at microsoft and integrating core windows products uh and office products uh with with fast api and then this person from uber says uh they're using it to spawn a rest server that can be query to obtain predictions again a machine learning use case and then netflix uh they built you know their crisis management orchestration uh framework around uh fast api which is awesome and then just a little you know search for jobs that have a fast api in the in the description you know i just did that search today and it was like 326 results so definitely not like a minuscule amount of of interest out there for fast api so then the ecosystem i mean we all you know there's so much stuff there's so many great tools in python and beyond uh and fast api there's examples of all of these things on the screen and more uh integrating fast api with these things so if you're going to build a web app or api and you're looking for a security solution or you know a solution to run background tasks or you you want to integrate it with your favorite database or orm you know there's so many different ways for front-end web frameworks you can use purely pythonic frameworks like jinji2 chameleon and mako or you can hook it up to reactor view and then you know of course it plays nice with the cloud google cloud aws etc and the machine learning uh i mean python really takes the cake for machine learning these days and so you can couple it with things like tensorflow pytorch pandas uh you know whatever you want to build kind of data driven machine learning apps and then testing devops there's of course the great uh ecosystem around testing that you can dip into for that if you want to test your app maybe maybe like you know run a bunch of traffic on it and see if it how it's going to perform you might use something like locust so you know everything in this picture and a lot more i guess is the point of this slide so so when we actually talk about fast api like what are the things that about it that have made it take off what are the core technological components that uh that really enable it to do well well i would argue that there's kind of three big pieces um and before we talk about those we just want to look at this little graphic someone put uh online so you know you have fast api and then you have something called starlit so starlet was an async web framework that came along uh kind of that renaissance period of python frameworks after async came out and uh fast api builds on starlet so starlit has the core technology and fast api harnesses that to kind of go you know go farther and then below that you have something called uvicorn so this is a asynchronous server gateway interface server and if you've used flask you've probably used something called g unicorn so that's a wizki server a web service gateway interface server and as these servers are just like that step change forward for uh for web frameworks and python and we'll talk a little bit more about that in a sec but basically starlet and uvicorn allow async support for fast api and then as far as automated documentation goes you know doc we'll look at this a little bit more but fast api offers automated documentation so it talks about uh you can host documentation for your app automatically which is great and that's something through swagger and op open api and then lastly a big component that helps with development speed and just all around performance and just makes you know taps into the kind of modern python that i was talking about is pedantic pedantic is a library for validating data which is very important when you start to think about how you're exchanging data on the internet with the client and server and we're going to talk about all these later but these are kind of the three big things that uh make fast apis stand out because they're all built into fast api the battery's included if you will so the first battery i mean async await async technology so uh you know python 3.6 ac io came out and it was the you know it basically is a library that uh allows you to write concurrent code using the async await syntax and so then in a web api context it alleviates latency between the server and the client so basically the server can work on multiple requests at the same time and so the client doesn't have to sit there wait for synchronous requests to be processed one after another and so this abstracted away the need for stuff like task queues or multi-threading uh to do the same thing which is inherently kind of a nasty thing when you when you look at python because of some of the limitations around the threads and concurrency there so this is a huge change you know a huge step forward for python and it's really still pretty new uh and so you have a lot of stuff uh getting converted over to async now uh and so fast api is kind of leading the charge there and so just one quote up you know from the evicorn webpage uh is ascii should enable an ecosystem of python web frameworks that are highly competitive against node and go in terms of achieving high throughput and i o bound context it also provides support for http 2 and websockets which cannot be handled by whiskey so this really opened the door for python to kind of play in the like modern web framework space and compete against stuff like node.js and go and then you know over here we have just a little graphic that shows the advantages of async for synchronous you know synchronous uh a server is going to have to do tasks one by one these tasks are going to add up this is going to take time it's going to be less uh you know it's less performant your app's not going to be as fast and then async you can just kind of spawn up a bunch of tasks and uh you know the you just have to wait as long as the longest one takes to finish which in this case was 20 seconds so you know you you speed up your uh execution time by like you know factor of uh two here um and then so you know people publish benchmarks around uh different frameworks and speeds and fast api is pretty close to the top uh it's just a it's a fast framework um you know and this is competitive with stuff like node.js and go like i said so this is just kind of a graphic that shows that and there's the link at the bottom if you're interested about that so the second battery i talked about going back to the previous slide can you show us again that node.js and go which where are they so these sorry these are just python here uh i'm sure you could probably get those in there too but um yeah these are just python okay how how how fast does it compare to node.js and go the way you know don't know off the top of my head but it's it's up there i think they're all in the same kind of strata of speed and performance um i don't know off the top of my head though exact i'm actually not exactly sure if i understand this exact uh metric they're using they're basically just pinging a server a bunch of times using the same machine and measuring the uh the you know responses per second so it's you know it's something that if you want to get deeper into you can read about right here but unfortunately i'm not the expert on that i i just wanted to clarify whether those like how do the python framework compares to other languages go was supposed to be the the language for those things and if you're saying so i i had to do something similar a few years ago um when we had to figure out which framework to use for some machine learning service and machine learning models and we started not even like with machine learning models but practically just like a super simple function behind it and just to use some um like test framework that would go and send like you you can say like and 100 users and there's like test frameworks out there that you can you can use and so we we did something similar like uh again like about three years ago and uh so we we back then tornado came up to the top um and the fast flask was somewhere at the bottom but again like because we are using distributed uh uh second severe framework that didn't matter that much but that's kind of how you do this probably you just like bring up a server at least that's how how we did how we tested you bring up a web server and then you use this test framework that is going to do all the all these requests for you and then measure all the metrics yeah no yeah that that's exactly what they did uh and this is updated continuously so if you go here you can really see stuff across different languages i don't have one that shows all the languages on top i think uh like there's some really fast.net languages and asp stuff but i i don't know exactly where python ranks there but within just these here so a lot of these like sanic uh quart tornado these were um really popular frameworks back in like 2017 after async came out because they were all kind of you know really performant uh because they were able to use async and and now um there's they're you know fast api is actually better than those so it's just interesting to see how the landscape shifts around a little bit and i think that the general rule is like if it's up here in the top 10 or so there's not going to be like a gigantic difference as you know in terms of like a user uh noticing the speed or anything like that but it definitely on paper you know they stand out so yeah i mean that's something i could follow up on and try to get a little bit more information around like cross language performance uh but you know the the creators of fast api do kind of say that it's it's very competitive with those specifically those two languages so i'm going to take their word for it so so the next built like kind of battery that makes fast api stand out is data validation so this is a quote that just i think summarizes pedantic very well and just describes what it does so it's a very useful package that makes dealing with data must usually are similar to what python's built-in data classes do uh where pedantic shines though is dealing with validating and sanitizing data so what it does in just kind of three bullet points is you know it uses type hints type annotations which uh you know it's also sort of new a feature to python and then it forces those tie pins at runtime and then provides user friendly errors when the data doesn't match the type in and then this way you can define how your data should be modeled just with python and then it gets validated with pedantic so in a web context this makes it so that you can build uh you know response models request models so the data that's coming in and out of your app your api so that the app knows how to handle that data because when you think about it i mean everything sent over the internet is basically a string so this is a way for your app to convert the data and return it in the right way if you didn't have this it would be kind of a nightmare so this slide i'll show you an example um so basically we look like there's two things so on this side of the screen we're going to define a user class and we're going to have it inherent this base model class from pedantic so this is kind of the thing that makes pedantic work you just plug it into a class and then you have your uh fields in your class with the type annotations uh you know and then you can create some fake data here like say you know id is a string signup's just another string uh this friends field is a list and then you can map that data you just had to your class you can basically just convert it over and then since it's a pydantic class when you start printing stuff out like expecting different pieces of your uh your you know your class you can you actually get the real thing so you see here like this is a list of ants for friends and it actually prints out three integers right uh like this when you print out the the sign up it actually prints out a date time versus a string here uh when you you know this is passed in as a string this id but when you print it out it's a integer so it's a way of like you're going to get messy data given to you over the internet because that's just how the internet is and so that your app is robust enough to handle that uh and use it in the right way without like totally crashing that's that's where pedantic comes in and helps a lot and then and that's not the only thing it helps with i will actually before i show the next slide so this is just a this is an example so this is a like little sample fast api app over here so we have the same kind of uh data model you know object or class we created and we have our fast api app we're instantiating there and we're going to add a post route so that if someone's going to want to create an item they would do a post request to this route here and they would pass in something that resembles this class that we defined up here so then what pedantic does uh through fast api is it reads the body of the request as a json object and then it converts the corresponding types just like we saw over here and then it validates the data and it says you know if the data is not valid it's going to return a clear error message it's going to say exactly what field is not matching uh and you know where the data is incorrect uh and then when you're the nice thing about it is that as a developer you define this class here and once you import it and you're using it in your code you the like type annotations and code every student will show up automatically so it definitely helps you know you the developer kind of know what your data is supposed to look like and model it better which is an important thing especially when you start doing with databases and stuff like that so before you run forward when it converts the date time or any other thing do you have like conversion rules like this like have to be in specific order or like daytime comes in a specific format and you specify it and or there's a default one or is it smart enough to figure out if i pass the date in a different format or something like that like iso format when we know how to convert it yeah that's a good question um i think it's it's just smart enough uh i think you know i i haven't messed a lot i think that you would really only see a lot of iterations like that where you'd have you could experiment a lot of different combinations with like a date time like you said with a string versus integer i think it's pretty easy for it to figure that out um you know dictionary similarly at date time might you know if you did like you know a weird string in here or something like that would it still know that it's a date time that's a good question i have not tested that um but i can try to figure that out and get back to you that's a good question but if you put in like a european format like you know the the month and the date or different like yeah it's like it's like month date first and then you know all of those things doesn't know how to does it have like default rules or can you hint it or i think well i think with date times at least you can add uh time zone specifications and things like that formatting to the date time uh you know method that you're importing so i think you can get around i think you can solve for edge cases like that just with the date time module itself and just and and you know passing that in here uh i'm not 100 sure though like like i said it's it's a good question i don't know that's okay it's definitely worth checking out i'll i'll leave you it's like there's definitely so useful yeah i don't know that's a great question i mean there's definitely some magic that goes on with pedantic uh so it's again it's kind of a new thing in python it's you know there's very useful but um you know still i find myself having to remind myself to use it sometimes when i'm not building like a web app or something like that uh just because i'm used to my old ways but i really encourage people to check it out even if they're not building you know web apps per se it does help with just data modeling in general um so we so we talked about how fast api kind of uh you know converts data you know in a meaningful way to to make your app run uh so that's you know one that's one thing fast uh pedantic does in fast api one other thing it does which is huge is it actually helps generate automat documentation for you so it generates these json schema definitions for your model so the model would be for for example this uh this item class which is uh passed here so fast api sees that and says okay let me create a schema uh to help to you know document what your api is doing and then when you go to this you know every fast api has this you can disable it if you want to but if you visit the docs path at the end of your app's domain or if you're on localhost if you just do localhost docs you'll get a nice screen like this that shows all of your endpoints and you know custom strings you can put in for custom explanations for the user but it also shows you know exactly kind of like what the model that's being passed in is like what's the schema is it you know is it uh you know what does basically this look like to the user so the user could see oh i'm supposed to pass you know these fields and this one's optional i don't need to pass that this one's optional i don't need to pass that so it's a way of just you know handing the documentation over to the developers they're gonna be using your app uh in an automated way which is huge because it's a pain to write documentation um so this is another one of those batteries included i was talking about and we'll look at an example later of this uh in in you know kind of real life um oh i guess we'll do that now okay so um so this is a kind of simple simple app and let me know if my screen's too small i could probably make it a little bigger uh you know you can get pre like i said one of the things about fast api is you can make it really simple or really complicated it's up to you so if you want to keep it simple you can totally do that if you want to keep going make it you know more complicated handle new use cases you can do that this example is one that's very very simple uh in the fast api meetup we uh we we do uh we actually have like you know a full web app with you know bootstrap html on the front end and everything like that uh so if if you're curious i'll give you the link at the end of this presentation you can go check it out github or you can drop in one of the meetups and that's kind of the app we use uh for the meetups but this app is pretty simple so we're just going to import a few things we're going to import yuvacorn which is that server that you know the ascii server we're going to import fast api of course we need that and then we're going to import the base model from pedantic and then a couple other things some some type definitions uh from from typing and then um of course you know just date time because daytime is so useful for a lot of things and so this app uh api it's just a toy app it basically makes it so that you can create and list chores that you create you like you know it's a chore a chore app so i'm going to put out new chores in and i'm going to be able to look at them after i put them in pretty simple so this is the model i'm going to call it a model even though it says class it's the data model for our chores that we're going to be having built in the app so it takes three fields there's a name days until alert you know days until you have to do the chore that's an integer and then a date added that's an optional could be you know a string um and then this is our you know database quote you know in quotes it's not really a database it's just a list but it's a list of those chores that i just defined up there so i just define this this class right here so it's a list of those as you can see um in a you know real app we'd of course use a database we uh in our in the fast api meetup group we use sqlite just because it's so easy to use and local but uh fast api can work with you know sql alchemy uh you know any type of cloud database you want it to um you know there's plenty options here but we'll just keep it simple and use this list and so then i'll have two functions here and you're gonna notice they're async functions so what's async mean well it just means that by wrapping a function with this async keyword it will work with the async await syntax which is what fast api offers now you don't have to use async if you don't want to that's something i should have mentioned before fast api supports synchronous and asynchronous programming so you don't have to use async it's flexible in that way that if you want to build stuff that you know run synchronously just like your you know flask app that you're familiar with might you can totally still do that uh but in this case i'm just going to use async so this is our first function or you know service and it's just going to get our to-do's back and it's going to return a list of these or i should say list chores i call it to do's um but it's just going to return a list of these chores back and then this point down here yes would you mind if i interrupted you for a question from the chat oh yeah yeah sorry go ahead yeah so eric is asking if you have observed any performance issues with using uh sqlite uh so i've never used it in production um i've just kind of used it for like playing around toy apps the thing the thing that's nice about sequel light is that they uh released async support like really early on uh async came out you know a few years ago they were like really on the ball getting it out the door uh sql alchemy did not release async report in beta until like last october so they were just way behind and so if you wanted to play around with fast api and learn stuff then you were kind of you couldn't use sql alchemy for like a long time um that said i have seen some weird stuff in sqlite where uh stuff just kind of persists throughout sessions maybe i'll have a session where i tear down the sqlite database and then spin it up again and it'll like still have like something locked in there or something like that so it won't do all the kind of migration stuff like i wanted it to uh but again i haven't used in production so i can't really talk about that i've i if i'm gonna write something in production i usually use like a postgres database or uh you know something on the cloud like dynamodb or something like that or mongodb which is not sql but uh still is there is there something that uh they had to comment about that sequel light no they had some issues and collier thank you so much yeah no i've just seen it's a lightweight database that basically can interact with different systems so um in our experience we have seen a little bit of performance issues with it but um i was just curious from your from your experience yeah i mean i don't think i would recommend using it in like a production environment or just even an environment where people need need it to be used you know all the time i don't know even if it's like an internal app or something like that i would just say go straight postgres because it's not you know i don't think it's made for for production stuff what's nice about it is it's just a file so you can commit it put it on github someone else can pull it down you know it doesn't you don't have to connect to you know a docker container running your postgres uh database or anything like that so uh you know it's it's simple just to get kind of like proof of concepts up and running and things like that um but yeah i mean it's i don't think i would recommend using it much more than that so thank you um sql light gets a bad rap um that goes back to you know i first you know rails was a rails developer and a lot of the examples would use sql light and people would throw a fit about it i actually use it in production for things that i aren't going to get a lot of traffic so if i'm doing i work with a military non-profit and they have their golf tournament and like 30 people are going to use the app at most yeah so something like that it's really not an issue if you've got simple data and it's something you know internal like uh i wrote some friends a little api that does something that there's like three users and they use it once a day um so the flip side of that is do you want to go through the trouble of installing postgres if you're just spinning up a droplet or a little ec2 thing or pay amazon to use rds when you only need to persist a little bit of data and especially if it's read-only oh yeah yeah i mean for that kind of stuff it's hard to beat that just because uh yeah like like you said you could you don't have to pay anyone for it and you could just bring it with you like if you're on an airplane or something like that you don't need to you know connect to some remote server or anything like that to run it you can literally copy and paste the file to someone else and they get all the data so it's it's really convenient and they're on the ball with async so it's you know it's hard to fault them it's just you know there's a there's a place for everything i guess the last thing i'll say is i remember discovering that it only has five types and thinking wow it only has five types and then i was like oh wait it only has five types so you can implement your own date time stuff in your app uh it's really simple and for for most of the stuff you're doing that's not like customer work it's fine yeah just yeah that's why that's why we we're using it with uh you know the meetup group is because we wanted to focus more about building the apps and you know implementing new different things and i mean it works with everything it will work with graphql for example it will work with different things so you know instead of spending a whole session talking about databases and what you know the right what database we should use we just said oh we're just going to use this because people didn't really want to focus on that they want to focus on learning about the framework so uh for for that it works great so um all right well so you know back to this kind of simple app we got going on uh we basically have two functions here one's gonna get our chores back and sorry it's called to do i don't know why to do chores same thing the other one's gonna add uh chores uh and add a chore and so if you look here it's gonna append a dictionary to our list here of chores and the one thing i'll say about this is that if you notice this is going to take a chore argument of the type sure that we you know descri defined up here in our data model and then it's just going to pass in those elements that came those fields that came from our uh class you know in as the dictionary keys so we got the name the days until alert this one uh i just hard coded this just so that you would have the you know real time you created the chore [Music] and then system return to string so it's going to say you know to do added and so then after that we have some different routes so this is how a route looks in fast api and if you're used if you've used flask then this will probably look pretty similar and it's very minimal i mean if you want to add a post get route post route anything it all you have to do is wrap it with a little decorator here so your api um is the thing that you defined up here this fast api object and then you just do api dot get forget request and then pass the path that you're going to go to so in this case this is the you know index the root route this is just when you visit the app uh if you want to give it a name you can the name is really for more for documentation purposes and then the function under there you know you can name this function whatever you want to and then for example someone visits this uh kind of root page here they're going to see this message come up you know this just whatever jason kind of message here so so collier you're running over this because it seems that everyone knows this and this is simple however we in this group sometimes the people who are only starting to get to python so i ask you to elaborate a little bit more what happens behind the scene what is the route at all because you just showed it but people don't connect so please slower yeah for the sake of those who are you to python that don't know what the route is or how it is so please please sure that was a web browser of course yeah so a route is basically a uh endpoint and an endpoint is a url so if i was gonna go to some url uh you know in my app i could just type something like this in so this is a this is a route right here and of course i don't have routes defined for a lot of these things um and the url there at the beginning is the explain to them basically what it is because they don't know some people yeah so so when um i let me go down a little bit so when you when you build something like a fast api app uh you're gonna run a server on your computer so the server that we talked about is called uvicorn and it's uh all it does is it opens a port to some uh you know domain it has access to on your computer so in this case it's your local host and which is you know you could type in localhost you could type in the actual uh oops that's my local this is different anyway yeah sorry yeah so you know this host name aka localhost this is where you can define it whatever you want to as long as your server has access to that but this is where the app is going to run and so then when you start going and making uh http requests whether through the browser through a library like requests which is a python library to do requests um then it's going to hit this route and uh you know whatever endpoint you defined up here is what you know the app's going to listen for so the app you know if you make a request on an endpoint the app's going to see that and it's going to do something and so that's what this function does underneath the thing that's listed so in this case you know this uh this at this route here that's just kind of a forward slash that just means you know the root kind of endpoint which doesn't really go anywhere and so if i went here they you know the server which is running said oh he went here let's return let's do this thing and so what this does is returns a little json object dictionary and it says you know message hello fast api you could have that say whatever you want but that's just kind of like the home page and then for other endpoints i define like the to do's and point um if i go there it's going to give me a list of the chores and it's going to do that by calling this function which i defined up here which just returns that quote database list of chores that we defined earlier so if i do that yeah these are the chores that i've defined so far so walk the dog water the plants take out the trash um you know that's that's what this endpoint does and then the difference between a git and a post is that if i get an endpoint it's just going to return the data back if i do a post i'm going to actually put data to the server so the server is going to wait for the data to come in it's going to do something with the data and so in this case this last route here is a create it's basically create a to do create a chore and so to do that i'm gonna i'm not gonna use the browser for that i'm gonna use a little script over here which uses uh this library called requests which i'm sure a lot of you familiar with um and so you know whatever uh new chore we think we want to add maybe we want to say um i don't know rake the leaves or something it's about to be fall i don't know about you but i get a ton of leaves and it's not gonna be fun so i'm going to do this post request here and it's just going to return back what i sent to it [Music] and the reason it's going to do that is because first it did this add chore thing so i passed it a chore this dictionary name and days until alert and then it added to this list which of course you won't see here because it's this is hard coded in that and the script and then um it's going to return that chore back again that that's that dictionary back and so then when i go back here and get my chores again then you got rake the leaves now so that is a really simple uh you know get and post pattern for an api and that's really like the most basic kind of you know api kind of functionality that you might use i mean you could have an api that's just gets you could have one that has git post create update delete like a crud you know structure but in this case uh yeah i didn't want to make it too complicated so just to get in a post uh yeah so that's that's a really high look at just a fast api app and and if you use flask before it's probably gonna look really similar um and i think that's a good thing just so you don't have to relearn a lot of new stuff it's nice and simple so um any questions before can you show us this is great by the way like thank you for explaining this in detail but can you show us uh what's the documentation like for this small app oh yeah yeah i apologize before you do that before you do that go back and you might want to explain response model and how pedantic handles serialization yeah so so a response model is a way for uh the app to know oh okay he this person who's either submitting a request or you know submitting a uh well yeah submitting a request to either get stuff or add stuff you know what is the data look like that there's what is it supposed to look like and so if you remember this it's supposed to look like this there's a name which is a string there's a days until alert that's an integer and then there's an optional field for a date added so i heart went ahead and hard coded that so we can kind of ignore that but basically every data that every uh you know piece of data that's passed to the app or comes out of the app in this case if it has this chore model uh defined as the response model that's what it's supposed to come back as so then fast api says oh you passed me um you know a string and uh integer you know so we're good uh or if you get the stuff back then it's gonna go ahead and convert those things back to j nice json object so it's going to look at the you know actual uh you know data types that came out of the database and it's just going to convert them back into the json object that it was supposed to be so jason obviously can have string integer floats uh lists all types of different objects in it so in order for this to come back to you the right way that's what this is doing here it's saying return a list of chores and then if we're going to add a to-do then it's going to look for something in that format called you know you know with a string and an integer in it and if it's not it's going to do those it's going to attempt to do those simple conversions that we uh talked about earlier which was you know converting something that looks like an integer but it's really a string into an integer and maybe a date time into a date time so um that's that's kind of where pitante comes in is with this response model uh logic uh did that you think that answered or explained well enough yeah just the pedantic handles the serialization you're not having to call like json.dumps or anything it handles the serialization uh in this context for you good point yeah that's a good point um yeah so if and i think flask you would have to do that you would have to say jason dumps uh with other apis like maybe if you build an api and like a lambda function i think you would have to do that too um maybe you actually could just use piantic in that case but yeah so this is where pedantic shines it just kind of takes a little bit of extra coding out of your hands you don't have to write extra conversions on dictionaries and json objects and things like that so and it's also nice because when you're developing the code and you're testing stuff or you're just writing the code yourself if you run the app and try to do stuff it'll show you the error messages like really quickly like almost at run time when you even run the app it'll check stuff and it will send you back and say hey this is not the right type and then you have you can know exactly what to go fix so it just makes the development experience a lot faster when you have little helper things like that um uh can you have a uh can you have multiple classes so here you have class store uh if you have multiple classes and you have a complex structure that's part of your request json does finance support that in terms of fragments yeah yeah you can have so this short class that inherits uh you know inherits the attributes from the pedantic base model it'll keep you can keep passing that along as far as you want so you can have this chore class b you know go inherit to another class if you want that's maybe like a regimen or something like that which is like a collection of chores or something like that so so as soon as you kind of uh you know instantiate this class based on this base model from pedantic it goes all the way down so it's it's nice like that it's just you know the classic python you know polymorphism kind of built in so so yeah you can you can keep going with this and you probably will at some point because you're going to want to use you're going to want to use these not just for validating the data that comes back and forth from the app but also when you start working with databases i know we kind of skipped over databases but there's a way for pidantic and sql alchemy to just work right together and so sql alchemy will look at the pedantic data class the types and it will craft uh the tables that you create in the database based on those so it's it's really flexible and useful show us show us the the the the the expectation yeah so every fast api app unless you explicitly disable it you can just go to docs and it brings up a nice screen here and if you want to you can add a lot of text here add extra you know explanations around stuff if you want to customize it you know for maybe it's like your internal company app and you want to use like you know your company logo or other things on it you can do all that um but so what this is this is basically just a list of the endpoints that we have in the app so we have the slash route which is home and this thing doesn't take any parameters and it returns just a string and if we go back here i mean it just kind of matches this because this doesn't really do anything it just returns a string here and then when we start looking at these like if i'm going to get all these to do's then this is going to take uh as far as like what's getting passed back and forth uh it has an example value here you know which is just kind of like uh the the fields that are in that um chore object here so it just passes that pedenta pedantic uh class along to the schema so it actually just create you know creates this nice example for you and then it actually creates this list too so if you want to look at you know a little bit more detailed description about what each field is it adds that as well so you then me as the developer i can come along and say oh this is exactly what i'm going to get back when i do a get request on this route here um and then similarly if i'm going to post a to-do if i'm going to like add a chore then this is similar i mean it's it's you know example value and then a schema so this is what i as a developer is using this api would come along and say okay i need to pass you know the chore object looks like this so i need to pass a you know dictionary json object just like i'm doing over here with with these fields in it so that's just nice out of the box kind of documentation and you can add as much extra stuff as you want in terms of descriptions on each route the whole thing you can do some pretty fancy stuff too you can add like more examples and things like that so it's always nice as a developer when you read someone else's api docs and it's like really good documentation because there's plenty of times where it's thought and it's just you have to kind of just trial and error until you figure it out so this is super useful um and if any and the important thing is if you don't want to show all this information you can disable this too and you could disable it for like certain routes if you wanted to so that's that's docs and that's kind of just a really high level sample app um so any questions yeah i don't have one so uh like it's kind of similar to the question before that so if you have like more complex types of uh like models like for example here you have a chore and then but then the fields are more complex let's say you have something like that's nested second nested type of structure like how do you define it do you define another let's say you have you have and i'm trying to come up with something um i mean you you have a field that has a bunch of other fields inside like you define another class and then you refer it in here or how do you yeah i mean you could do something like this maybe um so you might do something like uh you know chore list or something okay and then this would be a list of chores something like that i see you okay so you can just kind of keep building on what you built before um just keep building it on right all right so you can use the the class that you the model that you you for before and that's how you can nest like like more complex data structures yes yeah cool um so i don't want to take a whole lot more time but we're kind of at the end anyway um so where you know as someone is learning fast api what what do i recommend as someone who's been learning it for the last i don't know i i guess i started learning it back in like april-ish may um so you know if you want to keep learning um there's some really good documentation out there there's just a few resources so i put together this google sheet here uh which is in the fast ap the austin fast api uh google drive and uh it has just a spreadsheet of like here's an article and here's the things that they talk about in the article um and you know it covers there's tons of content in there and i can probably just open it up really quick so you know there's if you're interested in postgres there's you know articles on that if you're interested in you know maybe using elasticsearch uh or pi test or docker there's you know just different articles i found and i'm constantly adding this list so you know go ahead and check it out if you want and then if you want to just look at kind of what the experts say these you know this is uh documentation from uh the creators of fast api and they just they continuously add new articles about uh how to do stuff and so if you have a specific use case you want to do like do you want to use some machine learning models or something like that then this is a good uh this is a good resource for you um and then as far as courses go i mean i do recommend if you really want to get into it like there's nothing that really beats like a course to help you learn and so these are a few different ones so the talk python guy michael kennedy he makes really good stuff so these are two of his classes and then the test driven folks uh test driven io they have three classes now uh for fast api i think they're all they're like 30 each so they're like you know not too expensive uh and they're really good so those are just some ideas um and then uh you know give a little plug for for the meetup um so you know it's a it's pretty new meet up we've only had four meetings so far or this will be our fourth one this month but uh basically the goal is to build and showcase apis and web apps and we spend about an hour each meetup and we spend you know 30 to 45 minutes going through a coding walk-through uh which includes kind of a presentation like the one today with you know more like architectural content like slides and diagrams and things like that and then we have kind of general discussion at the end like 15 30 minutes or so and so these are some resource links there's the meetup group link and then the github repo and then the google drive which has these slides and some other older slides too and then this is what we've covered so far we just talked about kind of the introduction and then you know what you could use to build an app as far as designing the the file structure in june and then we talked about authentication in august and then this month we're going to talk about graphql so early days but you know a lot to do ahead so please come check us out you're going to repeat this multiple times like each time like you can do like in like six months courses or like several courses and repeat each year or are you open to new topics i'm asking that's a good question yeah so we we're definitely trying to kind of constantly uh talk about new things um so you know for example graphql we had some people that are interested in using it for either their jobs or uh you know just out of curiosity and so that's kind of you know once we passed some of the basics uh we started talking about more advanced topics like graphql um but you know like it's an open discussion we we talk about the code all the time we talk about uh you know we rehash the topics a lot so it's not like even if you don't have experience you can certainly come join and um you're going to get a lot of content so you'll be able to learn about the basics anyway uh and then there's nice slides and documentation for each of these subjects in the past too so if you want to learn about just the intro uh or you know the basic fundamentals behind building an app like the you know what what the folders look like what the files look like um then you could look at the slide from the the second meetup or if you want to just learn about authentication uh you can look at the slides from that meetup so there's there's lots of content that we just keep adding okay so so just to ju just to emphasize the point so people will hear it strong here the people from the python meet up here do usually come to this meetup do do join the fast api meetup it's not on the same page as us it's a different meetup page but do go there if you're interested especially the beginners if you want to learn the web framework this is where you should start learning and don't be afraid of asking questions there the people there may be experts but they will help you absolutely no i mean we're we're all learning and i appreciate that and uh i'll just say that you know it's a really exciting time to learn web development and api stuff because once you build your awesome machine learning models you're going to have to host them to the world so now python's just as strong in you know web development as it is in machine learning and other stuff so you know if you learn python you have so many tools at your disposal now and so this is a really nice shiny new toy for people to play with as they start to go out and kind of show what they've built to the world so so yeah thank you for having me i appreciate it yeah oh thank you all right thank you so much oh yeah definitely you for that that was great yeah that was awesome thanks so much
Info
Channel: Austin Python Meetup
Views: 28
Rating: 5 out of 5
Keywords:
Id: ehPEz3tpcb0
Channel Id: undefined
Length: 61min 43sec (3703 seconds)
Published: Sun Sep 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.