HTMX + Flask: Modern Python Web Apps, Hold the JavaScript Transcripts
Chapter: Feature 2: Active search
Lecture: Performing the server-side search

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, I think it's time to go ahead and perform the search. That might sound complicated. We have all these videos, and we want to search their
0:07 title or their author or things like that. And we need to break the words apart into small pieces and do and clauses, it could be complicated.
0:16 Luckily not so much, not so much. We've already got a lot of this in place, and it's not really relevant on how
0:22 we do the search. So we're just gonna run it. Before we get quite that far. One thing I would like is to go over and change our navigation over
0:31 here. We've got our stuff that goes to the right. So here's this feed. Let's go look at this real quick. So it's this thing over
0:39 here. Remember it's slow, it takes like five seconds or so to load, but eventually it will get a feed of those.
0:45 What I would like is to put something like that for search. So we'll come over here and just say /search and what do we put here,
0:55 what font-Awesome thing do we put here? I don't know. Let's go find out on font-awesome. So if you're not familiar with font-awesome,
1:02 it's a rally great place to get all sorts of fun stylable icons. We're going to come down here and say we want search. You know, I think that one,
1:11 that plane one right there will be good enough. So we just click to copy and paste that sucker right there. If we go refresh it. Not that one.
1:25 Let's go somewhere else. There we go notice we have our search. If we click it. Oh, it does not take us to the right place because it should
1:31 be /videos/search. Go back and try again. Here we go videos/search. Okay, so we've got this up here,
1:44 we want to actually perform the search if somebody supposedly added an input here and we could submit it back, let's go and perform that search.
1:53 So we're going to start over here. This thing is going to come to life and it knows about the search text and
1:59 all those kinds of things. It also needs to be able to pass the video models, the actual video objects down to this search template.
2:09 So it makes a lot of sense for this thing to come along and do that as well. So we'll say self.videos
2:15 which let's define this to be a List[Video], that's going to require some imports isn't it? I want to make that nothing. But if there is search text,
2:27 let's go ahead and do a search. So we'll say self.search_text and self.search_text.strip(), make sure there's really something there,
2:37 then we want to actually perform So videos is going to be, you guessed it, the video_service search. self.search_text. That's it.
2:51 That's all we gotta do. So you can come down here and look at how this search implementation works, but again, we're not really using a real database,
2:57 so it's, it's not super relevant, honestly. But let's put a breakpoint right here and run this and then do some kind of search. All right.
3:07 Let's go to our search and the first time there should be no search text, but as we step over. Notice what is search_text.
3:16 None. Okay, good. We'll come down here. And off it goes. So let's go and put our search?search_text=apple. Now,
3:29 if we step down, what is the search text? Oh, it's Apple. Very, very good. We're going to come down here,
3:36 run the search, step over, let's see what our vm videos are. Look at that. We've got a bunch of videos. We've got one about WWDC,
3:45 one about the M1, one about Apple and one about Python developers exploring Apple. I think our searche is working,
3:53 what do you think? Really all we did was go in and check, look if some search text comes along. Let's create a place to store the results.
4:02 Set them up for if there's no search, it's the first time we hit the page will get an empty set of results.
4:09 And then if there is, go and just use the video service and let it do the search for us.

Talk Python's Mastodon Michael Kennedy's Mastodon