Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Flask Blueprints
  • Table Of Contents Toc
Flask Blueprints

Flask Blueprints

By : Joel Perras
3.3 (3)
close
close
Flask Blueprints

Flask Blueprints

3.3 (3)
By: Joel Perras

Overview of this book

Flask is a small but powerful web development framework for Python. Though Flask is termed a micro-framework, it is no way lacking in functionality; there are many extensions available to Flask which helps it to function at the same level as other large frameworks such as Django and Ruby on Rails. This book will demonstrate how to develop a series of web application projects with the Python web micro-framework, and leverage extensions and external Python libraries and APIs to extend the development of a variety of larger and more complex web applications. The book will start by explaining Python’s Virtualenv library and how to create and switch between multiple virtual environments. You’ll first build an SQL database-backed application, which will use Flask-WTF, Flask-SQLAlchemy, Jinja templates, and other methods. Next you’ll move on to a timeline application, built using concepts including pytest-Flask, the Blinker package, data modelling for user timelines, exception handling, and creating and organizing CLI tools. Moving on, you’ll discover how to implement a photo timeline application where you’ll explore topics such as writing and running celery tasks, API error handling and testing, and Werkzeug middlewares. Finally, the book walks you through creating an application which fetches data from GitHub and stores it locally. You will also learn how to install and configure Flask-Click extension.
Table of Contents (9 chapters)
close
close

Simplifying common operations – using the virtualenvwrapper tool

A tool that I use frequently is virtualenvwrapper, which is a very small set of smart defaults and command aliases that makes working with virtual environments more intuitive. Let's install this to our global system now:

$ pip install virtualenvwrapper

Note

This will also install the virtualenv package as well in case it is not already present.

Next, you'll want to add the following lines to the end of your shell startup file. This is most likely ~/.bashrc, but in case you've changed your default shell to something else such as zsh, then it could be different (for example, ~/.zshrc):

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

The first line in the preceding code block indicates that new virtual environments that are created with virtualenvwrapper should be stored in $HOME/.virtualenvs. You can modify this as you see fit, but I generally leave this as a good default. I find that keeping all my virtual environments in the same hidden folder in my home directory reduces the amount of clutter in individual projects and makes it a bit more difficult to mistakenly add a whole virtual environment to version control.

Note

Adding an entire virtual environment to version control might seem like a good idea, but things are never as simple as they seem. The moment someone running a slightly (or completely) different operating system decides to download your project, which includes a full virtualenv folder that may contain packages with C modules that were compiled against your own architecture, they're going to have a hard time getting things to work.

Instead, a common pattern that is supported by pip and used by many developers is to freeze the current state of the installed packages in a virtual environment and save this to a requirements.txt file:

(testing) $ pip freeze > requirements.txt

This file may then be added to a version control system (VCS). As the intent of the file is to declare which dependencies are required for the application, and not provide them or indicate how they should be constructed, users of your project are then free to obtain the required packages in any way they so choose. Generally, they will install them via pip, which can handle a requirements file just fine:

(testing) $ pip install –r  requirements.txt

The second line adds a few convenient aliases to your current shell environment in order to create, activate, switch, and remove environments:

  • mkvirtualenv test: This will create an environment named test and activate it automatically.
  • mktmpenv test: This will create a temporary environment named test and activate it automatically. This environment will be destroyed once you invoke the deactivate script.
  • workon app: This will switch you to the app environment (already created).
  • workon (alias lsvirtualenv): When you don't specify an environment, this will print all the existing environments that are available.
  • deactivate: This will disable the currently active environment, if any.
  • rmvirtualenv app: This will completely remove the app environment.

We'll use the following command to create an environment to install our application packages:

$ mkvirtualenv app1

This will create a blank app1 environment and activate it. You should see an (app1) tag in your shell prompt.

Note

If you are using a shell other than Bash or ZSH, this environment tag may or may not appear. The way in which this works is that the script that is used to activate the virtual environment also modifies your current prompt string (the PS1 environment variable) so that it indicates the currently active virtualenv. As a result, there is a chance that this may not work if you're using a very special or non-standard shell configuration.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Flask Blueprints
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon