HTMX + Flask: Modern Python Web Apps, Hold the JavaScript Transcripts
Chapter: Course conclusion and review
Lecture: Core elements of the starter Flask app

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this course we did not start from file new project. No we started from an existing Flask application that already worked and that had already quite
0:10 a bit of structure and web design and everything in place, and we added some features
0:14 to some of the pages and then added new pages in the case of the Active
0:18 Search. So let's just review a couple of the core building blocks and pillars of this app as we got started.
0:26 So remember, our models, our data exchange that sort of stood in for our database and
0:31 if we use SQLModel, could actually be our database models or Pydantic models.
0:37 So we've got for example our category which holds a bunch of videos and we just
0:41 basically derived from the baseModel from Pydantic, create the fields and give them types like str, str and List[Video],
0:49 another pedantic model. And then we can immediately just load up some sort of JSON
0:54 document and hand it off to Pydantic, and it will load these up and create them and that's basically how our database, our lightweight database worked.
1:02 Another really important idea was the idea of View Models. The view models job is to exactly exchange the data that the template needs.
1:12 Go to the view, it gets the data may be off the URL and so on and then this thing is going to go and get the rest of the data
1:19 may be from a database or an API, possibly from a form, do the validation and then sync that back to the
1:26 HTML Jinja template. So we have this ViewModelBase. It had all sorts of good things like it would store the request,
1:33 it would create this merged request dictionary like headers, cookies, form and so on.
1:38 All that data together. We would derive from it like here's our IndexViewModel
1:43 and it has a list of categories we got from the database, and then it turned
1:47 those into rows because we want three categories per row and that was easy to iterate
1:52 over, so hen we got to Jinja we didn't have to write complex Python code embedded in HTML. You should never do that,
1:59 do it in the view model and then hand off the final data to the template. Then we just return the dictionary out of our index view for our homepage and
2:08 that shows all the categories right there. Finally we had a lot of conventions in the way we structured our code.
2:16 Remember we had our views and they kind of drove everything. We have feed home and videos.
2:20 Then over in the viewmodels folder we would have view models that corresponded to view
2:25 methods inside of that home. So we have the folder home, and then we had the name of that function inside home.
2:33 So we have an index function and So in the view models we have viewmodels/home/index_viewmodel.
2:40 We also have this shared/viewmodelbase and then we have the same basic organization
2:45 for templates. We have their home, set of views and then in there we have a function called index. It's gonna use the HTML Jinja template index.html.
2:56 Hopefully you're familiar with this at this point. We've been working with it for a long time, and I find this organization very helpful
3:01 You don't have to use it. You can use a different organization but just have something like this where,
3:07 you know, I'm working on this function or this HTML template. Here's all the other pieces and how it fits together.

Talk Python's Mastodon Michael Kennedy's Mastodon