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
0:01 will not have a single HTML page whatsoever.
0:05 Chances are you're gonna have some kind of page that talks about it or some extra
0:10 information, maybe people can go to the API and see what their
0:13 API keys are and so on.
0:15 Yeah, you could do that in two web frameworks separately,
0:18 but why would you if you don't have to?
0:20 So it's very likely you want to render basic HTML templates,
0:24 not as much as, say,
0:25 Flask, Django, Pyramid and so on,
0:27 but you still probably do want to work with them.
0:29 And so doing that is super easy. In
0:32 FastAPI, we have good built in support
0:35 for Jinja. Two things we gotta do.
0:37 We've got to create a templates object,
0:39 which is a Jinja2 template.
0:40 So we just say, when somebody asks for a template from you, go look in
0:44 that purple folder called "templates", which we marked as a temple folder in PyCharm.
0:48 We set the directory language and so on.
0:51 And then once we're doing this,
0:52 when we have some code that needs to return a template,
0:55 we just need to do two things, create a template response and say the name of
0:59 the template. But we also have to make sure that our API method,
1:02 our HTML, our view method, gets the request object and then just forwards it on,
1:08 because the underlying templates need the starlette requests in order to do their work,
1:13 so just pass it on like this.