A Guide to Python’s Flask Web Interface

In this blog post, I’d like to demonstrate how to build a web app that creates QR codes using WiFi information that an end-user can input. Having built this app, you will hopefully come to the same conclusion as I did: command line apps and web apps are just text-based endpoints to arbitrary Python code. Once you’ve realized this, the process of making both web and command-line apps will hopefully be much less mystical!

What is Flask?

Many people who use our QR codes do not want to generate them from the command line. Browser-based interfaces let us know that all our users will have roughly the same experience, regardless of the way their system is set up. Perhaps more importantly, we can worry less about the tedious parsing of command line arguments. A consistent front-end lets the developer focus on application logic instead of infrastructure.

Flask is a bare-bones Python framework for building apps that use the web browser as the front-end, rather than the command-line as the front-end. Flask abstracts away lower-level tasks, such as setting up a development web server, managing information flow from the browser to the Python interpreter, and more. Using Flask thus allows you, the developer, to focus on the application logic rather than worrying about infrastructural things.

Flask is a bare-bones framework for attaching your code to to user actions in a browser, via simple callback functions. Flask is often called a “micro-framework” to distinguish it from feature-rich, higher overhead options like Django. For more details, I’d encourage you to check out the Flask website; aside from that, though, nothing beats building an app to learn how to use a framework, so hopefully this app we are going to build will also augment your intuition about Flask!

Structuring a Flask app

In order to build a Flask app, you’ll need the following minimal directory structure:

├── templates
└── app.py

We write our Flask app into app.py. In the templates/ directory, we store the HTML templates that our Flask app will use to display to the end user. In our previous blog post, we had the following structure:

Back to Top