There are many different frameworks to use today when it comes to web development. It can become quite daunting to pick one that’s right for you when you’ll read the pros and cons of each language. Not every framework is built for all tasks that you will encounter in the wild which is why choosing the wrong programming language can be just as bad as shooting yourself in the foot.

Python Flask — Web Development, one drop at a time.

Python has been around since the late 80s. It has been the framework of some of the world’s most visited sites such as LinkedIn and Pinterest. Not only is it capable of creating large scale web products, but it has been used by the tech giants such as NASA for scripting calculations and Google as it has rich libraries and diverse framework access. The versatility speaks for itself here.

So what is Flask then?

Flask is a microframework that was released on April 1st, 2009, however, it is no fool. It prides itself on how simple it is at its core but how extendable it can be. You chose which imports you need for your project so that it does not become bogged down by loading extensions that your app doesn’t use or need. It is based on Pocoo’s two projects Jinja and Werkzeug, a template engine and WSGI application respectively.

A Python Flask App using GET/POST requests to query a database and render the correct template.

Through Jinja, data (from databases or JSON files) can be injected into HTML documents to change the content that a user would see. This can help create massive web applications using the same template iteratively, creating a much friendlier development process. It also allows Parent/Child templating to cut back on duplicate code.

Flask Navigation works a little differently from basic HTML A HREF’s

Above you can see what may look like an unusual navigation template. In Flask, ‘url_for’ is used for both changing template and rendering images as these are stored in default named folders ‘templates’ and ‘static’. This is standard to the framework and allows uniform between projects however it can be changed if the developer finds the need for such action.

The interesting thing in the code above is that a second variable has been added to the ‘url_for’ code which will be added to the URL. This second variable can be queried back within the Python file to create more dynamic web pages! For example, in the screenshot above ‘items’ is a webpage that when loaded through Python Flask has a variable ‘search’ that it needs to be passed into it. These 9 variables are linked to key data that is stored in a database that will be extracted through this simple templating structure. Below you will see the app route being loaded with a variable ‘<search>’ being adapted to the end of the URL. This variable is passed down through to the SQLite query which creates an array of results for the chosen link.

Python file using the <search> variable from the navigation

Where it all ends up! Jinja uses the results from the array in the previous image to populate this HTML table.

The Results! The Url shows we are on the items page and we are querying ‘cable_tray’ so only this data will show!

What does this mean?

This might not lead up to the one-page websites that seem to be the trend just now, however, it’s also possible for Flask to deliver one-page websites. What we have just seen above is how to easily pass data from one page to another just through the URL through a very painless effort. This is just the base of what Flask can do for you as well as these are all basic functions that come built-in. Through extensions such as SQLalchemy and Flask-Forms can add much more complex functionality to pages at relative ease!

If you would like to learn more there is a massive amount of documentation available free for people of all levels of programming. These can be found through the Flask Homepage, Pocoo and, as always, Stackover Flow when you just can’t find it.