https://codecapsules.io/docs/tutorials/build-flask-htmx-app/ [ ] [ ] Skip to content logo Code Capsules Building a Full Stack Flask HTMx Application ( ) ( ) [ ] Initializing search * Getting Started * Deployment * Reference * Tutorials * FAQ * Comparisons * Community * Video Guides logo Code Capsules * Getting Started * [ ] Deployment Deployment + Angular + Bootstrap + Django + Django + MySQL + Django + MongoDB + Express + Express + Mongo + Flask + Flask + HTMx + Flask + MongoDB + Flask + MySQL + Go + HTML + HTML5up + Java (Spring) + Java (Spring) + MySQL + MEAN (Mongo, Express, Angular) + MERN (Mongo, Express, React) + Next + Next + Mongo + Next + Express + Next + Express + Mongo + NodeJS Discord Bot + NodeJS Telegram Bot + Python Discord Bot + Python Telegram Bot + Polling NodeJS Telegram Bot + Polling Python Telegram Bot + React + Slack + Svelte + Vue + WhatsApp * [ ] Reference Reference + Capsule Billing + Capsule Management + Custom Domains + File Data Capsule + MongoDB Data Capsule + MySQL Data Capsule + Procfile + Python Development + Team Management * [*] Tutorials Tutorials + Slackbot with Node.js + MERN Job Board + Web File Store + Node.js Telegram Bot + Python Telegram Bot + Flask API + [ ] Flask HTMx Flask HTMx Table of contents o Overview and Requirements o Setting up the Project # Create Project Folder # Creating a Virtual Environment # Installing Dependencies # Initialize an Empty Git Repository # Linking to GitHub o Building the HTMx Frontend o Building the Flask Backend # Declare and Initialize app Module # Register App Models # Create Views o Running our App + Custom Domains + Persistent Sleep Tracker Part 1 + Persistent Sleep Tracker Part 2 + Front-end Portfolio + Stripe Checkout and Email Subscription with Flask + Game Catalogue API with Node.js and MySQL * [ ] FAQ FAQ + Teams, Spaces and Capsules + What is a Capsule? + What is a Space? + What is a Team? + How Do I Add a Custom Domain? * [ ] Comparisons Comparisons + PaaS Providers + Telegram Hosting Providers + SaaS vs PaaS vs IaaS * [ ] Community Community + Introducing Code Capsules + Hack Days and Free Tech Consulting + Founder Fridays * [ ] Video Guides Video Guides Table of contents * Overview and Requirements * Setting up the Project + Create Project Folder + Creating a Virtual Environment + Installing Dependencies + Initialize an Empty Git Repository + Linking to GitHub * Building the HTMx Frontend * Building the Flask Backend + Declare and Initialize app Module + Register App Models + Create Views * Running our App Building a Full Stack Application with Flask and HTMx cover image [This tutorial explains how to build an application with Flask and HTMx. If you want to host an HTMx and Flask application on our PaaS, you can find a short deployment guide here that uses the same project.] Recent trends in the modern web saw single page frameworks like React.js and Angular take over traditional multipage websites, mainly due to the lack of interactivity offered by HTML. It is worth noting however, that single page applications (SPAs) brought this interactivity at the cost of added complexity. This is where a new HTML extension called HTMx enters and shines. HTMx gives traditional HTML sites more interactivity while keeping things simple, as it allows you to make requests from any HTML element and not just and
tags. But this is not HTMx's only benefit. Other benefits include: * Making it possible to perform partial page reloads in HTML * Support for PUT and DELETE methods in addition to GET and POST * Not being limited to click and submit event triggers only * Lightweight set up - no additional dependencies need to be installed to get it working In this tutorial, we'll explore the benefits of HTMx by building a full stack application using Flask and HTMx. Our application will be a book recommendation app that supports CRUD functionality. The final app will look a bit like this: Flask HTMx Application Overview and Requirements After building our application, you'll want to deploy it to production so you can show it to friends and family. You will therefore need the following to complete the tutorial: * Git set up and installed, and a registered GitHub account * Python3 installed * An IDE or text editor of your choice Setting up the Project With all the requirements in place, we can go ahead and set up our project. Let's start by creating the project folder and then we can set up a virtual environment within the project folder. Create Project Folder Next, let's create a folder to house our application's source code. Run the commands below in the terminal to create the folder and navigate into it. mkdir flask-htmx cd flask-htmx From here onwards, the flask-htmx directory will be referred to as the project's root folder. Creating a Virtual Environment A virtual environment allows you to isolate the packages required to develop Python applications from your computer system. We recommend you use a new virtual environment for every application you develop so as to not corrupt dependencies for other applications. In the terminal run the following command to create a virtual environment within the project's root folder: python3 -m venv env To activate the virtual environment, enter either of the following: MacOS source env/bin/activate Windows .\env\Scripts\activate After activating the virtual environment, the name env should appear in brackets on the leftmost part of your terminal in your current line. This signals that the activation was successful. Virtual environmeent active Installing Dependencies We can now install our required packages to the virtual environment we activated in the previous step. Run the command below: pip3 install flask flask-sqlalchemy gunicorn You might notice there's no dependency for HTMx in our dependency list. This is because it will be added as a script tag inside the head of our HTML templates. Initialize an Empty Git Repository While in the project's root folder, enter the command, git init to initialize a git repository. This will allow you to track changes to your app as you build it. Create a .gitignore file and within the file add the line below: env/ This excludes the env folder from being tracked by git as we only want to track changes in our project files. Linking to GitHub Head over to GitHub and create a new repository. Then in your project's root folder run the command below from the terminal replacing the username and repository_name with your own values from GitHub. git remote add origin git@github.com:username/repository_name.git This will link your local repository to the one on GitHub. Building the HTMx Frontend With the setup complete, we can now begin building our app. We will start with the HTMx frontend, and for this you need to create an app/ templates folder inside the project's root folder. Next, create an index.html file inside the templates folder, and populate it with the code below: Flask HTMX ALPINE App There's not much going on in the code snippet above, except for line 5 and 8, which are responsible for loading Bootstrap and HTMx into our index.html page. This gives you the power to build an interactive page just by including the