Introduction to Flask :)


First thing we want to do is to install pip:

Now we will install Flask: pip install Flask

Basic Server

from flask import Flask

app = Flask(__name__)

def hello_world():
return 'Hello World!'

if __name__ == '__main__':

Time to run


// Its now running on our machine, only visible locally.

* Running on

To make it publically visible add:'')

To turn on debug (to automatically refresh on saving):


def index():
return 'Index Page'

def hello():
return 'Hello World'

Passing variables to the routes

def show_user_profile(username):
# show the user profile for that user
return 'User %s' % username

def show_post(post_id):
# show the post with the given id, the id is an integer
return 'Post %d' % post_id

Now lets render a template

from flask import render_template


def hello(name=None):
return render_template('hello.html', name=name)

Create a template folder and hello.html inside it


in hello.html

<!doctype html>
<title>Hello from Flask</title>
{% if name %}
<h1>Hello {{ name }}!</h1>
{% else %}
<h1>Hello World!</h1>
{% endif %}

Return json for an api

from flask import jsonify
def get_json():
return jsonify(name='Brian',