Flask Series: Prepare the Environment

Saturday, Apr 11, 2015 17:08 · 417 words · 2 minutes read

Flask Series

  1. Prepare the Environment
  2. Structure the Application
  3. Application Configuration
  4. Templating
  5. Model
  6. Testing
  7. Views and Web Forms
  8. Error Management
  9. Security
  10. Optimizations
  11. Healthcheck and Monitoring
  12. Internationalization
  13. Deployment

What is a Flask?

Flask is a Python micro web framework based on Werkzeug and Jinja2. It is BSD licensed.

Setup

Once you have Python installed, you can install and configure pip, setuptools and virtualenv.

pip

pip is a recommended tool for installing Python packages.

How to install pip?

The most secure way to install pip on your machine is by downloading get-pip.py and run the following command, which may require administrator / root rights:

curl -O https://bootstrap.pypa.io/get-pip.py
python get-pip.py

How to upgrade pip?

Once installed, it is very easy to upgrade pip to a newer version:

pip install -U pip

pip issues

Follow the solutions explained in the following blog post.

setuptools

setuptools is a library used for packaging Python projects that allows easily to download, build, install, uninstall and upgrade these Python packages.

How to install setuptools?

pip install setuptools

virtualenv

virtualenv is a very helpful tool, used to keep the Python project dependencies in different places, allowing to build isolated environments. It solves dependency, versioning and permissions issues, where common packages are shared between multiple projects.

How to install virtualenv?

pip install virtualenv

virtualenvwrapper

virtualenvwrapper is set of extensions for the virtualenv tool, which allows easily to create, used and delete virtual environments.

How to install and configure virtualenvwrapper on UNIX systems?

pip install virtualenvwrapper

… and configuration is done like this …

export WORKON_HOME=~/Envs
mkdir -p $WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh

How to install and configure virtualenvwrapper on Windows systems?

pip install virtualenvwrapper-win

Create a new virtual environment

mkvirtualenv flask_env

Install some Python packages:

(flask_env)$ pip install flask

Activate a virtual environment

workon flask_env

Deactivate the current virtual environment and switch back to the default one

deactivate

More virtualenvwrapper commands can be found here.

My First Flask Application

Now that you have pip, setuptools and virtualenv installed and configured, you could implement your first Flask application.

First you should activate the already created virtual environment:

workon flask_env

Create a new python module named app.py and paste the following code within it:

Run the following command to start the flask application:

python app.py

Now you could test your Flask application:

  • http://localhost:5000/

By default the flask web application will run on port 5000. You could change that option by specifying the port parameter of the run method:

In the next blog post I will write how to structure your Flask project.

comments powered by Disqus