Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Flask Framework Cookbook

You're reading from   Flask Framework Cookbook Over 80 hands-on recipes to help you create small-to-large web applications using Flask

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher
ISBN-13 9781783983407
Length 258 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Shalabh Aggarwal Shalabh Aggarwal
Author Profile Icon Shalabh Aggarwal
Shalabh Aggarwal
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Flask Configurations FREE CHAPTER 2. Templating with Jinja2 3. Data Modeling in Flask 4. Working with Views 5. Webforms with WTForms 6. Authenticating in Flask 7. RESTful API Building 8. Admin Interface for Flask Apps 9. Internationalization and Localization 10. Debugging, Error Handling, and Testing 11. Deployment and Post Deployment 12. Other Tips and Tricks Index

Environment setup with virtualenv

Flask can be installed using pip or easy_install globally, but we should always prefer to set up our application environment using virtualenv. This prevents the global Python installation from getting affected by our custom installation by creating a separate environment for our application. This separate environment is helpful because you can have multiple versions of the same library being used for multiple applications, or some packages might have different versions of the same libraries as dependencies. virtualenv manages this in separate environments and does not let a wrong version of any library affect any application.

How to do it…

We will first install virtualenv using pip and then create a new environment with the name my_flask_env inside the folder in which we ran the first command. This will create a new folder with the same name:

$ pip install virtualenv
$ virtualenv my_flask_env

Now, from inside the my_flask_env folder, we will run the following commands:

$ cd my_flask_env
$ source bin/activate
$ pip install flask

This will activate our environment and install Flask inside it. Now, we can do anything with our application within this environment, without affecting any other Python environment.

How it works…

Until now, we have used pip install flask multiple times. As the name suggests, the command refers to the installation of Flask just like any Python package. If we look a bit deeper into the process of installing Flask via pip, we will see that a number of packages are installed. The following is a summary of the package installation process of Flask:

$ pip install -U flask
Downloading/unpacking flask
…........
…........
Many more lines.........
…........
Successfully installed flask Werkzeug Jinja2 itsdangerous markupsafe
Cleaning up...

Note

In the preceding command, -U refers to the installation with upgrades. This will overwrite the existing installation (if any) with the latest released versions.

If we notice carefully, there are five packages installed in total, namely flask, Werkzeug, Jinja2, itsdangerous, and markupsafe. These are the packages on which Flask depends, and it will not work if any of them are missing.

There's more…

To make our lives easier, we can use virtualenvwrapper, which, as the name suggests, is a wrapper written over virtualenv and makes the handling of multiple virtualenv easier.

Tip

Remember that the installation of virtualenvwrapper should be done at a global level. So, deactivate any virtualenv that might still be active. To deactivate it, just use the following command:

$ deactivate

Also, it is possible that you might not be able to install the package at a global level because of permission issues. Switch to superuser or use sudo in this case.

You can install virtualenvwrapper using the following commands:

$ pip install virtualenvwrapper
$ export WORKON_HOME=~/workspace
$ source /usr/local/bin/virtualenvwrapper.sh

In the preceding code, we installed virtualenvwrapper, created a new environment variable with the name WORKON_HOME, and provided it with a path, which will act as the home for all our virtual environments created using virtualenvwrapper. To install Flask, use the following commands:

$ mkvirtualenv flask
$ pip install flask

To deactivate a virtualenv, we can just run the following command:

$ deactivate

To activate an existing virtualenv using virtualenvwrapper, we can run the following command:

$ workon flask
You have been reading a chapter from
Flask Framework Cookbook
Published in: Nov 2014
Publisher:
ISBN-13: 9781783983407
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image