For me, deployment is one of the most exciting parts of the development process. Knowing code is live and serving a real-world purpose. Unfortunately, while developing, the gulf between by local development branch and a live production environment feels oceanic. That’s why planning for deployment from the start can help accelerate your development! So today, I’ll explore a deployment-ready Flask app that you can use to keep your branch just one commit away from production.
The problem and the goal
Deployment is a big topic. When planning to release your applications, you often have to consider security, scalability, reliability, and a strategy to continuously redeploy your app when you need to make updates. Among other things. Depending on the architecture of your app (clients, servers, APIs, containers), the tools available to help involve lots of configuration. Docker, Kubernetes, Jenkins, and cloud-based DevOps platforms have to be flexible enough to accommodate the needs of any application. But, let’s say you just want to make a blog or webpage simple enough to run on a single computer. One where you can tolerate a less-than-100% uptime. Then, you shouldn’t be forced to deal with containers, VMs, or DevOps. And, luckily, open-source communities build tools to automate or pre-configure pipelines that keep your development or use-case one click away from production. Today, I present a repository that show-offs this ability.
The application and tech stack
The code I wrote for this post is available at https://github.com/wright-benjamin-1701/heroku-library. And in the spirit of today’s post, it is very much subject to update and change. The code is running at https://heroku-library-54e06410c2c5.herokuapp.com.
In short, the app is just a list of links to sites I visit frequently. A collection of personal bookmarks to keep my most visited sites one or two clicks away. There is little styling so far, but there is some interesting technology under the hood. Here’s the big picture.
- The backend: Flask powers the backend of this application. The site is basically a homepage that displays a default list of links. Then, each hyperlink either takes you to a list in a new “category” or opens a new tab following that link.
- The frontend: The coolest part about the linking system is that it’s all done with just two HTML templates. One is for the header, footer, and base styles, and the other template is just to display a list of links! The Flask backend determines the right list elements to show at runtime based on dynamic routing. Since I can use either internal or external links, I can easily make a list of my favorite lists to put right on the homepage!
- The requirements: You can test the app locally with gunicorn! If you are on Windows, you can use waitress instead, or run gunicorn from WSL. I recommend installing the requirements in a virtual environment.
There are two very special files in the project repository: requirements.txt and Procfile. Now, it’s a good practice to keep a requirements.txt in Python projects so that no matter where your code is, it’s easy to spin up a virtual environment with all the libraries you depend on. But in this case, it’s absolutely necessary. Here’s the reason:
Heroku
Regular readers know that I do not accept payment or sponsorships to endorse or advertise subscription projects. I am, however, happy to give them honest reviews. Heroku is one of the solutions available to simplify deployment of applications. The most basic service is perfect for the use case and site I described above. It is unfortunate that Heroku used to provide this level of service for free and since revoked that privilege. But the point of this post is just to familiarize you with the tool and what’s required to deploy to it. If you don’t actually need to deploy your app, you just want to know what’s required to do it, no need to subscribe to Heroku!
Heroku’s automated deployment for Python applications is easy to configure and needs to know just two things. First, what are your Python dependencies? Second, should Heroku run your application? That’s what the two special files are for. Heroku looks at the requirements.txt to install your Python requirements, and it looks at the Procfile for instructions on how to run your application. Without those two files, it would be possible to run this application on a computer in a local development environment.
But by adding just four lines across those two files, now my website is one click away from running on the public web! I configured my Heroku deployment so that any commit to my GitHub repository triggers an update to my Heroku deployment. That means if I want to add CSS styles, or more UI or more pages, I just sync my development branch to GitHub, and the changes will go live a few minutes later.
Future development
It was fun to take advantage of the HTML template language to render Python objects into HTML. And using dynamic routing with templates to generate the site with just two HTML files gives me a lot of ideas for how I can push this simple framework further. I would like to create a more complex example with login and authentication so users can create their own lists of links. I know simple bookmarking programs already exist out there, but frankly the UI isn’t simple or seamless enough to use across my various browsers and devices. Also, I would like to try leveraging Heroku’s ability to run containers and orchestrate containerized services in more complex applications. But that will be another repository and another post. This page will always remain one click away from deployment.