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

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