DEV Community

Yiftach Beer
Yiftach Beer

Posted on

Dynamic websites with Flask

I use Python for both work and hobby projects. Some of the projects I create require external access for which I use Flask, for example:

  • a minimalistic UI that allows someone to go over some items and give feedback over them (I use Flask to display each in its own page)
  • allowing me to call some function on my computer (e.g. play a sound) from my phone (I wrap my function in a Flask route)
  • etc.

Flask seems great for when each of these pages is static. However, if some need arises and I need to make them dynamic (e.g. checking some checkbox makes some component visible), I'm not sure what today's best practice is. Flask does give me the option to use javascript as part of the templates it displays, but using plain javascript seems outdated - the proper way to build reactive pages is using React/Angular etc. But when these are combined with Flask, tutorials I've seen suddenly assume you only use Flask as an API (just returning JSON data) and not as a proper server, which seems like a small addition requiring a large overhaul.

Which setups work for you for making Flask pages display dynamic pages?

Top comments (2)

Collapse
 
ryanlwh profile image
Ryan Lee • Edited

For simple reactive pages, you can look at a JavaScript library called HTMX. It will allow you to keep using the templates of Flask while providing server-client reactivity. Once you get the hang of it, you can optimize your server by sending only partial templates to the HTMX requests.

I never find vanilla JS outdated for simple pages. Some front-end-only interactions can also be done with CSS only. I believe using an entire framework for a few simple pages is overkill.

There are also new projects like ReactPy that try to add reactivity to Python, but I have yet to try them out. ReactPy supports a Flask backend.

Collapse
 
yiftachbeer profile image
Yiftach Beer

HTMX/plain javascript sound perfect for my needs. Thanks!