Are you interested in the intersection of Python and Hypermedia-Driven Applications? Head on over to the discussions tab, introduce yourself, and let's get to work!
What is PyHAT? 🧐
PyHAT is more than just a snake with a hat 🐍🤠. It stands for Python htmx ASGI Tailwind—a web stack that allows you to build powerful web applications using nothing more than... drumroll... Python, htmx, and Tailwind.
Quick, take me to the tools already!
Our Goal
We want to promote hypermedia driven applications. That's it. That's the goal.
Okay, well, more specifically, we want to promote htmx within the Python ecosystem.
Why Should I Care?
Does any of this sound like you:
- I want to stick with just Python and HTML/CSS, but not sacrifice front-end functionality.
- I don't want to have to use a complicated front end framework.
- I don't enjoy agonizing over CSS class names.
- I want to maintain a consistent design without any bikeshedding.
If the above sounds like you then you are in the right place!
Maybe This Isn't For Me
One of the links above goes to When Should You Use Hypermedia over at htmx.org, and is a pretty great read if you want to asses if this is for you. It also expounds on the following points:
Hypermedia might not be a good fit:
- …If your UI has many, dynamic interdependencies
- i.e., Google Maps, Google Sheets
- ...If you require offline functionality
- …If your UI state is updated extremely frequently
- i.e., Online game
- …If your team is not on board
But Will it Work in Production?
Yes! Here is a very good example of a project a company underwent using HTMX with Django in production. You can also watch the original video from DjangoCon EU 2022, titled From React to htmx on a real-world SaaS product: we did it, and it's awesome!
Some highlights from the article.
- The effort took about 2 months (with a 21K LOC code base, mostly JavaScript)
- No reduction in the application’s user experience (UX)
- They reduced the code base size by 67% (21,500 LOC to 7200 LOC)
- They increased python code by 140% (500 LOC to 1200 LOC), a good thing if you prefer python to JS
- They reduced their total JS dependencies by 96% (255 to 9)
- They reduced their web build time by 88% (40 seconds to 5)
- First load time-to-interactive was reduced by 50-60% (from 2 to 6 seconds to 1 to 2 seconds)
- Much larger data sets were possible when using htmx, because react simply couldn’t handle the data
- Web application memory usage was reduced by 46% (75MB to 45MB)
Glossary 📜
- Asynchronous Server Gateway Interface (ASGI) - A standard that allows an application to talk to a server, allowing for multiple, asynchronous events per application.
- component - A reusable custom element. Within JavaScript, it is a self-contained element with its own properties/methods that are reusable. In this context, the term is more broadly applied to any reusable elements (which may include hypermedia or other design elements).
- dependency - Any application (library or package) that are required to run your application.
- fragments - Refers to partial content of a an HTML template. See also: template fragments
- hypermedia - Medium of information including graphics, audio, video, text, and hyperlinks, typically represented on the web as HTML
- Hypermedia Driven Application (HDA) - Uses declarative, HTML embedded syntax to achieve front-end interactivity, while interacting with the server in terms of hypermedia (HTML) instead of a non-hypermedia format (JSON).
- partials - A loose term, sometimes referring to "partial" content that can be displayed in a template, or partial content to be generated from within a template block.
- Server Side Rendering (SSR) - Generating static HTML markup on the server before it is rendered in the browser on the front-end.
- Single Page Application (SPA) - A web app implementation that loads a single web document, and subsequently updates content through JavaScript APIs.
- template fragments - a relatively rare SSR template library feature that allow you to render a fragment or partial bit of the content within a template, rather than the entire template.
Usage ✏️
Htmx can be used with any backend framework. Currently, there is a lot of experimentation in the Python space, which is exciting! But that also means that there are a lot of disparate approaches.
The best advice here is to get familiar with some of the core packages (htmx, tailwind). Then feel free to check out any of the packages below.
Official Resources 📚
- htmx - htmx gives you access to AJAX, CSS Transitions, WebSockets and Server Sent Events directly in HTML, using attributes, so you can build modern user interfaces with the simplicity and power of hypertext. Htmx has no outside dependencies outside of a vanilla JavaScript file referenced in your HTML
<head>section. - tailwindcss - Rapidly build modern websites without ever leaving your HTML. Tailwind provides a standalone CLI tool that does not require npm or any other JavaScript dependencies. (You can install it through
pipusing the pytailwindcss library)
Introductory Resources 🔰
- How to create a Django form (using HTMX) in 90 seconds 🐎
- A simple, short guide to start using HTMX with Django very quickly 🐎
- Add instant database search with Django and HTMX 🕵️
- Build an instant database search using Django and HTMX in 6 steps 🕵️
- Add infinite scroll with Django and HTMX in 60 seconds ∞
- Shows how to add infinite scroll to your Django app using HTMX in a simple guide ∞
- simple site
- Provides thorough documentation on building a site from the ground up with FastAPI, Jinja, htmx, and Tailwind.
- Rapid Prototyping with Flask, htmx, and Tailwind CSS
- In this tutorial, you'll learn how to set up Flask with htmx and Tailwind CSS. (testdriven.io)
- Django, HTMX and Alpine.js: Modern websites, JavaScript optional
- Building a modern front end in Django without reaching for a full-blown JavaScript framework. Choosing the right tools for the job, and bringing them into your project.
Introductory Courses 🏫
Design, Theory, and Patterns 🧠
- Django + htmx patterns
A compilation of patterns for writing Django projects that use htmx, with complete example code. - htmx Essays
A collection of essays by Carson Gross, the creator of htmx. Some specific essays of note for those not familiar with his teachings:- Hypermedia-Driven Applications - This web stack could have been called PyHDA, this essay gives a great primer on how a PyHAT application should look, architecturally.
- Locality of Behaviour (LoB) - A concept you will see referred to a lot around here. "The behaviour of a unit of code should be as obvious as possible by looking only at that unit of code"
- Splitting Your Data & Application APIs: Going Further - A great essay (responding to a great article).
If you split your API into Data and Application APIs...you should consider changing your Application API from JSON to Hypermedia (HTML) & using a hypermedia-oriented library like htmx to reap the benefits of the hypermedia model (simplicity, reliability, flexibility, etc.)
- Why We Should Stop Using JavaScript According to Douglas Crockford (Inventor of JSON)
A short video of Douglas Crockford explaining why we should stop using JavaScript. - 3 IRL use cases for Python and HTMX - From the author: There is nothing HTMX does that you couldn't do in another way. But HTMX pairs wonderfully with traditional server side frameworks and gives you clean, correct, results quite fast. You won't get candy crush bling level with it, but you will get something practical, which is regularly all what I need.
- HTML First - HTML First is a set of principles that aims to make building web software easier, faster, more inclusive, and more maintainable by... 1) Leveraging the default capabilities of modern web browsers, 2) Leveraging the extreme simplicity of HTML's attribute syntax, 3) Leveraging the web's ViewSource affordance.
- You don't need JavaScript for that - It's one of the core principles of web development and it means that you should Choose the least powerful language suitable for a given purpose... On the web this means preferring HTML over CSS, and then CSS over JS.
- Django HTMX Components - Live demos and code repository of common Django + HTMX patterns. They are designed to be copy-pasted into your project and customized to your needs.
Third Party Packages 📦
Demos
Templates
Helper Libraries
Frameworks
- Litestar
Litestar is a full-on ASGI web framework (think FastAPI, Sanic, Starlette, etc...) So why is it included here? With their most recent 2.0 release, the creators have included htmx support out of the box. A specialHTMXRequestprovides easier access to HX-request header objects, and anHTMXTemplateobject that includes attributes for common htmx actions (pushing url, re_swap, re_targets, etc...) - Forge Packages
Forge is a set of Django packages that work well together, but can also be used independently. These include some htmx/tailwind specific packages highlighted below. Note that these are opinionated approaches, but they provide a robust set of features to enhance your developer experience.- forge-htmx - The forge-htmx Django package adds a couple of unique features for working with HTMX. One is template fragments and the other is view actions.
- forge-tailwind
- Use Tailwind CSS with Django without requiring JavaScript or npm.
- django_htmx_ui
A django app that combines and helps leverage the full-stack django framework, the frontend htmx framework, the django-htmx library, and the jinja template engine. It provides extended django Views with htmx build-in functionality, CRUD Views for django models, extra Mixins to use with your Views to make life easier, a ready-to-use jinja environment, Middlewares for automations, and extra utils and decorators for common use cases. - Ludic
Ludic is a lightweight ASGI web framework that allows the building of dynamic HTML pages using pure Python. It is based on Starlette and offers seamless HTMX integration, a component-based approach, and intuitive f-string templating for a smooth and powerful development experience.