Latest / Talk Python To Me / #437: HTMX for Django Developers (And All of Us)
Transcript
- 0:00Are you considering or struggling with replacing much of the interactivity of your Django app with
- 0:04front-end JavaScript? After all, your users do expect an interactive and modern app, right?
- 0:10Before you make a rash decision, you owe it to yourself to check out HTMX. It goes really well
- 0:16with Django. We have Christopher Trudeau here to run us through the whole awesome list of HTMX and
- 0:22Python things and tell us about his new HTMX and Django course. This is Talk Python To Me,
- 0:28episode 437, recorded October 5th, 2023. Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:50This is your host, Michael Kennedy. Follow me on Mastodon, where I'm @mkennedy,
- 0:54and follow the podcast using @talkpython, both on fosstodon.org. Keep up with the show and listen
- 1:00to over seven years of past episodes at talkpython.fm. We've started streaming most of our episodes live
- 1:07on YouTube. Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about
- 1:13upcoming shows and be part of that episode. This episode is brought to you by IRL,
- 1:19an original podcast from Mozilla. When it comes to artificial intelligence, AI, what's good for
- 1:25trillion dollar companies isn't necessarily good for people. That's the theme of season seven of IRL,
- 1:30Mozilla's multi-award winning podcast hosted by Bridget Todd. Season seven is all about putting
- 1:36people over profit in AI. Check them out and listen to an episode at talkpython.fm/IRL.
- 1:42And it's brought to you by Sentry. They have a special live event, like a mini online conference,
- 1:47where you can connect with the team and take a deep dive into different products and services
- 1:52every day for a week. Join them for launch week, new product releases, exclusive demos,
- 1:58and discussions with experts from their community on the latest with Sentry. You'll see how Sentry's
- 2:03latest product updates can make your work life easier. Visit talkpython.fm slash
- 2:08sentry-launch-week to register for free. Hey, Christopher, welcome to Talk Byther Me.
- 2:15Ah, it's been a little over a year. I just looked it up. It's nice to be back.
- 2:19Yes, the annual appearance.
- 2:22Sure. You want to make it annual? I can go for three or four, and that's a little like we'll see into the future.
- 2:27We both spend a decent amount of time behind microphones these days, don't we?
- 2:33I seem to. Yes, yes. It does seem to be the way it is.
- 2:37It's a funny world, right?
- 2:38I was just looking it up because I figured, you know, your standard interview question,
- 2:41so what have you been doing? It's like, well, I've put out over a dozen courses since the last time I was on.
- 2:46So, yes, as you said, it's a lot of time behind the mic, so not so much the live mic.
- 2:52So if I'm doing a little nervous, say the word so too many times, it makes me more human than in my courses, I guess.
- 2:58That's right. The magic of editing.
- 3:01That's right.
- 3:02That was a concise and quick response.
- 3:05Well, it's great to have you back on the show and looking forward to talking about one of my very, very favorite topics,
- 3:12HTMX at this time in the context of Django.
- 3:15Yeah, I was actually just the other day listening to your episode.
- 3:20What is it?
- 3:21428 when you had Vincent and Gibson on and you delve into HTMX a little bit.
- 3:27They seem to be big fans.
- 3:29It's one of the sort of semi-gags in the course that we're going to talk about a little bit is I just kept telling people that I'm in love.
- 3:36Like it really is the answer.
- 3:38I'd love to see it become part of the HTML standard.
- 3:41It's a much, much happier way of coding.
- 3:44Makes me feel like the old days, things are much simpler with it.
- 3:48Yeah, I totally agree.
- 3:49And Carson Gross, the creator of it, does often say it's kind of like HTML just kind of stopped growing when JavaScript came along.
- 3:56And this is what it should have been.
- 3:57But well, if I've got to do it, I'll take one for the team and build HTMX for everyone.
- 4:02But you're right.
- 4:03It really should just be built in more or less, shouldn't it?
- 4:05Yeah.
- 4:05Every once in a while you come across something where you sort of look at it and you're like, wait, this is the right way.
- 4:09Crap.
- 4:11I've been doing it wrong for almost 30 years.
- 4:14This is the right way.
- 4:15So why didn't anyone tell me?
- 4:17That's right.
- 4:17Yeah.
- 4:18Yeah.
- 4:19It's a little bit of a, oh, wait, if I'd had this so long ago, it would have made this and this and this so much easier.
- 4:26So, yeah, it's definitely a part, definitely some forward progress.
- 4:30It is.
- 4:30Well, you mentioned when I had the Django chat guys on the show not too long ago, that was when we talked about Django.
- 4:38Surprise, surprise.
- 4:39But mostly, you know, we started out with the focus of Django turning 18.
- 4:44And, you know, maybe we can just start with Django itself before we dive into HTMX, because I think Django more than the other frameworks has its opinions and its styles and its way of doing things.
- 4:56And so integrating with HTMX, I think, is a pretty interesting thing that, you know, other people and you've done with, as we'll talk about through this course that I'm going to mention in a moment.
- 5:06But, yeah, just, you know, what are your thoughts on Django over the years and Django being, you know, old enough to vote now?
- 5:12Well, you know, there's this line I came across that I've since stolen.
- 5:16Somebody said, it's not a dinosaur, it's a shark.
- 5:19And I love that concept, right?
- 5:21It's still growing and it's still changing.
- 5:23It's still very actively developed.
- 5:25But, you know, I've been playing with Django 5 recently and there's nothing in there that broke any of my four code.
- 5:31And if you're going to be coding with the same library over and over again, that's actually desirable, right?
- 5:37You want the new features, but you don't want to have to go and redo something every single time.
- 5:41You know, I don't want to knock view.
- 5:43It's got some strengths.
- 5:44We'll talk a little bit about that as well when we start getting into some of the HTMX stuff.
- 5:48But by example, because I don't use it very frequently, it seems every time I go back, it's like, oh, they've changed how this happens and I have to rethink this, right?
- 5:55And I don't run into that with Django.
- 5:57It's become so large that it also addresses different problems for different people.
- 6:02I love that they're adding all this asynchronous stuff.
- 6:04I will probably never use it.
- 6:05And I think it's great that it's out there because it solves a problem for the folks who are trying to use that kind of thing and they don't have to go off to another library.
- 6:15But for those of us who don't use it, it's like, nope, that's fine.
- 6:18Whatever.
- 6:18It doesn't break anything.
- 6:19They don't have to start from scratch.
- 6:21And they keep chugging along.
- 6:23And, you know, both Will and Carlton sort of commented about the community and they're right.
- 6:29It's there.
- 6:30You know, Django, the project site that has all of the add-ons, there's like over 4,000 different apps and libraries that you can use.
- 6:37So it's not just the framework.
- 6:39It's not just the core.
- 6:40It's the ecosystem.
- 6:41And it makes a big difference.
- 6:43It makes your life a lot easier that way.
- 6:45A lot of choice.
- 6:45More than almost any other individual library in Python.
- 6:49Django's got a massive community behind it, doesn't it?
- 6:51Yeah.
- 6:52Yeah.
- 6:52Well, you know, there aren't other conferences, right?
- 6:55There's PyCon and there's DjangoCon.
- 6:56And everything else is go to a PyCon and talk about it in a room.
- 7:01And then there's an entire conference for it.
- 7:02And two of them, right?
- 7:04There's Europe and the US, right?
- 7:06So it's a big deal.
- 7:07It is.
- 7:08And it's been a while since I've been to a requests con, although I do like requests.
- 7:12Yeah.
- 7:12Yeah.
- 7:12Also, you mentioned the async stuff and some of these new things.
- 7:16Python and Django, it's just amazing how stable they are in the sense of two to three aside, right?
- 7:22Like it's very, very, very rare.
- 7:24Not never, but very rare that you have to go and fix code because a new version of Python came out.
- 7:29Yeah.
- 7:29It's the same thing, right?
- 7:30So 3.12 just launched, been playing with the RCs for a little bit, just, you know, swap out the virtual env and my code just keeps chucking along, right?
- 7:39Like there's no, I haven't run into a problem even with beta versions in years, right?
- 7:45So that, and that stability is, it's important, right?
- 7:49It's like you kind of said, we're still, still hurting a little bit from the scars from two to three.
- 7:54So I think everybody's being cautious in the right way.
- 7:57Yeah.
- 7:57I think that's probably true.
- 7:59Also some of those new features that maybe you won't use or I won't use or whatever.
- 8:04I think they're still really important to have because I don't really hear it so much anymore, actually, but there used to be a lot of talk of like, oh, we're switching to go because of async.
- 8:13We need async.
- 8:14So we're switching to go and because we need, you know, and even if you individually don't use it, having those features in Django or in Python, they're really important because they kind of knock that conversation.
- 8:29out, right?
- 8:29They're like, oh, we had all these people love Python, but they had this one special case.
- 8:33So they were forced to leave like, well, now they're not necessarily forced to leave because of that.
- 8:37And I think, you know, that's positive for everyone, even if many people don't use it directly.
- 8:41Yeah.
- 8:42Well, for sure.
- 8:42Right.
- 8:43Like if, you know, if async is what was stopping you from using Django and switching to FastAPI, then, you know, now you don't necessarily have to.
- 8:51Right.
- 8:51And again, I'm a fan of FastAPI.
- 8:54A lot of it comes down to what you're building.
- 8:56And, you know, I often get the question also, well, why would I pick Django versus this versus that?
- 9:01And I'm like, well, if you're, if you're building an actual project where you need web pages as well as the API, as well as this and as well as that.
- 9:09Well, you can do that with FastAPI plus SQLAlchemy plus something else, but, you know, it's all self-contained in Django.
- 9:14And so things tend to work better.
- 9:16So those edge cases that you talk about that might cause you to go off to another library, even if they don't do it quite as well, it might just having it enough so that you don't leave is makes the difference for sure.
- 9:28It does.
- 9:29It does.
- 9:29There's a whole show comparing Django to FastAPI right there, isn't there?
- 9:33Probably.
- 9:34And Django or Django Ninja.
- 9:36Probably several.
- 9:36Probably several.
- 9:37Yes.
- 9:38Probably, but not this show, not this one.
- 9:40All right.
- 9:40What's next?
- 9:41Let's talk about your book next.
- 9:42Sure.
- 9:43So one of the things you've been up to in addition to courses is writing books, Django in action.
- 9:49Almost released.
- 9:50Is that right?
- 9:51What's the status?
- 9:52So speak in singular.
- 9:54It's this is my first.
- 9:55So be kind.
- 9:56It's in early release.
- 9:58So you're able to purchase the first.
- 10:01I think we're up to seven chapters.
- 10:03And essentially, if you buy the electronic edition, then you get a chapter.
- 10:08About a chapter or two a month.
- 10:09It kind of comes out piecemeal.
- 10:11It's called Django in action.
- 10:12It's on Django.
- 10:13It's divided into three parts.
- 10:16So part one primarily is sort of that intro stuff.
- 10:20How to get projects going.
- 10:21What a view is routes and all that kind of good thing.
- 10:23Part two is around the tools that are built into Django.
- 10:27So how to use the admin, you know, slightly more advanced topics.
- 10:31And I'm currently in the process of writing part three.
- 10:34And the intent of part three is how to extend your use to the third party libraries.
- 10:40You know, you just mentioned Django Ninja.
- 10:42So I'm hoping to have a chapter both on Ninja and a chapter on HTMX.
- 10:46And both of these will guide people to, okay, great.
- 10:48So now you've got Django, but you want to build X.
- 10:51I need an API.
- 10:52Well, how do I do that?
- 10:53Well, there's Ninja and there's the DRF.
- 10:54I want to have a little more functionality, a little more dynamicism.
- 10:59Oh, okay.
- 10:59Well, a good answer to that now is HTMX.
- 11:02So this is how you wrap those things together.
- 11:04So the intent is to try and cover that.
- 11:06I'm sort of hoping that there's enough there in part three that folks who have done a bit of Django before are still interested in it and can pick some new things up as you go along.
- 11:17And, you know, with the release of Django 5, I've been busy going back in and, you know, adding, hey, and this is what they added in 5 and this is what changes.
- 11:27So trying to cover all of that.
- 11:29Full publication will probably be early next year.
- 11:33But if you're on the electronic copy, you can start now and go from there.
- 11:36So, yeah, it's available.
- 11:37And I guess while I'm on the topic, I've been getting a lot of great feedback and we've got a forum for folks who've read the book.
- 11:44And it makes a big difference to me because, you know, somebody says, oh, hey, I did this and it didn't quite work.
- 11:50And I'm like, oh, okay, it shouldn't.
- 11:51But let's add a couple sentences about that.
- 11:53So it's kind of great to have this dynamic process going and prove it as I go along, which is so.
- 11:59So those folks who have been participating in the early release, I very much appreciate that feedback.
- 12:05It's cool to be able to build it out in public, not just drop 300 pages of printed something.
- 12:10Well, you know, it's something I'm sure you, you know, you encounter with the courses all the time, right?
- 12:15Like I've got, we'll call it a particular sense of humor, shall we?
- 12:19And when, you know, I put something in a course that I find funny, it's not like you're, there's no audience, right?
- 12:26Like you have no idea whether or not the joke lands.
- 12:29It's sort of the same thing.
- 12:31Like you get, you never get any of that kind of feedback with a course and the book can very much be the same way.
- 12:36So, yeah, that early release in the forum helps a little bit.
- 12:40So, and so, yeah, nobody's, nobody's going to tell me whether or not I'm funny,
- 12:44but they can at least catch whether or not the code works, which is useful.
- 12:47Exactly.
- 12:48And speaking of projects you've been working on, one of the things that kind of inspired me to have you on the show
- 12:53to just dive into Django and HTMX is you actually did a talk Python course, HTMX and Django,
- 12:59modern Python web apps, hold the JavaScript, which is awesome.
- 13:02That's a really fun, just under two hours course that really shows people like how to integrate HTMX into Django.
- 13:09Yeah.
- 13:10And, you know, we should shout out the, there's a sister course on there that you started out with,
- 13:15with Flask.
- 13:16And essentially I took the same concepts and then stuck it in the Django world.
- 13:19So if you're coming from Django instead of Flask, then this, I guess it's a,
- 13:25it's sort of the equivalent of translating it into Spanish.
- 13:27So we cover a lot of the same kind of concepts in both courses.
- 13:32We start with this video collector project.
- 13:35So it's an actual project with, you know, clickable links and it allows you to sort of store videos that you see from YouTube
- 13:41and shows a little image of the thumbnail and, you know, the, the, who the author is.
- 13:47So a little database kind of application.
- 13:50We start there with a working project and then we HTMX-ify it.
- 13:54I'm just going to make words up.
- 13:55Yeah.
- 13:55It's a verb.
- 13:56We can do it.
- 13:56We're making verbs today.
- 13:57Come on.
- 13:57That's, it works.
- 13:58In both courses, we add like a click to edit.
- 14:01So you've got a little link, you click on the link and it turns into a form.
- 14:04Infinite scroll.
- 14:05The idea is you scroll down that content keeps getting added.
- 14:08Search as you type, which to me is just such a beautiful example, right?
- 14:12Like if you've already got search on the website, HTMX, it's like four lines of code.
- 14:17Like it's, it's startlingly easy to do it in a dynamic fashion.
- 14:22And it's complete with things like it updates the URL.
- 14:25So you can do deep linking and you can do a, you know, back button goes to the previous search.
- 14:30And all that basically comes by adding just a few attributes.
- 14:33So that's, that's beautiful.
- 14:35And then the Django version of the course also has a fourth example, which is how to take HTMX and integrate it in with bootstraps.
- 14:43You've got libraries on top of libraries and how sort of that JavaScript event system works in that situation and how you can get those things going.
- 14:50So, so yeah, so it's very example driven.
- 14:54Let's add features and learn how HTMX works along the way.
- 15:00This portion of talk Python to me is brought to you by IRL, an original podcast from Mozilla.
- 15:05When it comes to artificial intelligence, AI, what's good for trillion dollar companies isn't necessarily good for people.
- 15:11Can the risk and rewards be balanced?
- 15:13That's the theme of season seven of IRL, Mozilla's multi award-winning podcast hosted by Bridget Todd.
- 15:20Season seven is all about putting people over profit in AI.
- 15:24I think you'll find episode two pretty interesting.
- 15:26As you surely know, LLMs like ChatGPT are all the rage these days.
- 15:31Do they seem like magic?
- 15:33Well, it turns out that much of their power comes from millions of people entering and correcting data in these LLMs.
- 15:39Episode two, the humans in the machine gives us a glimpse into the world of these people behind the AIs.
- 15:45For policy junkies, IRL looks at the idea that we're all just guinea pigs in a big AI experiment,
- 15:51like the meal planning app that suggests bizarre recipes such as Oreo vegetable stir fries and flawed technologies that compose more deadly risks when it comes to something going wrong,
- 16:01like self-driving cars blocking emergency responders.
- 16:05You'll also hear from people building more responsible ways to test new AI technology.
- 16:10And we find out why it's taking so long to regulate this massive industry.
- 16:14That's IRL season seven from Mozilla.
- 16:16Check them out and listen to an episode at talkpython.fm/IRL.
- 16:21The link is in your podcast player show notes.
- 16:23Thank you to IRL and Mozilla for sponsoring the show.
- 16:29You know what surprised me about getting into HTMX was you add all this functionality to this existing app,
- 16:36kind of like you do in this course or I did in the Flask course,
- 16:39and the code actually gets cleaner and simpler at the end.
- 16:42Yeah.
- 16:42Yes.
- 16:43How can it do more and be more easy to understand and clean than the previous version, you know?
- 16:48Yeah.
- 16:49And, you know, we're going to spend a bunch of time talking about today, too,
- 16:52is there's some libraries out there that make it even better.
- 16:54And some of these I found after writing the course.
- 16:57So it's sort of like, oh, well, hey, if I do that and go back, use that and I will write even less code, which is fantastic.
- 17:03Yeah.
- 17:04All right.
- 17:04Let's, you know, not everyone has already had this love affair that you and I have had with HTMX.
- 17:10So maybe you could just give us like a really quick overview of what HTMX is.
- 17:16And I think maybe the way to do it would be just we could make a concrete example,
- 17:19like click to edit or something like that.
- 17:21We could just show like maybe you could just talk us through an example real quick.
- 17:25And that would suffice is what the idea is.
- 17:27Yeah.
- 17:27So it's HTMX is a JavaScript library and it's a JavaScript library that if you're lucky means you'll never have to write JavaScript.
- 17:35So you essentially drop it into your page.
- 17:37And how it works is you apply a set of attributes to your HTML.
- 17:44And those attributes affect how things work.
- 17:47All of the attributes are namespaced.
- 17:49They all start with HX.
- 17:50And the key ones are HX get and HX post, which you can see on the example there on the button.
- 17:57There's an HX get.
- 17:58And essentially what happens is for a button or a link, you're replacing something like an H ref with this concept.
- 18:05And when you click the button or the link that get fires an Ajax call to the server.
- 18:12And the intent is instead of getting a full new page like you normally would when you click a link, it will send back a little piece.
- 18:21And that little piece is then injected into the page.
- 18:24And you can use different attributes to control how.
- 18:27The example that's up on the screen here is using the HX swap attribute.
- 18:32And that essentially says replace the entire outer div with the content that comes back from the server.
- 18:38So the div with the first name, last name, email, which is just a read only, gets replaced with the form that comes down from the server.
- 18:48So now essentially we've got click to edit on something that was display and now is a form.
- 18:52That's the general idea.
- 18:55HTMX sets some headers so that on the server side you can tell whether or not it's actually a HTMX call or not.
- 19:03And in the Django world, this works really, really well.
- 19:06So a common pattern with a Django view when you're dealing with forms is say have a get and a post inside of the same view.
- 19:13So you do a get to get the page and then the post when you're actually submitting the form for that page and you keep it all together inside of the same view function.
- 19:22HTMX can do something very similar and essentially instead of checking for get or post, you check for this HTMX header.
- 19:29And then you can either send down the whole page or the little partial replacement.
- 19:33And like in this example, it's the form that goes with it.
- 19:35In addition to things like this form, you can attach it to say we kind of talked about searches you type.
- 19:42So you can attach these kinds of attributes to say an input tag.
- 19:46And then when maybe you want to bring one of those up, we've got it somewhere.
- 19:50Yeah, perfect.
- 19:51There's additional things that you can set that change how it behaves.
- 19:54So for example, you want to activate this event when someone has typed something in.
- 19:59So you're looking at the JavaScript key up event.
- 20:01You only want to do it if the input has changed, not just if the event fired.
- 20:06So if I'm using my arrow keys, it doesn't send something off to the server.
- 20:10And then you can put a delay in so that if we get a bunch of events at the same time, it's not until there's a pause where you send it to the server.
- 20:17So if somebody is rapidly typing, you don't send every single keystroke up.
- 20:20In the course I also cover, you can set up conditionals on it as well.
- 20:26So I've got to check in one version of one sample that checks whether or not there's actually content.
- 20:31So you have to have typed at least three letters, for example.
- 20:34Right.
- 20:34So you can put these kinds of checks in.
- 20:37And essentially, it really is very much like roundtrip 1.0 web, except what you're getting down is instead of a whole new page is you're getting a partial.
- 20:47And the HTMX library injects this in.
- 20:50It's a way of doing some very 2.0 stuff in a very 1.0 way, which means you have very little JavaScript to have to write.
- 20:58So personally, I have always found React to be overkill.
- 21:03And that's not I'm not trying to slag React people.
- 21:05Programming languages are a religious topic, right?
- 21:07This isn't me saying you shouldn't.
- 21:09This is me saying it doesn't work for me.
- 21:11I started coding for the web back in the Perl's CGI days.
- 21:15And when JavaScript was first introduced, one of the arguments for it was, oh, you don't need a tool chain, right?
- 21:20It just worked.
- 21:21And, you know, you just refresh the page and it goes.
- 21:24So the whole concept of like TypeScript and a transpiler and web packaging and all the other stuff that's attached to modern web dev, I find exhausting.
- 21:33And because of that, I've kind of always headed towards Vue because it's a little lighter weight.
- 21:38But even then, I'm often doing things against recommendation.
- 21:41I have been known to serve Vue JavaScript files through the Django template engine so that I can embed JSON objects.
- 21:49And that's a no-no, but it's worked for me.
- 21:52But I often find with Vue, even though it's lighter weight, because it's written for folks who primarily use Node, there's some assumptions in the documentation.
- 22:00There's always this bit of a challenge to sort of get it to work in Django.
- 22:04And what I've, you know, I've gone back and looked at everywhere I've coded with Vue.
- 22:09HTMX solves like 95% to 99% of the things that I would have done with Vue, that I've done with Vue in the past.
- 22:18And the only exception was I once wrote a drag and drop card game.
- 22:23And I don't know if I could do that in HTMX, but pretty much everything else I could replace with HTMX.
- 22:29And the amount of code I'd have to write would be a lot less.
- 22:33The amount of JavaScript I'd have to write, which I've always sort of felt is a necessary evil.
- 22:38It would almost just go away.
- 22:40And now it would be unnecessary and still evil.
- 22:43But yeah, that's me on my soapbox.
- 22:45So yeah, that's the short version of it.
- 22:48You've played with it a bit.
- 22:49Did I miss anything?
- 22:50Something you should highlight?
- 22:51No, I think that was a really great summary.
- 22:53The one thing I would like to throw out there that is not immediately apparent to people, but is a tremendous advantage of HTMX,
- 23:00is that typical web development puts you into different mindsets for different parts of the application and context.
- 23:09So I'm working on the database.
- 23:10So I've got to think about database access and security in that context.
- 23:14Now I'm working on server-side code.
- 23:15And what can I do on the server?
- 23:17What kind of code can I run on the server?
- 23:19What packages do I have available?
- 23:21Now I'm on the client side.
- 23:22I can't do direct stuff.
- 23:23I have to have an API to interact with the server.
- 23:25So how do I build the API?
- 23:27And there's all the restrictions of the code, regardless whether it's JavaScript or something else, in the front end.
- 23:31And so you have these context switches of like, where are you in the app?
- 23:35And with HTMX, it's like, everything's on the server.
- 23:38Everything's in one language.
- 23:39Everything's in Python and Django.
- 23:41And whatever you do, you do it in Python and Django in this context or whatever your web framework is.
- 23:46And the title of the course, Hold the JavaScript.
- 23:50When I did the original Flask course, I got a bunch of flack from somebody on the thing that artists formerly known as Twitter.
- 23:58Like, why are you hating on JavaScript?
- 24:00This is just not what the world needs.
- 24:02I'm like, I'm not hating on JavaScript.
- 24:04I'm hating on the idea of having to write in three or four languages to accomplish one thing.
- 24:08And evidence of this, like, evidence of this benefit is it's popular in the node community.
- 24:13Right?
- 24:14It's popular in the JavaScript community.
- 24:16And they don't hate JavaScript.
- 24:18But they, like us, would also like to not have two different styles of JavaScript, two different modes of JavaScript, two different contexts of code execution in just one place.
- 24:28And I think it's easy to see, like, oh, how does this do something similar but cleaner than React?
- 24:33It also transforms, like, where and how your code runs.
- 24:38And I think that's really a big deal.
- 24:39Yeah.
- 24:39Well, at risk of getting too far off topic, right?
- 24:42Like, the web is a giant house of cards.
- 24:45And it always has been.
- 24:47And our answer every single time has been, oh, let's add another layer of cards.
- 24:51And we've been adding layers of cards for, you know, 20 years.
- 24:55And it's shaky in places, right?
- 24:58So anything that takes a couple layers off the top and simplifies how you think about things, I think, is definitely a benefit.
- 25:04And like you said, HTMX is tool agnostic.
- 25:07It's still agnostic, right?
- 25:08So it's got nothing to do with Python.
- 25:10It works with whatever your stack is.
- 25:13And that in itself is something that is a thing of beauty.
- 25:15Because if you're a node person, knock yourself out, right?
- 25:19You want to PHP, whatever.
- 25:21Go for it.
- 25:22It doesn't matter.
- 25:23Because essentially, it's back to the fundamental idea that HTML is based on, which is that hypertext concept of I click on something, it goes and gets a new thing and it puts it in the page.
- 25:33And in this case, instead of it being the whole page, it's a partial.
- 25:37It's a powerful, powerful tool.
- 25:39Indeed.
- 25:39Just to kind of put a finalize this whole thought here.
- 25:43It's like, so Mark's out there in the audience asking.
- 25:44So most business cases, use cases can be solved with HTMX.
- 25:48My vote is, yeah.
- 25:49Especially if you put business use cases.
- 25:52If you're writing forms over data, like sort of enterprise-y apps, like 99%, right?
- 25:57All the crud stuff.
- 25:58Like I said, I think the only thing I would, the only place it doesn't really fit is when you're getting to the level of something like games.
- 26:05And in fact, so we'll talk about it briefly, but there's a white paper on HTMX.org that talks about one of the companies that actually did a conversion.
- 26:13And, you know, they had a fully running site and converted it over to HTMX and they had some big wins by doing that.
- 26:21Could come back to it in a minute.
- 26:23But I'm hard pressed to think of a business use case that wouldn't be improved by using HTMX instead.
- 26:28Yeah.
- 26:29And it's so simple that like a lot of times the use case was, well, just the web app.
- 26:33It's not dynamic.
- 26:34It doesn't do that.
- 26:35You can add a lot of cool dynamic features to an existing app rather than like, oh, we have this really complex React app
- 26:42that we're going to simplify.
- 26:43I imagine usually it's kind of like a lift up the feature set then bring down as well there.
- 26:47Some of this comes out of things like HTML5 as well, right?
- 26:50So having email type on your input field means that there's a certain amount of validation that's happening in the browser now, right?
- 26:57So things that we used to have to do in JavaScript, now the browser is taking care of some of that.
- 27:01So by taking some of those kinds of features, you know, I don't have to have that check manually written on the browser side anymore.
- 27:09And then adding HTMX to give that dynamicism, it just sort of tops it up, right?
- 27:14So it's like I said, it's a step forward.
- 27:16The one other area where HTMX seems like it's just not going to work is if you want to build offline progressive web apps.
- 27:22Because offline is like the antithesis of HTMX.
- 27:26Yeah.
- 27:26So what I thought we could do for our conversation here would be to focus on just a bunch of tools that people can bring to bear on making working mostly with Django,
- 27:38but also some of the other web frameworks like other Python web frameworks and HTMX together make that more seamless.
- 27:44And we worked together to put together quite a list here, didn't we?
- 27:48So there's two awesome lists we'll talk about.
- 27:51So the 95% of what we're talking about, we found sourced off of these two curated lists.
- 27:57The first one's called awesome HTMX.
- 27:59And the second one is called awesome Python HTMX.
- 28:04The first one is a collection of all languages.
- 28:07So it's got a section on Python.
- 28:09But, you know, as we were saying, if you're right there, HTMX from Rails.
- 28:13So if you're coming from somewhere else and you want to use HTMX, there's a whole lot of pieces in here.
- 28:20And it ranges from things like tools and articles and that kind of stuff.
- 28:25The second one is a Python specific one, which is awesome Python HTMX.
- 28:30They call themselves PyHat.
- 28:32A little bit overlap between the tool sets.
- 28:35But again, this is just sort of some master lists for the places you can go and get content and examples and all that kind of good stuff.
- 28:44And in fact, one of the first white papers that I wanted to talk about a little bit is on the awesome Python HTMX list.
- 28:52As I mentioned, there's a link to it on this list.
- 28:55And as I mentioned, this is actually a case study off of HTMX.org.
- 29:00It was presented at a Django con in 2022.
- 29:04So if you don't feel like reading, you can go off and see the video.
- 29:07But it's about a conversion.
- 29:09And it took them about two months to completely convert from a React based site to HTMX.
- 29:16They had zero reduction in functionality.
- 29:1967% reduction in the size of the code base.
- 29:23So that's significant, right?
- 29:24Like that's two thirds of your code you no longer have to maintain because it's gone.
- 29:28Increase the Python code by 140%, right?
- 29:32So that's sort of the tradeoff, right?
- 29:33We were moving some of that JavaScript stuff onto the server side.
- 29:37They reduced their total JavaScript dependencies by 96%, which essentially means it's almost gone.
- 29:43As a result of that, they had shorter web build times, shorter page loads, less memory.
- 29:49And because of the less memory, they were also able to handle larger data sets.
- 29:54So they were able to put more content on the page at a time using HTMX than React
- 29:59because the amount of overhead React put on the page meant it was eating up space and it just couldn't handle it.
- 30:06It's one white paper.
- 30:07And of course, it's on the HTMX.org site.
- 30:09So of course, it's going to be biased towards you should use HTMX.
- 30:12It does show that this is being used in the real world and with a lot of success.
- 30:16It's definitely something that you kind of want to look at.
- 30:19I can't remember if I sent you the link, but there's another one recently that showed up on ByteCode.
- 30:25This is a site that frequently linked to in the PyCoder newsletter.
- 30:30A lot of really good content with the ByteCode guy there.
- 30:33He's done something called...
- 30:36Nope, that's not it.
- 30:36It's three in real life use cases for Python and HTMX.
- 30:43It's Byte with an I.
- 30:44That's why.
- 30:44It's the other kind of Byte.
- 30:46He's a little cranky.
- 30:47This is more of an anecdote than case study.
- 30:50But essentially, he's gone through three kinds of projects that he's recently done with Python that are web-based and went,
- 30:58oh, let's use HTMX instead.
- 31:00And it's made his life easier.
- 31:02And so essentially, he's just saying, hey, here are some cases where I've actually done this and it's been useful.
- 31:06To the question we got earlier, really, it is that sort of, hey, I've got a form and I want the form to be slightly more dynamic.
- 31:12So, hey, it would be an awful lot easier if I just stick this in and go.
- 31:15And if I remember correctly, he's using FastAPI with this as well, right?
- 31:19So it's not just Django or Flask.
- 31:21There's lots of choices out there.
- 31:23Yeah, it works really well.
- 31:24Yeah, he's using FastAPI.
- 31:26FastAPI works equally well with it.
- 31:28And some of the tools we'll talk about are relevant as well.
- 31:31So very cool.
- 31:35This portion of Talk Python is brought to you by Sentry.
- 31:38You've heard me sing the praises of Sentry for their error tracking and performance monitoring plenty of times on the show.
- 31:44But this time is different.
- 31:46They have a special live event, like a mini online conference, where you can connect with the team and take a deep dive into different products and services every day for a week.
- 31:55Join them for launch week, new product releases, exclusive demos, and discussions with experts from their community on the latest with Sentry.
- 32:03You'll see how Sentry's latest product updates can make your work life easier.
- 32:07New announcements will be released every day on YouTube at exactly 9 a.m. Pacific time.
- 32:13On Monday, November 13th, performance.
- 32:16Tuesday, user feedback and session replay.
- 32:18Wednesday, data resiliency and platform.
- 32:21Thursday, frameworks, community, and integrations.
- 32:24And finally, Friday, November 17th, open source sustainability.
- 32:28I'm already signed up.
- 32:29Join me at talkpython.fm/sentry.fm.
- 32:33Dash launch dash week.
- 32:34The link is in your podcast player show notes.
- 32:36I'll see you there.
- 32:37Thank you to Sentry for their continued support of Talk Python To Me.
- 32:44Some other things on the list I'm just seeing on the screen right now is like there's an IntelliJ,
- 32:47aka PyCharm, plug-in for HTMX.
- 32:51So it gives you autocomplete for all your HTMX stuff and probably for VS Code as well.
- 32:56So if you're going to use one of those editors and do HTMX, you know, be sure to install that
- 33:00so you get autocomplete and validation for the different things.
- 33:04So cool.
- 33:04All right.
- 33:05Well, let's talk about maybe the big first one, Django dash HTMX.
- 33:10Just brought under extensions for using Django with HTMX.
- 33:14So this is the go-to.
- 33:15This is not only is this the right way to do things, but this is also, in fact, talk about
- 33:20this in the Django version of the course.
- 33:23There's also about a half dozen libraries out there that all said, oh, we're just going to
- 33:27do what he did, but we're going to do it for our library.
- 33:30So this has been an inspiration to other folks as well.
- 33:32It's by a gentleman named Adam Johnson, and it's essentially some Django middleware and some
- 33:38utility classes.
- 33:39So in Django, each view gets a request object.
- 33:43This library adds a member to that request object called HTMX.
- 33:47You can use that member as a Boolean.
- 33:51And if it evaluates to true, then it was there's an HTMX header.
- 33:55So it was an HTMX call.
- 33:56And if it's false, it wasn't.
- 33:58So this is what I was referring to earlier about that idea of being able to have one view
- 34:03manage multiple things, because essentially you just check this Boolean and either I'm
- 34:07feeding out the full page or I'm feeding out the partial.
- 34:10Right.
- 34:11Because the big concept in HTMX is there'll be a request for the page, which shows you the
- 34:15whole thing.
- 34:16But an interaction won't want all the page content.
- 34:18They just want the fragment that the new version of the fragment, like the new table
- 34:22row that has been edited or something like that.
- 34:25Right.
- 34:25That's right.
- 34:25Yeah.
- 34:25On the server, you got to decide what version do I give them?
- 34:29This answers that question.
- 34:30Exactly.
- 34:31And this is so, you know, to take it back to the click to edit example that we talked about
- 34:35at the top.
- 34:36You know, the first call to the page, no HTMX.
- 34:39It shows the content of the page, including the person's name and email address or whatever
- 34:43it is.
- 34:44And then when somebody clicks the link and that needs to be replaced by the form, you can use
- 34:48the same view.
- 34:49And that view then just returns the form as a partial rather than the whole thing.
- 34:53And so checking this Boolean allows you to tell which mode am I in and how do I want to
- 34:57do that?
- 34:58There's other information on as well.
- 35:00So you can check things like the target and the triggers.
- 35:03So these are other attributes.
- 35:04Didn't mention the triggers when I sort of explained things at the top.
- 35:07The trigger is that event management piece that I was talking about with like searches
- 35:11you type.
- 35:12So the trigger says a key press or there's another one that allows you to do on scroll down.
- 35:18Right.
- 35:18So when the tag is revealed, then trigger the call.
- 35:22So the Django HTMX will give you information about what the event was, what triggered, how
- 35:27it worked, et cetera.
- 35:28So if you need to make decisions or if you've got multiple HTMX things happening inside the
- 35:33same page, you can suss it out that way.
- 35:35Quite frankly, the Boolean mechanism is enough for me to want to install this library.
- 35:40The rest of it I don't use very often, but just being able to check which mode I'm in and
- 35:47not have to check that header myself is worth installing the library.
- 35:51And it feels very, very Django-esque because it essentially just replaces that get post pattern
- 35:56that I was talking about earlier, but instead with HTMX.
- 36:00There's also some other utilities in here.
- 36:02The way that HTMX works, if a URL is hit, that's a 404, it just eats it.
- 36:09There's a chunk of debug JavaScript that comes with this library that you can put in so that
- 36:13you actually get the Django 404 page.
- 36:15So it essentially overrides the response back from the view and spits out an actual debug
- 36:22page that you're used to.
- 36:23So it allows you to debug your HTMX a little better.
- 36:26And then there's some classes and functions for things like polling and controlling redirects
- 36:32and a few other things that are also useful for typing as well.
- 36:35Right.
- 36:36So if you're doing a type check that you want to replace, that this call comes back
- 36:40with HTMX, then you can put it inside of your typing that this is this kind of class that
- 36:45comes back.
- 36:46So very, very handy library.
- 36:48Very strongly recommended that if you're doing Django and HTMX, this is the place to go.
- 36:53Okay.
- 36:53This is the big one.
- 36:55All right.
- 36:55Next up.
- 36:56This is from the multilingual awesome list.
- 36:59Django JS lib HTMX.
- 37:02So this one's pretty lightweight.
- 37:04It pretty much allows you to use tags to drop the library in.
- 37:08So it essentially just means you're scriptifying some of the things that you're using in the
- 37:13library.
- 37:13I haven't used it myself.
- 37:15I think a lot of it depends on how deep your pages have to go.
- 37:19Personally, I would just stick this in and hard code it myself.
- 37:22It's there if you want it.
- 37:23Yep.
- 37:23Sounds good.
- 37:24HTMX Flask.
- 37:25This one, I think, is really interesting.
- 37:27And you might be saying, like, well, why do I?
- 37:29It's Django we're talking about.
- 37:30Why do I care about Flask?
- 37:31But we already went through two of the examples on the HTMX.org site.
- 37:35And what you see is the client side.
- 37:37But there's no visibility to what the server side looks like in that example code.
- 37:41But this, this will help maybe.
- 37:43There are three things called HTMX Flask, Flask HTMX, and Flask HTMX with a capital.
- 37:49So they all start to blur together.
- 37:53There.
- 37:53Yes.
- 37:54This one is essentially if you at the top, if you're not listening, if you were watching,
- 37:59Michael showed the HTMX site and a couple of the examples.
- 38:03This is a Flask S.
- 38:06They've redone all of those examples from the server in Flask.
- 38:09So if you're looking for how to implement these things rather than just in pure HTML and you want to see them in the Flask server, you can go that here.
- 38:17And because it ties back to the actual HTMX.org page, it allows you to sort of see how all the pieces fit and look your code up as you go.
- 38:27Yeah, I think the examples are really awesome and simple on HTMX.org, but they don't, they don't tell the whole story if you're trying to actually implement it.
- 38:34And if you're like, I just don't really know how to do that with my web framework.
- 38:38Here's at least a Python set of examples that'll get you pretty close.
- 38:41Yeah.
- 38:41There's a few of these as well.
- 38:43Near the tail end here, we'll talk about a couple others that there's one which is actually a board game, which is a nice top to bottom full example.
- 38:51But this one essentially relates the example content off the HTMX site to your Flask server.
- 38:56Nice.
- 38:56All right.
- 38:56Server sent events.
- 38:58If you're using Starlette, this is a tech demo essentially by Jan Vlasinski.
- 39:04And essentially he's using FastAPI, HTMX and Starlette.
- 39:09And pages, the example is actually quite simple.
- 39:12There's almost no content on it.
- 39:14It's just a little bit of, it almost looks like debug.
- 39:17So there's a few variables which are randomly generated inside of an asynchronous call on the server.
- 39:22You run it using Yuvacorn.
- 39:24And essentially when you visit the page, the values update on the fly.
- 39:28So the sleep in the server code is pretty short.
- 39:31It's about a third of a second.
- 39:33So the debug messages go flying by rather quickly.
- 39:36I bet they do.
- 39:36But it gives you an idea essentially how to wire these kinds of things together.
- 39:40I've never used Starlette myself.
- 39:42Have you played with it at all?
- 39:44Only by way of using FastAPI and LightStar.
- 39:49Okay.
- 39:49So they're both built on top of it, but not directly.
- 39:52But, you know, server sent events are interesting because we've had traditional web interaction, which we kind of talked about already.
- 40:01And then you've got web sockets for super dynamic bidirectional communication.
- 40:06But a lot of times people use web sockets because they just want to receive stuff that happens on the server on the client, like a dashboard, right?
- 40:14The dashboard doesn't need to update the server and broadcast.
- 40:16It just wants all its pieces update live.
- 40:19And so servers and events are like a lightweight web socket type thing.
- 40:22And it's really cool to be able to plug that in with HTMX, I think.
- 40:25Yeah.
- 40:26I haven't played with it myself, but HTMX also has a polling mechanism.
- 40:29So if you're not using technology on the server side that can enable this, you can get around it the other way.
- 40:35Obviously, an event from the server is far less noisy than polling.
- 40:39But by using, it basically gives you a couple different options.
- 40:42So if you happen to be using something that isn't asynchronous and you don't want to go all the way down that path, you could also look at the polling techniques inside of HTMX to get you to solve similar kinds of problems.
- 40:53Yeah.
- 40:53If you're not building a massively busy website, maybe it's totally fine to just pull it, right?
- 40:57Yeah.
- 40:58Well, some of it also has to do with the frequency, right?
- 41:00Like if it's data that only needs to update, you know, once every 30 seconds or so, then while somebody's on a page, that might be the right way to go.
- 41:07Yep.
- 41:07All right.
- 41:08Now, we'll start pulling some items off the Pi Hat.
- 41:12Sure.
- 41:13Pi Hat example on here.
- 41:14Yeah.
- 41:14Another list here.
- 41:15We started talking a little bit about this.
- 41:17I've been hopping around a bit.
- 41:18So that white paper that I talked about is on here and a bunch of some of the other tools that we've got as well.
- 41:24Good overall list has some sort of a nice breakdown that it's got sort of the resources as in like, here are some articles you can read.
- 41:32They've got a link to your Flask course.
- 41:34So I think we need to do a PR here.
- 41:36And we definitely got to do a PR.
- 41:38And I appreciate the Jenga one in there.
- 41:40We do.
- 41:41I appreciate however this got here.
- 41:43Thank you, people.
- 41:44Yep.
- 41:45But yeah, we got to do a PR.
- 41:46So some good links in here to some obviously to some quality content.
- 41:50That's right.
- 41:52That's right.
- 41:53Yeah.
- 41:54So there's a bunch of different ones in here.
- 41:56I think probably the helper libraries is the section that has like really what people are imagining when they're thinking awesome lists, at least awesome lists for programming.
- 42:05So make a like.
- 42:07Yeah.
- 42:08In fact, so there's three on here that I think I'm going to want to sort of cover back to back because they're all essentially the same idea.
- 42:14So I mentioned that Django HTMX is like is sort of the go to way of doing the Django stuff.
- 42:20And it inspired a bunch of other people.
- 42:22Well, there's ASGI HTMX, Starlette HTMX and HTMX Flask.
- 42:26And all three of these.
- 42:28Sorry, I'm flapping around as we go along.
- 42:31You're trying to keep up.
- 42:32So all three of these are essentially like the Django HTMX library, but for those appropriate other libraries.
- 42:39So they essentially add that meta information to the requests or in the case of ASGI to the scope.
- 42:46And it's the same kind of thing.
- 42:48It gives you an idea of what the header is there and what the other information is.
- 42:51So if you're if you're not on Django, if you're on Flask, then HTMX Flask or ASGI HTMX will work and essentially solve the same kind of problems that Django HTMX does.
- 43:01And all three of those are listed on that pie hat piece.
- 43:04So that's ASGI HTMX is by Flormon Monka.
- 43:08Starlette HTMX is by Felix Ingram.
- 43:11And HTMX Flask is by Sergey Ponsfrixis.
- 43:16Not sure how to say his last name there.
- 43:18Yeah.
- 43:18A lot of good work going on in this space.
- 43:20All right.
- 43:21How about Django HTMX patterns?
- 43:23Ah, yes.
- 43:24Good old spooky Lukey.
- 43:25So he's got an awesome GitHub handle here.
- 43:28This is Luke Plant, who is or was, I'm not sure the time frame on that, but was heavily involved with Django.
- 43:35He was a core developer for a while.
- 43:36And essentially, he's written up a article here on good ways of interacting with Django and HTMX.
- 43:43There's some code examples to go with it.
- 43:46We're going to talk in a minute about the block, render block library.
- 43:49He's a fan of that approach.
- 43:51Essentially, this has got good reasons for how you group your snippets together.
- 43:55That idea of having your code and all your code in the same view, as well as keeping your snippets, possibly even the same file.
- 44:02He talks about these kinds of things, as well as sort of hands you off to some of the toolkits that we're talking about as well to help you do this.
- 44:10I'm a fan.
- 44:11He's also got a great article called Django views the right way, which is,
- 44:15He tends to write fairly opinionated pieces.
- 44:17And of course, when you agree with his opinion, that's fantastic.
- 44:21He must be brilliant.
- 44:21So he's very...
- 44:25Finally, someone's standing up for our way of thinking.
- 44:26Exactly.
- 44:27So he's very pro-function-based views.
- 44:29And for folks who aren't Django people, that's a Coke versus Pepsi thing versus class-based views.
- 44:34But because I tend to...
- 44:36My bias heads in that direction.
- 44:37He must be right.
- 44:38So everything else he's written is brilliant as well.
- 44:40So I've got a lot of good information in here to dig around some best practices, as well, as I said, to some references to the kinds of libraries we're talking about to make your life easier.
- 44:50Yeah.
- 44:50I think these kinds of things, like these pattern ideas, really help you think about, how am I going to add that into my application?
- 44:57Right.
- 44:57Instead of just, here's some libraries, and here's HTMX, and here's...
- 45:01How does it look right?
- 45:04How do you make your Django code better and cleaner by putting this stuff together?
- 45:08Yeah.
- 45:08Well, and honestly, like my tongue planted firmly in my cheek with the whole function versus class thing, notwithstanding.
- 45:14One of the things I love about these kinds of opinion pieces is it allows you to help sort of formulate, oh, does that fit in my version or does it not fit in my version?
- 45:23And how does this map to what I'm doing?
- 45:26It allows you to sort of think more deeply about the patterns that you're using as you're coding.
- 45:30You might leave an article like that and go, oh, now I understand more about why I chose what I did, and I'm going to stick with it.
- 45:36It might be the opposite of what that article is there, but it opens you up to other ways of thinking about these things, right?
- 45:42It comes from that practice of doing it rather than, like you said, just a couple quick examples.
- 45:47Another thing about patterns that I've always loved, design patterns, programming style, is when you think about a problem, if you just think about kind of the idea of the pattern, like what are the steps?
- 45:59It's hard to think about it.
- 46:00You're really down in the details.
- 46:02But if you can think about as the whole thing that the pattern applies to and what of its benefits and trade-offs, you can think at a much higher level.
- 46:10And sort of it lets you not think in detail, right?
- 46:14Like you could say, well, I want to have, you know, outside the web, you can say, I want to have a way where we just have one variable, just copy of it.
- 46:20It's just one and everybody shares it instead of passing it around like, okay.
- 46:25Or you could say, we have a singleton.
- 46:26Like, boom.
- 46:27Like that one statement, just what are the benefits?
- 46:31What are the drawbacks?
- 46:31What are the use cases?
- 46:32Like it's testing hard.
- 46:33It makes reuse really easy because you just grab it.
- 46:36And it's sort of like that for HTMX, right?
- 46:39As you think like, oh, this pattern applies right here.
- 46:41Let's go.
- 46:42Or it doesn't because of.
- 46:44And it becomes some of it's a vocabulary thing as well, right?
- 46:46So there's this concept we're going to spend a little bit of time talking about in a minute that is the partials, which is those little snippets that I was talking about.
- 46:53And in fact, if I remember correctly, the term partials has been stolen.
- 46:57It's something Rails uses, right?
- 46:59By using that same vocabulary, it allows you to sort of talk about these things, like you said, at that higher level concept, right?
- 47:05So that the patterns help with the vocabulary, help with you communicate about those concepts rather than spending the first 10 minutes of your conversation trying to establish whether or not you're talking about the same thing.
- 47:14Yes, exactly.
- 47:15Exactly.
- 47:16Speaking of, we have fragments and we have partials.
- 47:18And this is a little bit of an alternate, like this is a Coke versus Pepsi as well.
- 47:23Considering you're the maintainer of a sister library to this, maybe this one should be you.
- 47:29Maybe you want to talk a little bit about Jinja partials.
- 47:31Well, so my understanding here with the Jinja partials one is you can express parts of your page to contain the template data that you want.
- 47:43So for example, here's a whole thing, you know, with a HTML and a head and a body.
- 47:46And then it has one thing that says, this is a block and it says just a paragraph.
- 47:49This is the magic number with that value.
- 47:51With this thing, what you can do is you can say, just grab that thing we called content and provide the data over to it.
- 47:59Because I don't want to show the whole page.
- 48:00I want to show just this section of it.
- 48:03And there's an essay over on HTML.org, something about the locality of behavior and things like that.
- 48:11I believe it's something titled like that.
- 48:14And the idea is I have my entire HTML listed all in one as if it was a static page.
- 48:20And I can use the server side to grab pieces out of the HTML and make them dynamic sections, right?
- 48:26Like I described, like grab this block, render it using the template engine, the Jinja2 fragments.
- 48:33Okay.
- 48:34So that's the, I don't know, Coke?
- 48:36Coke or Pepsi?
- 48:37Who gets, which one does it get?
- 48:39Let's call this one the Coke style.
- 48:41And that, I think, is really useful, right?
- 48:43Because what is your alternative?
- 48:44Have that HTML in two places?
- 48:46That's not a great option, right?
- 48:48You don't want to have, it's just like you wouldn't duplicate code.
- 48:50You don't want to duplicate the design and the HTML and stuff that makes up the part of the page
- 48:55and have inline for the main one and then a second copy just so you can make it dynamic.
- 49:00So I'm not against this.
- 49:01I think this is really a neat idea here to be able to grab that section and render that.
- 49:07Pepsi side.
- 49:08Michael's library called Jinja Partials, which says, instead of putting it all into one big place,
- 49:14let's treat it more like function-based programming.
- 49:17So what you can do is you can say, I'm going to define an HTML partial or piece that just has
- 49:26that section that you're interested in, right?
- 49:28So maybe you might have like one part that displays a particular video in our example from the course,
- 49:35right?
- 49:36And that thing has to be past certain bits of information, like what the video is,
- 49:41so you can get the ID and so on.
- 49:43And then when you use it, you kind of, instead of putting the HTML inline,
- 49:46you call it like a function in the HTML.
- 49:49So you would say like div, here's the thing that contains it, you know, render partial,
- 49:53here's the thing.
- 49:54And you call it.
- 49:55So it's kind of two sides of the same coin, but like very much Coke, Pepsi is a good analogy
- 50:00here.
- 50:00Like this one says, I would like to maybe be able to reuse that across different pages,
- 50:05just like you would a function in Python.
- 50:08I might want to use it in more than one place.
- 50:10And so it makes sense to have it in its own separate thing, right?
- 50:14Like in this example, you might want to show a video, but you might want to do it on like a list
- 50:18of videos.
- 50:18And you might want to do it on a profile page where you have favorites, right?
- 50:22And those wouldn't, the fragment style, you would have to duplicate that HTML.
- 50:26So this one allows you to have like a nice, simple way to do that, right?
- 50:32And then on the server side, it makes it simpler because I don't know if there's any examples
- 50:36here.
- 50:36I'm sure there is.
- 50:37But on the server side, all you do is you just say the template response is the partial
- 50:42and you just provide the data and you don't have to do like, if it's the full thing or
- 50:46if it's a partial thing, I'm going to do different stuff on the server.
- 50:49Right.
- 50:50So that's the Jinja partials.
- 50:53I don't have a Django partials, but you know, this, this applies for Flask, FastAPI and others.
- 50:59So on the Django template library, the include tag is either slightly more powerful than the
- 51:08Jinja one or just better documented.
- 51:10A lot of the reasons you wrote this library, I can do with the existing Django include tag.
- 51:15So there's like a tiny little use case where it doesn't overlap, but like everything,
- 51:19everything in the course, for example, I was able to do quite simply with just using the
- 51:23include tag.
- 51:23There is a library out there that is the, what did you say it was the Coke version?
- 51:28And that is the Django render block.
- 51:30This is by Patrick cloak.
- 51:33And essentially he's using very, something very similar to the Jinja2 fragments.
- 51:37And it essentially allows you to pull out a template block from a page.
- 51:43This has actually spooky lookies preferred approach.
- 51:46It's in his patterns.
- 51:47I think in this case, I'm a Pepsi guy.
- 51:50I'm with you.
- 51:50I would rather have the piece inside its own file.
- 51:53But I think the reason behind having it in the blocks is essentially you can turn conditionals
- 51:59on, have that one page, the whole thing gets rendered.
- 52:02And then the snippets are always using the same library and you can just flag, hey, I want
- 52:07this partial piece coming out of it.
- 52:09Essentially, these are very, very similar concepts.
- 52:12Just the Django versus the Django templating, because of course, Django is in Django as well.
- 52:18So the Django native templating versus the Django piece.
- 52:21And you can take either approach depending on what works for you.
- 52:24Yep.
- 52:24And just to wrap this up before I get a lot of email, a lot of email, there's a whole section
- 52:29that says, why not just use include or macro from Django?
- 52:32And there's a great long discussion here with lots of examples.
- 52:36The short version is with macro, if you want to render the template alone, you have to
- 52:42have a third page that just calls the macro, that just embeds the macro and then calls it
- 52:49in an empty page for the template.
- 52:50Why not use include?
- 52:52Well, with include, you have to have the thing, the variable names in the partial match the consumer.
- 53:00So it's as if you had to say, I want to function, I want to call a function, but your local variable
- 53:05names have to match exactly the parameter names in every use case that you can't say like
- 53:11user where there was a username.
- 53:13Nope.
- 53:13You can't call this function that you're supposed to call your variable local variable user.
- 53:16You know, it's, it's, so there's a look at that discussion and then, then email me if
- 53:21you have to.
- 53:21And that's, that's why the Django one doesn't have this problem is because the include tag
- 53:26in Django has a with keyword, which allows you to essentially say, I want this, this bit
- 53:31of context passed in as that value.
- 53:33So you can essentially, the equivalent there would be with video equals V email equals user
- 53:39dot email to accomplish the same thing, which is why I could get away with not needing the
- 53:43extra library.
- 53:44Nice.
- 53:44There is somebody down here who said, I'm not sure what their name is, but I think they
- 53:49found an internal, like a deeply internal function from within Jinja.
- 53:56I think I'm not sure if it's in Jinja or Flask, but you're able effectively to do the
- 54:00same thing, but it's like, I don't know.
- 54:02I'm not a fan of like grabbing some random internal thing and going, I think we'll probably
- 54:06just use that.
- 54:06You know, I also just, there's a chameleon's partial.
- 54:09If you're a chameleon template person that does the same thing, it's a Jinja partial as
- 54:13well.
- 54:14Yeah.
- 54:14When you first showed this to me, I looked it up in the Jinja docs because I don't use
- 54:17Jinja as often.
- 54:18And it kind of hints that this is there, but it was really, really unclear.
- 54:23It just sort of gave one example and didn't explain it.
- 54:26And so, yeah, it doesn't surprise me that there's something that can be done, but maybe
- 54:32it's, maybe it's publicly available.
- 54:34And if they got documented a little better, it might solve the problem and you could, you
- 54:38know, close up that chunk of code you had to maintain.
- 54:40Yeah.
- 54:41I mean, I'm not necessarily in love with it.
- 54:43I just, I created it because I want to have really nice clean code.
- 54:45Right.
- 54:46Yeah, exactly.
- 54:46Of the problem.
- 54:47You know, pull up the Django render block.
- 54:49You already really talked about that one.
- 54:51That's on the similar side as to the Jinja fragments.
- 54:54Partial.
- 54:55To fragments.
- 54:56Sorry.
- 54:56That's what we talked about.
- 54:57That's right.
- 54:58Where the, all the stuff is together.
- 55:00And I think that's a good idea if you're never, ever going to use that block of that
- 55:05bit of HTML and dynamic bit anywhere else.
- 55:07But as soon as you are, then it's like, well, it's starting to get, I don't know.
- 55:11That's my philosophy.
- 55:12Yeah.
- 55:12One of the reasons I use the include template, even for, I use it even just to shrink this
- 55:17shrink the size of the HTML file.
- 55:18Like I don't even care if it's not reused.
- 55:20Sometimes I just find it easier to read because HTML is so verbose.
- 55:24I sometimes just find it easier to read something that goes, oh, include row from this row file.
- 55:30I'm like, oh, okay.
- 55:30I don't have to think about how many divs are in the row.
- 55:32I understand what's in that row.
- 55:34And it allows me to read it easier.
- 55:35So obviously the cost of that is a performance of compiling the template, but I'll take that
- 55:40for code readability every day of the week.
- 55:42Just throw more CPU at it.
- 55:43Yeah.
- 55:44I'm a hundred percent with you.
- 55:45And yeah, you don't have to worry about whether the 17th div is matching or not.
- 55:50Yeah.
- 55:50All that stuff.
- 55:51Yeah.
- 55:51Yeah.
- 55:51Yeah.
- 55:52You talked about Flask HTML, the ASGI one.
- 55:55HX requests.
- 55:57What's this one about?
- 55:58This is similar, right?
- 56:00It solves the same problem, but in a different fashion.
- 56:03So this is by a gentleman named Yakov Lundsen.
- 56:06And essentially he's using a lot of the class-based view philosophy, not that he's actually using
- 56:12class-based views, but essentially you are building out your requests, HX request handling
- 56:19as classes.
- 56:20And he's gone to the level of defining custom tags that you use instead of the attribute, and
- 56:27it will automatically wire that together.
- 56:29So you don't even have to declare your URLs.
- 56:32And this is, and again, you know, back to that Coke versus Pepsi thing.
- 56:35This is that sort of that, the definition between function-based and class-based, the contrast
- 56:40between function and class-based.
- 56:41Class has always made me a little uncomfortable because they're a little bit magic.
- 56:45It often means writing less code, but if for someone who isn't used to it, they have to
- 56:50go digging to understand what's happening.
- 56:52He's definitely taken a bit of a magic approach here.
- 56:55There's less code, but you can, you've got this magical wiring of change email as you've
- 57:02got up on the screen, getting wired to the actual HX change email underneath and being
- 57:06able to find all that code.
- 57:07So if you're a class-based view person, this will probably feel very, very natural and a
- 57:12good approach to take.
- 57:13If you're not already intimately familiar with class-based views, this wouldn't be somewhere
- 57:18where I would send you.
- 57:19I'm going to add more overhead to figure out how to put all that together than just to use
- 57:24the other libraries.
- 57:24Yeah.
- 57:24That's right.
- 57:25Well, we have a lot of things we've been going through, but I think we're getting close
- 57:28to the end of the list here.
- 57:29Django dashboards.
- 57:31Yeah.
- 57:31Actually, why don't we just skip down to a Wella Club and maybe we can cap it out right
- 57:37there?
- 57:37Because I thought it's probably a really good example.
- 57:39Multilingual version, the original.
- 57:41So this is, if you want to go one place to see everything working together, this is the
- 57:48place to go.
- 57:48So this is also by Adam Johnson.
- 57:50So he's the same guy who does Django HTMX.
- 57:53A Wella is a Nambian game that is kind of similar to another game called Hus.
- 57:58I will admit I couldn't fully wrap my head around the rules of the game.
- 58:03It's essentially like a little board with holes in it and there's stones in the board.
- 58:08There's a good picture up there.
- 58:09Good.
- 58:10And the rules are you on your turn, you have to move some of your stones.
- 58:14And if you move them correctly, you're done.
- 58:16Your turn is done.
- 58:17If you don't move them correctly, you can take other people's stones, whatever.
- 58:20So it's one of those very simple games with a lot of strategy.
- 58:24And like I said, I didn't quite fully wrap my head around the game, but that's not what
- 58:28I was here for.
- 58:28He's written a HTMX based version on the web here.
- 58:34You've got a fully automated computer opponents.
- 58:37You can play against the computer.
- 58:38It has the layout of the board with the number of rows and stones.
- 58:43And then there's a number for how many stones are in each hole.
- 58:46And if you click one of the links, the one of the numbers,
- 58:50it automatically moves the stones according to the rules.
- 58:53And all of this, of course, as you can imagine, is updated through HTMX.
- 58:57It even has the base version of this ships with the Django debug toolbar on so you can watch
- 59:04what is going on.
- 59:06And then in the background, because it will host multiple games at a time, we were talking
- 59:10a bit about that polling concept.
- 59:12It's using the HTMX's polling mechanism.
- 59:15And in the top right hand corner, there's a little report of how many games are in progress
- 59:19at the moment.
- 59:20So I really, really liked this example.
- 59:22So there's other examples that are good on the list, but most of them are toys.
- 59:25They either show off something like an event coming from the server and, you know, no cleanliness.
- 59:30One of the other examples I played with was like a little book database.
- 59:33And, you know, it worked.
- 59:34But if you typed in a number in a place that was a name in a place where it was supposed to
- 59:38have number, it would crash.
- 59:40This is actually a working application.
- 59:41And so you've got a nice solid example to how everything works, all the different attributes
- 59:47and go.
- 59:47So if you're comfortable enough with, if you're new to Django, then maybe this isn't where you
- 59:52start.
- 59:52But if you're comfortable with Django and you really just want to see a one place where
- 59:56all the HX parts are working together and it's a fun way to learn something, this would
- 1:00:01definitely be the example I would use.
- 1:00:02Excellent.
- 1:00:03Yeah, it looks like quite a cool way to build things with HTMX.
- 1:00:08I'm not even sure I would have thought HTMX totally applies to this level of kind of
- 1:00:12interactivity because it says you can do player versus player games with the polling trigger
- 1:00:17and stuff like that.
- 1:00:18It's a pretty deep example.
- 1:00:19But of course, you kind of expect it out of the HTMX, Django HTMX guy, right?
- 1:00:23So and actually, so as an aside, there's also a relatively decent demo inside of that library
- 1:00:29as well.
- 1:00:30So if you grab, if you git clone Django HTMX, there's an examples directory that also has not
- 1:00:36quite as comprehensive this, but that's another good place to start as well if you're looking
- 1:00:40for sample code.
- 1:00:40So plenty of good stuff out there with good real world purpose.
- 1:00:46Cool.
- 1:00:46Well, quite the look inside of HTMX, Christopher.
- 1:00:50Thank you.
- 1:00:50Yeah, it's been fun.
- 1:00:52Yeah.
- 1:00:52So many things these days to make it work with Django.
- 1:00:55When I started out, like many of these libraries didn't exist.
- 1:00:58I would have probably just grabbed one and used it had I had it at the time, you know?
- 1:01:02It definitely makes a big difference.
- 1:01:04And the HTMX community is, I accidentally found out very, very vibrant.
- 1:01:09Somebody found the future table of contents of my book and posted on, as you said, the artist
- 1:01:16formerly known as Twitter.
- 1:01:17Hey, there's an HTMX chapter coming in this Django book.
- 1:01:20And my feed just exploded for the next three days of everybody going, yay, HTMX.
- 1:01:25So there's a very strong and excited community out there that's playing with this tool.
- 1:01:30And as I said, it makes a real big difference in your ability to build stuff quickly and effectively.
- 1:01:35Sure does.
- 1:01:36And speaking to the long-term sustainability, right at the top of the HTMX.org page, it says,
- 1:01:41we're excited to announce that HTMX has been accepted into the first class of the GitHub
- 1:01:46open source accelerator.
- 1:01:47So more momentum for HTMX.
- 1:01:50Excellent.
- 1:01:50All right.
- 1:01:51Final call to action.
- 1:01:51People want to get started with this stuff.
- 1:01:53They want to dive into it a little bit more.
- 1:01:55What do you tell them?
- 1:01:55Start with the page you've got up right now, for sure.
- 1:01:58HTMX.org is a good place to go.
- 1:02:01And, you know, at risk of sounding like an infomercial, we have a couple of really high
- 1:02:05quality courses that you could also check out.
- 1:02:07Yeah.
- 1:02:08It would definitely be places to go with things.
- 1:02:11And of course, you know, we'll link in the show notes to all those lists.
- 1:02:15But there's a lot of content there if you just kind of want to go digging and play around.
- 1:02:18Yeah.
- 1:02:19We only just touched on a little tiny bit of it.
- 1:02:21So plenty more to go into.
- 1:02:23Lots of interviews.
- 1:02:24Lots of examples.
- 1:02:25Lots of blog posts and articles that you can go check out.
- 1:02:28And your book.
- 1:02:29Your book will be out in the spring.
- 1:02:30So people can check that out as well.
- 1:02:31Appreciate it.
- 1:02:32Yes.
- 1:02:32Yeah.
- 1:02:33Thank you so much for being on the show.
- 1:02:34Always nice to catch up with you.
- 1:02:36Glad to be here.
- 1:02:36Yeah.
- 1:02:37See you.
- 1:02:37Bye.
- 1:02:37This has been another episode of Talk Python To Me.
- 1:02:41Thank you to our sponsors.
- 1:02:43Be sure to check out what they're offering.
- 1:02:45It really helps support the show.
- 1:02:46When it comes to artificial intelligence, AI, what's good for trillion dollar companies isn't
- 1:02:53necessarily good for people.
- 1:02:55That's the theme of season seven of IRL, Mozilla's multi-award winning podcast hosted by Bridget
- 1:03:00Todd.
- 1:03:01Season seven is all about putting people over profit in AI.
- 1:03:05Check them out and listen to an episode at talkpython.fm/IRL.
- 1:03:09Century.
- 1:03:10They have a special live event like a mini online conference where you can connect with the team
- 1:03:15and take a deep dive into different products and services every day for a week.
- 1:03:20Join them for launch week.
- 1:03:21New product releases, exclusive demos and discussions with experts from their community on the latest
- 1:03:27with Century.
- 1:03:28You'll see how Century's latest product updates can make your work life easier.
- 1:03:32Visit talkpython.fm/century dash launch dash week to register for free.
- 1:03:39Want to level up your Python?
- 1:03:40We have one of the largest catalogs of Python video courses over at talkpython.
- 1:03:44Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:03:49And best of all, there's not a subscription in sight.
- 1:03:52Check it out for yourself at training.talkpython.fm.
- 1:03:55Be sure to subscribe to the show.
- 1:03:57Open your favorite podcast app and search for Python.
- 1:04:00We should be right at the top.
- 1:04:01You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:04:06and the direct RSS feed at /rss on talkpython.fm.
- 1:04:11We're live streaming most of our recordings these days.
- 1:04:13If you want to be part of the show and have your comments featured on the air,
- 1:04:17be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:04:21This is your host, Michael Kennedy.
- 1:04:23Thanks so much for listening.
- 1:04:25I really appreciate it.
- 1:04:26Now get out there and write some Python code.
- 1:04:28I'll see you next time.