Learn Python with Talk Python's 270 hours of courses

HTMX - Clean, Dynamic HTML Pages

Episode #321, published Sat, Jun 19, 2021, recorded Tue, May 25, 2021

Have you wanted to add more interactivity and liveness to your web application? If you built it using Flask, Django, or some other Python web framework, that thought probably didn't fill you with joy. Because it might mean that you need to change a bunch of code and rewrite a significant bit of your app using a full-on front-end framework like Vue.js or React.js.

In this episode, we meet Carson from Big Sky Software. He's the creator of HTMX. This front-end JavaScript library let's you leverage the server side aspects of your Python web app and add amazing interactivity but keep the logic on the server, near the database, and implemented in Python.

You're going to love it.

Watch this episode on YouTube
Play on YouTube
Watch the live stream version

Episode Deep Dive

Guest Introduction and Background

Carson Gross, the creator of HTMX and founder of Big Sky Software, has been developing web applications since the early days of CGI scripts in Perl. He spent much of his career in Java and Ruby on Rails, later transitioning to the concept of “hypermedia-driven” development. His passion for server-side logic and streamlined front-end design led him to build Intercooler.js, which evolved into the now-popular HTMX library.

What to Know If You're New to Python

If you’re just getting started with Python and are curious about building interactive web apps with minimal JavaScript, here are a few pointers:

  • Understand that Python web frameworks (e.g., Flask, Django) handle most server-side tasks.
  • Recognize that HTMX can be added incrementally to existing Python web apps to enhance interactivity.
  • Realize you don’t have to rewrite your entire app in JavaScript—HTMX lets you keep your Python-based workflows intact.

Key Points and Takeaways

  1. HTMX as a Bridge Between Server and Client HTMX extends HTML with custom attributes, letting you make AJAX calls and partial page updates without a full JavaScript front-end. By annotating elements (like a button or form) with HTMX attributes, the page can dynamically refresh specific sections. This keeps logic on the server side—written in Python—while offering rich client-side experiences.

  2. Incremental Adoption and “Complexity Budget” Carson and Michael discuss the idea that developers have a “complexity budget.” Rather than switching entirely to a JavaScript-heavy framework (e.g., React, Vue), you can sprinkle HTMX where it’s needed. This incremental approach helps keep maintenance simple and allows teams to continue leveraging their server-side (Python) expertise.

  3. Locality of Behavior vs. Separation of Concerns Traditional web apps often place JavaScript in separate files, creating a disconnect between HTML structure and the logic behind interactions. HTMX embraces “locality of behavior,” meaning the code describing what a button or form does lives right within that element’s markup. This reduces “spooky action at a distance” and makes your code more self-documenting.

    • Tools and Links:
  4. Interactive Patterns Without Heavy JavaScript The episode highlights several patterns made simple by HTMX, such as “click to edit,” “infinite scroll,” and “live search.” These are commonly associated with big front-end frameworks, but HTMX proves they can be done with minimal JavaScript if you return HTML fragments from the server.

  5. Reduced Rework for Existing Apps If you have an established Python web app in Flask, Django, or similar, you can adopt HTMX without major rewrites. By returning small HTML fragments via server-side templates (e.g., Jinja, Django templates), you instantly upgrade the user experience with partial page updates rather than full refreshes.

  6. Easier Maintenance and Debugging HTMX includes features like a debug mode to see what requests are being made, making it simpler to diagnose issues. Combined with the “locality of behavior” pattern, front-end debugging becomes more straightforward than juggling multiple JS files and frameworks.

  7. Revisiting REST and Hypermedia (HADEOUS) Carson reminds us that Roy Fielding’s REST dissertation was originally about HTML as hypermedia. Rather than building complicated JSON-based APIs for everything, HTMX resurrects this “hypermedia-driven” mindset. You can still build JSON APIs when needed, but HTML as a first-class data format simplifies many web interactions.

  8. Use Cases: Real Python and Beyond RealPython.com’s quiz section is mentioned as a real-world example: They added dynamic interactivity (e.g., step-by-step quizzes) powered by Intercooler.js (HTMX’s predecessor). Any site that wants to keep a Python backend while offering quick partial updates can follow a similar approach.

  9. HTML Fragments and Partial Updates Whether it’s an edit form or infinite scrolling, the idea is to return small chunks of HTML, not entire pages. This not only speeds up perceived performance but also preserves the mental model of server-side rendering. Instead of building an entirely separate JSON pipeline, you return the exact markup needed to refresh part of the page.

  10. Minimal JavaScript for Reactive-Like UIs People often choose full front-end frameworks to achieve reactivity and partial updates. HTMX covers a large portion of these “small to medium interactivity” needs. While truly advanced features might still require React or Vue, the majority of typical CRUD and form-driven apps can thrive with HTMX.

  • Tools and Links:
    • Sculpt (mentioned for Python in the browser, though not an HTMX requirement)

Interesting Quotes and Stories

“You don't have to use JavaScript for everything—HTMX lets you keep your Python-based workflows intact.” — Carson

“Your application has a complexity budget. Spend it effectively.” — Carson

“HTMX is all about taking HTML and pushing it forward as a hypermedia.” — Carson

Key Definitions and Terms

  • Locality of Behavior: Keeping the behavior (e.g., AJAX calls) in the same place as the HTML element that triggers it, rather than burying logic in separate JS files.
  • Complexity Budget: The concept that every project has a limited capacity for complexity before it becomes unmanageable—HTMX helps maintain it by incrementally adding only necessary interactivity.
  • HADEOUS (Hypermedia as the Engine of Application State): The original REST concept that the client transitions between states by following links in hypermedia (like HTML), not just JSON endpoints.
  • Partial Page Updates: Techniques that reload only sections of a page with new HTML fragments instead of re-rendering the entire page or using a full front-end framework.

Learning Resources

Here are a few relevant courses if you want to dive deeper into Python web frameworks and HTMX-driven development:

Overall Takeaway

HTMX simplifies the path to highly interactive web apps without requiring a massive shift to front-end JavaScript frameworks. By extending HTML rather than replacing it, you maintain a clean separation of concerns, keep Python-based logic where it belongs (on the server), and only add as much client-side complexity as needed. Carson’s work on HTMX demonstrates that small, focused updates with HTML fragments can deliver a powerful user experience while preserving the simplicity and maintainability of Python.

Links from the show

HTMX: htmx.org
HTMX on Twitter: @htmx_org

Talk Python's HTMX + Flask course: talkpython.fm/htmx
Talk Python's HTMX + Django course: talkpython.fm/htmx-django

Essays: htmx.org/talk

Examples: htmx.org/examples
Active Search: htmx.org/examples/active-search
Click to edit: htmx.org/examples/click-to-edit
Infinite scroll: htmx.org/examples/infinite-scroll

YouTube Live Stream: youtube.com
Episode transcripts: talkpython.fm

--- Stay in touch with us ---
Subscribe to Talk Python on YouTube: youtube.com
Talk Python on Bluesky: @talkpython.fm at bsky.app
Talk Python on Mastodon: talkpython
Michael on Bluesky: @mkennedy.codes at bsky.app
Michael on Mastodon: mkennedy

Talk Python's Mastodon Michael Kennedy's Mastodon