How This Tutorial Is Organized
The tutorial is divided into four articles:
- Introduction – Dive into web development with Flask. This article explains the basics of web development with Flask
- Part I – Setting up a local development environment
(You are here)
- Step 1: Create virtual environment for all required dependencies
- Step 2: required packages: Flask, psycopg2, SQLAlchemy
- Step 3: Create requirements.txt file with all dependencies listed
- Step 4: Install PostgreSQL 9.3 and create the local database
- Step 5: Organize the structure of the application
- Step 6: Make Flask say “Hello”
- Part II – Application development
- Part III – Deployment to Heroku
Step 1: Create Virtual Environment for All Required Dependencies
The code presented in this article was tested against Python 2.7. So, if you haven’t yet installed it, go ahead and get Python. Then, download pip. Pip is a Python package manager – a tool for installing Python packages from the Python Package Index. In most cases, you shouldn’t install packages globally. Instead you should dive in a virtual environment concept. It is a tool to create isolated Python environments to give access to only the packages that a particular application can use. Thanks to virtual environments, you can use different versions of packages with no conflicts. In other words, it is a directory with a copy of the Python interpreter, a copy of the pip installer and copy of everything needed to run a particular Python program.
For the initial setup, we’re going to use Virtualenvwrapper (it provides a set of wrappers to manage virtual environments):
$ pip install virtualenvwrapper
Create a virtual environment for your app. I’ll name mine todoapp.
In order to use the created virtual environment type:
Step 2: Install Required Packages: Flask, psycopg2, SQLAlchemy
$ pip install Flask
To integrate our app with PostgreSQL we need to install:
- Psycopg2 – a Python adapter for Postgres
- Flask-SQLAlchemy – Flask wrapper for SQLAlchemy (a powerful relational database framework that offers a high level ORM and low level access to a database’s native SQL functionality for Python.)
pip install psycopg2 Flask-SQLAlchemy
Step 3: Create requirements.txt File With All Dependencies Listed
Our application must have a requirements.txt that contains all the package dependencies with the exact versions.
$ pip freeze > requirements.txt.
Step 4: Install PostgreSQL 9.3 and Create Local Database
We’ll need to get a Postgres database set up to store our todo list. We’ll also add Python ORM SQLAlchemy to our app. Once you have Postgres installed, create a database and name it todoapp to use as a local database.
Switch to postgres account.
sudo su - postgres
Run PostgreSQL command line client.
Create a database user with a password.
create user patrycja with password 'mypassword';
Create a database instance.
create database todoapp owner patrycja encoding 'utf-8';
Step 5: Organize the Structure of the Application
The structure for our web app will be as follows:
The todoapp.py file is the Python code that will import the app and execute.
The views.py file contains a view function.
The models.py file contains SQLAlchemy models.
The templates folder contains Jinja2 templates.
For our needs, the structure is simplistic. If you want to know how to structure a large Flask application, take a look at this tutorial
Step 6: Make Flask Say “Hello”
In a todoapp.py create an instance of the Flask application as follows:
from flask import Flask app = Flask(__name__) from views import * if __name__ == '__main__': app.run()
Notice that the view module (module with all the view functions – the ones with route() decorator) should be imported after the application object is created. When you look below, view the module import app object from the todoapp module. If our views module in todoapp.py were imported at the top of the file, we would have a circular import.
from todoapp import app @app.route('/') def index(): return '
Run the application:
(todoapp) $ python todoapp.py * Running on http://127.0.0.1:5000/
Open the browser and type http://127.0.0.1:5000/ in the address bar. You will see the the Hello greeting.
|« Previous Part||Next Part »|