Build Web APIs Using Python and FastAPI
with Jay Miller
Python works for the web. Learn how FastAPI allows developers to build web APIs quickly. Jay Miller teaches us how.
Topics
Resources & Links
Transcript
Captions provided by White Coat Captioning (https://whitecoatcaptioning.com/). Communication Access Realtime Translation (CART) is provided in order to facilitate communication accessibility and may not be a totally verbatim record of the proceedings.
JASON: What's up, everyone? Welcome to another episode of Learn with Jason. Today on the show, we are bringing on just an absolutely delightful human. Jay, how are you doing?
JAY: Yo, the dream is being fulfilled.
JASON: I'm so happy to have you here. We had a chance to connect at GitHub Universe. It was so much fun, we saw each other, hey, what's up. And we somehow ended up in this very deep conversation about whether or not you should value yourself, how to charge -- it was like unbelievably good conversation. Which maybe someday we should just do a whole thing about that. That was awesome.
JAY: Yeah, that's got to get recorded somewhere. I said a bunch of things. I don't remember all of them. But I think most of them were good. Love yourself. Charge people for your work. If you don't -- they will just -- you'll burn out.
JASON: Yeah, absolutely. So, with that, let me just give you the floor real quick were for folks who aren't familiar with you and your work, let's hear a bit of your background.
JAY: Yeah, so, I'm Jay Miller, I'm a soon to be staff developer advocate. More on that in the future. I have been in development for like nine years? Eight, nine years now. I'm mostly in the developer advocacy space. Mostly as an automator, Script Kiddie in the Python world. I'm super-involved in the Python Community. Not just the here's how you write the code, but here is how you care for the people. And last year I came the founder of a cool project we're doing called Black Python Devs. How do I explain a community called "Black Python Devs." So, yeah, just doing that. Happy to be here. I do some JavaScript from time to time. Not that much. So, I'm -- I'm super-excited to learn -- learn how I can better talk to primarily JavaScript folks about Python stuff. Also, I know Ben. Hey, what's up, Ben?
JASON: Yeah, yeah, the chat is already popping off. Looks like people are excited.
JAY: Whoa... so, we should say, first. I'm not Sebastian. So, don't ask me questions that Sebastian knows the answer to. [ Laughter ]
JASON: All right. So, the chat is ahead of me here. Because they've already shown more knowledge of Python than I have. So, today, we are gonna dig into -- as you said -- another framework for JavaScript developers for building for the web. And we're gonna talk about that framework specifically -- FastAPI -- in a minute. But first, I want to talk a little bit about Python. Because Python is something that I -- I don't know. I feel like I've always been really close to it. But I've never actually written -- I've maybe written one line of Python in my entire career. I've just never landed on it, right?
JAY: Yeah.
JASON: And so, you -- are you all right?
JAY: Yeah, I'm good. Hold on. I have a different cam.
JASON: Did it just fall off your computer?
JAY: Yeah, it did. [ Laughter ]
JASON: Amazing. That's got to be a first on the show.
JAY: Like a pro, though. Ready to just switch it up and go to the next one.
JASON: Oh, my god. Well, I hope it's okay. Looked like it turned back on.
JAY: No, it was my phone. Apparently these MagSafe holders aren't as good as they used to. I think the glue ripped out or something.
JASON: Not as safe as they would...
JAY: Mag-danger, let's go!
JASON: All right. So, Python is something that I've largely been close to, but not actually used. So, I want to talk a little bit about just Python in general. Why would a developer reach for Python -- why did you reach for Python?
JAY: So, I actually reached for Ruby first.
JASON: Okay.
JAY: I had questions. I asked those questions. And I was told my questions were too inferior for actual help, I should learn Ruby. And then be like -- yeah. So, at that point, I'm like, I'm done with Ruby. That's not the way. Then I went to JavaScript. I still actually prefer vanilla JavaScript when I'm just doing stuff. Like if I want to do a thing, I'm just reaching for vanilla JavaScript. Make this button do a thing, cool, script, end script. I don't want the framework. I just want to go into my Dev console and knock it out. But then, yeah. I got -- I think at the time -- this was probably, what? 2013? 2014? So, like React was just showing up. Angular, I think, was around still. I tried some Angular stuff. That wasn't, you know, my jam. React was confusing. I was like, why not just use vanilla JavaScript? And people were like, shush, you. We're having fun here.
JASON: That is -- if that isn't the most JavaScript developer -- shush! I'm having fun!
JAY: Yeah. So, then I -- I was like, I was just trying to figure out what's gonna be my thing? I was fresh out of the military. I was a sys admin. And I was trying to just automate some of the stuff I was doing. Some the day-to-day stuff. A lot of that was just scraping the web, oh, hey, I need to do cell phone orders. How do I jump into my browser and do web order stuff? I was also getting into podcasting so I wanted to do transcriptions, I wanted to do some RSS stuff. So, someone was like, oh, you should use Python. Python's, you know, super-cool and super-hot and that's all the fun -- all the cool kids are using it. So, I go to this Meetup -- actually, hold on. Rewind. First I go on to StackOverflow and I'm like, hey, I'm having this problem and I can't figure this out. And it's a super-simple problem, whatever, whatever. This is like day three of me using Python. And I get the same Ruby on Rails response. So, I'm like, oh, man, I'm out of this. There's no way. I then go -- I see that there's like a local meetup. And I jump into the local meetup. And at the time I was in San Diego, I'm in Atlanta now. The San Diego Python Community is like... it's got hall of famers in it. Like straight up. It's got like core Devs, it's got, you know, someone -- like Python Software Foundation Fellows. It's got the who's who in Python Community like in leadership of it. So, I'm still trying to figure out this problem. And one of the people come up and is like, hey, you're new and we just had this conversation. Not only does this person help me with the problem, they also take an interest in the actual project and I'm working and keep asking questions and keep that excitement level up. And at that point, one of other folks comes and says, hey, do you want to go get boba? These are my people. All right, I'm in. I don't know. You don't know me, but also, like you've done all the things by community that just made me really dive in.
JASON: Yes.
JAY: And at that point, I was like, this is it. This is it for me. And I just kept, you know, kept going with it. And I mean, to this day, there's -- it's that same feeling. Not just San Diego, it's PyAtl, it's Py Colorado. That same vibe that everyone there is just super-excited that people are in the room and just wanting to learn. Yeah. I mean, honestly I didn't pick it for the codebase because I would have been banging my head on React still probably. But like the community was what made me really want to stick to Python.
JASON: I think that's such a -- like... I feel like when a community really understands that, it's such a game changer.
JAY: Yeah.
JASON: Because there are -- there are different pockets of community across all the different frameworks and languages and, you know, different disciplines. And sometimes you feel like the attitude is we've got the best tech, you're lucky to be here. Right? And it's kind of -- like you talked about. They're kind of standoffish. That's not a good question. Go do some remedial stuff. Jeez, this feels bad. And then you talk to other folks with and be another community will be like you said, just so stoked that somebody is interested in the same things that they're in. How nice is that we have overlapping interests, you know? And that, to me, is -- is like -- that's the secret to building a healthy community is just remembering that like there was a time when you were the only one that you knew who was into the thing you're into. And when you found somebody else, you were so excited you had somebody to talk to about it.
JAY: Yeah.
JASON: And not losing that as you get more experience. Clearly serve into this. Or I'm very important, everybody likes the thing I like. Get out of here.
JAY: Yeah. It's definitely that moment of like, when we go to PyCon, whether we go to DjangoCon US, and the other conferences, we're catching up with friends. Not did you see the latest framework? No one is doing that. Oh, hey, man, you spent the last five years in Alaska and you're moving. Like what was that like? You know? Or hey, I haven't seen you since... I think there was one in the chat talking about Django Girls meetups. I got to do my first Django Girls meetup last year. It was so dope of seeing people from all different career paths, all different walks, some of them in school, some in elementary school even, and being like, hey, what do you want to make? Let's not focus on the, like, I want to teach you the in and outs of Python. It's like, I want you to make the thing that you want to make. And I want to see that smile on your face when you've made something really cool.
JASON: I love that. This -- this description of the community is making me want to become a Python developer, I'm not gonna lie.
JAY: Hey, we got a home for you.
JASON: I love it. Okay. So, community sounds amazing. Python itself can do a lot of things. What are some of its strengths? What are the applications where Python really shines.
JAY: It's slow -- no, I'm not supposed to say that. They get mad at me when I say that. I do want to address the speed thing really quick. You're going to hear from people -- these are the naysayers, Python is slow. Python is like a Honda Civic. Straight up. Do you really need a Bugatti? Like do you need the fastest car on the street? Is a Honda Civic enough? A Honda Civic has plenty of parts out there, it's super-flexible. If something breaks, $5, you replace the part and it's there. So many after market parts. It's great. This community around it, fantastic. It's fast enough. Let's not play around like, oh, but I need 18 -- I need 18 million requests per second. No, you don't. You don't work for NASA. You don't work for, you know -- you don't need it.
JASON: To be honest, I'm just hoping that I get 18 million lifetime requests on this.
JAY: exactly! Exactly! So, yeah. I want to get that out of the way. So, me, the thing about Python is that when written correctly, you can walk through it and understand what's happening just by looking at the code. It's written to be -- in my opinion -- it's written to be read by humans, not by programmers.
JASON: I gotcha. Yeah. And so, for use cases like I hear about Python a lot in like data. It seems like --
JAY: Yeah.
JASON: -- data scientists really like Python. Where else do you see Python and the use cases where it gets heavily used?
JAY: It's where you don't see it. It is on the web. It's very much on the web. I don't know -- there's a small company called Instagram that is a big user of Django and Python in general. There are a lot of Django sites out there. Django is another web framework, it's a little bit older -- I think it's 20 years old or 19 years old. And then there's some other -- other web applications called Flask. Obviously the AI space is just booming with Python stuff. A lot of that work, as you mentioned, the data side of it, a lot of that work was done in Python and still is. Also, I personally like it for some of the automation and hacking stuff. I'm -- it's like adjacent stuff. If you have web with data, Python is good for that. It's good with data, good with web, it's good with data with web. That's how people learned of me in the Python space. I would go find public datasets. Some that would make people feel uncomfortable. Like, I don't know, police call records in my hometown and see what areas where some of my friends who were LGBTQIA+, hey, don't go to this area, during this time, they like to call the cop on you. It was purely like, okay, I can grab a CSV file, parse it with Pandas and turn it into a quick website or static website and then just let GitHub pages do the rest of the work. Or even just like GitHub do the rest of the work. And like treat it as a bunch of markdown files.
JASON: Yeah.
JAY: So, yeah, I mean, that's kind of where I've kind of fallen in love with it. They're doing some stuff where they're trying to bring Python to the web. I think it's pretty cool. You've also got a few big, big projects out there like the BeeWare project where the idea is you write Python. You output other languages. So, that's kind of a cool little fun thing. Like if you want to write an iOS app, be you only know Python, BeeWare is making it so that you can write Python and it converts it into -- I would say currently it might still be Objective-C, but I think they are working on Swift UI stuff.
JASON: That's cool.
JAY: Android too for Kotlin and things like that.
JASON: Very nice. Let's talk a little bit about FastAPI and then we can dive into the code itself. So, FastAPI is -- I'm gonna take a wild guess here: It's a fast way to build an API using Python.
JAY: That's what we're doing, that's what we're doing. Someone was asking, how do you spell it? It's bee, like the animal, ware. Add just another E in there and you're good to go.
JASON: Like that?
JAY: No, two Es. B-e-e.
JASON: Oh, BeeWare! There it is. There's BeeWare.
JAY: Yep, perfect.
JASON: I like that. Please tell me that it's got a very cute mascot.
JAY: Absolutely. And you can get a challenge coin if you make your first contribution at PyCon US which is happening in May. And I will be there. So, yeah. If you want to come say hi to me. You'll find me.
JASON: Very cool. Excellent. All right. So, yeah, let's talk about FastAPI. Maybe -- let me just start with like at a high level, what is FastAPI?
JAY: So, yeah. You got it right. It's the newest of the web frameworks. I call them the big three. Like the big three are Flask, Django, FastAPI. FastAPI is the newest which is why there isn't a 0.1 yet or a 1.0 yet. Which was one of the questions earlier. It's been around 5 years now? Maybe just 5 years. I know there's a running joke from the creator, Sebastian Ramirez, people are saying we want people with 5 years of FastAPI experience. This was back when FastAPI was 3 years old. So, you know, recruiter's gonna recruit. But ultimately the thing I like about it is that it -- it is -- people are spoiling the fun stuff. Don't look at the comments, don't look at the comments. It's an easy way to build APIs. But you can build other things. Obviously, it was built with the intention of building APIs which is why we're going use it today. And a lot of the features that come with that are nice. Think about the things that you would like with building like a REST API. Like think about, you know, you want documentation. You want solid endpoints, you want good status code support. All of those things. You want good validation. It partners really well with another tool that we're gonna be using called SQLModel also created by Sebastian as a part of this. And that is also built on yet another tool called Pydantic. Which allows for a ton of validation stuff. Yeah, being pedantic about things. So, yeah, if you want -- like I want this field to be an email field. Like inside of my data, I want this to be an email. There are like 15 ways you can do that. So, at the end of the day, you're like, huh. What if you could just say, hey! This field... it's an email field. And now it's like, cool. You've already got a lot of those built-ins designed in your API of like, I'm expecting this to be an email. I'm expecting this to be a valid email, otherwise you're going to get weird validation errors. Or I want this to be an integer. You don't have to do the like client-side validation, you know, hey, if this is in A through Z. Hey, this is an integer. It's built off of typing, which is now Python. It came around the same time that FastAPI did. And typing in Python is optional. Except for when it's not. And I say that in that typing is relatively new. It's a very hot topic. Typing is what you should do in Python if you are using a modern editor, if you want to -- like you get modern features and modern benefits. And ultimately, if you want to better understand your code.
JASON: Sure.
JAY: You don't -- ope, go ahead.
JASON: Does typing in Python work sort of like TypeScript and JavaScript where it's an additional thing? Or is it built into Python itself?
JAY: It's built into Python itself.
JASON: Ah, okay.
JAY: There is a typing module that does some greater typing things. And that was kind of how they integrated. Originally it was this thing that, this is this thing you need to import. And slowly integrated into the system. We're 5 years from that now, it's pretty much integrated into the system. Because of when FastAPI was built, there are a couple of things to import typing to have them instead of just using the Python stuff. But ultimately you're not gonna get the TypeScript-like you didn't define this so therefore now everything is broken-type errors. You're not going to get that in Python for the most part.
JASON: Awesome. Well, all right. Let's see, is there anything else you want to cover in the abstract before I switch us over and we start writing some code?
JAY: So, let's just talk about what our goal is for this.
JASON: Yes.
JAY: We're gonna try to build a CoolPoints API. So, at the end of this, you and I -- you -- because I guess you'll be doing the driving here.
JASON: I will be doing the driving.
JAY: You will be able to add people to a database, just use SQLite, don't get fancy with it. And from there, you'll be able to add CoolPoints -- maybe remove CoolPoints, we'll say add/remove CoolPoints. Jay's camera flipped off the monitor. Let's remove three cool points. And then we can talk about maybe some next step from there. And in the future, maybe I can learn some JavaScript so design the CoolPoints and maybe turn it into a full-fledged CoolPoints app.
JASON: That sounds like a great follow-up. This is going to be great because like I said, I've written almost no Python.
JAY: Cool.
JASON: And I've seen a little Python.
JAY: Okay.
JASON: So, getting through the syntax is going to be an adventure together. This is probably going to be the most challenging Learn with Jason that I've ever done because I'm literally wading into a new language.
JAY: Hey, I am your sherpa here to ferry you up the mountain of Python.
JASON: I'm pumped. I think this is gonna be so much fun. So, let's switch over to here.
JAY: Oh, no, don't go on there, jeez. What is that? [ Laughter ] Get off that!
JASON: And so, first and foremost, this is -- this is Jay's site. Which --
JAY: You're freezing again, man?
JASON: I'm freezing again.
JAY: You're freezing again.
JASON: I've never frozen in my life.
JAY: You're freezing like an early 2000s Batman movie. What's going on?
JASON: Chill out. Okay. [ Laughter ] So, we're talking about Python. Here is a link to the Python site.
JAY: Yep.
JASON: I luckily found out right before I started this call, I do in fact have Python installed on this computer which was great news. We have FastAPI here. So, I'm gonna drop a link to FastAPI. And then we were talking about BeeWare earlier. I dropped a link earlier, but this is the site and there's that cute mascot that I was hoping for. So, for someone like me fresh to all of this, what's the first thing I should do?
JAY: All right. So, if you don't have Python installed, it's going to depend on what operating system you're using. If you're using macOS, I would probably recommend using Brew. Some people don't like using Brew. Brew install Python is just the fastest way to do it. You can install it directly from the website, that is an option. But, you know, then you got to do other stuff.
JASON: Looks like I did install through --
JAY: Yeah, you installed through Brew. Someone mentioned Py env. Don't need that unless you're working with multiple versions of Python. If you're a package maternity. Anaconda is another way to do it. We're not going to be talking about Anaconda. Anaconda is Python with a lot of features added as well. It's very popular in the data science community. If you're learning Python, you learned it the pip way or the Anaconda way, I learned it the pip way, so, sorry. Like I've got no excuse other than like that's how I learned it. So, yeah. If you're gonna do one of those things, yeah, it is also very large. I'm gonna is stop looking -- I love the comments. I'm gonna stop looking at the comments for a second because otherwise my brain will start breaking. All right. So, the first thing that we're gonna do because this is a, like, this is just a good best practice. We're gonna create a virtual environment. And what that means is so JavaScript has this like local context of packages that can be installed. So, I can install a bunch of packages inside of one project, and that doesn't mean that other projects have access to them.
JASON: Okay.
JAY: Python has this ability, but not by default.
JASON: Okay.
JAY: If you install a package, you're installing it for your entire Python ecosystem. Sometimes that's good, sometimes that comes with conflicts. Especially when you have to worry about versioning. We're not doing any version stuff. You're just gonna install whatever the default is.
JASON: Okay.
JAY: There are two ways you can do this. You can do this in the terminal or you can do this with VSCode. I will just let's just do it in the terminal since you've got the terminal open.
JASON: All right.
JAY: You're gonna do Python-m --
JASON: Do I need to start a -- create a project first?
JAY: I saw VSCode window. I was like, ah, we're already here. We're already in the project.
JASON: I'm gonna get in it -- so, this gives us --
JAY: Nothing.
JASON: Empty folder. All right. So, oops. Now you said Python...
JAY: Dash M.
JASON: M.
JAY: Yep. And then you're gonna do v-e-n-v, that's virtual environment. There's another env, I don't know the difference. Someone is going to come for my head later, well, actually, this is how I do it. Then you got to throw a space in there. And then you're gonna want to do -- let's just do.venv because that's what VSCode would be looking for. And then just press center. And it's gonna do some stuff and it's gonna look like nothing changed. But now you see this .venv file. If you jump in, you see a bunch of files. Basically, it's like a Python Subversion that's just in there for you. Loving that background. That's dope.
JASON: Thank you.
JAY: Clean. So, clean.
JASON: Now we have our actual project open here.
JAY: Yep.
JASON: All right.
JAY: Let's open up a terminal. I want to see if it autodetected. It didn't. Here's the other way to do it. The reason I want to say "The other way to do it," if you're using Windows versus if you're using macOS or Linux, your steps are different. I don't know the Windows ones. I'm a soon to be former Microsoft employee that doesn't know the Windows commands. It's fine. Let's just open the command palette. Command shift P.
JASON: Command shift P.
JAY: And then you've already installed the Python extension. If you haven't installed the Python extension, that's not gonna show up. You want to install that. And see where it says "Create environment, go ahead and hit that. Venv, we've already created it, let's use the existing one. That's fine. If we had install requirements file, it would already do it. You should learn PowerShell. I know more PowerShell commands than I know like commandline commands. And it's gonna be doing all the same stuff. Close this terminal, reopen it. And we should see -- or, actually, I think we already saw it. We already saw this venv on the end there tells us that we're in that virtual environment. If you do which Python, or like you did before... by the way, just do which Python, don't add the 3 on the end. Oh, Python is not found. What? What did you do? My friend?
JASON: I don't know what I did.
JAY: That's fine, that's fine. It will be okay. Do which pip3. Okay. Oh, man. Okay. This is gonna be fun. I'm just gonna have to remember, if I say pip or I say Python, add the three on the end. That will be cool.
JASON: Okay.
JAY: You have to do some Brew doctor, Brew install stuff, yeah, it's a whole thing. Yeah. All right. So, I'm basically doing all of this stuff just to get our environment -- this is the mis en place thing. We're going to install things here, install FastAPI and SQL model.
JASON: Oh, wait. Somebody is saying that if I go into my venv activate.
JAY: It should already be activated.
JASON: I don't have one of those.
JAY: No, it's already there. If you do source.venv.bin activate, it would... it's already activated. I mean, you can type in Python and see if it's gonna work. But I would rather not just stick here doing Python things. It did it! Sweet!
JASON: Now do I get regular Python too?
JAY: Yeah.
JASON: Hooray! Thank you, kind Internet stranger!
JAY: Awesome. You win. You win. You've won the Internet. The hero of the Internet today. All right. So, yeah, we've got to install things. We install things with pip. It's just gonna be pip install. And then you're gonna do -- we're gonna do FastAPI. And we're gonna cheat. 'Cause FastAPI has another version of it that installs everything that we're gonna need. You're gonna want to put this in quotations. Nope. Like --
JASON: All of FastAPI?
JAY: Nope, all of FastAPI. FastAPI, and then add a bracket?
JASON: Pointy? Curly? Square boy?
JAY: Square boy. And then you're gonna type "All," and close that square bracket and that quotation. This is gonna install some extra pieces that we need. Sometimes if you're doing stuff in production, you kind of want your packages to be smaller, you know, that's cool.
JASON: Right.
JAY: and this is installing all the stuff. We need to install SQL model as well, do the same thing. But instead of FastAPI, just SQL model. That doesn't have to be in quotes, but if it isn't, no one is going to explain.
JASON: Like that?
JAY: That's it. Yep. Cool. And yeah, you're gonna get -- if you choose the Python life, you will be very familiar with a new release of pip is available. You can't keep up. You can only... there's always an update. Always an update. All right. We have done everything that we're going to do for now inside of the terminal. Let's start scaffolding our project, per se. And your right side explorer person. That's fine. I was too. And then I gave up because I can't find it.
JASON: I'm a right side and hidden most of the time.
JAY: I'm hidden left and sometimes I'll move certain things over to the right.
JASON: I can't do the left, it bounces my code back and forth.
JAY: I have so many windows open, it's going to bounce either way.
JASON: It was already over...
JAY: It's a mash. So, we're gonna create a folder and we're gonna call that folder "API." We could do all the stuff at the root, we're not going to. I'm trying to teach good practices here. So, inside of that, we're gonna create three Python files. You don't need to create three. We're gonna create three. We're gonna call one app.py. And then call one models.py, and then call one db.py. The reason I'm doing this, we're basically just separating our concerns. Your app is going to be just the FastAPI application. It's going to be how he handle routes and how we do stuff in there. Your models are going to be what are the models that we're creating with that our DB is going to interact with? And DB is all of our crud, create functions, update functions and all of that fun stuff. Again, this is how I do it. Other people might do it differently. Luckily, they're not on this stream. So, oh well. So, let's start in models. Because models are gonna be the things that we're -- let's go for circular imports! Ah... I don't know that answer. You have some people asking good questions. I just don't note answers to them.
JASON: Oh, yeah.
JAY: Yeah. I'm sure there are -- there might be. All right. So, the first thing that we have to do, just like in JavaScript, you add all your imports the models you're going to do. In Python, we import. So, we're gonna do this -- hm... all right. We're gonna do from instead of import.
JASON: Oh, oh, oh. Python does it the right way. from thing --
JAY: Import this.
JASON: I wish JavaScript would have gotten that right. Because autocomplete actually works when you do it this way.
JAY: Any time you say I wish JavaScript would have gotten this right from Python, I'm going to nod and agree. You can do import this, but when you do import, it grabs everything. Sometimes that's useful. Sometimes it's not. For what we're doing specifically, we're gonna not. So, from SQLModel, no, sorry. Yeah, no, that's right. From SQLModel. Pilot on or not, which is fine if you do.
JASON: I don't have Copilot on. You also -- I think you froze. I don't think it was me. Sorry. So, from SQLModel and then --
JAY: Import.
JASON: And the only reason I was wondering about Copilot was Copilot would just offer up a bunch of things. That I'm like, yeah, this usually sounds good. So, we're gonna import -- what was it? Item? If not item, we're gonna create a class called "item." We're gonna import SQLModel, it's going to be capital s-q-l. Yes, perfect. Does it are the ORM? SQLModel is the ORM for FastAPI. But it is not specifically for FastAPI. It can be used with FastAPI. And it used SQLAlchemy. It's Pydantic plus SQLAlchemy all smooshed into one class, which is great. That's what we need. Oh, comma field. Yep. Capital F. Something you'll learn about capitalization in Python -- so, capitalization does matter. Usually classes, which are a similar to JavaScript classes. Those are capitalized. Everything is like camel case -- not camel case, which is the --
JASON: Pascal.
JAY: Pascal case. Functions variables are usually lower cased and they use underscores. So --
JASON: Snake case, which is fitting for Python.
JAY: Yeah, you know?
JASON: You want to know what my favorite thing is? If this is snake case, this is screaming snake case. And that's one of the funniest things that I've ever learned about programming.
JAY: That's for constants. Yep. Everything in Python matters and not to question it. Um... indentation matters.
JASON: The snake...
JAY: Yeah, for those that don't know, Python is not named after the snake. It's named after Monty Python.
JASON: Oh, all of my jokes.
JAY: Yeah, there are a lot of references to Monty Python in documentation. It used to be called the Chi chop, which all the stuff was in Py PI, it was the cheese shop. You will hear references to silly hats.
JASON: I love that.
JAY: Yeah, there's a lot of comedy.
JASON: I just realized as you were talking about references that I forgot to tell everybody that we've got live captioning. Amanda is here from White Coat Captioning. You can find that, there's a link scrolling on the bottom of the screen -- if you need to -- [Whomp, whomp, whomp] I just managed to flip the cord for my headphones on to my sound board. That was not -- sorry. That wasn't like a sad trombone for Amanda.
JAY: Oh no!
JASON: Okay. Well -- good for me. [Amanda is laughing, it's cool] So, the live captioning, it's happening right now. You can go and check out the link and see everything that we're saying. Typed by a live human, Amanda, who does not serve a sad trombone. Made possible through the support of our sponsors, Netlify and Vets Who Code. Thank you for making that possible.
JAY: I'm a proud member of Vets Who Code. Shoutout to the crew.
JASON: Absolutely. What a great bunch. Okay. So, with that, we're back. We are looking at our code. We've imported the SQLModel in the field, and Monty Python reference, not a snake reference, I'm throwing out all my material. I'm ready to build an API.
JAY: Okay. Now we're going to create the model that is for our CoolPoint. I guess for this API, what data do we want to store? I'm guessing do we want to store the people that get cool points?
JASON: Yeah, right? So, we probably want -- issue like if I was gonna model this, I would have some kind of unique identifier. Whether that's a generated UUID or an email. Some kind of like human-readable identifier like their username or first name. And then the cool points themselves.
JAY: That sounds cool. Let's create a class. Yeah, perfect. CoolPeople, why not? There you go. See? You've already adopted it. And then we're going to say that our class inherits from that SQLModel. None of that, none of that, none of that.
JASON: None of whatever I'm doing. Just stop what I'm doing.
JAY: Get rid that have space there as well. So, we're gonna do open -- what was that parentheses? Yeah.
JASON: Round boy?
JAY: Round boy. Yeah, that one. I don't know what I call these. I'm just like, this thing.
JASON: In my brain they didn't have names until Cassie Evans came on the show, curly boy!
JAY: Yes, I have a show where we try to autodetect curly boys. Python has a F-string thing, you can do F, quotation marks and then curly braces and then a variable. And it will reference that variable directly. And we were trying to do, well, what happens when you need to inject curly boys into curly boys? So, that like how do we do that? And ultimately, we spent like -- we spent like 3 hours of just like... it take like six curly boy pairs. It's really fun. And in the end, we decided that curly braces will now be curly bois. With an I because, of course.
JASON: Of course with an I.
JAY: Anyways, round boys here. And add SQLModel to that and cap this off with a colon at the end. Boom. New line, it's already indented for you. Is that four spaces? Okay. Change that to four spaces. This isn't JavaScript.
JASON: Doesn't have to be four?
JAY: It doesn't have to be, but it has to be.
JASON: Wait, how do I do this? Indent using spaces?
JAY: You're probably gonna have to go into the settings.
JASON: Work space settings, tab size.
JAY: Four.
JASON: Tab size is four...
JAY: What?
JASON: What's happening?
JAY: Cue the Russell Westbrook meme, like, what? That should be right. Let's modify it elsewhere. Huh.
JASON: Modified elsewhere. No, I want you to do exactly what I'm saying.
JAY: Just do four.
JASON: Here we go. Give me that setting. And now this should be overridden.
JAY: Yep. In Python it does matter if you're using tabs or spaces. You can use either, as long as you do it throughout the entire code.
JASON: Got it.
JAY: Otherwise you get this weird mismatch error that like, we use spaces, four spaces is what's canon. We just use four spaces. All right. So, now we talked about you need an ID. So, like let's just make an ID. That's gonna be perfect. You're -- I feel like you're doing the colon thing because it's natural.
JASON: JavaScript-y.
JAY: That's good. Because normally you wouldn't, but in FastAPI, you do. Because it --
JASON: Ah!
JAY: It requires types. So, we're gonna give the type for this. The type for this is actually going to be a -- it's typing.optional. That's gonna be lower case. Optional is gonna be upper case. And I want to see if we get -- go look at the squiggly error. Import typing. Boom. You knew what to do. We could have done that. This is the one thing in FastAPI typing that I don't like. Because we have gotten away from importing typing from stuff. There's an actual -- you can just do a straight line. Like a pipe. And that does optional. But FastAPI doesn't support it because it would break a bunch of back code.
JASON: Oh, okay. Yeah.
JAY: So, you have to do typing optional for this. Then we're gonna do a square boi. And this is an integer. I mean, it could be whatever -- nope, pick, int, i-n-t, sorry! And then we're gonna do space equals. And this is going to be a field. So, it's field. That field that we imported. Let's open a round boi, yep. And are default's gonna be none. And I will explain this later. You're gonna want to do default equals none. Yeah... Comma... ah... da, da, da! What is it? There is a... there's a thing here... goes and checks notes really quick. I'm forgetting -- I'm forgetting what it's called. Doot, doo, doo -- it's it is primary key. And that's gonna equal true. Yeah, Python capitalizes true and false. We say it -- just that -- yeah. That part. We also forgot to do one thing. In that class up by SQLModel, add a comma.
JASON: Here?
JAY: Yeah. And we're gonna say table = true. And this is a --s that FastAPI-ism.
JASON: Okay.
JAY: Well, it's more of a SQLModel-ism. All right. What did we just write here? We're saying this ID, it's our primary key. So, this is gonna be our unique identifier. The fact that we identified it as none, none is capitalized by the way.
JASON: Okay. Why are you yelling at me?
JAY: And remove the spaces around that.
JASON: The Python way is no spaces?
JAY: No spaces inside of calls.
JASON: Okay.
JAY: But as assignments, there are spaces. And again, all of this is optional. It's not required. There's this phrase, it's a very dirty phrase. It's called "Pythonic." When someone tells you your code is not Pythonic, you have my permission to tell them their face isn't Pythonic and then just keep walking. I've had so many people have stopped using Python because before someone answered their question, they heard: Well, your code isn't really Pythonic. I'm doing my best to ban that phrasing from my lexicon. Anyway... so, we have basically said, this is gonna be our primary key. The fact that we assigned it as none means that by default, it's going to be empty. Once we save and commit this to the database, we're gonna let the database side of this handle assigning that key. It's just gonna autoincrement from zero --
JASON: Oh, I got it.
JAY: Like one, two, three, yada, yada, yada. But if we specify this isn't optional, that means we always have to provide a key. That means we have to be in the business of assigning what the key is.
JASON: Which like you could do with -- there's probably a UUID library or something, so we could quickly generate something, drop it in, et cetera. But it's kind of nice to not have to think about that. Especially for something like this where what we're doing is -- this isn't -- an auto-incrementing key is fine.
JAY: Yep. Now go to the next line. What else did we say we needed?
JASON: We needed their name.
JAY: That's fine.
JASON: And cool -- is it snake case for like a field name?
**JAY:**S that an attribute. So, it will be snake case, yeah. Yeah.
JASON: Okay. Do we need anything other than that?
JAY: I think that's all we need.
JASON: Yeah, I think that's good.
JAY: Someone is asking about auto-incrementing. Doing this will autoincrement once it goes to the database side. We don't need to worry about auto-incrementing on our end. Name, that's probably gonna be a string. Naming is hard. There's a great -- we don't have to do typing. We just do STR, STR for string, baby. That's a built-in Python type.
JASON: Much easier.
JAY: We don't even have to do field.
JASON: Oh!
JAY: Because it's required, we're not gonna give it an assigns value. We're just gonna say name had string.
JASON: Nice.
JAY: FastAPI will say, hey, that's got to be a thing. CoolPoints is interesting. So, we could give it a default of zero. And just say, like, you're gonna start with zero cool_points. That way we don't -- like if we're creating a new person, we don't have to do that. We could also start with any number. But we don't even have to do field. That's the beauty.
JASON: Oh, okay. So, you just started with zero.
JAY: Just equals zero. Yep, that's it. The only reason we're doing field is because we added the primary key stuff.
JASON: Oh! I get it. So, you only need field if you're doing something...
JAY: Database-y.
JASON: Special. I got it.
JAY: This is if for models. This is our model.
JASON: I am very happy with that. You know whey like about it, you mentioned earlier that when you write Python it feels human-readable. Obviously, there's some stuff that's computer science-y in here. N for integer, short for screen. Beyond that, though, I don't feel like I'm looking at anything wild.
JAY: Yeah.
JASON: My basis is the only language I've tried to learn recently that wasn't JavaScript was Rust.
JAY: Of course, it had to be Rust.
JASON: And I felt like a lot of times in Rust, I don't know what I actually just typed. With this, I feel like I know what I just typed. That's not a knock on Rust. It's just a positive thing for Python.
JAY: I'm just gonna sip my... sip my...
JASON: you're just very --
JAY: Again, with Python being a Honda Civic thing, we could make a Bugatti, and every time I hear that, sips. Since drink. Every time someone says Rust, it's like, drink! All right, so...
JASON: This is a good joke, if you start with Zero Cool, will there be a Crash Override later.
JAY: I don't get it, I'm guessing this is a video game reference.
JASON: That's from the movie Hackers.
JAY: Dang, I haven't seen that movie in so long.
JASON: In a minute. During the pandemic we decided to give it another watch. And it doesn't hold up, but it totally holds up if you know what I mean. Don't knock the Honda Civic, fair enough.
JAY: I'm not knocking it, it's a great car.
JASON: That's the whole point. If you get a Honda Civic, you will never have to think about a car for the next 250,000 miles.
JAY: Exactly. Exactly. That's why you pick Python. All right. So, all right. Let's go to the next one. And let's do... I'm trying to think of the right way to explain this and make it better. I think we put all of our stuff in app.py and then move some stuff out of app.py. Because I want you to get, you know, minimum time to, oh, wow, it worked! To do that.
JASON: Okay.
JAY: All right. So, from FastAPI, we're gonna FastAPI. This is a pattern that you'll often see Python modules do. There's usually some like big class or big thing that is named after the thing.
JASON: Okay.
JAY: Okay. And then... hm... from SQLModel we're gonna import... I'm trying to remember what it is now. Is it engine? Is it serve? Is it? Let's see, from SQLModel, good evening import session and we're gonna import create engine. And we're gonna import select. And I can't see what you're typing now. So, yep, that's it. That's good. I love it.
JASON: All right.
JAY: All right. So, let's start by creating an app. We're just gonna say app equals FastAPI.
JASON: Okay.
JAY: And we're gonna add some round bois to the end. And that's gonna initialize a FastAPI instance. So, Python has this thing called a decorator. Packages love using decorators. If you're writing your own Python code and you're reaching for a decorator, think about what you're doing.
JASON: Consider your choices.
JAY: Yes. Sometimes you need it. Sometimes you need it. Often it is used in conjunction -- decorators basically add functionality around a thing that you're defining. So, like, hey, I'm doing this thing. And I need to, like, add some zhuzh on it. That's where the decorator comes in. We're going to define routes, like patterns of things that will happen. And we're decorating them with the way that we want to interact with those patterns. So, for this we're gonna do an at sign, app, .get.
JASON: Okay.
JAY: And then we're gonna add some round bois. And this is the path to where we go. Perfect. Then we're gonna go to the line below it. And we're gonna define our def -- d-e-f -- space. Let's just call is Hello World. This is a function so when you do def, def is like function in JavaScript.
JASON: Oh, I gotcha.
JAY: Perfect.
JASON: I wanted to add some curly bois and I know that's not allowed.
JAY: No curly bois. That's for something different. We do need to add a colon at the end.
JASON: All right.
JAY: And return something. Return just like you do. This is actually where the curly bois show up. The FastAPI, we're creating a REST API, and most REST APIs are either text or JSON. By doing this curly bois, we're telling it we want it to be JSON.
JASON: Got it.
JAY: We could add some sugar on it the end, we definitely want it to be JSON, do some JSON stuff. FastAPI does that for us, don't worry about it. This is a dictionary. I think it's called Hash in JavaScript?
JASON: I --
JAY: It's like a key value pair, basically.
JASON: I would just call it an object. It might have a real name.
JAY: You're probably right. I don't know. I don't do JavaScript. All right. So, let's add just a message as the key. And the value's just gonna be Hello World. And both of these are strings.
JASON: Okay. Got it. Does single versus double quotes matter in Python?
JAY: No. Again, someone just tilted their nose and complained. Double quotes is kind of the standard, but single quotes work too. You're not gonna get yelled at for picking one or the other. Which you will get yelled at is if you one somewhere and then a different one somewhere else.
JASON: Just mix them just to like get somebody to twitch.
JAY: Oh, yeah. That's... [ Laughter ] Everything in my body wanted to jump through the screen right there. Like... it wasn't even -- it's like, ah! [ Laughter ] That's messed up... all right. So, this is a FastAPI app. This is really cool. Let's add a little bit of sugar to it, though.
JASON: Okay.
JAY: Above -- or sorry, below the def, below our function call here, right? Between the return and the def.
JASON: Got it.
JAY: Let's add three quotations. Perfect. And in there, we're gonna put outputs Hello World. This is a doc string. Actually, this --
JASON: Oh! Like a block comment.
JAY: Exactly.
JASON: Because I can do -- like I can break up it like this, right?
JAY: Yeah.
JASON: So, I've seen these before in the GraphQL schema language. Use the triple quote.
JAY: Yeah, and single line comments are just hash, octothorp, that whole thing. Yeah, also cool. All right. So, let's run this. Let's just see how we run this. So, we're gonna go into our terminal. Because we have to start a web server. We can't just do -- I mean, we could do Python app.py. It won't do anything. What we need to do is we're gonna use -- we're gonna use Uvicorn. Uvicorn is an asynchronous version of Gunicorn. I don't know if you're familiar with Gunicorn or not. It's like running npm serve or whatever.
JASON: Okay.
JAY: Same thing. We just have a bunch of them. Uvicorn is the one that comes built in -- or not built in with FastAPI. FastAPI installed it for you when you did that all thing.
JASON: Okay. Got it.
JAY: So, we're gonna type Uvicorn. Like unicorn with a V.
JASON: And it knows this without me having to do like a Python something?
JAY: Yes. It's using a namespace thing.
JASON: And that's because of this venv thing.
JAY: Yes. If it didn't, you would do Python, space, env, unicorn. I'm trying to make it easy.
JASON: I would have done it the hard way, this is helpful. Python is hard.
JAY: When I'm writing Python, I do it the hard way out of pattern. Some people say you should always do it the hard way. I say I just want people to be excited that they wrote something. So, let's just write it exactly how that works. Someone tried to cheat and say main colon app reload. That ain't it. Because we didn't call our project "Main." So, we're in API. It's actually gonna be API.app: App. And then let's just -- let's do a dash, dash relet. Why not?
JASON: Live watcher kind of thing?
JAY: Yeah, yeah. All right.
JASON: Okay.
JAY: Application startup, complete. We should see port 8000. Open that up and see what happens.
JASON: Okay. Localhost 8000.
JAY: Needs more quotes, I think.
JASON: Yay!
JAY: You even got a fancy tool that give the you all the stuff.
JASON: This is an extension I installed to pretty up JSON because I show it a lot on here.
JAY: I need the name of that extension. JSON prettier extension. Not the Prettier extension because that's its own extension.
JASON: I legitimately don't know what it's called. JSON viewer.
JAY: JSON viewer, perfect. So, here's that moment of wowness that's gonna pop up. This is the first wowness that I really like about FastAPI. So, you're at localhost 8000. Go to localhost 8000/docs. Boom. All right. Now open that. Let's hit that little get menu right there. Outputs Hello World. Tells you if there are parameters that you need to add. And you can just take it from there. I'm trying to see the look on your face. That's...
JASON: How dare you... [ Laughter ] Like, come on... how many hours of my life?
JAY: Right? FastAPI has built-in swagger.
JASON: The yeah, swagger, open API.
JAY: And you can modify this too. I believe there's another version of it that you can include. It's like in that FastAPI call that we did, app = FastAPI, you can provide some other bits and pieces to say, like, hey, I want it to look like this. I want the title to look like this. I want the color scheme. You can design that to be what you want. To me, this is the biggest advantage to picking FastAPI when you're building an API.
JASON: I mean, that's a huge advantage. Because -- so, like this is the reason that I think a lot of people have strayed toward GraphQL is the sort of auto-documentation.
JAY: Yep.
JASON: When you use GraphQL. Because Open API is dope, but you have to maintain your Open API. So, knowing that doing something like this is going to auto-maintain your Open API docs for you, that is slick. That is very, very cool.
JAY: Yep. It allows you to try out your API quickly. If you hit "Try it out," it's gonna give you the response. It's great. Hit execute, you've got -- you see your headers, see what your request is, see the response -- you can quickly start troubleshooting stuff.
JASON: So dope.
JAY: There's more to this and we will go to it in just a second. We have done a simple thing, you know what live reloading looks like. You don't need to change it to hello JSON. Let's add another route here.
JASON: Did you see that the comments we wrote just showed up here? So, we can add our own documentation about what this -- ah! So good.
JAY: That also supports markdown.
JASON: Oh, hold on. That I do want to see.
JAY: Okay. He says, unsure of himself now...
JASON: Oh, my god. Okay. Pack it up, y'all. Like this is it. This is what I've always wanted. I'm gonna have to learn Python now.
JAY: That's the thing. All right. Let's add another route.
JASON: Yes.
JAY: Let's start adding points. So, we need to add people. So, let's maybe let's create another route. This one's gonna be a post. Because it's not a get.
JASON: All right, it's a post, right, right, right. And this would be like people.
JAY: People, sure, yeah, I like that.
JASON: All right.
JAY: And then -- yep. We're gonna define.
JASON: Can I it type -- oh, my goodness. There we go.
JAY: Yep. So, now this -- this function call that we're doing -- I'll let you -- I'll let you finish your adds a people. I like it. You forgot your round bois in your function.
JASON: Oh, round bois!
JAY: And give it something to add. At the top, we need to reach for the model. So, from model import cool people. Hey, speaking of, we've got the creator of FastAPI in the chat. What's up, Sebastian? I hope to see you at PyCon this year. It's gonna be a party.
JASON: I'm sorry for what I'm doing to your framework, Sebastian. [ Laughter ]
JAY: Sebastian's good people. I have some wonderful photos of myself and Sebastian. We need to go get drinks next time, though. That was my only complaint. We did not drink together. When we go to add people, yep, we're gonna import those cool people into there.
JASON: Okay.
JAY: And that's just telling the API that like, hey, we need cool people for this call to work. Which is a good lesson in life. So, now let's -- let's do some databasing stuff. So, we need to scroll back up to the top. We're gonna do a call in between app and that first decorator there. We're gonna do an app thing as well. And this is gonna be called --
JASON: We're doing an app thing --
JAY: Yeah, we're gonna do that decorator again. It's gonna be app.event. OnEvent, boom. And then on_event is startup.
JASON: Is it in quotes?
JAY: It is, yes.
JASON: Okay.
JAY: Sebastian, if I get this wrong, just yell at me. Why are you mad?
JASON: Deprecated. Life span?
JAY: Okay. Let's check the docs on this. Because I often don't create the database on_event anymore. I -- this is the thing that we're gonna be moving into DB to do its own thing. See? On_event.
JASON: Life span...
JAY: Okay... what are we doing here? This is async context? Async def, live span. Oh, but you're making us do async stuff. And then we're passing live span equals live span. Okay. That makes sense. I can explain that. I can explain that without hurting myself. Copy and paste that code. And then we'll make some changes.
JASON: Okay. I need this context lib thing also?
JAY: Yeah.
JASON: Where am I? Where did I go? There's code. So, we're gonna go here.
JAY: Sebastian says, just use "Event." Let's just use Ent.
JASON: You've got that written. I'm also a little worried on time. We got like 20 minutes left.
JAY: Okay, okay, we're gonna speed run this. Okay. Create a new function, def, there's no tab in there.
JASON: What am I...
JAY: That's okay, that's okay. And then create session.
JASON: Create session, okay.
JAY: Yep. And now we need to add -- hmm... what are the words I'm thinking? We need to create the engine here? Say engine = create_engine. Boom. Okay. And then... let's do... return session... oh, did I freeze again?
JASON: No, just me.
JAY: Checking the chat.
JASON: Yeah.
JAY: And then the session is going to be that engine. So, we need to do a couple of things here. Yep. Boom. In this of that create engine, give it the path. We're using SQLite for in. So, this is just gonna be the pattern for SQLite. We could do in memory. It's better if we persist it just so that we don't have to keep re-creating them.
JASON: Sure, yeah, yeah.
JAY: So, inside of quotations, do SQLite, colon. There's one L in SQLite.
JASON: Oh, I remember this. Yes. SQLite.
JAY: And then we're gonna do three back ticks. Sorry, not back ticks what are those? Slashy bois.
JASON: These ones.
JAY: Yeah, that one. And then we got to name our database. Let's just call it "Cool people data," I don't know, dot DB.
JASON: Okay.
JAY: All right. Awesome. Also, Sebastian said that the life span API will improve. Hey, that's awesome. I want to keep an eye on that. Maybe I can contribute. I haven't contributed to FastAPI yet. That's on my list of things to do. All right. So, now that we've -- we've created this session. We're gonna use this session to -- ah, see? This is where I'm getting -- I'm getting a little confused on myself. Because we have to pass that session in.
JASON: Yes.
JAY: I don't think we return session here. I think we return nothing. See? This is why I don't like the startup events. Sebastian, I don't the startup events because of this reason. Put the engine outside of that function.
JASON: Just out here?
JAY: Just out there in the wild. I love it. And then do session equal that session engine below it. Yep. Okay.
JASON: Okay?
JAY: Yeah, I like it. Yep. All right. And we'll keep that for now. I might be missing something. So we might come back to that app on event thing. I normally put this in a separate module, which is why this is messing me up.
JASON: Okay.
JAY: And then I would just import session and then all of my database stuff is in db.py. So, let's scroll back to our post. Our post people here. And in that code, we're gonna say... with -- I don't think we have to do -- we don't have to do the with. Because we don't need a context manager. Just do session.add. Yep. Boom. And then we need to pass in the cool_people.
JASON: Okay. And that was gonna be name, and to start --
JAY: We just need to pass cool people.
JASON: Just need to pass cool people?
JAY: Just need to pass cool people.
JASON: Like that?
JAY: Yep.
JASON: Okay.
JAY: And below is that, do session.commit. Perfect. And then let's return... let's return another dictionary there. Yeah. And we'll just say... message success --
JASON: Oh, right. Quotes.
JAY: Yep. And then let's add a comma, new line. And then person, cool person or whatever. Whatever you want to label it as. And then that's gonna be CoolPeople.
JASON: All right.
JAY: Okay. What this is saying is, we're gonna require cool people being added. So, that's gonna need to be added.
JASON: Okay.
JAY: I actually think that you just can't call cool people and add people, you need to add to tell it what it is. At the top. Not in that one. In there.
JASON: Oh.
JAY: Yeah, we've got to tell it what CoolPeople is. And make sure that -- because you're going to run into a namespace thing -- do like -- just lower case it.
JASON: That's for all of these, right?
JAY: Yes. Because what you're saying is this instance of a cool person -- you could even call it a coolPerson. Because we're just adding one coolPeople. Hey, Patrick, you mentioned GraphQL. Patrick Arminio is like the GraphQL guru in the Python space.
JASON: Nice, nice .
JAY: Absolutely knows the thing. Yep. Cool person, yep, yep, yep. I like it. I like it. Let's check the terminal. See it it's working.
JASON: No module named models which means I did something wrong.
JAY: No, put a period in front of the models. Nope, nope, nope. Get rid of that period. We're gonna create another file. This file is going to be empty intentionally. Inside of API. It's gonna be double underscore init, double underscore dot Py. And that just means that now you're able to traverse through this folder and call any of those dependencies.
JASON: I got it. Now you're happy?
JAY: It should work now. It should be happy now. You probably need to close this call since it errored out. Just do -- yep. You know what to do.
JASON: I --
JAY: It still did it? Add a dot there. That's just saying, hey, that's gonna be a local version. Boom. There it goes.
JASON: Startup complete.
JAY: Yeah. All right, let's reload.
JASON: So, now --
JAY: We got people.
JASON: -- we got people. Try it out.
JAY: Yep. It will tell you what you need to add. So, you can just say, "jay," ten, yep.
JASON: Okay.
JAY: Success.
JASON: Wait, wait.
JAY: Internal service error. Go back into VSCode. It will tell us what the error is. Oh! We didn't create the metadata. This is a metadata thing.
JASON: Okay.
JAY: Cool. So, underneath that create engine. If we're gonna do SQLModel.metadata.create_all.
JASON: Like that?
JAY: Checks code. And you're gonna want to do engine ideas the curly boi. Where are we at here? That look good. Okay. So, now the thing I'm worried about is did it save that -- it shouldn't have saved the database because it shouldn't have been able to create the database. And all that's doing is that's just building that table out and providing all of the metadata stuff.
JASON: And I should leave the ID out, right? Because we want it to autoincrement for us?
JAY: Exactly.
JASON: All right. I'm going to do that. And then I'm going to execute. And it says, success! Hey! But it didn't give us back...
JAY: It doesn't give us the coolPeople. This is something I have seen. It's in the FastAPI docs. Because it's reaching for that value I believe before it was saved.
JASON: Ah. So, do we... I mean, that's... I'm kind of okay with that. Can we --
JAY: That's fine. That's fine.
JASON: Can we just do like a LoadPeople to?
JAY: Let's just do a get that shows all the people.
JASON: Okay. I'm going to set up a get_people. Let's see how far I can get with my new found Python skills. I'm going to say session.get_all -- no. All?
JAY: I can help you if you want me to help you.
JASON: I need you to help me. I don't know what the autocomplete is here.
JAY: So, session requires -- by the way, I think you left the comment on the screen. Session in this case is we're going to actually have to provide a statement. I don't believe there is a get all cool people. But you need to add -- you need to tell it what we're gonna be referencing. So, it needs that import in def. Yep.
JASON: What would it -- how do we do this? Is it like all?
JAY: Just say allPeople -- I'm wrong. I'm lying to you right now. We don't need that. We don't need that. I've written this code before. Opens it, looks at it. Ah... do, do, do, do... okay. So, what we're gonna do -- I need to go into this. All items -- so, we need to create a query.
JASON: Okay.
JAY: And then we need to execute that query.
JASON: Okay.
JAY: The query is going to say "Select." And then we're going to call coolPeople.
JASON: Like that?
JAY: No. You don't need the round bois inside of there. You need it after select, don't need it after coolPeople.
JASON: I see, I see.
JAY: Then we're gonna do session.exec for execute. And then we're gonna pass that query in.
JASON: Okay.
JAY: And then at the end we're gonna do dot all.
JASON: Called?
JAY: It is, yes.
JASON: Okay.
JAY: And let's save that as items. Say items equals that.
JASON: Okay.
JAY: And now let's return items.
JASON: Okay.
JAY: And we might -- we want to put that in just so is that we see the JSON pattern going. Let's put that in -- yeah, you can just say people colon items. Yep.
JASON: Okay. All right. So, now out here I've got... get people. I'm gonna try it out. And we get back...
JAY: Yep. We get back our people.
JASON: All right. Try this one more time. We're gonna add another people. And for this one, we'll drop out the ID.
JAY: Yep.
JASON: Oh, wait, I have to try it out first.
JAY: I do wish there was a way -- maybe this is an issue thing that can be filed. If we're doing that, if we've already defined it, maybe we say don't include optional values or something like that?
JASON: Okay. If I clear and execute again. There we go. We've got two peoples.
JAY: Two peoples.
JASON: Is this legible for everybody?
JAY: Yep.
JASON: I just realized it's kind of small. Okay. Look at that. We've got crud happening.
JAY: Yeah. And you've done things too. It's not just we've got a bunch of get calls. You've got a post in there. If you wanted to do like a -- a put, you could say, you know, put update a person. And then you just need to pass the ID in. You also can see -- you can check out the schemas here, say, hey, here's what these look like.
JASON: Yeah.
JAY: And you can rock from there.
JASON: And this is like extremely cool that -- to test that API. I'm just using the autogenerated like browser Open API docs here. Because otherwise I would be in my terminal, trying to remember cURL commands. and then it becomes a whole thing.
JAY: Opens up Postman.
JASON: And the fact that all these tools are great, but it's nice to define the code and switch back over here, reload the page and off we go. That's pretty slick.
JAY: Yeah. So, we got 10 minutes left. Do we want to start wrapping? Or do we want to -- you need cool_points.
JASON: What do I do to get cool_points? Because I'm currently in the negatives.
JAY: Let's just do a put. And we'll make -- it's a simple update, you know, update user type situation there. And yeah, some folks are talking about other API tools. So, DRF has something similar. It's not Swagger. You're right. I forgot what it's called too. Marsh mall low I've heard of, I think I have played with Marshmallow for 5 minutes. Not that much. I was talking to someone last night or may or may not be in the chat about doing this. And one of the things that we talked about is if you're trying to figure out who you're gonna pick, you're probably gonna go with the one that has the most community and the most documentation around it. Sebastian has done a phenomenal job of creating an amazing documentation spec. The docs for FastAPI make a bunch of people jealous. Because of how well-defined they've been and they've been since day one. So, I would say if you're -- if you're wanting to go with a community, go with the community that's gonna have good docs, go with the community that's gonna have a bunch of blog posts and people writing content on it. FastAPI you can't go wrong with. Django you can't go wrong with, the REST framework. It's a little bit more magical. You don't necessarily learn Ruby, you learn Rails. In Django, you learn it. And if you like magic, Flask will punch you in the face. Flask is like that person at the bar, everyone trusts Flasking. Everyone uses Flask. It's the most popular one. Flask is a no magic, no BS, you define everything we do here. So, this is a good medium between the two. All right. So, we're gonna do one thing different with this. We actually have to define what -- because we're doing a put, we have to know where to put it.
JASON: Right. We got to decide which people.
JAY: Yeah. Inside of that route, we're gonna put people, slash, and then what we want our people ID.
JASON: This is the standard place holder, did I do that right?
JAY: No.
JASON: Whomp, whomp. [Whomp, whomp sound]
JAY: And by "Close" I mean not. We're gonna bring more curly bois in the mix.
JASON: All right. Get some curly bois in here.
JAY: All right. And we're gonna call an ID. So, and let's call it item ID. I like it. Well, yeah, not --
JASON: The Python way?
JAY: Like that. And then when we do update people, we're gonna require the item ID is defined. And that's gonna be an integer.
JASON: Okay. So, item ID integer.
JAY: Yep.
JASON: Okay. I'm ready.
JAY: And we're also gonna need -- we also gonna need to also require cool people as -- why did I do that? I keep lying to you.
JASON: You just really want cool people.
JAY: I really just want cool people. Okay. So, let's just copy all the stuff we did above and get people.
JASON: Copying --
JAY: Yep.
JASON: Doing it, I'm ready. I imagine this is the one that changes.
JAY: Actually, it's the one before that. We're gonna do some simple database stuff here.
JASON: Okay.
JAY: So, we're gonna do select cool people, all that stuff. And then at the end, we're gonna throw a dot on there and say, where. Oh? Someone asked about my -- I used to have a microphone with the Kaotica eyeball on it. And the argument was that as big as my head, because it was. I sold that mic. I gave it to a friend getting into music production and I wanted to help him get off the ground. I gave him that mic.
JASON: We're using SQLite for the DB.
JAY: We're using SQLite, you could use postgres or MySQL. It's using SQLAlchemy in the background. So, a lot of this stuff wouldn't change. The only thing that would change is the URI that we pass in. And that would be it. Yeah. Okay. So, where ID is equal to item ID. Equal to item ID. Yep.
JASON: Like so?
JAY: Yep, yep.
JASON: Nice.
JAY: We type how we talk. You can't use Redis, but there are other tools that you can use that will allow you to use Redis. So, that's a thing. And then instead of all, we're gonna do first.
JASON: Okay.
JAY: And... go to the create -- because we do have -- we do have to add cool people here. Because we have to tell it what to update. I'm so confused on how it doesn't know what to update. Oh, it does. Okay. It does. Sorry, I'm looking at my notes and my notes are very different. So, we're gonna say, I mean, ID. We're gonna do a comma. This is up at def update people. So, line 44. Is this fast? It is fast and it is API. And then let's do a person equals cool People.
JASON: Like that?
JAY: Yep. Yeah. Now we're selecting CoolPeople and then we're going to do -- so, we have item, because this is not really in items anymore. So, just item. And then below that, we're gonna update the item itself. So, we're gonna say, item equals person. Yep. And then we're gonna do session.commit. And I just want to double check this, make sure I'm doing all the stuff that I did. Oh, of course, because I turned it into another function called update item. And then update item does -- yep. Item equals this, item equals that. Session.add item? That's not supposed to happen... and we'll do session.commit. We might have to do session.add item if it doesn't work. But try it like this. If someone in the chat is yelling you're supposed to do the add item. We're using SQLite, you could use postgres, it would be the exact same code. You are two, I believe.
JASON: Getting all my people. I am ID two. So, let's see if I can try this out -- oh, my query is gonna be --
JAY: Oh!
JASON: We probably just want to update the cool_points, right?
JAY: Yes, let's just update the cool_points.
JASON: And so, my item would be --
JAY: Oh, how would we -- that would just be the body.
JASON: Just put in the cool_points. And that's the integer, right?
JAY: Yeah.
JASON: And you could do item.cool_points?
JAY: You can. You did that on your own, I love it.
JASON: Cool_points equal cool_points and that should theoretically update.
JAY: It should.
JASON: Right down to the wire. We're gonna pull this off. So, chime it out too -- I'm gonna add two cool points. We're gonna execute.
JAY: What's the error?
JASON: The error is -- unexpected keyword argument of id.
JAY: Huh...
JASON: Did I -- does it need to be quoted or something?
JAY: No, that's my fault. I lied to you again. So, change where, just get rid of all of that. Just all that in.
JASON: Get it --
JAY: Get it out of here. Then throw a comma after CoolPeople.
JASON: Here?
JAY: Yep. And then do item ID. And we're gonna do instead of select, we're gonna do session.get. And then we're gonna delete that item. We're just gonna -- we're gonna make that item what query is currently so, query -- yep. Boom. You got it. You got it. This is --
JASON: All right. Here we go. Try it out. Two, two --
JAY: We're at time, what do we do?
JASON: We're at time. We updated. Let's get all people, try it out... execute. And look at me! I'm back in the positive on cool pints.
JAY: Yay!
JASON: We've done it everyone, right on time. With that, let's give people additional resources. So, we have been playing with Python today. This is my first ever Python app. Very, very excited about that. This is FastAPI which is what we have been using today. So, make sure you go and check that out. And we have been learning with Jay. So, make sure you go give a follow on all the places. Jay, for anybody who wants to continue, are there any other resources you recommend for folks?
JAY: I mean, for FastAPI, I'm gonna say just check out the docs. The documentation is arguably one of the best piece was documentation in Python. I would just say keep making stuff. Build stuff that's cool. I definitely want to learn how to turn this into an actual project. So, maybe you can teach me something.
JASON: We'll do an exchange. I'll come on your stream and teach you how to do that part.
JAY: I don't have this fancy stuff. Down and dirty where I'm from.
JASON: Look, I'll throw my phone on the floor on your stream.
JAY: I love it. That's perfect, that's perfect.
JASON: Okay, because we're out of time, I'm gonna fast version this. Thank you for hanging out, this episode like every episode is live captioned, Amanda here from White Coat Captioning, made possible through our sponsors, Netlify and Vets Who Code. Thank you for supporting and making that possible. To you enjoyed this episode, like the video, subscribe, tell your friends because that's what interests sponsors in making this show possible. And... you know, make sure you're checking out stuff on the schedule too. Because we have all sorts of fun stuff happening. We've got a good one next week. Learn about Million, the new compiler for React. I don't know what GenAI is. People keep saying the word, Adam Cowley is going to teach me what it is. We will learn about that. Jay, any words from the chat?
JAY: I love the shade, pull in Jay to rebuild his site. One, I would totally be down. But also, Jay built his own static site generator that powered his site. Yeah, if you really want to dive into Python and bang your head against the wall, yeah, check out Render Engine. That's a fun one.
JASON: Love it.
JAY: That's it for me. Have fun. I'm glad you got Chance coming up. Chance is also in San Diego. So,like -- I've asked Chance about the friendly. One of the best burgers in San Diego.
JASON: Yo, I was in San Diego and the first thing he did was take me to Friendlies. And it is -- that burger is the truth. It is so --
JAY: No joke, it's down, it's dirty. Anne's dry cleaning is around the corner. It's the best froyo place.
JASON: I thought it was dry cleaning.
JAY: I'm sorry, it's gelato. And it's so good and they do tastings.
JASON: I love it. We're gonna call this done. Thank you so much for hanging out. We will see you next week. Take care!