';
side-area-logo

Python: Flask – Getting Started

Welcome! In this article, I’ll be illustrating a general overview of the Flask micro web framework. I’ve developed a comprehensive web application (deployed locally) in Flask that is currently used commercially, and I’ve learned so much along the way. I know that there’s still much for me to learn – I’m discovering new things every day! If any of you learned programmers do come across something I write about that may be deprecated (or plain broken) – please let me know in the comments! This being said, however, everything I write here is from my own personal experience designing, developing, and successfully deploying a Flask application.

  • Overview
    Flask Overview
    A little bit about Flask 🙂
  • Organization Structure: Administrative
    Flask Management Files
    These are the files responsible for Flask's 'wellbeing'
  • Organization Structure: Vitals
    Application Content Files
    These are the files responsible for the actual content inside the app

Flask is, to get the technicalities out of the way, a micro web framework developed for Python. What that actually means is this: Flask is a package allowing you to develop web pages with Python. When I first stumbled upon Flask a long time ago (probably in late 2017) I was googling ‘how to develop web pages in Python’ or something along those lines and Flask was one of the first few search results that came up, together with Django. I flipped a coin and decided to choose Flask. Having just learnt HTML, CSS, and Python, I was stoked for a chance to combine all these languages skillsets together! On that note, the abovementioned three skillsets are all you’ll need to get a Flask application up and running.

Flask Overview

What IS Flask?

I’ll be highlighting the key components in any Flask application, so you’ll know what you’ll be working with – and what to expect when you’re done.

Flask applications, similar to other web applications, function best (and are easiest to understand) if you follow an ideal folder structure. I’ll won’t be conducting a deep-dive into the reasons behind these here (perhaps an story for another day) but you can find a link to my GitHub repository here if you’d like to fork your own copy of my arrangement (and follow along).

First, I’ll talk about the…administrative components inside the program. After that, I’ll go into the functionally vital components.

Structure: Administrative Components

and what they're used for

run.py
Server Configuration
Starting off slow, this is the file that Python actually runs in order to get your application going.
config.py
Flask App Configuration
Next, this file contains all the important configuration options related to running the application. This includes the SECRET_KEY, and (potentially) database settings (if you’re interested in incorporating a database into your application). Theoretically, the contents of this file could be inserted elsewhere, but I like keeping my code organized and tidy – easily expanded upon.
.flaskenv
Flask Environment Configuration
I know this is already the second ‘configuration’ file but just bear with me! This file is where you tell Python which file to run (i.e. run.py above), and set the type of flask environment (usually set to development, but you could choose production too)
requirements.txt
Application Dependencies
If you’ve read my previous article on virtual environments, then you should know exactly what this file is for! For the uninitiated, this file contains a list of all the program’s dependencies (i.e. packages and their versions) and allows a user to create a virtual environment containing only these dependencies.

So far I’ve covered all the files that are necessary to get the program up and running (i.e. the administrative components of the application’s structure). Now, into the good stuff.

Structure: Vital Components

and what they're used for

Application folder
Containing pretty much everything
Everything directly related to application function belongs in here, which includes everything I’m about to discuss from here on out!
Templates and Static folders
Front-End
Quite intuitively, all your HTML template files go inside the ‘templates’ folder, while your site images, CSS and JS (if any) go inside the ‘static’ folder (because they’re, well, static)
views.py
Application Controller
This file (commonly named views.py or routes.py) is the skeleton of the operation. Application routes, redirects, and everything related to the overall structure of the program happens in here. Ideally, you would try to outsource and package your functions and whatnot into other files, leaving the views.py reasonably clean. Code performing the heavy lifting (such as input processing, manipulating data, etc.) should all be performed in other files. If you have only few functions, a single functions.py file should suffice. Do, however, try to properly encapsulate your code!
forms.py
User Input
When you’re developing a web application, you’ll be relying on user input from the web page rather than the command prompt (…right?), and this file is where that magic happens. You’ll need to import a package called wtforms among others, and this file is where all the different Form objects are defined. You can then call the relevant Form object from the views.py file and interact with user input that way.

That’s it! Any Flask application can adapt from this structure and it’ll run – just make sure the correct import statements are in the correct place. With this information, you’re now one step closer to making a fully functioning web application in Python!

Recommend
  • Facebook
  • Twitter
  • LinkedIN
  • Pinterest
Share
Tagged in
There are 2 comments on this post
  1. Andrea
    February 12, 2021, 2:01 pm

    Best article ever!! Easy to understand and clear!!

Leave a reply