Beginning Flask

Julian, Thu 13 April 2017, Flask

decorators, Flask, learning, python, tutorial

Last week I wrote an article showing you how to print the contents of a dict to a table using Flask and HTML.

In the lead up to the post I did quite a lot of browsing, reading and researching to wrap my head around this whole Flask thing. It took me a little longer because not everything out there was in simple, human readable English. A lot of sources assume a high level of Python knowledge and not everything was explained simplistically (lots of big words!).

That’s why I decided to write this article explaining the absolute basics (Hello World!) in a manner I would have liked to seen.

What is Flask?

Odds are if you’re reading this, you already know what Flask is. I’m going to explain anyway.

Flask is a “web framework” you can use to get your Python code to appear in a web browser (I told you I’d make this simplistic!).

As with most programming languages, when you begin learning Python you’ll be making scripts that are completely command line based. Eventually you’ll wonder how the pros use Python to make web applications. Flask is one way to do this.

There’ll be a Python script that imports the Flask module and when run, generally passes data to a HTML file. When the web page is loaded, it runs the Python code associated with that web page.

What should you know before learning Flask?

I won’t sugar coat it. You’ll have a hard time if you don’t have a sound understanding of HTML and CSS.

Python wise, your app will only be as complex as the code you write so the more you want to do, the more you need to know.

The HTML knowledge needs to be there though. For example, HTML forms can be tricky just on their own. When you add Python and Flask to the mix it can get downright confusing (seriously, me).

You don’t have to know CSS as much I guess but everyone wants their page/app to look good right? Maybe I’m just a little shallow!

Explaining Hello World

Okay so here’s a simple Hello World Flask script you’ll probably find in every Flask tutorial:

from flask import Flask

app = Flask(__name__)

def hello():
    return 'Hello world! Can I get some CSS please?!’

Alright, what the heck is going on here? Let me break it down

from flask import Flask
app = Flask(__name__)
def hello():
return 'Hello world! Can I get some CSS please?!’

Things to Note

Why use Flask?

I asked myself this question a few times. Honestly, I’d say use it because it’s so simple and quick to get running. Bob and I were chatting this week about the Flask Template I made to create the HTML table and with little to no effort, he was able to take the template and use it for his new weather compare app (code here).

My current hurdle is not Flask itself but trying to tie it in with the HTML side of things. It’s been years since I did any deep HTML coding so I’m pretty rusty. At the time of writing, I’m finding that the Python code is functional but I can’t get the HTML to wrap around it the way I want.

Examples and Resources

Check out some of these examples and resources. Reading and running other code will help get this stuff to sink in.


Don’t be deterred by the learning curve. If you’re new to this, start simple and stick with it. Just a little bite every day. Start with printing static data then slowly move on to more complex ideas like printing variables and dicts. Just remember, Always Be Coding!

And remember, Keep Calm and Code in Python Flask! (And HTML and CSS I guess!)

— Julian

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book