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
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Flask Framework Cookbook

You're reading from   Flask Framework Cookbook Enhance your Flask skills with advanced techniques and build dynamic, responsive web applications

Arrow left icon
Product type Paperback
Published in Jul 2023
Publisher Packt
ISBN-13 9781804611104
Length 318 pages
Edition 3rd 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 (20) Chapters Close

Preface 1. Part 1: Flask Fundamentals
2. Chapter 1: Flask Configurations FREE CHAPTER 3. Chapter 2: Templating with Jinja 4. Chapter 3: Data Modeling in Flask 5. Chapter 4: Working with Views 6. Part 2: Flask Deep Dive
7. Chapter 5: Web Forms with WTForms 8. Chapter 6: Authenticating in Flask 9. Chapter 7: RESTful API Building 10. Chapter 8: Admin Interface for Flask Apps 11. Chapter 9: Internationalization and Localization 12. Part 3: Advanced Flask
13. Chapter 10: Debugging, Error Handling, and Testing 14. Chapter 11: Deployment and Post-Deployment 15. Chapter 12: Microservices and Containers 16. Chapter 13: GPT with Flask 17. Chapter 14: Additional Tips and Tricks 18. Index 19. Other Books You May Enjoy

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 an extension of 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.

What is a Python package?

A Python package can simply be thought of as a program that can be imported using Python’s import statement in a virtual environment or globally based on its installation scope.

How to do it...

Installing a Flask app can be 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 from the previous recipe:

#!/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.

To verify the successful installation of your package, import it inside a Python environment:

$ python
Python 3.8.13 (default, May  8 2022, 17:52:27)
>>> import my_app
>>>

See also

The list of valid trove classifiers can be found at https://pypi.python.org/pypi?%3Aaction=list_classifiers.

You have been reading a chapter from
Flask Framework Cookbook - Third Edition
Published in: Jul 2023
Publisher: Packt
ISBN-13: 9781804611104
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