Book Image

Flask Framework Cookbook - Second Edition

By : Shalabh Aggarwal
Book Image

Flask Framework Cookbook - Second Edition

By: Shalabh Aggarwal

Overview of this book

Flask, the lightweight Python web framework, is popular thanks to its powerful modular design that lets you build scalable web apps. With this recipe-based guide, you’ll explore modern solutions and best practices for Flask web development. Updated to the latest version of Flask and Python 3, this second edition of Flask Framework Cookbook moves away from some of the old and obsolete libraries and introduces new recipes on cutting-edge technologies. You’ll discover different ways of using Flask to create, deploy, and manage microservices. This Flask Python book starts by covering the different configurations that a Flask application can make use of, and then helps you work with templates and learn about the ORM and view layers. You’ll also be able to write an admin interface and get to grips with debugging and logging errors. Finally, you’ll learn a variety of deployment and post-deployment techniques for platforms such as Apache, Tornado, and Heroku. By the end of this book, you’ll have gained all the knowledge you need to confidently write Flask applications and scale them using standard industry practices.
Table of Contents (15 chapters)

Making a Flask app installable using setuptools

We now have a Flask app, but how do we install it like any Python package? It is possible that another application might depend on our application, or that our application is in fact an extension for Flask and would need to be installed in a Python environment so it can be used by other applications. In this recipe, we will see how setuptools can be used to create an installable Python package.

How to do it...

Installing a Flask app can be very easily achieved using the setuptools Python library. To achieve this, create a file called setup.py in your application's folder and configure it to run a setup script for the application. This will take care of any dependencies, descriptions, loading test packages, and so on.

The following is an example of a simple setup.py script for the Hello World application:

#!/usr/bin/env python 
# -*- coding: UTF-8 -*- 
import os 
from setuptools import setup 
 
setup( 
    name = 'my_app', 
    version='1.0', 
    license='GNU General Public License v3', 
    author='Shalabh Aggarwal', 
    author_email='[email protected]', 
    description='Hello world application for Flask', 
    packages=['my_app'], 
    platforms='any', 
    install_requires=[ 
        'flask', 
    ], 
    classifiers=[ 
        'Development Status :: 4 - Beta', 
        'Environment :: Web Environment', 
        'Intended Audience :: Developers', 
        'License :: OSI Approved :: GNU General Public License v3', 
        'Operating System :: OS Independent', 
        'Programming Language :: Python', 
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content', 
        'Topic :: Software Development :: Libraries :: Python Modules' 
    ], 
) 

How it works...

In the preceding script, most of the configuration is self-explanatory. The classifiers are used when the application is made available on PyPI. These will help other users search the application using the relevant classifiers.

Now, we can run this file with the install keyword, as follows:

$ python setup.py install

The preceding command will install the application along with all the dependencies mentioned in install_requires, that is, Flask and all of Flask's dependencies. Now the app can be used just like any Python package in a Python environment.

See also