Building a "When's the Next Toda City tocochan Bus?" Web App with Flask and Python 3
What is Flask?
If you look at the Flask Official Site, you’ll find the following sentence right at the top.
1 | Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions |
Flask is a small framework for Python, built on top of Werkzeug and Jinja 2.
- Made by Armin Ronacher (Australian)
- The name is a play on words against Bottle. (Flask)
- It started as an April Fools’ joke in 2010
- Looking at The Definitive Comparison of 3 Python Web Frameworks Drawing Attention in 2017, its popularity seems to be steadily growing (and the pay looks good too?)
What’s so great about it?
You can build a simple web application with a minimal setup.
You could do this fairly easily with Django or Rails too, but with Flask the steps are even shorter and simpler.
Tutorial
1 | from flask import Flask |
Criteria for choosing Flask
- The project isn’t large-scale. (You have to decide the file structure yourself or as a team.)
- You don’t use a database. Or even if you do, the database design isn’t complex.
- The library you want to use is written in Python, and you want Flask to act as a gateway to it.
Let’s get started!
There are plenty of articles for beginners, so I’ll skip that part and instead share a web application I built while learning.
I think seeing something that actually works, along with its source code, will give you a clearer picture.
This is an app that lets you specify a stop on Toda City’s local “tocochan bus” and counts down how many minutes until it arrives.
I built it because I use it myself, and I’m delighted that it has caught on quite a bit.
1 | ※ Incidentally, I confirmed with the Toda City Hall, and although it is unofficial, I have received permission to publish it. |
Here’s the source ♪
I think browsing the source will make it easier to understand what I tried to do.
What I tried
- Created a local Flask development environment with Docker
- Loaded data from a config file (config.py)
- Session functionality
- Applied Bootstrap
- Asynchronous communication with superagent.js
- Made it flickable with flickity.js
Production environment
- Sakura VPS, CentOS 6
- httpd 2.4 + wsgi + virtualenv + python3
My Sakura VPS hosts CakePHP and SpringBoot projects as well, so I keep it as a free-for-all playground environment.
Development period
Roughly one week. I was able to publish it in under 10 hours, using my lunch breaks at work and some Sunday DIY.
If anything, the learning cost was mostly on the JavaScript side.
Overall impression
It was a perfect fit for this kind of app, which doesn’t manage user information and has no database.
The structure didn’t get too complex, so it was just right.
As expected, Django is more suitable for large-scale projects.
On a personal note, being able to go through the approval process of calling Toda City Hall and getting permission to publish the web app, and being able to contribute to the local community, brought me a great deal of joy.
I’d like to keep upgrading it going forward.
I’d be happy if this is helpful when you use Flask.
That’s all.
Building a "When's the Next Toda City tocochan Bus?" Web App with Flask and Python 3
