Search icon CANCEL
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 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

Setting up a virtual environment

Flask can be simply installed using pip/pip3 or easy_install globally, but it’s preferable to set up an application environment using venv. This prevents the global Python installation from being affected by a custom installation as it creates a separate environment for the application. This separate environment is helpful as it allows you to have multiple versions of the same library for multiple applications; some packages might also have different versions of the same libraries as dependencies. venv manages this in separate environments and does not let the incorrect version of any library affect any application. In this recipe, we will learn how to create and manage these environments.

How to do it...

Python3 comes bundled with a venv module to create virtual environments. So, simply create a new environment called my_flask_env (or any other name of your choice) inside the folder of your choice where you want your development environment to live. This will create a new folder with the same name, as follows:

$ python3 -m venv my_flask_env

Run the following commands from inside the my_flask_env folder:

$ source my_flask_env/bin/activate
$ pip3 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...

So far, we have used pip3 install flask multiple times. As its 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 pip3, we will see that several packages are installed. The following is an outline of the package installation process of Flask:

$ pip3 install flask
Collecting Flask
...........
...........
Many more lines.........
...........
Installing collected packages: zipp, Werkzeug, MarkupSafe, itsdangerous, click, Jinja2, importlib-metadata, Flask
Successfully installed Flask-2.1.2 Jinja2-3.1.2 MarkupSafe-2.1.1 Werkzeug-2.1.2 click-8.1.3 importlib-metadata-4.11.4 itsdangerous-2.1.2 zipp-3.8.0

If we look carefully at the preceding snippet, we will see that multiple packages have been installed. Of these, five packages, namely, Werkzeug, Jinja2, click, itsdangerous, and markupsafe, are the packages on which Flask depends, and it will not work if any of them are missing. Others are sub-dependencies that are needed for the dependencies of Flask to work.

There’s more...

Before venv was introduced in Python 3.3, virtualenv was the standard library used to create and manage virtual environments. venv is a subset of virtualenv and misses out on the advanced features that virtualenv provides. For the sake of simplicity and to stay in the context of this book, I will use venv, but you are free to explore virtualenv and virtualenvwrapper.

See also

The references relating to this section are as follows:

Read more about virtualenv and virtualenvwrapper at https://virtualenv.pypa.io/en/latest/ and https://pypi.org/project/virtualenvwrapper/.

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