Setting Up Flask App

Posted by : at

Category : python

This tutorial is the second part of tutorial series for creating a web application using Flask.

  1. Part 1 - Set up Python 3.6 Development Environment

  2. Part 2 - Setting up Flask App - This tutorial

  3. Part 3 - Setting up Database using SqlAlchemy

  4. Part 4 - Setting up Flask App Configurations

  5. Part 5 - Setting up Flask Migrations

Part - 2 Setting Up Flask App

So first you need to setup the virtualenv for your project. For this project we will be using python3.6. We need to install virtualenv first. If you dont have virtualenv installed follow the below instructions:

$ sudo apt-get install python-virtualenv

Virtualenv is installed. Now to use it for my project:

$ cd flask-boilerplate
$ virtualenv -p python3.6 venv --no-site-packages

This will create a venv folder and activate python3.6 for my project. To check:

$ python -V Python 3.6.3
$ pip -V pip 9.0.1

Next I will be setting up flask for my project

$ pip install flask

This will setup flask for my project, Quickly setting up a route to check if everything works fine

Create a

from flask import Flask
app = Flask(__name__)

def hello_world():
    return 'Hello, World!'

if __name__ == '__main__'"",port=3000,debug=True)

Run the script in the project directory :

$ python

Opening up http://localhost:3000 , It displays Hello, World!

Now I want to change the structure of the project, so that it can be bit modular

/flask-boilerplate /myapp /templates /layouts /forms /pages -home.html -about.html /venv

Lets create about.html and home.html for our routes inside the templates/pages folder


<!doctype html>

      <h1>Hello, World ! </h1>


Similarly, about.html

<!doctype html>

      <h1>About Flask Boilerplate </h1>


We need to initialize myapp package using “” inside myapp folder


from flask import Flask, render_template
app = Flask(__name__)

def hello_world():
    return render_template('/pages/home.html')

def about():
    return render_template('/pages/about.html')


from myapp import app

if __name__ == '__main__':"", port=3000, debug=True)

Now I can run the app using

$ python

You can also add a FLASK configuration variable to run the app using flask command

$ export FLASK_APP =

Now you can run the app using the below command :

$ flask run

In the next part we will be setting up Flask SQLAlchemy for setting up our models and sqlite database to save our data.

Ads by Google