Book Image

Mastering Flask

By : Jack Stouffer
Book Image

Mastering Flask

By: Jack Stouffer

Overview of this book

Starting from a simple Flask app, this book will walk through advanced topics while providing practical examples of the lessons learned. After building a simple Flask app, a proper app structure is demonstrated by transforming the app to use a Model-View-Controller (MVC) architecture. With a scalable structure in hand, the next chapters use Flask extensions to provide extra functionality to the app, including user login and registration, NoSQL querying, a REST API, an admin interface, and more. Next, you’ll discover how to use unit testing to take the guesswork away from making sure the code is performing as it should. The book closes with a discussion of the different platforms that are available to deploy a Flask app on, the pros and cons of each one, and how to deploy on each one.
Table of Contents (20 chapters)
Mastering Flask
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Python package management with pip


In Python, programmers can download libraries from other programmers that extend the functionality of the standard Python library. As you already know from using Flask, a lot of Python's power comes from its large amount of community-created libraries.

However, installing third-party libraries can be a huge pain to do correctly. Say there is a package X that you wish to install. Simple enough, download the Zip file and run setup.py, right? Not quite. Package X relies on package Y, which in turn relies on Z and Q. None of this information was listed on package X's website, but they are required to be installed for X to work at all. You then have to find all of the packages one by one and install them, in the hope that the packages you are installing don't require any extra packages themselves.

In order to automate this process, we use pip, the Python package manager.

Installing the pip Python package manager on Windows

If you are on Windows, and your installed Python the current version, you already have pip! If your Python installation is not the most recent, the easiest thing to do is to simply reinstall it. Download the Python Windows installer at https://www.python.org/downloads/.

In Windows, the variable that controls which programs are accessible from the command line is path. To modify your path to include Python and pip, we have to add C:\Python27 and C:\Python27\Tools. Edit the Windows path by opening the Windows menu, right-clicking on Computer and clicking on Properties. Under Advanced system settings, click Environment Variables.... Scroll down until you find Path, double-click it, and add ;C:\Python27;C:\Python27\Tools to the end.

To make sure you have modified your path correctly, close and reopen your terminal and type the following into the command line:

pip --help

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

pip should have printed its usage message as shown in the following screenshot:

Installing the pip Python package manager on Mac OS X and Linux

Some Python installations of Linux do not come with pip, and Mac OS X installations don't come with pip by default. To install it, download the get-pip.py file from https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py.

Once you have downloaded it, run it with elevated privileges using the following:

$ sudo python get-pip.py

Then pip will be installed automatically.

pip basics

To install a package with pip, follow this simple step:

$ pip install [package-name]

On Mac and Linux, because you are installing programs outside the user-owned folders, you might have to prepend sudo to the install commands. To install Flask, simply run this:

$ pip install flask

Then, all requirements of Flask will be installed for you.

If you want to remove a package that you are no longer using, run this:

$ pip uninstall [package-name]

If you wish to explore or find a package but don't know its exact name, you may use the search command:

$ pip search [search-term]

Now that we have a couple of packages installed, it is common courtesy in the Python community to create a list of packages that are required to run the project, so others can quickly install every thing required. This also has the added benefit that any new member of your project will be able to run your code quickly.

This list can be created with pip by running this:

$ pip freeze > requirements.txt

What exactly did this command do? pip freeze run by itself prints out a list of the installed packages and their versions as follows:

Flask==0.10.1
itsdangerous==0.24
Jinja2==2.7.3
MarkupSafe==0.23
Werkzeug==0.10.4
wheel==0.24.0

The > operator tells Bash to take everything printed by the last command and write it to this file. If you look into your project directory, you will see the new file named requirements.txt that contains the output of pip freeze.

To install all the packages from this file, a new project maintainer will have to run this:

$ pip install -r requirements.txt

This tells pip to read all the packages listed in requirements.txt and install them.