#100DaysOfWeb in Python Transcripts
Chapter: Day 50: Responder framework
Lecture: Responder core concepts
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's close down our chapter on Responder by quickly looking at some of the core concepts. Incredibly, what you see on the screen here
0:07
is a complete Responder application assuming that you don't count that index file that's just the static HTML or dynamic HTML.
0:15
So, what are we going to do to get started with a templated HTML page-based view? Come in here and create an API from responder.api
0:23
similar to Flask's app. We're going to add a route as a decorator through api.route and we saw that you can even use curly braces like f-string
0:32
like syntax to define places where data is passed in the URL. Then we're going to use api.template and pass the relative file name
0:41
of the template we want to use. It assumes that you're talking about the template's folder, so within there
0:46
it's the home directory in the index.html file. We're also here passing a user key which has the value u. It didn't really say where u comes from
0:54
but, you know, you get it from somewhere, right? And you're passing along values like this that can be used inside that Jinja2 template.
1:01
And then, what we do is we just set the content of the response. It's a mutable object, so we don't return values we just change response.
1:08
Gives us a bare one, we set some values in it uses that result to generate the page. That was for a page backed by a template.
1:15
What if we want a JSON-based API view? Well it's pretty similar. We're going to have the route again and see we're passing the keyword this time.
1:23
And that key word appears in the search methods signature. Then, we're just going to create a JSON serializable thing.
1:30
The simplest is probably a dictionary or list of dictionaries, things like that. And we're going to set the media element right there.
1:36
And by setting media, we're telling it here is a JSON object. I want you to serialize and send back as
1:42
part of this API response with the content type being JSON. That's it, that's Responder! Remember, all the key features I told you
1:50
about at the beginning, that are not shown or covered here. Web sockets, async capabilities, background tasks. All that kind of stuff is in there
1:58
but we don't have time to cover it. You can go explore it yourself, it looks really cool.