Sitemap
Emil Mladenov

Emil Mladenov

I do front end things. I also love AI, space, design, branding and data analysis.

Getting started with Flask

2 min readMar 20, 2018
Press enter or click to view image in full size

Flask is an awesome micro framework for developing web applications with Python.

By using Flask, you can quickly get a web application running, complete with models, views and templates, it’s simple to use, has nice documentation and it’s easy to learn.

Prerequisites

You will need Python installed on your computer. Install flask by using pip like so:

Once you have Flask installed, create a project to contain your web application files in. To create and run a simple application, you will need just a couple of folders and a main Python file to host your view logic.

Press enter or click to view image in full size

Let’s get coding

Get Emil Mladenov’s stories in your inbox

Join Medium for free to get updates from this writer.

First, lets open our main file and let’s import the Flask module and a couple of methods.

We use the Flask constructor to create our app, and then we use a decorator function to set up our index route.

When your run main.py, your Flask application will run on your default port, and you will see a simple ‘hello, Flask!’ printed in the browser.

Let’s create a simple template and render it. Flask uses the Jinja templating language, which allows for simple programming logic like for loops, if statements and such.

Our simple template looks like this:

We also need to change our view to render the template and pass it some values.

The view will pass the title and news parameters to our template, and by using a simple for loop inside the template, we are able to render separate news items with unique titles and links. We also use a ternary statement to ensure that our heading title won’t get rendered without any content, if we forget to add a title (winkyface)

And that’s a wrap! This even barely scratches the surface of Flask, but it’s enough to help you grasp the fundamentals in the simplest way possible. My challenge for you is to render your own template!

--

--

Emil Mladenov
Emil Mladenov

Written by Emil Mladenov

I do front end things. I also love AI, space, design, branding and data analysis.