Modern APIs with FastAPI and Python Transcripts
Chapter: Accepting inbound data
Lecture: Showing recent events on the home page

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, we're able to create some reports.
0:02 And if we go and run our report apps and say
0:05 "show me the reports", I guess they're not there anymore.
0:09 A new report. This will be in New York City now we can see them.
0:13 There's some reports there. If we go to the home page,
0:16 you don't see anything. Just see
0:17 here's some examples of how you might call this,
0:19 and we are not even documenting all the API here.
0:22 But nonetheless, what I would like to do is
0:24 I would like to also show those reports right here on the home page,
0:28 just below this example. It doesn't necessarily make sense for this project,
0:31 but just to show you sort of integrating into the templates.
0:35 So let's go over here. Look at our view and
0:38 our home. And over here we're returning a template response.
0:41 And what else? Maybe we wanna include
0:43 this is all the data that is available to the template.
0:46 Maybe we wanna have events or reports or something like that.
0:51 How do we get that? Well, that is super easy. We go,
0:53 we put this into a separate library on purpose.
0:55 Report service like that, and we just say get reports, there we go.
1:00 That is it. Actually, is this async? It is
1:03 async, so we need to await it.
1:06 Await it, which means this needs to be async. There,
1:09 that's that. And this data over here is going to be available to the template,
1:14 so I'll put that maybe separate, like this.
1:17 We're gonna pass this data over,
1:18 and it will have events. And let's just go and add that down here, under
1:22 home, under index. What do we want to do with this? After the list, after
1:26 this. So if there are some recent weather events,
1:35 let's just do a ul with an li inside of it.
1:39 And then this we're going to repeat,
1:40 we'll say "for e in events".
1:48 Notice, we're not getting help here,
1:50 so we probably need to set our template language
1:54 to Jinja, there we go.
1:57 So we're going to put in here.
1:58 We're gonna put just put "e.description".
2:02 Let's just put the city first "
2:09 and a colon".
2:12 Alright, so nothing happening there yet.
2:17 We're gonna need to go and create one again,
2:18 aren't we? Let's go back to our little client and say,
2:21 I want to see the reports. Oh,
2:23 no, it should be there says New York City has a report.
2:26 Uh, what happened? I haven't restarted the app, and the app over here to
2:30 be rerun, to do this of course,
2:32 that's going to mean it's empty again, we're gonna need to do report.
2:37 It's raining, raining in Seattle. Wow.
2:42 And let's add another report. It's frosty in Portland.
2:47 Now we see them. There's two. Over here,
2:50 check that out. Recent weather events Portland,
2:53 US. It's frosty. Seattle, US.
2:56 It's raining. We could even do something to combine these.
2:59 That might be fun. We could go and say,
3:01 give us the actual temperature. And when we create these events,
3:05 right. So over here, when we do this add report,
3:10 we could actually store as part of the report,
3:12 stuff that we get back from our weather service here,
3:15 like submit a city we said,
3:17 Well, let's pull up the temperature and store that we could make this really interesting
3:20 and intertwined, but hopefully you get the idea.
3:23 Let me add one thing. If there's no recent weather events,
3:26 I would like to just not show that. So let's do that real quick.
3:35 Alright, so if we refresh this, that shows up.
3:38 But if I restart it which will empty out our events, now it's empty.
3:43 Once we add one again reports some weather it's raining,
3:48 raining Seattle. Refresh it. Boom, they come back.
3:51 OK, so just to show you the data that we're working with over the
3:55 API, totally makes sense to have maybe some client reporting it,
3:59 but the Web app showing it. Anyway,
4:02 Hopefully that diversion between creating the client and showing it here on the page was worthwhile
4:07 You can see it's really,
4:09 really, really easy to do.
4:11 That's the HTML we had to write.
4:13 That is the API or view method code
4:18 we had to write. All this stuff was already done, it's just putting the larger building blocks together.