Flask: A Flexible Micro-Framework for Backend Dev in Python

Flask: A Flexible Micro-Framework for Backend Dev in Python

Cooking up compact, high-power servers. If you haven’t read my previous articles, let me preface this one with a brief personal declaration: I am a bit of a data nut, and I am certainly a graph enthusiast — something that my friends and colleagues will heartily attest to. As a result of my interests, I often find myself working with data tables in my projects. Excel sheets, CSV files, SQL tables… you get the idea. For example, over the past two to three years, I have tracked and categorized every single transaction I’ve made on a personal finance sheet that I keep super secret.

If you haven’t read my previous articles, let me preface this one with a brief personal declaration: I am a bit of a data nut, and I am certainly a graph enthusiast — something that my friends and colleagues will heartily attest to.

As a result of my interests, I often find myself working with data tables in my projects. Excel sheets, CSV files, SQL tables… you get the idea. For example, over the past two to three years, I have tracked and categorized every single transaction I’ve made on a personal finance sheet that I keep super secret.

Now before you think I’m completely crazy, it’s easier than it sounds. Most of the work is just downloading transaction data from my card providers and chucking it into an ongoing table. Pretty straightforward. A few weeks ago, however, my friend gave me the great idea of building an application to automate my tracking even further. At the time I was looking for my next project anyway, so this sounded like a great opportunity to build something that I could personally make use of.

Now when I think of data table manipulation, I think of Python. Specifically, the Pandas and Numpy libraries, two of the most powerful modern-day toolkits for data analysis and table manipulation. This project would give me an excellent opportunity to explore the capabilities of both.

The problem was this: I wanted to build my GUI with React (mostly out of familiarity), and at the time the only backend framework I had experience with was the Ruby on Rails framework. Coercing a Rails backend to interface with Python scripting, while probably a fun project, would have been highly involved and entirely unnecessary for what I was trying to do. So instead, I decided to use a backend framework written in Python to make the most out of a toolkit better suited for the job I had in mind.

My solution: The Flask Python Framework

The Flask Logo and Motto

Flask is a micro framework for web development in the Python language. What micro framework means specifically is that it comes with very little in terms of available features and boilerplate code at the start of a project’s development. Now that’s not to say there aren’t libraries upon libraries of features and plugins available for the Flask framework, but by default, Flask will tend to not include a feature unless you as a developer have specified otherwise.

This differs largely from frameworks like Rails or Django (the latter being another highly popular Python web framework), where a large amount of features and conveniences are generated for you on project creation. For example, both of these frameworks provide a Database-Abstraction layer that allows developers to easily read and write to databases through object models (e.g. Rails through Active Model, Django through their Object-relational Mapper).

The downside of a micro framework is obviously the lack of these features on start-up. Many if not all of the features in the more extensive frameworks exist in the Flask library, but they have to be included explicitly. The major upside, however, is that if you aren’t going to use those features, it’s a lot easier to just make a Flask app and avoid needing to delete a lot of unnecessary files and dependencies. Micro framework applications come with a lot less code bloat, and can always be scaled up with additional tools as needed.

For my purposes, my backend simply needed to be an API for reading and writing data files, and returning formatted JSON objects. In fact, I didn’t even need a database. In my case, the Flask micro framework was much easier to implement, and provided me the flexibility to include further Python modules as needed.

Setting It Up:

Setting up a flask server was surprisingly simple. First and foremost, you install the flask module into a python environment manager of your choice.

With your activated environment, create a new file (naming it app.py is the default that Flask expects) and, in it, type the following:

from flask import Flask
    app = Flask(__name__)

    @app.route('/')
    def hello_world():
        return 'Hello, World!'

And that’s it! In six lines, we have written enough code for a running Flask server. It’s that easy. Typing ‘flask run’ into your command line at this point would boot up a server with a single route at the root domain that returns the string ‘Hello, World!’. Flask interprets the route request, and runs the function written for that particular route. The return value of the function is what gets returned as the response to the URL request, but we’ll explore request responses much more later.

The six-line server primarily speaks to the power of Flask as a micro framework; we can produce a running server in an extremely minimal amount of code and add on as we need to. This server, however, is far from useful and will require a lot more features to run exactly how we need it to, so let’s take an overview of some of Flask’s built-in features and see what we can build.

flask python backend web-development programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...

Hire Backend Developers India

Are you looking to hire experienced Backend Developers at a reasonable cost to boost-up your IT business? **[Hire Backend Developers India](https://hourlydeveloper.io/hire-back-end-developer/ "Hire Backend Developers India")** and accomplish...

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

Learn Flask - Python Web Development - Flask Crash Course For Beginners

Learn Flask - Python Web Development - Flask Crash Course For Beginners, in this video we are going to talk about about different concepts in flask. Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.