Modern APIs with FastAPI and Python Transcripts
Chapter: Building a realistic API
Lecture: Concept: Rendering HTML templates
0:00 It's unlikely that you're API will not have a single HTML page whatsoever.
0:06 Chances are you're gonna have some kind of page that talks about it or some extra information, maybe people can go to the API and see what their
0:14 API keys are and so on. Yeah, you could do that in two web frameworks separately, but why would you if you don't have to?
0:21 So it's very likely you want to render basic HTML templates, not as much as, say, Flask, Django, Pyramid and so on,
0:28 but you still probably do want to work with them. And so doing that is super easy. In FastAPI, we have good built in support
0:36 for Jinja. Two things we gotta do. We've got to create a templates object, which is a Jinja2 template.
0:41 So we just say, when somebody asks for a template from you, go look in
0:45 that purple folder called "templates", which we marked as a temple folder in PyCharm. We set the directory language and so on.
0:52 And then once we're doing this, when we have some code that needs to return a template,
0:56 we just need to do two things, create a template response and say the name of the template. But we also have to make sure that our API method,
1:03 our HTML, our view method, gets the request object and then just forwards it on,
1:09 because the underlying templates need the starlette requests in order to do their work, so just pass it on like this.