Python scripts are great for getting stuff done (especially repetitive tasks or pulling and aggregating information). In some cases it makes a lot of sense to put a minimal graphical interface on top so that others can enjoy the results of said scripts without having to bother with all the details of actually running it.
Indeed, one of the biggest challenges of writing scripts is the barrier for others to use them. "Oh to run my script you need to install python, git, create a virtualenv, clone the master branch, install these requirements, place the files there, oh btw it should be python3" as opposed to "I deployed my script with a simple page that has an EXECUTE ME button, go press that".
In recent years, I've created all sorts of scripts that gather information from devices (to validate configuration or search for something like a MAC address) or poll vendor APIs (for things such as security vulnerabilities, end of life information), but I'm the only one who knows how to run them. With a very thin layer of HTML on top of the script I can actually make them immediately accessible for my colleagues and as you will see below, it doesn't even take a lot of effort.
Enter tiny-flask
As with all good automation tasks, you do something enough times manually, it's bound to become annoying and in need of templating. Every time I was starting a new mini-project of adding a front-end to a script, I found I had to recreate the scaffolding of that webapp from other repos (look for a previous app, copy code, clean it up, rename files, fix breakage from renaming etc.). No more!
Tiny-flask is a basic python3 flask webapp blueprint for quickly adding a web interface to your python scripts. What you get:
- A flask webapp organized as a python package following the larger applications template.
- A HTML template starting point using Bootstrap 3.3.7 for pretty, modern and responsive webpages!
- A Dockerfile to build your very own container and easily deploy it anywhere.
My goal here is to have a development environment with a very straightforward deployment method (i.e. running it on a server somewhere). Essentially, you can download tiny-flask, save your script in the package folder and call your functions from the webapp. Once you've integrated your script's output with the html templates, deploying it to a more permanent home can be done in two ways:
- Put it on the server, creating the environment as you would on your machine, but running it with a proper web-server (instructions included).
- Package it as a Docker container, then run it on any Docker enabled server. I provide the Dockerfile and the two commands needed to build and run it.
Bonus reading material
My friend Roman Dodin was actually doing this back in 2016 and he wrote some amazingly detailed posts, well worth reading as he took things a lot farther than I aimed to with tiny-flask:
- Building Web front end for Python scripts with Flask
- Making a docker container for a Flask application
- Flask application in a production-ready container
Now you may ask why I even created another project when Roman offers a production-ready container with all the bells and whistles? The answer is: keeping it as simple as possible for a starter project and removing as many barriers to getting things done.
That's it for now
I hope this will prove useful for you, and if it does, please get in touch and let me know what cool stuff you've built with it! If you can't figure out for the life of you what to do with it, watch this space, as I will soon publish an example of what you can do with tiny-flask and that NXOSv lab I've been building!
And, as always, thanks for reading!