datamade / how-to

📚 Doing all sorts of things, the DataMade way
MIT License
80 stars 12 forks source link

Consider adopting HTMX #290

Open smcalilly opened 1 year ago

smcalilly commented 1 year ago

Background

HTMX is a framework for building interactive webpages. It uses HTML and a backend to serve up new data/content on specific parts of a webpage based on user interactions. Rather than reloading the entire webpage, it updates the html that you target.

It looks simple and I'm curious to try it, as it could be a good alternative when React might be too heavy for a project — like searching/filtering a table or interactive forms. I don't think it would fully replace JavaScript or React, but it seems promising for some of our common use cases. I like the approach to use HTML whenever possible and only use JS when absolutely necessary.

It grew out of intercooler.js. It's similar to the new Rails hotwire pattern, but less magic (aka more explicit/following the zen of python).

There's a django-htmx package for integrating within Django.

See:

Proposal

I want to see how well it would work and whether it's as good as advertised/folks talking it up. What's good and bad? What is promising? What are the limitations? What parts of our apps could this replace (table filtering, interactive forms, maps, etc)? Does it work well with JavaScript if a feature needs JS (like for a map)? Does it create maintainable code? Does it make sense? Do we like it?

These questions will be viewed with skepticism so we don't hurry to adopt this week's hottest new JavaScript framework.

Deliverables

I'll use HTMX to re-implement the search/filter table in the IL NWSS app. I'll do this outside of the project in investment time, that way we don't buy-in to a new framework for a project.

I'll implement it with the django-htmx plugin, and depending on how that goes, might also try to use the htmx library itself.

Timeline

This will take 1-2 investment days. I need to finish #272 first and then can work on this.

If anybody else has the bandwidth to try this before I do, please do!

smcalilly commented 1 year ago

leaving this here to refer to later: https://htmx.org/essays/a-real-world-react-to-htmx-port/

smcalilly commented 1 year ago

might be worth a read: https://hypermedia.systems/book/contents/

smcalilly commented 1 year ago

more htmx content, django specific: https://fly.io/blog/a-no-js-solution-for-dynamic-search-in-django/

smcalilly commented 10 months ago

another django link. this is a good example for how htmx naturally fits django patterns https://www.photondesigner.com/articles/submit-async-django-form-with-htmx

and here's a cool use case, real time notifications: https://www.youtube.com/watch?v=MziqE_2Euss

and a typeahead database search example, would be cool to do this with elasticsearch typeahead: https://www.photondesigner.com/articles/database-search-django-htmx

xmedr commented 9 months ago

I've been reading and messing with some htmx for a couple investment days and followed a django-htmx task list tutorial and pushed up my finished product here. Here are some initial thoughts

TL;DR

HTMX applies functionality through attributes on html elements to determine what actions trigger which reactions and what pieces of the html get swapped out, added to, removed, etc.

It’s definitely nice if you need quick ajax functionality. I do think because of it's nature and it's ease of use, it can be used in some projects to handle small forms or to get some functionality up and running quickly. However, it might be inflexible when working for different frontends, so I'm hesitant to suggest it for large components.

Pros

Cons

Maybes

Questions

smcalilly commented 7 months ago

Some more HTML propaganda: https://html-first.com/