Dynamically Update the HTML Code in Flask Using Htmx Library.

Bilal K.
2 min readMay 29, 2022

If you’ve been working with Python and Flask, you might know that to update the data on an HTML page, we need to refresh the page every time. That’s where JQuery and AJAX come to help. AJAX lets us dynamically update the data on the HTML page without the need to refresh it every time. The second-best alternative to AJAX is web sockets and it also works fine. But there’s one more way to update the data on the HTML page, Htmx is an easy to use JavaScript library for performing AJAX requests, triggering CSS transitions, and invoking Web Socket and server-sent events directly from HTML elements.

In this blog, we will be using the OpenCV module to display the live stream, Flask to manage the back end, and htmx to dynamically update the FPS on the webpage.

Approach:

  • Capture the video feed from the browser page using the webcam.
  • Display the video feedback to the browser.
  • Calculate the FPS, and dynamically update the FPS after every 0.1 seconds on the HTML page.

Don’t forget to import the libraries.

Make sure you have Python, OpenCV, and Flask already installed. I prefer Anaconda distribution but you are good to go without Anaconda if you have mentioned libraries…

--

--

Bilal K.

Software Engineer, I share tips and tools to help fellow developers level up.