Real Python Part 2
Real Python Part 2
of Contents
Introduction 1.1
Getting Started 1.2
SQLite 1.2.3
pip 1.2.4
virtualenv 1.2.5
2
SQL Functions 1.5.7
Controller 1.6.3
Views 1.6.4
Templates 1.6.5
Sanity Check 1.6.6
User Login 1.6.7
Sessions 1.6.8
Show Posts 1.6.9
Breakpoints 1.7.2
Post Mortem Debugging 1.7.3
Flask: FlaskTaskr, Part 1 - Quick Start 1.8
Getting Started 1.8.1
Configuration 1.8.2
Database 1.8.3
Controller 1.8.4
Templates and Styles 1.8.5
Sanity Check 1.8.6
Tasks 1.8.7
Add, Update, and Delete Tasks 1.8.8
3
Tasks Template 1.8.9
HTML 1.11.1
CSS 1.11.2
Chrome Developer Tools 1.11.3
Refactoring 1.13.2
Flask: FlaskTaskr, Part 6 - New features and Error Handling 1.14
New Features 1.14.1
Password Hashing 1.14.2
Custom Error Pages 1.14.3
Flask: FlaskTaskr, Part 7 - Deployment 1.15
4
Deployment 1.15.1
Intermission 1.18.4
Feature Branch Workflow 1.18.5
Fabric 1.18.6
Recap 1.18.7
Conclusion 1.18.8
5
seconds2minutes App 1.21.4
Database 1.24.2
URL Routing 1.24.3
Initial Views 1.24.4
Templates 1.24.5
Profile Page 1.24.6
6
Socrata (CrawlSpider and Item Pipeline) 1.25.5
Setup 1.29.1
Model 1.29.2
Setup an App 1.29.3
7
Stretch Goals 1.30.3
Test 1.32.7
Test Script 1.32.8
Conclusion 1.32.9
Prototyping 1.33.2
Project Setup 1.33.3
Landing Page 1.33.4
Bootstrap 1.33.5
About Page 1.33.6
8
Acknowledgements 1.36
9
Introduction
Introduction
This is not a reference book. Instead, we’ve put together a series of tutorials and
examples to highlight the power of using Python for web development. The purpose is to
open doors, to expose you to the various options available, so that you can decide the
path to go down when you are ready to move beyond this course. Whether it's moving
on to more advanced materials, becoming more engaged in the Python development
community, or building dynamic web applications of your own - the choice is yours.
This course moves fast, focusing more on practical solutions than theory and concepts.
Take the time to go through each example. Ask questions. Join a local Meetup group.
Participate in a hackathon. Take advantage of the various online and offline resources
available to you. Engage.
Regardless of whether you have past experience with Python or web development, we
urge you to approach this course with a beginner's mind. The best way to learn this
material is by challenging yourself. Take the examples further. Find errors in our code.
And if you run into a problem, use the "Google-it-first" approach/algorithm to find a
relevant blog post or article to help answer your question. This is how "real" developers
solve "real" problems.
By learning through a series of exercises that are challenging, you will screw up at times.
Try not to beat yourself up. Instead, learn from your mistakes - and get better.
NOTE: If you do decide to challenge yourself by finding and correcting code errors
or areas that you feel lack clarity, please contact us at [email protected] so
we can update the course. Any feedback is appreciated. This will benefit other
readers, and you will receive credit.
Why Python?
10
Introduction
Python is a beautiful language. It's easy to learn and fun, and its syntax (the rules) is
clear and concise. Python is a popular choice for beginners, yet still powerful enough to
to back some of the world’s most popular products and applications from companies like
NASA, Google, IBM, Cisco, Microsoft, Industrial Light & Magic, among others. Whatever
the goal, Python's design makes the programming experience feel almost as natural as
writing in English.
As you found out in the previous course, Python is used in a number disciplines, making
it extremely versatile. Such disciplines include:
1. System administration
2. 3D animation and image editing
3. Scientific computing/data analysis
4. Game development
5. Web development
With regard to web development, Python powers some of the world's most popular sites.
Reddit, the Washington Post, Instagram, Quora, Pinterest, Mozilla, Dropbox, Yelp, and
YouTube are all powered by Python.
Unlike Ruby, Python offers a plethora of frameworks from which to choose from,
including bottle.py, Flask, CherryPy, Pyramid, Django, and web2py, to name a few. This
freedom of choice allows you to decide which framework is best for your applications.
You can start with a lightweight framework (Flask, bottle.py) to get a project off the
ground quickly, adding complexity as your site grows. Such frameworks are great for
beginners who wish to learn the nuts and bolts that underlie web frameworks. Or if
you're building an enterprise-level application, the higher-level frameworks (Django,
web2py) bypass much of the monotony inherent in web development, enabling you to
get an application up quickly and efficiently.
Python 2 vs 3
When Python 3 was released back in 2008 there was very slow adoption of the new
version and even some backlash from the well established Python community. This has
lead to a bit of debate as to which version of Python to use/learn. Without a doubt,
Python 3 is the way forward. This course will use Python 3 exclusively, but that does not
mean Python 2 will be out of your reach. Most code written in 3, with a few minor
11
Introduction
tweaks, can also run in 2. Kenneth Love, an active contributor and educator in the
Python community as well as a friend of mine, feels the same. Check out his blog post
on the subject here.
Please be aware that learning both the Python language and web development at the
same time will be confusing. Spend at least a week going through the original course
before moving on to web development. Combined with this course, you will get up to
speed with Python and web development quickly and smoothly.
What's more, this book is built on the same principles of the original Real Python course:
We will cover the commands and techniques used in the vast majority of cases and
focus on how to program real-world solutions to problems that ordinary people actually
want to solve.
Each lesson contains conceptual information and hands-on, practical exercises meant to
reinforce the theory and concepts; many chapters also include homework assignments
to further reinforce the material and begin preparing you for the next chapter. A number
of videos are included as well, covering many of the exercises and homework
assignments.
Learning by Doing
Since the underlying philosophy is learning by doing, do just that; type each and every
code snippet presented to you.
12
Introduction
You will learn the concepts better and pick up the syntax faster if you type each line of
code out yourself. Plus, if you screw up - which will happen over and over again - the
simple act of correcting typos will help you learn how to debug your code.
The lessons work as follows: After we present the main theory, you will type out and
then run a small program. We will then provide feedback on how the program works,
focusing specifically on new concepts presented in the lesson.
Finish all review exercises and give each homework assignment and the larger
development projects a try on your own before getting help from outside resources. You
may struggle, but that is just part of the process. You will learn better that way. If you get
stuck and feel frustrated, take a break. Stretch. Re-adjust your seat. Go for a walk. Eat
something. Do a one-armed handstand. Ask for help if you get stuck for more than a few
hours. There is no need to waste time. If you continue to work on each chapter for as
long as it takes to at least finish the exercises, eventually something will click and
everything that seemed hard, confusing, and beyond comprehension will suddenly seem
easy.
With enough practice, you will learn this material - and hopefully have fun along the way!
Course Repository
Like the first course, this course has an accompanying repository. Broken up by chapter,
you can check your code against the code in the repository after you finish each chapter.
NOTE: You can download the course files directly from the repository. Press the
'Download ZIP' link to download the most recent version of the code as a zip
archive. Be sure to download the updated code for each release.
License
This e-book and course are copyrighted and licensed under a Creative Commons
Attribution-NonCommercial-NoDerivs 3.0 Unported License. This means that you are
welcome to share this book and use it for any non-commercial purposes so long as the
entire book remains intact and unaltered. That being said, if you have received this copy
for free and have found it helpful, I would very much appreciate it if you purchased a
copy of your own.
13
Introduction
The example Python scripts associated with this book should be considered open
content. This means that anyone is welcome to use any portion of the code for any
purpose.
Conventions
Formatting
Example code:
print("Hello world!")
Terminal commands:
$ python hello-world.py
File names:
hello-world.py.
Important terms:
Important term: This is an example of what an important term should look like.
14
Introduction
NOTE: This is a note filled in with bacon ipsum text. Bacon ipsum dolor sit amet t-
bone flank sirloin, shankle salami swine drumstick capicola doner porchetta
bresaola short loin. Rump ham hock bresaola chuck flank. Prosciutto beef ribs
kielbasa pork belly chicken tri-tip pork t-bone hamburger bresaola meatball.
Prosciutto pork belly tri-tip pancetta spare ribs salami, porchetta strip steak rump
beef filet mignon turducken tail pork chop. Shankle turducken spare ribs jerky
ribeye.
WARNING: This is a warning also filled in with bacon ipsum. Bacon ipsum dolor
sit amet t-bone flank sirloin, shankle salami swine drumstick capicola doner
porchetta bresaola short loin. Rump ham hock bresaola chuck flank. Prosciutto
beef ribs kielbasa pork belly chicken tri-tip pork t-bone hamburger bresaola
meatball. Prosciutto pork belly tri-tip pancetta spare ribs salami, porchetta strip
steak rump beef filet mignon turducken tail pork chop. Shankle turducken spare
ribs jerky ribeye.
SEE ALSO: This is a see also box with more tasty ipsum. Bacon ipsum dolor sit
amet t-bone flank sirloin, shankle salami swine drumstick capicola doner porchetta
bresaola short loin. Rump ham hock bresaola chuck flank. Prosciutto beef ribs
kielbasa pork belly chicken tri-tip pork t-bone hamburger bresaola meatball.
Prosciutto pork belly tri-tip pancetta spare ribs salami, porchetta strip steak rump
beef filet mignon turducken tail pork chop. Shankle turducken spare ribs jerky
ribeye.
Errata
We welcome ideas, suggestions, feedback, and the occasional rant. Did you find a topic
confusing? Or did you find an error in the text or code? Did we omit a topic you would
love to know more about. Whatever the reason, good or bad, please send in your
feedback.
You can find my contact information on the Real Python website. Or submit an issue on
the Real Python official support repository. Thank you!
NOTE: The code found in this course has been tested on Mac OS X v. 10.11.6,
Windows XP, Windows 7, Linux Mint 17, and Ubuntu 14.04 LTS.
15
Getting Started
Getting Started
Let's start with the basics...
16
Python Review
Python Review
Before we begin, you should already have Python installed on your machine. We will be
using Python 3.5.2 (the latest as of writing) for the majority of this course. If you do not
have Python installed, please refer to Appendix A for a basic tutorial.
To get the most out of this course, you must have at least an understanding of the basic
building blocks of the Python language:
Data Types
Numbers
Strings
Lists
Operators
Tuples
Dictionaries
Loops
Functions
Modules
Booleans
Again, if you are completely new to Python or just need a brief review, please start with
the original Real Python course.
17
Development Environments
Development Environments
Once Python is installed, take some time familiarizing yourself with the three
environments in which we will be using Python with: The command line, the Python
Shell, and an advanced Text Editor called Sublime Text. If you are already familiar with
these environments, and have Sublime installed, you can skip ahead to the lesson on
SQLite - "Installing SQLite".
For simplicity, all command line examples use the Unix-style prompt:
$ python3 big_snake.py
Windows equivalent:
18
Development Environments
Tips
1. Stop for a minute. Within the terminal, hit the UP arrow on your keyboard a few
times. The terminal saves a history - called the command history - of the
commands you've entered in the past. You can quickly re-run commands by
arrowing through them and pressing Enter when you see the command you want to
run again. You can do the same from the Python shell.
2. Tab can be used to auto-complete directory and file names. For example, if you're in
a directory that contains another directory called "directory_name", type CD then the
letter 'd' and then press Tab - the directory name will be auto-completed. Now just
press Enter to change into that directory. If there's more than one directory that
starts with a 'd' you will have to type more letters for the auto-complete to kick in.
For example, if you have a directory that contains the folders "directory_name" and
"downloads", you'd have to type cd di then tab for auto-complete to pick up on the
"directory_name" directory. Try this out. Use your new skills to create a directory.
Enter that directory. Create two more directories, naming them "directory_name"
and "downloads", respectively. Now test Tab auto-complete.
Both of these tips should save you a few thousand keystrokes in the long run. Practice.
Practice
1. Navigate to your "Desktop".
2. Make a directory called "test".
3. Enter the newly created directory.
4. Create a new directory within "test" called "test-two".
5. Move in one directory, and then create a new directory called "test-three".
6. Use touch to create a new file - touch test-file.txt .
7. Your directory structure should now look like this:
└── test
└── test-two
└── test-three
└── test-file.txt
8. Move out two directories. Where are you? You should be in the "test" directory,
correct?.
19
Development Environments
way.
11. Be sure to remove the "test" directory as well.
Questions
1. Open a new terminal/command line. Which directory are you in?
2. What's the fastest way to get to your root directory? We did not cover this. Turn to
Google if you need help.
3. How do you check to see the directories and files in the current directory?
4. How do you change directories? How do you create directories?
Try accessing the Shell through the terminal and print something out:
$ python3
>>> phrase = "The bigger the snake, the bigger the prey"
>>> print(phrase)
The bigger the snake, the bigger the prey
To exit the Shell from the terminal, press CTRL-Z + Enter within Windows, or CTRL-D
within Unix. You can also just type exit() then press enter:
>>> exit()
$
The Shell gives you an immediate response to the code you enter. It's great for testing,
but you can really only run one statement at a time. To write longer scripts, we will be
using a text editor called Sublime Text.
Sublime Text
Again, for much of this course, we will be using a basic yet powerful text editor built for
writing source code called Sublime Text. Like Python, it's cross-compatible with many
operating systems. Sublime works well with Python and offers excellent syntax
20
Development Environments
You can download the latest versions for Windows and Unix here. It's free to try for as
long as you like, although it will bug you a number of times a day to purchase a license.
Once downloaded, go ahead and open the editor. At first glance it may look just like any
other text editor. It's not. It can be used like that, but you can take advantage of all the
powerful built-in features it possesses along with the various packages used to extend
its functionality. There's way more to it than what meets the eye. But slow down. We
don't want to move too fast, so we'll start with the basics first and use it as a text editor.
Don't worry - you'll have plenty of time to learn all the cool features soon enough.
There are plenty of other free text editors that you can use such as Notepad++ for
Windows, TextWrangler for Mac, and the excellent, cross-platform editors Atom and
gedit. If you are familiar with a more powerful editor or IDE, feel free to use it. However,
we can't stress enough that you must be familiar with it before starting this course. Do
not try to take this course while also learning how to learn an IDE. You'll just add another
layer of complexity to the entire process.
Really, almost any text editor will do, however all examples in this course will be
completed using Sublime Text.
Python files must be indented with four spaces, not tabs. Most editors will allow you to
change the indentation to spaces. Go ahead and do this within your editor. Jump to this
link to see how to do this in Sublime. The example shows tab_size set to 2 spaces, but
be sure to set it to 4 for easy python coding.
WARNING: Never use a word processor like Microsoft Word as your text editor,
because text editors, unlike word processors, just display raw, plain text.
Homework
Create a directory using your terminal within your "Documents" or "My Documents"
directory called "RealPython". All the code from your exercises and homework
assignments will be saved in this directory.
To speed up your workflow, you should create an alias to this directory so you can
access it much quicker. To learn more about setting up aliases on a Mac, please
read this article.
21
Development Environments
22
SQLite
Installing SQLite
In a few chapters we will begin working with relational databases. You will be using the
SQLite database because it's simple to set up and great for beginners who need to learn
the SQL syntax. Python includes the SQLite library. We just need to install the SQLite
Database Browser:
Regardless of the operating system, you can download the SQLite Database Browser
from here. Installation for Windows and Mac OS X environments are relatively the same.
As for Linux, installation is again dependent upon which Linux flavor you are using.
Homework
Read Learn SQL Or Create A Simple Database With SQLite Database Browser to
learn the basics of the SQLite Database Browser.
23
pip
pip
pip is a package management system used for installing and managing Python
packages. In other words, it's used for installing third-party packages and libraries that
other Python developers create. For example, if you wanted to work with a third party
API like Twitter or Google Maps, you can save a lot of time by using a pre-built package,
rather than building all of the functionality yourself from scratch.
To uninstall a package:
NOTE: If you are in a Unix environment you may need to use sudo before each
command in order to execute it as the root user - sudo pip3 install numpy . You
will then have to enter your root password to install.
24
virtualenv
virtualenv
It's common practice to use a virtualenv (virtual environment) for your various projects,
which is used to create isolated Python environments (also called "sandboxes").
Essentially, when you work on one project, it will not affect any of your other projects.
It's absolutely essential to work in a virtualenv so that you can keep all of your Python
versions, libraries, packages, and dependencies separate from one another.
Some examples:
Python will work fine without virtualenv. But if you start working on a number of projects
with a number of different libraries installed, you will find virtualenv an absolute
necessity. Once you understand the concept behind it, virtualenv is easy to use. It will
save you time (and possibly prevent a huge headache) in the long run.
$ pyvenv-3.5 env
This created a new directory, "env", and set up a virtualenv within that directory.
Now you have a completely isolated environment, free from any previously installed
packages.
25
virtualenv
4. Now you just need to activate the virtualenv, enabling the isolated work
environment:
Unix:
$ source env/bin/activate
Windows:
$ env\scripts\activate
This changes the context of your terminal in that folder, "real-python-test", which
acts as the root of your system. You can tell when you're working in a virtualenv by
the directory surrounded by parentheses to the left of the path in your command-
line:
$ source env/bin/activate
(env)$
When you're done working, you can then exit the virtual environment using the
deactivate command. And when you're ready to develop again, simply navigate back
NOTE: You may see a lot of tutorials out there use the command pip and not
pip3 like we used above. When working inside of an activated virtual
26
Web Browsers
Web Browsers
We will be using Chrome for this course, because we will make use of a powerful, pre-
installed set of tools for web developers called Chrome Developer Tools. These tools
allow you to inspect and analyze various elements that make up a web page (or app).
You can view HTTP requests, test CSS style changes, and debug code, among others.
NOTE: Use the keyboard, save time! On a mac you can hit Cmd + Opt + I to
open the developers tools when inside a Chrome browser window. On Windows
and Linux use Ctrl + Shift + I .
27
Version Control
Version Control
Finally, to complete our development environment set up, we need to install a version
control system. Such systems allow you to save different "versions" of a project. Over
time, as your code and your project becomes bigger, it may become necessary to
backtrack to an earlier "version" to undo changes if a giant error occurs. It will happen
We will be using Git for version control and Github for remotely hosting our code.
Setting up Git
It's common practice to put projects under version control before you start developing by
creating storage areas called repositories (or repos).
This is an essential step in the development process. Again, such systems allow you to
easily track changes when your codebase is updated as well as reverted (or rolled back)
to earlier versions of your codebase in case of an error.
Take the time to learn how to use a version control system. It is a required skill for web
developers to have.
Start by downloading Git, if you don't already have it. Make sure to download the version
appropriate for your system. If you've never installed Git before you need to set your
global first name, last name, and email address. Open the terminal in Unix or the Git
Bash Shell in Windows (Start > All Programs > Git > Git Bash), then enter the following
commands:
We highly recommend reading Chapter 2 and 3 of the Git Book. Do not worry if you don't
understand everything, as long as you grasp the high-level concepts and workflow. You'll
learn better by doing, anyway.
Introduction to Github
Github is related to Git, but it is distinct. While you use Git to create and maintain local
repositories, Github is a collaborative, social network used to remotely host Git
repositories so-
28
Version Control
Your projects are safe from potential damages to your local machine,
You can show off your projects to potential employers (think of Github as your
online resume), and
Other users can collaborate on your project.
Get used to Github. You'll be using it a lot. It's the most popular place for programmers
to host their projects.
Congrats! You just set up a new repository on Github. Leave that page up while we
create a local repository using Git.
$ git init
Next add a file called README.md. It's convention to have such a file, stating the
purpose of your project, so that when added to Github, other users can get a quick
sense of what your project is all about.
$ touch README.md
Open that file in Sublime and just type "Hello, world! This is my first PUSH to Github."
Save the file. Now let's add the file to your local repo. First we need to take a "snapshot"
of the project in it's current state:
$ git add -A
This essentially adds all files that have either been created, updated, or deleted to a
place called "staging". Don't worry about what that means; just know that your project is
not part of the local repo yet.
29
Version Control
To add the project to your repo, you need to run the following command:
Sweet! Now your project has been committed (or added) to your local repo. Let's add it
to Github now.
Add a link to your remote repository. Return to Github. Copy the command to add your
remote repo, then paste it into your terminal:
Open your browser and refresh the Github page. You should now see the files from your
local repository on Github.
That's it!
Review
30
Version Control
Let's review.
Add the repo on Github. Run the following commands in your local directory:
$ git init
$ touch README.md
$ git add -A
$ git commit -m "some message"
$ git remote add origin https://github.com/<YOUR-USERNAME>/<YOUR-REPO-NAME>.git
$ git push origin master
Again, this creates the necessary files and pushes them to the remote repository on
Github.
Next, after your repo has been created locally and remotely - and you completed your
first PUSH - you can follow this similar workflow to PUSH as needed:
$ git add -A
$ git commit -am 'message'
$ git push origin master
NOTE: The string within the commit command should be replaced each time with
a description of the changes made in that commit.
That's it. With regard to Git, it's essential that you know how to:
Git Workflow
Let's review. You have one directory on your local computer called "RealPython". The
goal is to add all of our projects to that folder, and each of those individual folders will
have a separate Git repo that needs to also stay in sync with Github.
To do so, each time you make any major changes to a specific project, commit them
locally and then PUSH them to Github:
31
Version Control
$ git add -A
$ git commit -am "some message goes here about the commit"
$ git push origin master
Simple, right?
Next add a .gitignore file (no file extension!), which is a file that you can use to specify
files you wish to ignore or keep out of your repository, both local and remote. What files
should you keep out? If it's clutter (such as a .pyc file) or a secret (such as an API key),
then keep it out of your public repo on Github.
Please read more about .gitignore here. For now, add the files found here.
Your Setup
1. We have one directory, "RealPython", that will contain all of your projects.
2. Each of those project directories will contain a separate virtualenv, Git repo, and
.gitignore file.
3. Finally, we want to set up a requirements.txt file for each virtualenv. This file
contains a list of packages you've installed via pip. This is meant to be used by
other developers to recreate the same development environment. To do this, run
the following command when your virtualenv is activated:
NOTE: Although this is optional, we highly recommend setting up a RSA key for
use with Github so you don't have to enter your password each time you push
code to Github. Follow the instructions here.
Homework
Please read more about the basic Git commands here.
32
Interlude: Modern Web Development
Much of your learning will start from the ground up, so you will develop a deeper
understanding of web frameworks, allowing for quicker and more flexible web
development.
33
Front-end, Back-end, and Middleware
1. Front-end: The presentation layer. It's what the end user sees when interacting with
a web application. HTML defines the structure, while CSS provides a pretty facade,
and JavaScript provides user interaction. Essentially, this is what the end user sees
when interacting with your web application through a web browser. The front-end is
reliant on the application logic and data sources provided by the middleware and
back-end.
2. Middleware: This layer relays information between the front and back-end, in order
to:
NOTE: Don't worry if you don't understand all of this. We will be discussing each
of these technologies and how they fit into the whole throughout the course.
Developers adept in web architecture and in programming languages like Python, have
traditionally worked on the back-end and middleware layers, while designers and those
with HTML, CSS, and JavaScript skills, have focused on the front-end. These roles are
becoming less and less defined, especially in start-ups. Traditional back-end developers
are now also handling much of the front-end work. This is due to both a lack of quality
designers and the emergence of front-end CSS frameworks, like Bootstrap and
Foundation, which have significantly sped up the design process.
34
Model-View-Controller
Model-View-Controller (MVC)
Web frameworks reside just above those three layers, abstracting away much of the
processes that occur within each. There are pros and cons associated with this: It's great
for experienced web developers who understand the automation (or magic!) behind the
scenes.
This can be very confusing for a beginner, though - which reinforces the need to start
slow and work our way up.
Frameworks also separate the presentation (view) from the application logic (controller)
and the underlying data (model) in what’s commonly referred to as the Model-View-
Controller architecture pattern. While the front-end, back-end, and middleware layers
operate linearly, MVC generally operates in a triangular pattern:
We'll be covering this pattern numerous times throughout the remainder of this course.
Let's get to it!
35
Model-View-Controller
Homework
Read about the differences between a website (static) and a web application
(dynamic) here.
Want to learn more about MVC? Read Model-View-Controller (MVC) Explained --
With Legos
36
Flask: Quick Start
37
Overview
Overview
Flask grew from an elaborate April fool’s joke that mocked single file, micro frameworks
(most notably bottle.py) in 2010 into one of the most popular Python web frameworks in
use today. Small yet powerful, you can build your application from a single file, and, as it
grows, organically develop components to add functionality and complexity.
38
Installation
Installation
1. Within your "RealPython" directory create a new directly called "flask-hello-world".
2. Navigate into the directory and then create and activate a new virtualenv.
3. Now install Flask:
39
Hello World
Hello World
Because it's a well established convention, we’ll start with a quick "Hello World" example
in Flask. This serves two purposes:
This app will be contained entirely within a single file, app.py. Yes, it's that easy! Open
Sublime and within a new file add the following code:
$ python app.py
This launches the development server that's listening on port 5000. Open a web browser
and navigate to http://127.0.0.1:5000/. You should see the "Hello, World!" greeting.
40
Hello World
Test out the URL http://127.0.0.1:5000/hello as well. Once done, back in the terminal
press CTRL-C to stop the server.
app = Flask(__name__)
if __name__ == "__main__":
app.run()
1. We imported the Flask class from the flask library in order to create our web
app.
2. Next, an instance of the Flask class was created and assigned to the variable
app .
NOTE: Behind the scenes, the __name__ variable was set equal to "__main__" ,
indicating that we're running the statements in the current file (as a module) rather
than importing it. This conditional ensures that the app will only run if this file is
called from the command line (more on this later). Also, check out this link for
more information.
@app.route("/")
@app.route("/hello")
def hello_world():
return "Hello, World!"
41
Hello World
SEE ALSO: Want to learn more about decorators? Check out these two blog
posts - Primer on Python Decorators and Understanding Python Decorators in 12
Easy Steps!.
$ git add .
$ git commit -am "flask-hello-world"
$ git push origin master
From now on, I will remind you by just telling you to commit and PUSH to Github and the
end of each chapter. Make sure you remember to do it after each lesson, though!
42
App Flow
App Flow
Before moving on, let's pause for a minute and talk about the flow of the application from
the perspective of an end user:
The end user (you) requests to view a web page at the URL http://127.0.0.1:5000/hello.
The controller then handles the request determining what should be displayed back,
based on the URL that was entered, the functionality directly below the route definition,
as well as the requested HTTP method (more on this later):
@app.route("/hello")
def hello_world():
return "Hello, World!"
Since the function directly below the route definition simply returns the text "Hello,
World!", the controller can render the HTML as soon as the route handler is hit. In some
cases, depending on the request, the controller may need to grab data from a database
and perform necessary calculations on said data, before rendering the HTML. The
HTML is rendered and displayed to the end user:
Make sense? Don't worry if it's still a bit confusing. Just keep this flow in mind as you
develop more apps throughout this course. It will click soon enough.
43
Dynamic Routes
Dynamic Routes
Thus far we've only looked at static routes. Let's create something dynamic.
# dynamic route
@app.route("/test")
def search():
return "Hello"
Test it out.
NOTE: Whenever you update the code in your text editor, you must kill (CTRL+C)
and restart your server from the command line to see the changes in your
browser.
Now to make it dynamic first update the route to take a query parameter:
@app.route("/test/<search_query>")
Next, update the function so that it takes the query parameter as an argument that
simply returns it:
def search(search_query):
return search_query
Navigate to http://localhost:5000/test/hi. You should see "hi" on the page. Test it out with
some different URL parameters.
URLs are generally converted to a string, regardless of the parameter. For example, in
the URL http://localhost:5000/test/101, the parameter of 101 is converted into a string.
What if we wanted it treated as an integer though? Well, we can change how parameters
are treated with converters.
Flask converters:
44
Dynamic Routes
@app.route("/integer/<int:value>")
def int_type(value):
print(value + 1)
return "correct"
@app.route("/float/<float:value>")
def float_type(value):
print(value + 1)
return "correct"
First test - http://localhost:5000/integer/1. You should see the value 2 in your terminal.
Then try a string and a float. You should see a 404 error for each.
Second test - http://localhost:5000/float/1.1. You should see the value 2.1 in your
terminal. Both a string and an integer will return 404 errors.
45
Response Object
Response Object
Notice that in the response object ( return search_query ) we are only supplying text.
This object is actually a tuple that can take two more elements - the HTTP status code
and a dictionary of headers, both of which are optional:
If you do not explicitly define these, Flask will automatically assign a Status Code of 200
and a header where the Content-Type is "document" when a string is returned.
For example, navigate to this URL again, http://localhost:5000/test/hi. Next open Chrome
Developer Tools: Right click anywhere on the page, scroll down to "Inspect Element.",
click the "Network" tab within Developer Tools.
Notice the 200 Status Code and Content-Type. That was the expected behavior.
46
Response Object
@app.route("/name/<name>")
def index(name):
if name.lower() == "michael" :
return "Hello, {}".format(name), 200
else :
return "Not Found", 404
Here, the route can take an optional parameter of name, and the response object is
dependent on the assigned value. We also explicitly assigned a status code. With
Developer Tools open, try the URL http://localhost:5000/name/michael. Watch the
response. Then remove "michael" and try any other parameter. You should see the 404
status code. Finally, test the first URL out again, but this time, update the response
object to:
Same response as before, right? 200 status code and Content-Type of "document".
Flask is smart: It inferred the correct Status Code and Content-Type based on the
response type. Magic!
NOTE: Although, the Status Code can be implicitly generated by Flask, it's a
common convention to explicitly define it for RESTful APIs since client-side
behavior is often dependent on the status code returned. In other words, instead
of letting Flask guess the status code, define it yourself to make certain that it is
correct. Check out more on status codes here.
47
Debug Mode
Debug Mode
Flask provides helpful error messages and prints stack traces directly in the browser,
making debugging much easier. To enable these features along with automatic reload
simply add the following to the app.py file:
app.config["DEBUG"] = True
# error handling
app.config["DEBUG"] = True
# dynamic route
@app.route("/test/<search_query>")
def search(search_query):
return search_query
48
Debug Mode
After you save your file, manually restart your server to start seeing the automatic
reloads. Check your terminal, you should see:
Essentially, any time you make changes to the code and save, they will be auto loaded.
You do not have to restart your server to refresh the changes; you just need to refresh
your browser.
Save your code. Refresh the browser. You should see the new string.
We'll look at error handling in a later chapter. Until then, make sure you commit and
PUSH your code to Github.
SEE ALSO: Want to use a different debugger? See Working with Debuggers.
49
Interlude: Database Programming
Nearly every web application has to store data. Without data most web applications
would provide little, if any, value. Think of your favorite web app. Now imagine if it
contained no data. Take Twitter for example: Without data (in the form of Tweets),
Twitter would be relatively useless.
One of the most common methods of storing (or persisting) information is to use a
relational database. In this chapter, we'll look at the basics of relational databases as
well as SQL (Structured Query Language).
Databases
Databases help organize and store data. It's like a digital filing cabinet, and just like a
filing cabinet, we can retrieve, add, update, and/or remove data from it. Sticking with that
metaphor, databases contain tables, which are like file folders, storing similar
information. While folders contain individual documents, database tables contain rows of
data.
50
SQL and SQLite Basics
Records from one table can be linked to records in another table to create relationships.
More on this later.
Most relational databases use the SQL language to communicate with the database.
SQL is a fairly easy language to learn, and one worth learning. The goal here is to
provide you with a high-level overview of SQL to get you started. To achieve the goals of
the course, you need to understand the four basic SQL commands: SELECT, INSERT,
UPDATE, and DELETE.
Command Action
Although SQL is a simple language, you will find an even easier way to interact with
databases called Object Relational Mapping, which will be covered in future chapters. In
essence, instead of working with SQL directly, you work with Python objects, which
51
SQL and SQLite Basics
many Python programmers are more comfortable with, that abstract out the SQL
language. We'll cover these methods in later chapters. For now, we'll cover SQL, as it's
important to understand how SQL works for when you have to troubleshoot or conduct
difficult queries that require SQL.
Numerous libraries and modules are available for connecting to relational database
management systems. Such systems include SQLite, MySQL, PostgreSQL, Microsoft
Access, SQL Server, and Oracle. Since the language is relatively the same across these
systems, choosing the one which best suits the needs of your application depends on
the application’s current and expected size. In this chapter, we will focus on SQLite,
which is ideal for simple applications.
SQLite is great. It gives you most of the database structure of the larger, more powerful
relational database systems without having to actually use a server. Again, it is ideal for
simple applications as well as for testing out code. Lightweight and fast, SQLite requires
little administration. It's also already included in the Python standard library. Thus, you
can literally start creating and accessing databases without downloading any additional
dependencies.
Homework
Spend thirty minutes reading more about the basic SQL commands highlighted
above from the official SQLite documentation. If you have time, check out
W3schools.com's Basic SQL Tutorial as well. This will set the basic ground work for
the rest of the chapter.
Also, if you have access to the first Real Python course, go through chapter 13
again.
52
Creating Tables
Creating Tables
Let's begin. Make sure you've created a "sql" directory within then "RealPython"
directory. Create and activate your virtualenv as well as a Git repo.
Use the Create Table statement to create a new table. Here is the basic format:
# create a table
cursor.execute("""CREATE TABLE population
(city TEXT, state TEXT, population INT)
""")
Save the file as 01_sql.py. Run the file from your terminal:
$ python 01_sql.py
As long as error wasn't thrown, the database and table were created inside a new file
called new.db. You can verify this by launching the SQLite Database Browser and then
opening up the newly created database, which will be located in the same directory
53
Creating Tables
where you saved the file. Under the "Database Structure" tab you should see the
"population" table. You can then expand the table and see the "city", "state", and
"population" fields (also called table columns):
54
Creating Tables
NOTE: You can also use the ":memory:" string to create a database in memory
only:
conn = sqlite3.connect(":memory:")
Keep in mind, though, that as soon as you close the connection the database will
disappear.
No matter what you are trying to accomplish, you will usually follow this basic workflow:
What happens next depends on your end goal. You may insert new data (INSERT),
modify (UPDATE) or delete (DELETE) current data, or simply extract data in order to
output it the screen or conduct analysis (SELECT). Go back and look at the SQL
statements, from the beginning of the chapter, for a basic review.
NOTE: You can delete a table by using the DROP TABLE command plus the table
name you'd like to drop - i.e., DROP TABLE table_name . This of course deletes the
table and all the data associated with that table. Use with caution.
Homework
Create a new database called "cars", and add a table called "inventory" that
includes the following fields: "Make", "Model", and "Quantity". Don't forget to include
the proper data-types.
55
Inserting Data
Inserting Data
Now that we have a table created, we can populate it with some actual data by adding
new rows to the table:
# INSERT Command
# insert data
cursor.execute("INSERT INTO population VALUES('New York City', \
'NY', 8400000)")
cursor.execute("INSERT INTO population VALUES('San Francisco', \
'CA', 800000)")
Save the file as 02_sql.py and then run it. Again, if you did not receive an error, then you
can assume the code ran correctly. Open up the SQLite Database Browser again to
ensure that the data was added. After you load the database, click the second tab,
"browse data", and you should see the new values that were inserted.
1. As in the example from the previous lesson, we imported the sqlite3 library,
established the database connection, and created the cursor object.
2. We then used the INSERT INTO SQL command to insert data into the "population"
table. Note how each item (except the integers) has single quotes around it, while
the entire statement is enclosed in double quotes. Many relational databases only
allow objects to be enclosed in single quotes. This can get a bit more complicated
when you have items that include single quotes in them. There is a workaround,
though - the executemany() method which you will see in the next example.
56
Inserting Data
3. The commit() method executes the SQL statements and inserts the data into the
table. Anytime you make changes to a table via the INSERT, UPDATE, or DELETE
commands, you need to run the commit() method before you close the database
connection. Otherwise, the values will only persist temporarily in memory.
That being said, if you rewrite your script using the with keyword, your changes will
automatically be saved without having to use the commit() method, making your code
more compact.
Let's look at the above code re-written using the with keyword:
import sqlite3
with sqlite3.connect("new.db") as connection:
c = connection.cursor()
c.execute("INSERT INTO population VALUES('New York City', \
'NY', 8400000)")
c.execute("INSERT INTO population VALUES('San Francisco', \
'CA', 800000)")
# executemany() method
import sqlite3
57
Inserting Data
Save the file as 03_sql.py then run it. Double check your work in the SQLite Database
Browser that the values were added.
Essentially, a SQL injection is a fancy term for when a user supplies a value that looks
like SQL code but really causes the SQL statement to behave in unexpected ways.
Whether accidental or malicious in intent, the statement fools the database into thinking
it's a real SQL statement. In some cases, a SQL injection can reveal sensitive
information or even damage or destroy the database. Be careful.
For this exercise add the employees.csv file, from the course repository, to your "sql"
directory (or the directory that contains your script).
import sqlite3
58
Inserting Data
Run the file. Now if you look in SQLite, you should see a new table called "employees"
with 20 rows of data in it.
Try/Except
Remember this statement from above? "If you did not receive an error, then you can
assume the code ran correctly." Well, what happens if you did see an error? We want to
handle it gracefully. Let's refactor the code using Try/Except:
try:
# insert data
cursor.execute("INSERT INTO populations VALUES('New York City', 'NY', 8400000)"
)
cursor.execute("INSERT INTO populations VALUES('San Francisco', 'CA', 800000)"
)
Notice how we intentionally named the table "populations" instead of "population". Oops.
Any idea how you could throw an exception, but also provide the user with relevant
information about how to correct the issue? Google it!
59
Searching
Searching
Let's now look at how to retrieve data:
# SELECT statement
import sqlite3
# use a for loop to iterate through the database, printing the results line by
line
for row in c.execute("SELECT firstname, lastname from employees"):
print(row)
Notice the u character in the output. This just stands for a Unicode string. Unicode is
an international character encoding standard for displaying characters. This outputted
because we printed the entire string rather than just the values.
Let's look at how to remove the unicode characters so we can output just the values:
import sqlite3
1. First, the fetchall() method retrieved all records from the query and stored them
as a list of tuples.
2. We then assigned the records to the "rows" variable.
60
Searching
61
Updating and Deleting
import sqlite3
# update data
c.execute("UPDATE population SET population = 9000000 WHERE city='New York Cit
y'")
# delete data
c.execute("DELETE FROM population WHERE city='Boston'")
print("\nNEW DATA:\n")
rows = c.fetchall()
for r in rows:
print(r[0], r[1], r[2])
1. We used the UPDATE command to change a specific field from a record and the
DELETE command to delete an entire record.
2. We then displayed the results using the SELECT command.
3. We also introduced the WHERE clause, which is used to filter the results by a
certain characteristic. You can also use this clause with the SELECT statement.
For example:
This statement searches the database for cities where the state is CA. All other
states are excluded from the query.
Homework
62
Updating and Deleting
We covered a lot of material in the past few lessons. Be sure to go over it as many times
as necessary before moving on.
Using the "inventory" table from the previous homework assignment, add ( INSERT )
5 records (rows of data) to the table. Make sure 3 of the vehicles are Fords while
the other 2 are Hondas. Use any model and quantity for each.
solution
Update the quantity on two of the records, and then output all of the records from
the table.
solution
Finally output only records that are for Ford vehicles.
solution
NOTE: If you have trouble the first time through with these assignments, don't go
straight to the solution. Give yourself time to think and try again...and again...and
again. This is the path to learning how to code.
63
Working with Multiple Tables
# executemany() method
import sqlite3
rows = c.fetchall()
for r in rows:
print(r[0], r[1], r[2])
Did you notice the WHERE clause again? In this example, we chose to limit the results
by only outputting cities with populations greater than one million.
64
Working with Multiple Tables
import sqlite3
rows = c.fetchall()
for r in rows:
print(r[0], r[1])
We created a new table called "regions" that displayed the same cities with their
respective regions. Notice how we used the ORDER BY clause in the SELECT
statement to display the data in ascending order by region.
65
Working with Multiple Tables
Open up the SQLite Browser to double check that the new table was in fact created and
populated with data.
SQL Joins
The real power of relational tables comes from the ability to link data from two or more
tables. This is achieved by using the JOIN command.
Let's write some code that will use data from both the "population" and the "regions"
tables.
Code:
import sqlite3
# retrieve data
c.execute("""SELECT population.city, population.population,
regions.region FROM population, regions
WHERE population.city = regions.city""")
rows = c.fetchall()
for r in rows:
print(r[0], r[1], r[2])
1. Since we are using two tables, fields in the SELECT statement must adhere to the
following format: table_name.column_name (i.e., population.city ).
2. In addition, to eliminate duplicates, as both tables include the city name, we used
the WHERE clause as seen above.
Finally, let's organize the outputted results and clean up the code so it's more compact:
66
Working with Multiple Tables
import sqlite3
rows = c.fetchall()
for r in rows:
print("City: " + r[0])
print("Population: " + str(r[1]))
print("Region: " + r[2])
print("")
Homework
Add another table to accompany your "inventory" table called "orders". This table
should have the following fields: "make", "model", and "order_date". Make sure to
only include makes and models for the cars found in the inventory table. Add 15
records (3 for each car), each with a separate order date (YYYY-MM-DD).
solution
Finally output the car's make and model on one line, the quantity on another line,
and then the order_dates on subsequent lines below that.
solution
67
SQL Functions
SQL Functions
SQLite has many built-in functions for aggregating and calculating data returned from a
SELECT statement.
Function Result
# SQLite Functions
import sqlite3
# run sql
c.execute(values)
1. Essentially, we created a dictionary of SQL statements and then looped through the
68
SQL Functions
Homework
Using the COUNT() function, calculate the total number of orders for each make and
model.
solution
Output the car's make and model on one line, the quantity on another line, and then
the order count on the next line. The latter is a bit difficult, but please try it first
before looking at my answer. Remember: Google-it-first!
solution
69
Example Application
Example Application
We're going to end our discussion of the basic SQL commands by looking at an
extended example. Please try the assignment first before reading the solution. The
hardest part will be breaking it down into small, manageable bites. You've already
learned the material; we're just putting it all together. Spend some time drawing out the
workflow as a first step before writing any code.
Criteria:
1. Add 100 random integers, ranging from 0 to 100, to a new database called
newnum.db.
2. Prompt the user whether they would like to perform an aggregation (AVG, MAX,
MIN, or SUM) or exit the program altogether.
Break this assignment into two scripts. Name them sql-insert.py and sql-search.py.
Now stop for a minute and think about how you would set this up. Take out a piece of
paper and actually write it out. Create a box for the first script and another box for the
second. Write the criteria at the top of the page, and then begin by writing out exactly
what the program should do in plain English in each box. These sentences will become
the comments for your program.
First Script
Import libraries (we need the random library because of the random variable piece):
import sqlite3
import random
c = connection.cursor()
70
Example Application
Create table called "numbers" with value "num" as an integer (the DROP TABLE
command will remove the entire table if it exists so we can create a new one):
Use a for loop and the random.randint() method to insert 100 random values from 0
to 100:
for i in range(100):
c.execute("INSERT INTO numbers VALUES(?)",(random.randint(0,100),))
Full Code:
Second Script
Again, start with writing out the steps in plain English.
71
Example Application
run that function. However, if they enter number not associated with a function, ask
them to enter another number. If they enter the number 5, break the loop and exit
the program.
Clearly, step 4 needs to be broken up into multiple steps. Do that before you start writing
any code.
Good luck!
Code:
72
Example Application
prompt = """
Select the operation that you want to perform [1-5]:
1. Average
2. Max
3. Min
4. Sum
5. Exit
"""
# retrieve data
cursor.execute("SELECT {}(num) from numbers".format(operation))
# if user enters 5
elif x == "5":
print("Exit")
# exit loop
break
73
Example Application
We asked the user to enter the operation they would like to perform (numbers 1 to 4),
which queried the database and displayed either the average, minimum, maximum or
sum (depending on the operation chosen). The loop continues forever until the user
chooses 5 to break the loop.
74
SQL Summary
SQL Summary
Basic SQL syntax...
Insert
Update
UPDATE table_name
SET column1=value1
WHERE some_column=some_value;
Delete
75
Chapter Summary
Chapter Summary
This chapter provided a brief summary of SQLite and how to use Python to interact with
relational databases. There's a lot more you can do with databases that isn't covered
here. If you'd like to explore relational databases further, there are a number of great
resources online, like ZetCode and tutorialspoint's Python MySQL Database Access.
Also, did you remember to commit and push to Github after each lesson?
76
Flask Blog App
Requirements:
1. After a user logs in they are presented with all of the blog posts.
2. Users can add new, text-only blog entries from the same screen, read the entries
themselves, or log out.
That's it.
77
Project Structure
Project Structure
1. Within your "realpython" directory create a "flask-blog" directory.
2. Create and activate a virtualenv.
3. Make sure to place your app under version control by adding a Git repo.
4. Set up the following files and directories:
├── blog.py
├── static
│ ├── css
│ ├── img
│ └── js
└── templates
First, all of the logic (Python/Flask code) goes in the blog.py file. The "static"
directory holds static files (anything that is not dynamic) like JavaScript files, CSS
stylesheets, and images. Finally, the "template" folder houses all of our HTML files.
The important thing to note is that the blog.py acts as the application controller.
Flask works with a client-server model. The server receives HTTP requests from the
client (the web browser), then returns content back to the client in the form of a
response:
78
Project Structure
NOTE: HTTP is the method used for most web-based communications; the
'http://' (or 'https://') that prefixes URLs designates an HTTP request. Literally
everything you see in your browser is transferred to you via HTTP.
5. Install flask:
79
Model
Model
Our database has one table called posts with two fields - title and post. We can use the
following script to create and populate the database:
import sqlite3
Save the file within your "flask-blog" directory as sql.py. Run it. This reads the database
definition provided in sql.py and then creates the actual database schema and adds a
number of entries. Check the SQLite Browser to ensure the table was created correctly
and populated with data. Notice how we escaped the apostrophes in the INSERT
statements.
80
Controller
Controller
Like the controller in the Quick Start app (app.py), this script will define the imports,
configurations, and each view.
# blog.py - controller
# imports
from flask import Flask, render_template, request, session, \
flash, redirect, url_for, g
import sqlite3
# configuration
DATABASE = 'blog.db'
app = Flask(__name__)
if __name__ == '__main__':
app.run(debug=True)
# configuration
DATABASE = 'blog.db'
...snip...
81
Controller
that are defined using ALL CAPITAL LETTERS. So by defining DATABASE in blog.py we
know that the Flask will find it and add it to our configuration for this app.
More information can be found in the configuration section, which is used for defining
application-specific settings.
NOTE: Stop. You aren't cheating and using copy and paste - are you?
82
Views
Views
After a user logs in, they are redirected to the main blog homepage where all posts are
displayed. Users can also add posts from this page. For now, let's get the page set up,
and worry about the functionality later.
{% extends "template.html" %}
{% block content %}
<h2>Welcome to the Flask Blog!</h2>
{% endblock %}
{% extends "template.html" %}
{% block content %}
<h2>Welcome to the Flask Blog!</h2>
<h3>Please log in to access your blog.</h3>
<p>Temp Log in: <a href="/main">Log in</a></p>
{% endblock %}
Save these files as main.html and login.html, respectively, in the "templates" directory. I
know you have questions about the strange syntax - i.e., {% extends "template.html" %}
- in both these files. We'll get to that in just a second.
Now update blog.py by adding two new functions for the views:
@app.route('/')
def login():
return render_template('login.html')
@app.route('/main')
def main():
return render_template('main.html')
Updated code:
83
Views
# blog.py - controller
# imports
from flask import Flask, render_template, request, session, \
flash, redirect, url_for, g
import sqlite3
# configuration
DATABASE = 'blog.db'
app = Flask(__name__)
@app.route('/')
def login():
return render_template('login.html')
@app.route('/main')
def main():
return render_template('main.html')
if __name__ == '__main__':
app.run(debug=True)
In the first function, login() , we mapped the URL / to the function, which in turn sets
the route to login.html in the templates directory. How about the main() function?
What's going on there? Explain it to yourself by saying it out loud.
84
Templates
Templates
Templates are HTML skeletons that serve as the base for either your entire app or
pieces of your app. They eliminate the need to code the basic HTML structure more than
once. Separating templates from the main business logic (blog.py) helps with the overall
organization.
<!DOCTYPE html>
<html>
<head>
<title>Welcome, friends!</title>
</head>
<body>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
There's a relationship between the parent, template.html, and child templates, login.html
and main.html. This relationship is called template inheritance. Essentially, the child
templates extend, or are a child of, template.html. This is achieved by using the {%
extends "template.html" %} tag. This tag establishes the relationship between the
So, when Flask renders main.html it must first render template.html. And then renders
main.html inside of it.
Notice that both the parent and child template files have identical block tags: {% block
content %} and {% endblock %} . These define where the child templates, login.html and
main.html, are filled in on the parent template. When Flask renders the parent template,
template.html, the block tags are filled in with the code from the child templates:
85
Templates
Code found between the {% %} tags in the templates is a subset of Python used for
basic expressions, like for loops or conditional statements. Meanwhile, variables, or
the results from expressions, are surrounded by {{ }} tags.
SEE ALSO: The are a number of different templating formats. Flask by default uses
Jinja2 as its templating engine. Read more about Jinja2 templating from this blog post.
86
Sanity Check
Sanity Check
Ready? Fire up your server ( python blog.py ), navigate to http://localhost:5000/, and
let's run a test to make sure everything is working up to this point.
You should see the login page, and then if you click the link, you should be directed to
the main page. If not, kill the server. Make sure all your files are saved. Try again. If you
are still having problems, double-check your code against the above code snippets.
87
User Login
User Login
Now that we have the basic structure set up, let's have some fun and add the blog's
main functionality. Starting with the login page, set up a basic HTML form for users to log
in to so that they can access the main blog page.
Add the following username and password variables to the configuration section of
blog.py:
USERNAME = 'admin'
PASSWORD = 'admin'
Also in the configuration section, add the SECRET_KEY , which is used for managing user
sessions:
SECRET_KEY = 'hard_to_guess'
WARNING: Make the value of your secret key really, really hard, if not impossible,
to guess. Use a random key generator to do this. Never, ever use a value you pick
on your own. Or you can use your OS to get a random string:
>>> import os
>>> os.urandom(24)
'rM\xb1\xdc\x12o\xd6i\xff+9$T\x8e\xec\x00\x13\x82.*\x16TG\xbd'
Now you can simply assign that string to the secret key: SECRET_KEY =
rM\xb1\xdc\x12o\xd6i\xff+9$T\x8e\xec\x00\x13\x82.*\x16TG\xbd
# configuration
DATABASE = 'blog.db'
USERNAME = 'admin'
PASSWORD = 'admin'
SECRET_KEY = 'hard_to_guess'
Update the login() function in the blog.py file to match the following code:
88
User Login
This function compares the username and password entered against those from the
configuration section. If the correct username and password are entered, the user is
redirected to the main page and the session key, logged_in , is set to True . If the
wrong information is entered, an error message is flashed to the user.
NOTE: Notice how we had to specify a POST request. By default, routes are set
up automatically to handle GET requests. If you need to add different HTTP
methods, such as a POST, you must add the methods argument to the decorator.
For example:
89
User Login
Looking back at the code from above, walk through this function, login() , line by line,
saying out loud what each line accomplishes.
{% extends "template.html" %}
{% block content %}
<div class="login-wrapper">
<h2>Welcome to the Flask Blog!</h2>
<h3>Please log in to access your blog.</h3>
<form action="" method="post">
<div>
<label for="name">Username:</label>
<input type="text" id="username" name="username" value="{{
request.form.username }}">
</div>
<div>
<label for="mail">Password:</label>
<input type="password" id="password" name="password" value="{{
request.form.password }}"/>
</div>
<div>
<button type="submit">Log In</button>
</div>
</form>
</div>
{% endblock %}
If you are unfamiliar with how HTML forms operate, please visit this link.
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('login'))
The logout() function uses the pop() function to reset the session key to the default
value when the user logs out. The user is then redirected back to the login screen and a
message is flashed indicating that they were logged out.
90
User Login
NOTE: The pop() function used here is defined within the session class. It is
not the pop() function native to python that is used on lists.
Add the following code to the template.html file, just before the content tag, {% block
content %} -
<!DOCTYPE html>
<html>
<head>
<title>Welcome, friends!</title>
</head>
<body>
<div class="container">
{% for message in get_flashed_messages() %}
<div class="flash">{{ message }}</div>
{% endfor %}
{% if error %}
<p class="error"><strong>Error:</strong> {{ error }}</p>
{% endif %}
<!-- inheritance -->
{% block content %}
{% endblock %}
<!-- end inheritance -->
</div>
</body>
</html>
{% extends "template.html" %}
{% block content %}
<h2>Welcome to the Flask Blog!</h2>
<p><a href="{{ url_for('logout') }}">Log out</a></p>
{% endblock %}
View it! Fire up the server. Manually test everything out. Make sure you can log in and
log out and that the appropriate messages are displayed, depending on the situation.
91
User Login
92
Sessions
Sessions
Now that users are able to log in and log out, we need to protect main.html from
unauthorized access. Currently, it can be accessed without logging in. Go ahead and
see for yourself: Launch the server and point your browser at http://localhost:5000/main.
See what I mean?
Sessions store user information in a secure manner, usually as a token, within a client-
side cookie. In this case, when the session key, logged_in , is set to True , the user has
the rights to view the main.html page. Go back and take a look at the login() function
so you can see this logic.
The login_required decorator, meanwhile, checks to make sure that a user is authorized
before allowing access to certain pages. To implement this, we will set up a new function
which will be used to restrict access to main.html.
Functools is a module used for extending the capabilities of functions with other
functions, which is exactly what decorators accomplish.
def login_required(test):
@wraps(test)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return test(*args, **kwargs)
else:
flash('You need to log in first.')
return redirect(url_for('login'))
return wrap
This checks to see if logged_in is in the session. If it is, then we call the appropriate
function (e.g., the function that the decorator is applied to), and if not, the user is
redirected back to the login screen with a message stating that a log in is required.
93
Sessions
@app.route('/main')
@login_required
def main():
return render_template('main.html')
Updated code:
# blog.py - controller
# imports
from flask import Flask, render_template, request, session, \
flash, redirect, url_for, g
import sqlite3
from functools import wraps
# configuration
DATABASE = 'blog.db'
USERNAME = 'admin'
PASSWORD = 'admin'
SECRET_KEY = 'hard_to_guess'
app = Flask(__name__)
def login_required(test):
@wraps(test)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return test(*args, **kwargs)
else:
flash('You need to log in first.')
return redirect(url_for('login'))
return wrap
94
Sessions
request.form['password'] != app.config['PASSWORD']:
error = 'Invalid Credentials. Please try again.'
status_code = 401
else:
session['logged_in'] = True
return redirect(url_for('main'))
return render_template('login.html', error=error), status_code
@app.route('/main')
@login_required
def main():
return render_template('main.html')
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('login'))
if __name__ == '__main__':
app.run(debug=True)
When a GET request is sent to access main.html to view the HTML, it first hits the
@login_required decorator and the entire function, main() , is momentarily replaced (or
wrapped) by the login_required() function. Then when the user is logged in, the
main() function is invoked, allowing the user to access main.html. If the user is not
Test this out. But first, did you notice in the terminal that you can see the client requests
as well as the server responses? After you perform each test, check the server
responses:
1. Log in successful:
Here, the credentials were sent with the POST request, and the server responded
with a 302, redirecting the user to main.html. The GET request sent to access
main.html was successful, as the server responded with a 200.
Once logged in, the session token is stored in a client-side cookie. You can view
this token in your browser by opening up Developer Tools in Chrome, clicking the
"Application" tab, then in the side bar underneath the "Storage" heading click on
cookies. You should see this:
95
Sessions
2. Log out:
When you log out, a GET request is sent and the subsequent response redirects
you back to login.html. Again, this request was successful.
3. Log in failed:
If you enter the wrong credentials you get a 401 ('not authorized') because that is
what we told the login method to return on a failed login attempt. You might want to
go back up and look at the login route to re-examine how this is working.
Here we got a 302 which means redirect and a 200 because we hit the login page
successfully.
The server stack trace comes in handy when you need to debug your code. Let's say
you forgot to add the redirect to the login() function, return
redirect(url_for('main')) . If you glance at your code and can't figure out what's going
96
Sessions
You can see that the POST request was successful, but nothing happened after. This
should give you enough of a hint to know what to do. This is a rather simple case, but
you will find that when your codebase grows just how handy the server log can be when
debugging.
97
Show Posts
Show Posts
Now that basic security is set up, we need to display some information to the user. Start
by displaying the current posts.
@app.route('/main')
@login_required
def main():
g.db = connect_db()
cur = g.db.execute('select * from posts')
posts = [dict(title=row[0], post=row[1]) for row in cur.fetchall()]
g.db.close()
return render_template('main.html', posts=posts)
2. cur = g.db.execute('select * from posts') then fetches data from the posts table
array of dictionaries. Each dictionary contains the data from a row retrieved from the
posts table. This array is assigned to the variable posts .
4. posts=posts passes that variable to the main.html file.
Edit main.html to loop through the dictionary in order to display the titles and posts:
98
Show Posts
{% extends "template.html" %}
{% block content %}
<h2>Welcome to the Flask Blog!</h2>
<p><a href="{{ url_for('logout') }}">Log out</a></p>
<br/>
<br/>
<h3>Posts:</h3>
{% for p in posts %}
<strong>Title:</strong> {{ p.title }} <br/>
<strong>Post:</strong> {{ p.post }} <br/>
<br/>
{% endfor %}
{% endblock %}
99
Add Posts
Add Posts
Finally, users need the ability to add new posts. We can start by adding a new function
to blog.py called add() :
@app.route('/add', methods=['POST'])
@login_required
def add():
title = request.form['title']
post = request.form['post']
if not title or not post:
flash("All fields are required. Please try again.")
return redirect(url_for('main'))
else:
g.db = connect_db()
g.db.execute('insert into posts (title, post) values (?, ?)',
[request.form['title'], request.form['post']])
g.db.commit()
g.db.close()
flash('New entry was successfully posted!')
return redirect(url_for('main'))
First, we used an if statement to ensure that all fields are populated with data. Then,
the data is added, as a new row, to the database table.
100
Add Posts
{% extends "template.html" %}
{% block content %}
<h2>Welcome to the Flask Blog!</h2>
<p><a href="{{ url_for('logout') }}">Log out</a></p>
<h3>Add a new post:</h3>
<form action="{{ url_for('add') }}" method="post" class="add">
<div>
<label>Title:</label>
<input name="title" type="text">
</div>
<div>
<label>Post:</label>
<textarea name="post" rows="5" cols="40"></textarea>
</div>
<div class="">
<input class="button" type="submit" value="Save">
</div>
</form>
<br>
<br>
<h3>Posts:</h3>
{% for p in posts %}
<strong>Title:</strong> {{ p.title }} <br>
<strong>Post:</strong> {{ p.post }} <br>
<br>
{% endfor %}
{% endblock %}
We sent an HTTP POST request when the form was submitted, which was handled on
the server-side by the add() view function. This, in turn, redirected us back to
main.html with the new post:
101
Style
Style
Now that the app is working properly, let's make it look a bit nicer. To do this, we can edit
the HTML and CSS. We will be covering HTML and CSS in more depth in a later
chapter. If you are unfamiliar with either of them, just follow along for now.
.container {
background: #f4f4f4;
margin: 2em auto;
padding: 0.8em;
width: 30em;
border: 2px solid #000;
}
.login-wrapper {
text-align: center;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.login-wrapper label {
display: inline-block;
}
input {
margin-bottom: 10px;
}
button {
padding: 5px;
margin: 10px 0;
}
.flash, .error {
background: #000;
color: #fff;
padding: 0.5em;
}
102
Style
Save this as styles.css and place it in your "static/css" directory. Then add a link to the
external stylesheet within the head, <head> </head> , of the template.html file:
<link rel="stylesheet"
href="{{ url_for('static', filename='css/styles.css') }}">
This tag is fairly straightforward. Essentially, the url_for() function generates a URL to
the styles.css file, saying, "Look in the static folder for styles.css".
Feel free to play around with the CSS more if you'd like. If you do, send us the CSS, so
we can make it look better.
103
Conclusion
Conclusion
Let's recap:
Make sure to commit your code to Git and then push to Github!
104
Interlude: Debugging in Python
Always keep in mind that while pdb's primary purpose is debugging code, it's more
important that you understand what's happening in your code while debugging. This in
itself will help with debugging.
105
Workflow
Workflow
Let's look at a simple example.
import sys
from random import choice
while True:
print("To exit this game type 'exit'")
answer = input("What is {} times {}? ".format(
choice(random2), choice(random1)))
# exit
if answer == "exit":
print("Now exiting game!")
sys.exit()
Run it. See the problem? There's either an issue with the multiplication or the logic within
the if statement.
Let's debug!
import pdb
Next, add pdb.set_trace() within the while loop to set your first breakpoint:
106
Workflow
import sys
import pdb
from random import choice
while True:
print("To exit this game type 'exit'")
pdb.set_trace()
# exit
if answer == "exit":
print("Now exiting game!")
sys.exit()
When you run the code you should see the following output:
$ python pdb_ex.py
To exit this game type 'exit'
> /debugging/pdb_ex.py(13)<module>()
-> answer = input("What is {} times {}? ".format(
(Pdb)
Essentially, when the Python interpreter runs pdb.set_trace() , execution stops, and
you'll see the next line in the program as well as a prompt waiting for input.
From here you step through your code to see what happens, line by line. You have
access to a number of commands, which can be daunting at first, but on a day-to-day
basis you'll only use a few of them:
l : displays the entire program along with where the current break point is
107
Workflow
NOTE If you don't remember the list of commands you can always type ? or
help to see the entire list.
(Pdb) n
> /debugging/pdb_ex.py(14)<module>()
-> choice(random2), choice(random1)))
(Pdb) n
What is 12 times 10? 120
> /debugging/pdb_ex.py(17)<module>()
-> if answer == "exit":
(Pdb) p answer
'120'
Next, let's continue through the program to see if that value, 120 , changes:
(Pdb) n
> /debugging/pdb_ex.py(22)<module>()
-> elif answer == choice(random2) * choice(random1):
(Pdb) n
> /debugging/pdb_ex.py(25)<module>()
-> print("Wrong!")
(Pdb) p answer
'120'
Since the answer does not change there must be something wrong with the program
logic in the if statement, starting with the elif .
108
Workflow
import sys
from random import choice
while True:
print("To exit this game type 'exit'")
answer = input("What is {} times {}? ".format(
choice(random2), choice(random1))
)
# exit
if answer == "exit":
print("Now exiting game!")
sys.exit()
test = int(choice(random2))*int(choice(random1))
# determine if number is correct
# elif answer == choice(random2) * choice(random1):
# print("Correct!")
# else:
# print("Wrong!")
We assigned the value we are using to test our answer against to the variable test .
Debug time!
$ python pdb_ex.py
To exit this game type 'exit'
> /debugging/pdb_ex.py(13)<module>()
-> answer = input("What is {} times {}? ".format(
(Pdb) n
> /debugging/pdb_ex.py(14)<module>()
-> choice(random2), choice(random1)))
(Pdb) n
What is 2 times 2? 4
> /debugging/pdb_ex.py(17)<module>()
-> if answer == "exit":
(Pdb) n
> /debugging/pdb_ex.py(21)<module>()
-> test = int(choice(random2))*int(choice(random1))
(Pdb) n
> /debugging/pdb_ex.py(8)<module>()
-> while True:
(Pdb) p test
20
109
Workflow
There's our answer. The value in the elif varies from the answer value, 20 vs. 4 .
Thus, the elif will always return "Wrong!".
Refactor:
import sys
from random import choice
while True:
print("To exit this game type 'exit'")
num1 = choice(random2)
num2 = choice(random1)
answer = int(input("What is {} times {}? ".format(num1, num2)))
# exit
if answer == "exit":
print("Now exiting game!")
sys.exit()
Ultimately, the program was generating new numbers for comparison within the elif ,
causing the user input to be wrong each time. Additionally, in the comparison - elif
answer == num1 * num2 - the answer is a string while num1 and num2 are integers. To
110
Breakpoints
Breakpoints
One thing we didn't touch on is setting breakpoints, which allow you to pause code
execution at a certain line. To set a breakpoint while debugging, you simply call the
break command and then add the line number that you wish to break on - b <line #>
Simple example:
import pdb
def add_one(num):
result = num + 1
print(result)
return result
def main():
pdb.set_trace()
for num in range(0, 10):
add_one(num)
if __name__ == "__main__":
main()
111
Breakpoints
python pdb_ex2.py
> /debugging/pdb_ex2.py(12)main()
-> for num in range(0, 10):
(Pdb) b 5
Breakpoint 1 at /debugging/pdb_ex2.py:5
(Pdb) c
> /debugging/pdb_ex2.py(5)add_one()
-> result = num + 1
(Pdb) args
num = 0
(Pdb) b 13
Breakpoint 2 at /debugging/pdb_ex2.py:13
(Pdb) c
1
> /debugging/pdb_ex2.py(13)main()
-> add_one(num)
(Pdb) b 5
Breakpoint 3 at /debugging/pdb_ex2.py:5
(Pdb) c
> /debugging/pdb_ex2.py(5)add_one()
-> result = num + 1
(Pdb) args
num = 1
(Pdb) c
2
> /debugging/pdb_ex2.py(13)main()
-> add_one(num)
Here, we started the debugger on line 11, then set a breakpoint on line 5. We continued
the program until it hit that breakpoint. Then we checked the value of num , 0 . We set
another break at line 13, then continued again and said that the result was 1 - result
= 0 + 1 - which is what we expected. Then we did the same process again and found
that the next result was 2 based on the value of num - result = 1 + 1 .
112
Post Mortem Debugging
def add_one_hundred():
again = 'yes'
while again == 'yes':
number = input('Enter a number between 1 and 10: ')
new_number = (int(number) + 100)
print('{} plus 100 is {}!'.format(number, new_number))
again = input('Another round, my friend? ("yes" or "no") ')
print("Goodbye!")
This function simply adds 100 to a number inputted by the user, then outputs the results
to the screen.
NOTE: Here, Python provided us with a traceback, which is really just the details
about what happened when it encountered the error. Most of the time, the error
messages associated with a traceback are very helpful.
Now we can use pdb to start debugging where the exception occurred:
113
Post Mortem Debugging
Start debugging!
So we know that the line new_number = (int(number) + 100) broke the code - because
you can't convert a string to an integer.
This is a simple example, but imagine how useful this would be in a large program with
multiple scripts that you can't fully visualize. You can immediately jump back into the
program where the exception was thrown and start the debugging process. This can be
incredibly useful. We'll look at an example of just that when we start working with the
advanced Flask and Django material later in the course.
Cheers!
Homework
Watch this video and this video on debugging
Read An Introduction to Python Debugging
114
Flask: FlaskTaskr, Part 1 - Quick Start
Before beginning, take a moment to review the steps used to create the blog application.
Again, we'll be using a similar process - but it will go much faster. Make sure to commit
your changes to your local Git repo and push to Github frequently.
115
Getting Started
Getting Started
For simplicity's sake, since you should be familiar with the workflow, explantations will
not address what has already been explained.
Navigate to your "realpython" directory, and create a new directory called "flasktaskr".
Navigate into the newly created directory. Create and activate a new virtualenv. Install
Flask:
Add a new directory called "project", which is the project root directory. Create the
following files and directories within the root directory:
├── static
│ ├── css
│ ├── img
│ └── js
├── templates
└── views.py
NOTE: In the first two Flask apps, we utilized a single-file structure. This is fine for
small apps, but as your project scales, this structure will become much more
difficult to maintain. It's a good idea to break your app into several files, each
handling a different set of responsibilities to separate out concerns. The overall
structure follows the Model-View-Controller architecture pattern.
116
Configuration
Configuration
Remember how we placed all of the blog app's configuration directly in the main
controller? Again, it's best practice to actually place these in a separate file, and then
import that file into the controller. There's a number of reasons for this, but in the end, it
separates our app's logic from configuration and static variables.
Create a configuration file called _config.py and save it in the project root:
import os
DATABASE = 'flasktaskr.db'
USERNAME = 'admin'
PASSWORD = 'admin'
WTF_CSRF_ENABLED = True
SECRET_KEY = 'myprecious'
What's happening?
1. The WTF_CSRF_ENABLED config setting is used for cross-site request forgery
prevention, which makes your app more secure. This setting is used by the Flask-
WTF extension.
2. The SECRET_KEY config setting is used in conjunction with the WTF_CSRF_ENABLED
setting in order to create a cryptographic token that is used to validate a form. It's
also used for the same reason in conjunction with sessions. Make sure the secret
key is nearly impossible to guess. Use a random key generator.
117
Database
Database
Based on the main functionality - each task consists of a name, due date, priority, status,
and a unique ID - we need one database table, consisting of these fields - task_id ,
name , due_date , priority , and status . The value of status will either be a 1 or 0
# project/db_create.py
import sqlite3
from _config import DATABASE_PATH
1. Notice how we did not specify the task_id when adding rows into the table as it's
an auto-incremented value. Also, we used a status of 1 to indicate that each of the
tasks are considered "open" tasks. This is a default value.
2. We imported the DATABASE_PATH variable from the configuration file we created just
a second ago.
Save this in the "project" directory as db_create.py and run it. Was the database
created? Did it populate with data? How do you check? SQLite Browser. Test it out.
118
Database
119
Controller
Controller
Add the following code to views.py:
# project/views.py
import sqlite3
from functools import wraps
# config
app = Flask(__name__)
app.config.from_object('_config')
# helper functions
def connect_db():
return sqlite3.connect(app.config['DATABASE_PATH'])
def login_required(test):
@wraps(test)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return test(*args, **kwargs)
else:
flash('You need to login first.')
return redirect(url_for('login'))
return wrap
# route handlers
@app.route('/logout/')
def logout():
session.pop('logged_in', None)
flash('Goodbye!')
return redirect(url_for('login'))
120
Controller
if request.method == 'POST':
if request.form['username'] != app.config['USERNAME'] \
or request.form['password'] != app.config['PASSWORD']:
error = 'Invalid Credentials. Please try again.'
return render_template('login.html', error=error)
else:
session['logged_in'] = True
flash('Welcome!')
return redirect(url_for('tasks'))
return render_template('login.html')
What's happening?
You've seen this all before, but let's quickly review:
1. Right now, we have one view, login , which is mapped to the main URL, / .
2. Sessions are configured, adding a value of True to the logged_in key, which is
removed (via the pop method) when the user logs out.
3. The login_required() decorator is also configured. Do you remember how that
works? You can see that after a user logs in, they will be redirected to tasks ,
which still needs to be specified.
NOTE: Refer to the Flask Blog App chapter for further explanation on any details
of this code that you do not understand.
Commit your code. Make sure to add a .gitignore file to the "flasktaskr" directory:
env
venv
*.pyc
__pycache__
*.DS_Store
project/_config.py
Let's set up the login and base templates as well as an external stylesheet.
121
Templates and Styles
{% extends "_base.html" %}
{% block content %}
<h1>Welcome to FlaskTaskr.</h1>
<h3>Please login to access your task list.</h3>
{% endblock %}
122
Templates and Styles
<!DOCTYPE html>
<html>
<head>
<title>Welcome to FlaskTaskr!!</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
</head>
<body>
<div class="page">
{% if error %}
<div class="error"><strong>Error:</strong> {{ error }}</div>
{% endif %}
<br>
{% block content %}
{% endblock %}
</div>
</body>
</html>
Stylesheet
For now we'll temporarily "borrow" the majority of the stylesheet from the Flask tutorial.
Find the main.css file in the book2-exercises in the assets/flasktaskr-01 directory. Copy
and paste this code into a new file, and then save it as main.css in the "css" directory
within the "static" directory.
123
Sanity Check
Sanity Check
Instead of running the application directly from the controller (like in the blog app), let's
create a separate file. Why? In order to remove unnecessary code from the controller
that does not pertain to the actual business logic. Again, we're separating out concerns.
# project/run.py
Save this as run.py in your "project" directory. The directory structure within the project
directory should now look like this:
├── _config.py
├── db_create.py
├── flasktaskr.db
├── run.py
├── static
│ ├── css
│ │ └── main.css
│ ├── img
│ └── js
├── templates
│ ├── _base.html
│ └── login.html
└── views.py
$ python run.py
Navigate to http://localhost:5000/. Make sure everything works thus far. You'll only be
able to view the styled login page (but not login) as we have not set up the tasks.html
page yet. Essentially, we're ensuring that the-
App runs
Templates are working correctly
Basic logic in views.py works
124
Sanity Check
So, we've split out our app into separate files, each with specific responsibilities:
Remember when all those responsibilities were crammed into one single file? This new
structure should be much easier to follow.
125
Tasks
Tasks
1. Once signed in, users can add new tasks
2. Users can view all incomplete tasks from the same page
3. Users can also delete tasks and mark tasks as complete
Add the following route handler and view function to the views.py file:
@app.route('/tasks/')
@login_required
def tasks():
g.db = connect_db()
cursor = g.db.execute(
'select name, due_date, priority, task_id from tasks where status=1'
)
open_tasks = [
dict(name=row[0], due_date=row[1], priority=row[2],
task_id=row[3]) for row in cursor.fetchall()
]
cursor = g.db.execute(
'select name, due_date, priority, task_id from tasks where status=0'
)
closed_tasks = [
dict(name=row[0], due_date=row[1], priority=row[2],
task_id=row[3]) for row in cursor.fetchall()
]
g.db.close()
return render_template(
'tasks.html',
form=AddTaskForm(request.form),
open_tasks=open_tasks,
closed_tasks=closed_tasks
)
What's happening?
126
Tasks
We queried the database for open and closed tasks and assigned them to two variables,
open_tasks and closed tasks . We then passed those variables to the tasks.html page.
These variables will then be used to populate the open and closed task lists,
respectively.
Make sense?
form=AddTaskForm(request.form),
AddTaskForm() will be the name of a form used to, well, add tasks. This has not been
created yet.
127
Add, Update, and Delete Tasks
128
Add, Update, and Delete Tasks
# Delete Tasks
@app.route('/delete/<int:task_id>/')
@login_required
def delete_entry(task_id):
g.db = connect_db()
g.db.execute('delete from tasks where task_id='+str(task_id))
g.db.commit()
g.db.close()
flash('The task was deleted.')
return redirect(url_for('tasks'))
129
Add, Update, and Delete Tasks
What's happening?
The last two functions pass in a variable parameter, task_id , from the tasks.html page
(which we will create next). This variable is equal to the unique task_id field in the
database. A query is then performed and the appropriate action takes place. In this
case, an action means either marking a task as complete or deleting a task. Notice how
we have to convert the task_id variable to a string, since we are using string
concatenation to combine the SQL query with the task_id , which is an integer.
130
Tasks Template
Tasks Template
Find the tasks.html file in the assests/flasktaskr-01 directory of book2-exercises . Copy
and paste this code into your own tasks.html within your templates directory.
Read over the html and see if you can sort out what's going on before continuing.
What's happening?
Although a lot is going on in here, the only thing you have not seen before are these
statements:
Essentially, we pulled the task_id from the database dynamically from each row in the
database table as the for loop iterates. We then assigned the id to a variable, also
named task_id , which is then passed back to either the delete_entry() function -
@app.route('/delete/<int:task_id>/') - or the complete() function -
@app.route('/complete/<int:task_id>/') .
Make sure to walk through this code line by line. You should understand what each line
is doing. Add comments to help.
131
Add Tasks Form
# project/forms.py
class AddTaskForm(Form):
task_id = IntegerField()
name = StringField('Task Name', validators=[DataRequired()])
due_date = DateField(
'Date Due (mm/dd/yyyy)',
validators=[DataRequired()], format='%m/%d/%Y'
)
priority = SelectField(
'Priority',
validators=[DataRequired()],
choices=[
('1', '1'), ('2', '2'), ('3', '3'), ('4', '4'), ('5', '5'),
('6', '6'), ('7', '7'), ('8', '8'), ('9', '9'), ('10', '10')
]
)
status = IntegerField('Status')
Notice how we're importing from both Flask-WTF and WTForms. Essentially, Flask-WTF
works in tandem with WTForms, abstracting much of the functionality.
132
Add Tasks Form
NOTE: The validators and choices are set up correctly in the form; however, we're
not currently using any logic in the new_task() view function to prevent a form
submission if the submitted data does not conform to the specific validators. We
need to use a method called validate_on_submit() , which returns True if the
data passes validation, in the function. We'll look at this further down the road.
Make sure you update your views.py by importing the AddTaskForm() class from
forms.py:
133
Sanity Check
Sanity Check
Finally, test out the functionality of the app.
Fire up your server again. You should be able to log in now. Ensure that you can view
tasks, add new tasks, mark tasks as complete, and delete tasks.
That's it for now. Next time we'll speed up the development process by adding powerful
extensions to the application.
134
Sanity Check
├── _config.py
├── db_create.py
├── flasktaskr.db
├── forms.py
├── run.py
├── static
│ ├── css
│ │ └── main.css
│ ├── img
│ └── js
├── templates
│ ├── _base.html
│ ├── login.html
│ └── tasks.html
└── views.py
Commit your code to your local repo and then push to Github.
NOTE: If you had any problems with your code or just want to double check your
code, be sure to view the flasktaskr-01 folder in the course repository.
135
Flask: FlaskTaskr, Part 2 - SQLAlchemy and User Management
136
Flask: FlaskTaskr, Part 2 - SQLAlchemy and User Management
Task Complete
Database Management No
User Registration No
User Login/Authentication No
Database Relationships No
Managing Sessions No
Error Handling No
Testing No
Styling No
Test Coverage No
Permissions No
Blueprints No
New Features No
Password Hashing No
Error Logging No
Deployment Options No
Automated Deployments No
Before starting this chapter, make sure you can log in with your current app, and then
once you're logged in check that you can run all of the CRUD commands against the
tasks table:
137
Flask: FlaskTaskr, Part 2 - SQLAlchemy and User Management
If not, be sure to grab the code from flasktaskr-01 from the course repository.
Homework
Please read over the main page of the Flask-SQLAlchemy extension. Compare the
code samples to regular SQL. How do the classes/objects compare to the SQL
statements used for creating a new database table?
Take a look at all the Flask extensions here. Read them over quickly.
138
Database Management
Database Management
As mentioned in the Database Programming chapter, you can work with relational
databases without having to touch (very much) SQL. Essentially, you need to use an
Object Relational Mapper (ORM), which translates and maps SQL commands and your
database schema into Python objects. It makes working with relational databases much
easier as it eliminates having to write repetitive code.
ORMs also make it easy to switch relational database engines without having to re-write
much of the code that interacts with the database itself due to the means in which
SQLAlchemy structures the data.
That said, no matter how much you use an ORM you will eventually have to use SQL for
troubleshooting or testing quick, one-off queries as well as advanced queries. It's also
really, really helpful to know SQL, when trying to decide on the most efficient way to
query the database, to know what calls the ORM will be making to the database, and so
forth. Learn SQL first, in other words. For more, check out this popular blog post.
Note: One major advantage of Flask is that you are not limited to a specific ORM
or ODM (Object Document Mapper) for non-relational databases. You can use
SQLAlchemy, Peewee, Pony, MongoEngine, etc. The choice is yours.
Setup
Start by installing Flask-SQLAlchemy:
Delete your current database, flasktaskr.db, and then create a new file called models.py
in the root directory. We're going to recreate the database using SQLAlchemy. As we do
this, compare this method to how we created the database before, using vanilla SQL.
139
Database Management
# project/models.py
class Task(db.Model):
__tablename__ = "tasks"
def __repr__(self):
return '<name {0}>'.format(self.name)
We have one class, Task() , that defines the tasks table. The variable names are used
as the column names. Any field that has a primary_key set to True will auto-increment.
# config
app = Flask(__name__)
app.config.from_object('_config')
db = SQLAlchemy(app)
Make sure to remove the following code from views.py since we are not using the
Python SQLite wrapper to interact with the database anymore:
140
Database Management
import sqlite3
def connect_db():
return sqlite3.connect(app.config['DATABASE_PATH'])
@app.route('/logout/')
def logout():
session.pop('logged_in', None)
flash('Goodbye!')
return redirect(url_for('login'))
@app.route('/tasks/')
@login_required
def tasks():
open_tasks = db.session.query(Task) \
.filter_by(status='1').order_by(Task.due_date.asc())
closed_tasks = db.session.query(Task) \
.filter_by(status='0').order_by(Task.due_date.asc())
return render_template(
'tasks.html',
form=AddTaskForm(request.form),
open_tasks=open_tasks,
closed_tasks=closed_tasks
)
141
Database Management
new_task = Task(
form.name.data,
form.due_date.data,
form.priority.data,
'1'
)
db.session.add(new_task)
db.session.commit()
flash('New entry was successfully posted. Thanks.')
return redirect(url_for('tasks'))
@app.route('/complete/<int:task_id>/')
@login_required
def complete(task_id):
new_id = task_id
db.session.query(Task).filter_by(task_id=new_id).update({"status": "0"})
db.session.commit()
flash('The task is complete. Nice.')
return redirect(url_for('tasks'))
@app.route('/delete/<int:task_id>/')
@login_required
def delete_entry(task_id):
new_id = task_id
db.session.query(Task).filter_by(task_id=new_id).delete()
db.session.commit()
flash('The task was deleted. Why not add a new one?')
return redirect(url_for('tasks'))
142
Database Management
# project/_config.py
import os
DATABASE = 'flasktaskr.db'
USERNAME = 'admin'
PASSWORD = 'admin'
CSRF_ENABLED = True
SECRET_KEY = 'my_precious'
# project/db_create.py
# insert data
db.session.add(Task("Finish this tutorial", date(2016, 9, 22), 10, 1))
db.session.add(Task("Finish Real Python", date(2016, 10, 3), 10, 1))
What's happening?
143
Database Management
2. We then populate the table with some data, via the Task object from models.py to
specify the schema.
3. To apply the previous changes to our database we need to commit using
db.session.commit()
Since we are now using SQLAlchemy, we're modifying the way we do database queries.
The code is much cleaner. Compare this method to the actual SQL code from the
previous chapter.
NOTE: Remember when we created the Task class? We extended the db.Model.
This is how the create_all command knows to create our Task schema.
$ python db_create.py
The flasktaskr.db should have been recreated. Open up the file in the SQLite Browser to
ensure that the table and the above data are present in the tasks table.
144
Database Management
CSRF Token
Finally, since we are issuing a POST request, we need to add {{ form.csrf_token }} to
all forms in the templates. This applies the CSRF prevention setting to the form that we
enabled in the configuration.
145
Database Management
Test
Fire up the server. Ensure that you can still view tasks, add new tasks, mark tasks as
complete, and delete tasks.
Nice. Time to move on to user registration. Don't forget to commit your code and PUSH
to Github.
146
User Registration
User Registration
Let's allow multiple users to access the task manager by setting up a user registration
form.
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, unique=True, nullable=False)
email = db.Column(db.String, unique=True, nullable=False)
password = db.Column(db.String, nullable=False)
def __repr__(self):
return '<User {0}>'.format(self.name)
Run db_create.py again. Before you do so, comment out the following lines:
If you do not do this, the script will add that data to the database again.
Open up the SQLite Browser. Notice how it ignores the table already created, tasks, and
just creates the users table:
147
User Registration
Configuration
Remove the following lines of code in _config.py:
USERNAME = 'admin'
PASSWORD = 'admin'
We no longer need this configuration since we will use the information from the users
table in the database instead of the hard-coded data.
We also need to update forms.py to cater for both user registration and logging in.
148
User Registration
class RegisterForm(Form):
name = StringField(
'Username',
validators=[DataRequired(), Length(min=6, max=25)]
)
email = StringField(
'Email',
validators=[DataRequired(), Length(min=6, max=40)]
)
password = PasswordField(
'Password',
validators=[DataRequired(), Length(min=6, max=40)])
confirm = PasswordField(
'Repeat Password',
validators=[DataRequired(), EqualTo('password', message='Passwords must ma
tch')]
)
class LoginForm(Form):
name = StringField(
'Username',
validators=[DataRequired()]
)
password = PasswordField(
'Password',
validators=[DataRequired()]
)
Next we need to update the Controller, views.py. Update the imports, and add the
following code:
149
User Registration
#################
#### imports ####
#################
################
#### config ####
################
app = Flask(__name__)
app.config.from_object('_config')
db = SQLAlchemy(app)
This allow access to the RegisterForm() and LoginForm() classes from forms.py and
the User() class from models.py.
Here, the user information obtained from the register.html template (which we still need
to create) is stored inside the variable new_user . That data is then added to the
database, and after successful registration, the user is redirected to login.html with a
150
User Registration
Templates
Go to the repository and grab the updated html from the assests/flasktaskr-02 folder.
Now let's add a registration link to the bottom of the login.html page:
<br>
Test it out. Run the server, click the link to register, and register a new user. You should
be able to register just fine, as long as the fields pass validation. Everything turn out
okay? Double check my code, if not. Now we need to update the code so users can
login. Why? Since the logic in the controller is not searching the newly created database
table for the correct username and password. Instead, it's still looking for hard-coded
values in the _config.py file:
if request.form['username'] != app.config['USERNAME'] or \
request.form['password'] != app.config['PASSWORD']:
151
Authentication
Authentication
The next step for allowing multiple users to login is to change the login() function
within the controllers as well as the login template.
Controller
Replace the current login() function with:
This code is not too much different from the old code. When a user submits their user
credentials via a POST request, the database is queried for the submitted username and
password. If the credentials are not found, an error populates; otherwise, the user is
logged in and redirected to tasks.html.
Templates
Update login.html with the code from the assests folder in the repository.
Test it out. Try logging in with the same user you registered. If done correctly, you
should be able to log in and then you'll be redirected to tasks.html.
152
Authentication
Can you tell what happened? Can you predict what the logs will look like when you
submit a bad username and/or password? Or if you leave a field blank? test it.
153
Database Relationships
Database Relationships
To complete the conversion to SQLAlchemy we need to update both the database and
task template.
First, let's update the database to add two new fields to tasks table: posted_date and
user_id . The user_id field also needs to link back to the User table.
created a certain task as well as find out all the tasks created by a certain user:
Let's look at how to alter the tables to create such relationships within models.py.
The user_id field in the tasks table is a foreign key, which binds the values from this
field to the values found in the corresponding id field in the users table. Foreign keys
are essential for creating relationships between tables in order to correlate information.
154
Database Relationships
SEE ALSO: Need help with foreign keys? Take a look at the W3 documentation.
1. One to One (1:1) - For example, one employee is assigned one employee id
2. One to Many (1:M) - one department contains many employees
3. Many to Many (M:M) - many employees take many training courses
In our case, we have a one to many relationship: one user can post many tasks:
If we were to create a more advanced application we could also have a many to many
relationship: many users could alter many tasks. However, we will keep this database
simple: one user can create a task, one user can mark that same task as complete, and
one user can delete the task.
155
Database Relationships
# project/models.py
import datetime
class Task(db.Model):
__tablename__ = "tasks"
def __repr__(self):
return '<name {0}>'.format(self.name)
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, unique=True, nullable=False)
email = db.Column(db.String, unique=True, nullable=False)
password = db.Column(db.String, nullable=False)
tasks = db.relationship('Task', backref='poster')
def __repr__(self):
return '<User {0}>'.format(self.name)
156
Database Relationships
If we ran the above code, it would only work if we used a fresh, empty database. But
since our database already has the "tasks" and "users" tables, SQLAlchemy will not
redefine these database tables. To fix this, we need a migration script that will update
the schema and transfer any existing data:
# project/db_migrate.py
import sqlite3
from datetime import datetime
# save all rows as a list of tuples; set posted_date to now and user_id to 1
data = [(row[0], row[1], row[2], row[3],
datetime.now(), 1) for row in c.fetchall()]
Save this as db_migrate.py under the root directory and run it.
Note that this script did not touch the "users" table; it is only the "tasks" table that has
underlying schema changes Using SQLite Browser, verify that the "posted_date" and
"user_id" columns have been added to the "tasks" table.
157
Database Relationships
Controller
We also need to update the adding of tasks within views.py. Within the new_task
function, change the following:
new_task = Task(
form.name.data,
form.due_date.data,
form.priority.data,
'1'
)
to:
158
Database Relationships
new_task = Task(
form.name.data,
form.due_date.data,
form.priority.data,
datetime.datetime.utcnow(),
'1',
'1'
)
there is more than one user? Later, in a subsequent section, we will change this to
capture the user_id of the currently logged-in user.
Templates
Now, let's update the tasks.html template. Again head over to the repository and grab
the tasks.html out of the assets/flasktaskr-02 folder.
The changes are fairly straightforward. Can you find them? Take a look at this file along
with forms.py to see how the drop-down list is implemented.
Fire up your server and try adding a few tasks. Register a new user and add some more
tasks. Make sure that the current date shows up as the posted date. Look back at my
code if you're having problems. Also, we can see that the first user is always showing up
under Posted by - which is expected.
159
Database Relationships
.inline-form .input-group{
display: inline-block;
margin: 10px 5px;
}
.input-group input {
width: 100%;
}
160
Managing Sessions
Managing Sessions
Do you remember the relationship we established between the two tables in the last
lesson?
Well, with that simple relationship, we can query for the actual name of the user for each
task posted. First, we need to log the user_id in the session when a user successfully
logs in. So make the following updates to the login() function in views.py:
Next, when we post a new task, we need to grab the user id from the session and add it
to the SQLAlchemy ORM query. So, update the new_task() function:
161
Managing Sessions
Here, we grab the current user in session, pulling the user_id and adding it to the
query.
Another pop() method needs to be used for when a user logs out:
@app.route('/logout/')
def logout():
session.pop('logged_in', None)
session.pop('user_id', None)
flash('Goodbye!')
return redirect(url_for('login'))
Now open up tasks.html. In each of the two for loops, note this statement:
Go back to your model. Notice that since we used poster as the backref, we can use it
like a regular query object. Nice!
162
Managing Sessions
Register a new user and then login using that newly created user. Create a new task
and watch how the "Posted By" field now gets populated with the name of the user who
created the task.
With that, we're done looking at database relationships as well as the conversion to
SQLAlchemy. Again, we can now easily switch SQL database engines (which we will
eventually get to). The code now abstracts away much of the repetition from straight
SQL so our code is cleaner and more readable.
Next, let's look at form handling as well as unit testing. Take a break, though. You
earned it.
Your project structure with the "project" folder should now look like this:
├── _config.py
├── db_create.py
├── db_migrate.py
├── flasktaskr.db
├── forms.py
├── models.py
├── run.py
├── static
│ ├── css
│ │ └── main.css
│ ├── img
│ └── js
├── templates
│ ├── _base.html
│ ├── login.html
│ ├── register.html
│ └── tasks.html
└── views.py
If you had any problems with your code or just want to double check your code
with mine, be sure to view the flasktaskr-02 folder in the course repository.
163
Flask: FlaskTaskr, Part 3 - Error Handling and Testing
Task Complete
Error Handling No
Testing No
Styling No
Test Coverage No
Permissions No
Blueprints No
New Features No
Password Hashing No
Error Logging No
Deployment Options No
Automated Deployments No
164
Flask: FlaskTaskr, Part 3 - Error Handling and Testing
Let's get to it. First, make sure your app is working properly. Fire up the app. Register a
new user, log in, and then add, update, and delete a few tasks. If you come across any
problems, compare your code to the code from flasktaskr-02 in the course repository.
In this section we're going to look at error handling and testing. You will also be
introduced to a development strategy known as Test Driven Development.
165
Error Handling
Error Handling
Errors that occur during execution are called exceptions. Such errors need to be
addressed since they stop execution.
We need to add in an error message in order to provide feedback so that the user knows
how to proceed. Fortunately, WTForms can handle this for us for any form that has a
validator attached to it.
Open forms.py. There are already some validators in place. For example, in the
RegisterForm() class, the name field has a Length validator, indicating the inputted
class RegisterForm(Form):
name = StringField(
'Username',
validators=[DataRequired(), Length(min=6, max=25)]
)
email = StringField(
'Email',
validators=[DataRequired(), Length(min=6, max=40)]
)
password = PasswordField(
'Password',
validators=[DataRequired(), Length(min=6, max=40)])
confirm = PasswordField(
'Repeat Password',
validators=[DataRequired(), EqualTo('password', message='Passwords must ma
tch')]
)
166
Error Handling
class RegisterForm(Form):
name = StringField(
'Username',
validators=[DataRequired(), Length(min=6, max=25)]
)
email = StringField(
'Email',
validators=[DataRequired(), Email(), Length(min=6, max=40)]
)
password = PasswordField(
'Password',
validators=[DataRequired(), Length(min=6, max=40)])
confirm = PasswordField(
'Repeat Password',
validators=[DataRequired(), EqualTo('password')]
)
Now we need to display the error messages to the end user. To do so, simply add the
following code to the views.py file:
def flash_errors(form):
for field, errors in form.errors.items():
for error in errors:
flash(u"Error in the %s field - %s" % (
getattr(form, field).label.text, error), 'error')
register.html
login.html
tasks.html
You can find the updated html in assests/flasktaskr-03 in the course repository.
NOTE: Instead of labels, notice how we're using placeholders. This is purely an
aesthetic change. If it does not suite you, you can always change it back.
167
Error Handling
.error {
color: red;
font-size: .8em;
background-color: #FFFFFF;
}
Did this work? Fire up the server again. Try inputting invalid values in the form. The
proper error messages should display. Did you notice that the open and closed tasks
don't show up? This is because our logic in the new_task route renders the template
without the tasks if we encounter an error.
To update that, create helper functions out of the opening and closing of tasks.
def open_tasks():
return db.session.query(Task).filter_by(
status='1').order_by(Task.due_date.asc())
def closed_tasks():
return db.session.query(Task).filter_by(
status='0').order_by(Task.due_date.asc())
168
Error Handling
@app.route('/tasks/')
@login_required
def tasks():
return render_template(
'tasks.html',
form=AddTaskForm(request.form),
open_tasks=open_tasks(),
closed_tasks=closed_tasks()
)
169
Error Handling
Essentially, the code within the try block attempts to execute. If it fails due to the
exception specified in the except block, the code execution stops and the code within
the except block is ran. If the error does not occur then the program fully executes and
the except block is skipped altogether.
Try registering a user again with a username that already exists. The error should be
handled correctly now.
NOTE: You will never be able to anticipate every error in your code. Error
handlers (when used right) help to catch common errors so that they are handled
gracefully.
170
Testing
Testing
Testing your application is an absolute necessity, especially as your app grows in size
and complexity. Tests help ensure that as the complexity of an application grows the
various moving parts continue to work together in a harmonious fashion. In other words,
tests help reveal when code isn't working correctly and when it breaks altogether.
Every time you add a feature to an application, fix a bug, or change some code you
should make sure the code, new and old, is adequately covered by tests and that the
tests all pass after you're done.
171
Getting Started
Getting Started
We will use the unit test framework unittest, which is part of the Python standard library,
to write our tests.
The structure is simple: Each test case is written as a separate method within a larger
class.
import unittest
class TestCase(unittest.TestCase):
if __name__ == '__main__':
unittest.main()
You can break classes into several test suites. For example, one suite could test the
managing of users and sessions, while another could test user registration, and so forth.
Such test suites are meant to affirm that the desired outcome does not deviate from the
actual outcome.
172
Getting Started
# project/test.py
import os
import unittest
TEST_DB = 'test.db'
class AllTests(unittest.TestCase):
############################
#### setup and teardown ####
############################
if __name__ == "__main__":
unittest.main()
173
Getting Started
$ python project/test.py
.
----------------------------------------------------------------------
Ran 1 test in 0.223s
OK
It passed!
What happened?
1. The setUp() method was invoked which created a test database (if it did not
already exist) and applied the database schema from the main database. It also
created a test client, which handles requests and sends back responses for us to
test. It essentially mocks out the entire Flask app.
2. The test_user_setup() method was called, inserting data into the "users" table.
3. Lastly, the tearDown() method was invoked which dropped all the tables in the test
database.
Try commenting out the tearDown() method and run the test script once again. Check
the database in the SQLite Browser. Is the data there? While the tearDown() method is
still commented out, run the test script a second time.
----------------------------------------------------------------------
Ran 1 test in 0.044s
FAILED (errors=1)
What happened?
An exception was thrown because the username and email address was not unique (as
defined in our User() class in models.py). Delete test.db and add the tearDown()
method back in before moving on.
Assert
174
Getting Started
Each test should have an assert() method to either verify an expected result or a
condition or indicate that an exception is raised.
Let's quickly look at an example of how assert() works. Update test.py with the
following code:
175
Getting Started
# project/test.py
import os
import unittest
TEST_DB = 'test.db'
class AllTests(unittest.TestCase):
############################
#### setup and teardown ####
############################
if __name__ == "__main__":
unittest.main()
In this example, we're testing whether a new user is successfully added to the database.
We then pull all the data from the database, test = db.session.query(User).all() ,
extract just the name, and then test to make sure the name equals the expected result -
176
Getting Started
which is "michael".
Run the test suite in the current form. It should pass. Then change the assert statement
to -
======================================================================
FAIL: test_users_can_register (__main__.AllTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 41, in test_users_can_register
assert t.name != "michael"
AssertionError
----------------------------------------------------------------------
Ran 1 test in 0.044s
FAILED (failures=1)
Change the assert statement back to assert t.name == "michael" . Run the tests again
to make sure nothing broke.
Semantics aside, this is not a course on testing. Testing is an art. It takes years and
years of practice to hone this skill. This course teaches you how to test, and then, over
time, you'll learn what to test. It's your decision whether you want to write more granular
unit tests or higher-level integration tests on your apps going forward.
177
Getting Started
Current Functionality
Let's test the our app's functionality up until this point, function by function. For now, let's
add all of our tests to the test.py file. We'll refactor this later after the tests work.
Take out a piece of paper and review the code in the views.py file, making a note of
everything that can be tested. You want at least enough tests to cover each function. Try
to break this down between users and tasks.
Not Logged
Function What are we testing?
logged in in
Let's go through and start testing, one by one. Make sure to run your tests after each
new test is added to avoid code regressions.
Homework
Although not specifically about Flask, watch this excellent video on testing Python
code.
Users
178
Getting Started
Form is present
def test_form_is_present(self):
response = self.app.get('/')
self.assertEqual(response.status_code, 200)
self.assertIn(b'Please sign in to access your task list', response.data)
This test catches the response from sending a GET request to '/' and then asserts that
the response static code is 200 and that the words 'Please sign in to access your task
list' are present.
Why do you think we went the extra mile to test that specific HTML is present?
If we just tested for a 200 response code, we don't know what the end user actually
sees. It could be JSON or an entirely different HTML page.
NOTE: In the assertIn syntax we added a b before the text that we are looking
for. When using the assertIn method, unittest is looking for a bytes-like object and
this b converts the string to the appropriate type.
def test_users_cannot_login_unless_registered(self):
response = self.login('foo', 'bar')
self.assertIn(b'Invalid username or password.', response.data)
179
Getting Started
to the database, then you can be confident that your app is protected against dangerous
injections. To be safe, we can test the schema, starting with valid data.
def test_users_can_login(self):
self.register('Michael', '[email protected]', 'python', 'python')
response = self.login('Michael', 'python')
self.assertIn(b'Welcome!', response.data)
Finally, let's test some bad data and see how far the process gets
def test_invalid_form_data(self):
self.register('Michael', '[email protected]', 'python', 'python')
response = self.login('alert("alert box!");', 'foo')
self.assertIn(b'Invalid username or password.', response.data)
def test_form_is_present_on_register_page(self):
response = self.app.get('register/')
self.assertEqual(response.status_code, 200)
self.assertIn(b'Please register to access the task list.', response.data)
This should be straightforward. Maybe we should test that the actual form is preset
rather than just some other element on the same page as the form?
180
Getting Started
def test_user_registration(self):
self.app.get('register/', follow_redirects=True)
response = self.register(
'Michael', '[email protected]', 'python', 'python')
self.assertIn(b'Thanks for registering. Please login.', response.data)
Look back at your tests. We're already testing for this, right? Should we refactor?
def test_user_registration_error(self):
self.app.get('register/', follow_redirects=True)
self.register('Michael', '[email protected]', 'python', 'python')
self.app.get('register/', follow_redirects=True)
response = self.register(
'Michael', '[email protected]', 'python', 'python'
)
self.assertIn(
b'That username and/or email already exist.',
response.data
)
def logout(self):
return self.app.get('logout/', follow_redirects=True)
Now we can test logging out for both logged in and not logged in users:
def test_logged_in_users_can_logout(self):
self.register('Fletcher', '[email protected]', 'python101', 'python101')
self.login('Fletcher', 'python101')
response = self.logout()
self.assertIn(b'Goodbye!', response.data)
def test_not_logged_in_users_cannot_logout(self):
response = self.logout()
self.assertNotIn(b'Goodbye!', response.data)
181
Getting Started
Run the tests. You should get a failure since users not logged in can still access that end
point, /logout . To fix that simply add the @login_required decorator to the view:
@app.route('/logout/')
@login_required
def logout():
session.pop('logged_in', None)
session.pop('user_id', None)
flash('Goodbye!')
return redirect(url_for('login'))
def test_logged_in_users_can_access_tasks_page(self):
self.register(
'Fletcher', '[email protected]', 'python101', 'python101'
)
self.login('Fletcher', 'python101')
response = self.app.get('tasks/')
self.assertEqual(response.status_code, 200)
self.assertIn(b'Add a new task:', response.data)
def test_not_logged_in_users_cannot_access_tasks_page(self):
response = self.app.get('tasks/', follow_redirects=True)
self.assertIn(b'You need to login first.', response.data)
Seems like you could combine the last two tests, right?
Tasks
For these next set of tests assume that only users that are logged in can add, complete,
or delete tasks. Why? We already know that only logged in users can access the 'tasks/'
endpoint. No need to test that again.
182
Getting Started
def create_task(self):
return self.app.post('add/', data=dict(
name='Go to the bank',
due_date='10/08/2016',
priority='1',
posted_date='10/08/2016',
status='1'
), follow_redirects=True)
def test_users_can_add_tasks(self):
self.create_user('Michael', '[email protected]', 'python')
self.login('Michael', 'python')
self.app.get('tasks/', follow_redirects=True)
response = self.create_task()
self.assertIn(
b'New entry was successfully posted. Thanks.', response.data
)
def test_users_cannot_add_tasks_when_error(self):
self.create_user('Michael', '[email protected]', 'python')
self.login('Michael', 'python')
self.app.get('tasks/', follow_redirects=True)
response = self.app.post('add/', data=dict(
name='Go to the bank',
due_date='',
priority='1',
posted_date='02/05/2014',
status='1'
), follow_redirects=True)
self.assertIn(b'This field is required.', response.data)
183
Getting Started
def test_users_can_complete_tasks(self):
self.create_user('Michael', '[email protected]', 'python')
self.login('Michael', 'python')
self.app.get('tasks/', follow_redirects=True)
self.create_task()
response = self.app.get("complete/1/", follow_redirects=True)
self.assertIn(b'The task is complete. Nice.', response.data)
def test_users_can_delete_tasks(self):
self.create_user('Michael', '[email protected]', 'python')
self.login('Michael', 'python')
self.app.get('tasks/', follow_redirects=True)
self.create_task()
response = self.app.get("delete/1/", follow_redirects=True)
self.assertIn(b'The task was deleted.', response.data)
Remember when we set up the 'users' table and defined the relationship between users
and tasks, establishing a one-to-many relationship: one user can create a task, one user
can mark that same task as complete, and one user can delete the task? If user A adds
a task then that task can only be updated or deleted by user A can update and/or delete
that task.
def test_users_cannot_complete_tasks_that_are_not_created_by_them(self):
self.create_user('Michael', '[email protected]', 'python')
self.login('Michael', 'python')
self.app.get('tasks/', follow_redirects=True)
self.create_task()
self.logout()
self.create_user('Fletcher', '[email protected]', 'python101')
self.login('Fletcher', 'python101')
self.app.get('tasks/', follow_redirects=True)
response = self.app.get("complete/1/", follow_redirects=True)
self.assertNotIn(
b'The task is complete. Nice.', response.data
)
184
Getting Started
By now you probably already know that test is going to fail, just from manual testing
alone. We need to write the code to get it to pass. This marks the beginning of Test
Driven Development! We'll pick up here next time.
├── _config.py
├── db_create.py
├── db_migrate.py
├── flasktaskr.db
├── forms.py
├── models.py
├── run.py
├── static
│ ├── css
│ │ └── main.css
│ ├── img
│ └── js
├── templates
│ ├── _base.html
│ ├── login.html
│ ├── register.html
│ └── tasks.html
├── test.db
├── test.py
└── views.py
NOTE: The code for this chapter can be found in the flasktaskr-03 folder in the
course repository
185
Interlude: Intro to HTML and CSS
This is a two part tutorial covering HTML, CSS, JavaScript, and jQuery, where we will be
building a basic todo list. In part one, the focus is on HTML and CSS.
Webpages are made up of many things, but HTML (Hyper Text Markup Language) and
CSS (Cascading Style Sheets) are two of the most important components. Together,
they are the building blocks for every single page on the Internet.
Think of a car. It, too, is made up of many attributes. Doors. Windows. Tires. Seats. In
the world of HTML, these are the elements of a webpage. Meanwhile, each of the car's
attributes are usually different. Perhaps they differ by size. Or color. Or wear and tear.
These attributes are used to define how the elements look. Back in the world of web,
CSS is used to define the look and feel of a webpage.
186
HTML
HTML
HTML provides structure, making it viewable by a web browser.
To start, let's add some basic structure. Copy and paste the below structure into your
text editor. Save the file as index.html in a new directory called "front-end".
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
1. title <title>
2. heading <h1>
3. break <br>
4. paragraph <p>
187
HTML
<!DOCTYPE html>
<html>
<head>
<title>My Todo List</title>
</head>
<body>
<h1>My Todo List</h1>
<p>Get yourself organized!</p>
<br>
</body>
</html>
SEE ALSO: Mozilla has an excellent reference guide for all HTML elements for all
HTML elements.
Additional Tags
Let's add some more tags:
188
HTML
<!doctype html>
<html>
<head>
<title>My Todo List</title>
</head>
<body>
<h1>My Todo List</h1>
<p>Get yourself organized!</p>
<br>
<form>
<input type="text" placeholder="Enter a todo...">
</form>
<br>
<button>Submit!</button>
<br>
</body>
</html>
We added:
Check it out in your browser. Kind of bland, right? Fortunately, we can quickly change
that with CSS!
189
CSS
CSS
While HTML provides structure, CSS is used for styling. From the size of the text to the
background colors to the positioning of HTML elements, CSS gives you control over
almost every visual aspect of a page.
CSS and HTML work in tandem. CSS styles are applied directly to HTML elements, as
you will soon see.
NOTE: There are three ways that you can assign styles to HTML tags - inline,
internal, and external. Inline styles are placed directly in an HTML tag. Internal
styles fall within the head of the HTML. These should be avoided, as it's best
practice to separate HTML and CSS (don't mix structure with presentation!).
First, we need to "link" the HTML page and CSS stylesheet. Add the following code to
the <head> section of the HTML page just above the title:
190
CSS
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/
bootswatch/3.0.3/flatly/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="main.css">
<title>My Todo List</title>
</head>
<body>
<div class="container">
<h1>My Todo List</h1>
<p>Get yourself organized!</p>
<br>
<form>
<input type="text" placeholder="Enter a todo...">
</form>
<br>
<button>Submit!</button>
<br>
</div>
</body>
</html>
Save the file. The first CSS file is a bootstrap stylesheet, while the second is a custom
stylesheet, which we will create in a few moments. For more information on Bootstrap,
please the Getting Started with Bootstrap 3 blog post.
Open the page in your web browser. See the difference? Yes, it's subtle - but the font is
different along with the style of the input boxes. What styles does the container class
add?
Custom Styles
Create a main.css file and save it in the same folder as your index.html file. Then add
the following CSS to the file:
.container {
max-width: 500px;
padding-top: 50px;
}
191
CSS
1. We have the .container selector, which is associated with the selector in our
HTML document, followed by curly braces.
2. Inside the curly braces, we have properties, which are descriptive phrases, like
font-weight , font-size , or background-color .
3. Values are then assigned to each property, which are preceded by a colon and
followed by a semi-colon. CSS Values is an excellent resource for finding the
acceptable values given a CSS property. Bookmark it!
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/
bootswatch/3.0.3/flatly/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="main.css">
<title>My Todo List</title>
</head>
<body>
<div class="container">
<h1>My Todo List</h1>
<p class="lead">Get yourself organized!</p>
<br>
<form id="my-form" role="form">
<input id="my-input" class="form-control" type="text" placeholder="Enter a
todo...">
</form>
<br>
<button class="btn btn-primary btn-md">Submit!</button>
<br>
</div>
</body>
</html>
Do you see the selectors? Look for the new id s and class es. The id s will all be
used for JavaScript (in the next part of this tutorial), while the class es are all Bootstrap
styles. If you're curious, check out the Bootstrap page to see more info about the classes
used.
192
CSS
What do you think? Good? Bad? Ugly? Make any additional changes that you'd like.
193
Chrome Developer Tools
Open up the HTML page we worked on. Right Click on the heading. Select "Inspect
Element".
Notice the styles on the right side of the Developer Tools pane associated with the
heading. You can change them directly from that pane. Try adding the following style:
color: red;
This should change the color of the heading to red. Check out the live results in your
browser. You can also edit your HTML in real-time. With Dev Tools open, right click the
paragraph text in the left pane, select "Edit as HTML", and then add another paragraph.
194
Chrome Developer Tools
Again, this is a great way to test temporary HTML and CSS changes live in your
browser. You can also debug and learn how to imitate a desired HTML, CSS, or
JavaScript effect from a different webpage.
Make sure both your .html and .css files are saved.
In the second tutorial we'll add user interactivity with JavaScript and jQuery so that we
can actually add and remove todo items.
Homework
If you want some extra practice, go through the Codecademy series on HTML and
CSS.
Want even more practice with CSS? Try CSS Diner.
195
Flask: FlaskTaskr, Part 4 - Styles, Test Coverage, and Permissions
...............F.
======================================================================
FAIL: test_users_cannot_complete_tasks_that_are_not_created_by_them (__main__.AllT
ests)
----------------------------------------------------------------------
----------------------------------------------------------------------
Ran 17 tests in 0.661s
FAILED (failures=1)
Right where we left off. Before we write the code to get that test to pass, let's put some
of your new HTML and CSS skills to use!
196
Flask: FlaskTaskr, Part 4 - Styles, Test Coverage, and Permissions
Task Complete
Styling No
Test Coverage No
Permissions No
Blueprints No
New Features No
Password Hashing No
Error Logging No
Deployment Options No
Automated Deployments No
197
Templates and Styling
Bootstrap is a front-end framework that makes your app look good right out of the box.
You can just use the generic styles; however, it's best to make some changes so that
the layout doesn't look like a cookie-cutter template. The framework is great. You'll get
the essential tools (mostly CSS and HTML but some Javascript as well) needed to build
a nice-looking website at your disposal. As long as you have a basic understanding of
HTML and CSS, you can create a design quickly.
You can either download the associated files - Bootstrap and jQuery - and place them in
your project directory:
└── static
├── css
│ └── bootstrap.min.css
├── js
│ ├── jquery-1.11.3.min.js
│ └── bootstrap.min.js
└── styles.css
Or you can just link directly to the styles in your _base.html file via a public content
delivery network (CDN), which is a repository of commonly used files.
NOTE If you think there will be times where you'll be working on your app without
Internet access then you should use the former method just to be safe. Your call.
Parent Template
Add the following files to _base.html:
198
Templates and Styling
Now add some bootstrap styles. Update your _base.html with the html in the assests
folder of the project repository.
Without getting into too much detail, we just pulled in the Bootstrap stylesheets, added a
navigation bar to the top, and used the bootstrap classes to style the app. Be sure to
check out the bootstrap documentation for more information as well as this blog post.
If it helps, compare the current template to the code we had before the changes. Have a
look at the flasktaskr-03 version in the repo.
Fire up the app and take a look. See the difference. Now, let's update the child
templates.
Again, go into the repo and grab the flasktaskr-04 version of the login.html, register.html,
tasks.html from the assets folder.
Custom Styles
Update the styles in main.css. Remove everything in there and replace it with these four
classes. Bootstrap is doing the rest.
199
Templates and Styling
body {
padding: 60px 0;
background: #ffffff;
}
.footer {
padding-top: 30px;
}
.error {
color: red;
font-size: .8em;
}
.input-group {
margin: 15px 0;
}
Continue to make as many changes as you'd like. Make it unique. See what you can do
on your own. Show it off. Email it to us at [email protected] to share your app.
$ python project/test.py
You should still see one error. Before we address it though, let's install Coverage.
200
Test Coverage
Test Coverage
The best way to reduce bugs and ensure working code is to have a comprehensive test
suite in place. Working in tandem with unit testing, Coverage testing is a great way to
achieve this as it analyzes your code base and returns a report showing the parts not
covered by a test. Keep in mind that even if 100% of your code is covered by tests, there
still could be issues due to flaws in how you structured your tests.
IMPORTANT: In this section all commands are written assuming that your
working directory is root/project/.
Install:
201
Test Coverage
Check the report in the newly created "htmlcov" directory by opening index.html in your
browser.
If you click on one of the modules, you'll see the actual results, line by line. Lines
highlighted in red are currently not covered by a test.
We do not need to explicitly handle form errors in the / endpoint - error = 'Both
fields are required.' - since errors are handled by the form. So we can remove these
else:
error = 'Both fields are required.'
Re-run coverage:
Our coverage for the views should have jumped from 95% to 97%. Nice.
202
Test Coverage
NOTE: Make sure to add both .coverage and "htmlcov" to the .gitignore file. These
should not be part of your repository.
NOTE: If you get stuck with coverage, try calling coverage erase and starting
over. Each time you run coverage run test.py , coverage remembers and is
basing its calculations on the state of the test suite at that point. Changes to the
code may make it necessary to erase coverage and start fresh.
203
Nose Testing Framework
Running Nose
Now you should be able to run Nose, along with coverage:
FAILED (failures=1)
204
Nose Testing Framework
Homework
Refactor the entire test suite. We're duplicating code in a number of places. Also,
split the tests into two files - test_users.py and test_tasks.py. Please note: This is a
complicated assignment, which you are not expected to get right. In fact, there are a
number of issues with how these tests are setup that should be addressed, which
we will look at later. For now, focus on splitting up the tests, making sure that the
split does not negatively effect coverage, as well as eliminating duplicate code.
NOTE: Going forward, the tests are based on a number of refactors. Be sure to do
this homework and then compare your solution/code to mine found in the repo. Do
your best to refactor on your own and tailor the code found in subsequent sections
to reflect the changes based on your refactors. If you get stuck, feel free to
use/borrow/steal the code from the repo, just make sure you understand it first.
205
Permissions
Permissions
To address the final failing test,
test_users_cannot_complete_tasks_that_are_not_created_by_them() , we need to add user
permissions into the mix. There's really a few different routes we could take.
First, we could use a robust extension like Flask-Principal. Or we could build our own
solution. Think about what we need to accomplish? Do we really need to build a full
permissions solution?
Probably not. Right now we're not even really dealing with permissions. We just need to
throw an error if the user trying to update or delete a task is not the user that added the
task. We can add that functionality with an if statement. Thus, something like Flask-
Principal is too much for our needs right now. If this is an app that you want to continue
to build, perhaps you should add a more robust solution. That's beyond the scope of this
course though.
For now, we'll start by adding the code to get our test to pass and then implement a
basic permissions feature to allow users with the role of 'admin' to update and delete all
posts, regardless of whether they posted them or not.
NOTE If you completed the last homework assignment, you should have two test
files, test_users.py and test_tasks.py. Please double check the code in the repo to
ensure that we're on the same page.
$ nosetests
206
Permissions
$ nosetests
.......F............
======================================================================
FAIL: test_users_cannot_complete_tasks_that_are_not_created_by_them (test_tasks.Ta
sksTests)
----------------------------------------------------------------------
----------------------------------------------------------------------
Ran 21 tests in 1.029s
FAILED (failures=1)
@app.route('/complete/<int:task_id>/')
@login_required
def complete(task_id):
new_id = task_id
task = db.session.query(Task).filter_by(task_id=new_id)
if session['user_id'] == task.first().user_id:
task.update({"status": "0"})
db.session.commit()
flash('The task is complete. Nice.')
return redirect(url_for('tasks'))
else:
flash('You can only update tasks that belong to you.')
return redirect(url_for('tasks'))
Here, we're querying the database for the row associated with the task_id , as we did
before. However, instead of just updating the status to 0 , we're checking to make sure
that the user_id associated with that specific task is the same as the user_id of the
user in session.
$ nosetests
.....................
----------------------------------------------------------------------
Ran 21 tests in 0.942s
OK
Yay!
207
Permissions
Refactor
Before declaring a victory, let's update the test to check for the flashed message:
def test_users_cannot_complete_tasks_that_are_not_created_by_them(self):
self.create_user('Michael', '[email protected]', 'python')
self.login('Michael', 'python')
self.app.get('tasks/', follow_redirects=True)
self.create_task()
self.logout()
self.create_user('Fletcher', '[email protected]', 'python101')
self.login('Fletcher', 'python101')
self.app.get('tasks/', follow_redirects=True)
response = self.app.get("complete/1/", follow_redirects=True)
self.assertNotIn(
b'The task is complete. Nice.', response.data
)
self.assertIn(
b'You can only update tasks that belong to you.', response.data
)
Run your tests again. They should still pass! Now, let's do the same thing for deleting
tasks ...
def test_users_cannot_delete_tasks_that_are_not_created_by_them(self):
self.create_user('Michael', '[email protected]', 'python')
self.login('Michael', 'python')
self.app.get('tasks/', follow_redirects=True)
self.create_task()
self.logout()
self.create_user('Fletcher', '[email protected]', 'python101')
self.login('Fletcher', 'python101')
self.app.get('tasks/', follow_redirects=True)
response = self.app.get("delete/1/", follow_redirects=True)
self.assertIn(
b'You can only delete tasks that belong to you.', response.data
)
208
Permissions
@app.route('/delete/<int:task_id>/')
@login_required
def delete_entry(task_id):
new_id = task_id
task = db.session.query(Task).filter_by(task_id=new_id)
if session['user_id'] == task.first().user_id:
task.delete()
db.session.commit()
flash('The task was deleted. Why not add a new one?')
return redirect(url_for('tasks'))
else:
flash('You can only delete tasks that belong to you.')
return redirect(url_for('tasks'))
Admin Permissions
Finally, let's add two roles to the database - user and admin . The former will be the
default, and if a user has a role of admin they can update or delete any tasks.
209
Permissions
def test_default_user_role(self):
db.session.add(
User(
"Johnny",
"[email protected]",
"johnny"
)
)
db.session.commit()
users = db.session.query(User).all()
print(users)
for user in users:
self.assertEquals(user.role, 'user')
Basically, we need to update the model, update the migration script, and then run the
migration script.
210
Permissions
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, unique=True, nullable=False)
email = db.Column(db.String, unique=True, nullable=False)
password = db.Column(db.String, nullable=False)
tasks = db.relationship('Task', backref='poster')
role = db.Column(db.String, default='user')
def __repr__(self):
return '<User {0}>'.format(self.name)
# project/db_migrate.py
# # save all rows as a list of tuples; set posted_date to now and user_id to 1
# data = [(row[0], row[1], row[2], row[3],
# datetime.now(), 1) for row in c.fetchall()]
211
Permissions
$ python db_migrate.py
If all goes well you shouldn't get any errors. Make sure the migration worked. Open the
SQLite Database browser. You should see all the same users plus an additional 'role'
column.
212
Permissions
So, now that users are associated with a role, let's update the logic in our complete()
and delete_entry() functions to allow users with a role of admin to be able to update
and delete all tasks.
def test_admin_users_can_complete_tasks_that_are_not_created_by_them(self):
self.create_user()
self.login('Michael', 'python')
self.app.get('tasks/', follow_redirects=True)
self.create_task()
self.logout()
self.create_admin_user()
self.login('Superman', 'allpowerful')
self.app.get('tasks/', follow_redirects=True)
response = self.app.get("complete/1/", follow_redirects=True)
self.assertNotIn(
'You can only update tasks that belong to you.', response.data
)
def test_admin_users_can_delete_tasks_that_are_not_created_by_them(self):
self.create_user()
self.login('Michael', 'python')
self.app.get('tasks/', follow_redirects=True)
self.create_task()
self.logout()
self.create_admin_user()
self.login('Superman', 'allpowerful')
self.app.get('tasks/', follow_redirects=True)
response = self.app.get("delete/1/", follow_redirects=True)
self.assertNotIn(
'You can only delete tasks that belong to you.', response.data
)
def create_admin_user(self):
new_user = User(
name='Superman',
email='[email protected]',
password='allpowerful',
role='admin'
)
db.session.add(new_user)
db.session.commit()
213
Permissions
@app.route('/logout/')
@login_required
def logout():
session.pop('logged_in', None)
session.pop('user_id', None)
session.pop('role', None)
flash('Goodbye!')
return redirect(url_for('login'))
Here, we're simply adding the user's role to the session cookie on the login, then
removing it on logout.
Update the complete() and `delete_entry() functions by simply updating the conditional
for both:
Now the if user_id in session matches the user_id that posted the task or if the user's
role is 'admin', then that user has permission to update or delete the task.
Retest:
214
Permissions
$ nosetests
.........................
----------------------------------------------------------------------
Ran 25 tests in 1.252s
OK
Awesome.
Next time
This brings us to a nice stopping point. When we start back up, we'll look at restructuring
our app using a modular design pattern called Blueprints.
Homework
Be sure to read over the official documentation on Blueprints. Cheers!
215
Flask: FlaskTaskr, Part 5 - Blueprints
Task Complete
Testing Yes
Styling Yes
Permissions Yes
Blueprints No
New Features No
Password Hashing No
Error Logging No
Deployment Options No
Automated Deployments No
216
Flask: FlaskTaskr, Part 5 - Blueprints
1. Read about the benefits of using Blueprints from the official Flask documentation.
2. Run the test suite again, making sure all tests pass.
3. Ensure all dependencies have been added to requirements.txt: pip freeze >
requirements.txt .
217
What are Blueprints?
For a blog, you could have a Blueprint for handling user authentication, another could be
used to manage posts, and one more could provide a robust admin panel. Each
Blueprint is really a separate app, each handling a different function. Designing your app
in this manner significantly increases overall maintainability and re-usability by
encapsulating code, templates, and static files/media. This, in turn, decreases
development time as it makes it easier for developers to find mistakes, so less time is
spent on fixing bugs.
NOTE: Before moving on, please note that this is not a complicated lesson but
there are a number of layers to it so it can be confusing. Programming in general
is nothing more than combining various layers of knowledge on top of one
another. Take it slow. Read through the lesson once without touching any code.
Read. Take notes. Draw diagrams. Then when you're ready, go through it again
and refactor your app. This is a manual process that will only make sense to you if
you first understand the "what and why" before diving in.
Example Code
Let’s look at a sample Blueprint for the management of posts for a blog. admin , posts ,
and users represent separate apps.
218
What are Blueprints?
├── _config.py
├── run.py
└── blog
├── __init__.py
├── admin
│ ├── static
│ ├── templates
│ └── views.py
├── models.py
├── posts
│ ├── static
│ ├── templates
│ └── views.py
└── users
├── static
├── templates
└── views.py
# blog/posts/views.py
@post_blueprint.route("/")
def read_posts():
posts = Post.query.all()
return render_template('posts.html', posts=posts)
219
What are Blueprints?
Here, a new Blueprint is initialized that gets assigned to the blueprint variable. Each
view function is bound with the @blueprint.route , which let's Flask know that each
function is available for use within the entire app.
Again, the above code defined the Blueprint, now we need to register it with the main
Flask object:
# blog/init.py
blueprint = Flask(__name__)
blueprint.register_blueprint(post_blueprint)
That's it.
220
Refactoring
Refactoring
Now, let's convert FlaskTaskr over to the Blueprint pattern.
Step 1: Planning
First, we need to determine how we should logically divide up the app. The simplest way
is by functionality:
Next, we need to determine what the new directory structure will look like. What does
that mean exactly? Well, we need a folder for each Blueprint, "users" and "tasks", each
containing:
├── __init__.py
├── forms.py
├── static
├── templates
└── views.py
221
Refactoring
├── _config.py
├── db_create.py
├── db_migrate.py
├── flasktaskr.db
├── forms.py
├── models.py
├── run.py
├── static
│ ├── css
│ │ └── main.css
│ ├── img
│ └── js
├── templates
│ ├── _base.html
│ ├── login.html
│ ├── register.html
│ └── tasks.html
├── test.db
├── test_tasks.py
├── test_users.py
└── views.py
Thus, we'll probably want the following structure when all is said and done:
222
Refactoring
├── db_create.py
├── db_migrate.py
├── project
│ ├── __init__.py
│ ├── _config.py
│ ├── flasktaskr.db
│ ├── models.py
│ ├── static
│ │ ├── css
│ │ │ └── main.css
│ │ ├── img
│ │ └── js
│ ├── tasks
│ │ ├── __init__.py
│ │ ├── forms.py
│ │ └── views.py
│ ├── templates
│ │ ├── _base.html
│ │ ├── login.html
│ │ ├── register.html
│ │ └── tasks.html
│ ├── test.db
│ └── users
│ ├── __init__.py
│ ├── forms.py
│ └── views.py
├── requirements.txt
├── run.py
└── tests
├── test_tasks.py
└── test_users.py
Study this new structure. Take note of the difference between the files and folders within
each of the Blueprints and the project root.
223
Refactoring
directory.
5. Add a "tests" directory outside the "project" directory, and then move both
test_tasks.py, and test_users.py* to the new directory.
6. Then move the following files from outside the project directory - db_create.py,
db_migrate.py, and run.py.
├── db_create.py
├── db_migrate.py
├── project
│ ├── __init__.py
│ ├── _config.py
│ ├── forms.py
│ ├── models.py
│ ├── static
│ │ ├── css
│ │ │ └── main.css
│ │ ├── img
│ │ └── js
│ ├── tasks
│ │ └── __init__.py
│ ├── templates
│ │ ├── _base.html
│ │ ├── login.html
│ │ ├── register.html
│ │ └── tasks.html
│ ├── users
│ │ └── __init__.py
│ └── views.py
├── run.py
└── tests
├── test_tasks.py
└── test_users.py
Users Blueprint
224
Refactoring
Views
Pull up the flasktaskr-05/users/views.py file from the inside assests folder within the
exercises repo and cut and paste it into you project. Have a look at the code.
We defined the users Blueprint and bound each function with the
@users_blueprint.route decorator so that when we register the Blueprint, Flask will
Forms
Back to the assets folder in the repo, flasktaskr-05/users/forms.py. Again, cut and paste
this into you project.
Tasks Blueprint
We'll do the same for the tasks blueprint.
Views
Pick out the differences between this code and the original task crud methods.
Forms
tasks.html
Last one! Keep the tasks.html in project/templates. Update it with the code in the assests
folder.
PAUSE
Before continuing, be sure you have thoroughly compared the new and old code.
Understanding the difference is essential.
__init__.py
225
Refactoring
# project/__init__.py
app = Flask(__name__)
app.config.from_pyfile('_config.py')
db = SQLAlchemy(app)
run.py
# run.py
From:
To:
Step 5: Testing
Since we moved a number of files around, your virtual environment may not hold the
correct requirements. To be safe, remove the virtualenv, then and create a new one. Be
sure that you have the most recent dependencies written to your requirements.txt file. If
226
Refactoring
you are not sure run the freeze command (remember the syntax?) before deleting your
virtualenv.
$ deactivate
$ rm -rf env/
$ pyvenv-3.5 env
$ source env/bin/activate
$ pip install -r requirements.txt
$ python run.py
If all went well, you shouldn't get any errors. Make sure you can load the main page.
Update db_create.py
227
Refactoring
# db_create.py
# insert data
db.session.add(
User("admin", "[email protected]", "admin", "admin")
)
db.session.add(
Task("Finish this tutorial", date(2015, 3, 13), 10, date(2015, 2, 13), 1, 1)
)
db.session.add(
Task("Finish Real Python", date(2015, 3, 13), 10, date(2015, 2, 13), 1, 1)
)
$ python db_create.py
Make sure to check the database in the SQLite Browser to ensure all the data was
added properly.
228
Refactoring
NOTE: If you had trouble following the restructuring, please view the "flasktaskr-
05" directory in the exercises repo.
import os
import unittest
OK
229
Flask: FlaskTaskr, Part 6 - New features and Error Handling
Task Complete
Testing Yes
Styling Yes
Permissions Yes
Blueprints Yes
New Features No
Password Hashing No
Error Logging No
Deployment Options No
Automated Deployments No
Alright. Let's add some new features utilizing test driven development.
230
Flask: FlaskTaskr, Part 6 - New features and Error Handling
231
New Features
New Features
Display User Name
Here we just want to display the logged in user's name on the task page, on the right
side of the navigation bar.
Test
def test_task_template_displays_logged_in_user_name(self):
self.register(
'Fletcher', '[email protected]', 'python101', 'python101'
)
self.login('Fletcher', 'python101')
response = self.app.get('tasks/', follow_redirects=True)
self.assertIn(b'Fletcher', response.data)
Here we assert that the rendered HTML contains the logged in user's name. Run your
tests to watch this one fail. Now write just enough code to get it to pass.
Code
First, let's add the user's name to the session during the logging in process in
users/views.py:
session['name'] = user.name
Simply add the above code to the if block in the login() function. Also, be sure to
update the logout() function as well:
@users_blueprint.route('/logout/')
@login_required
def logout():
session.pop('logged_in', None)
session.pop('user_id', None)
session.pop('role', None)
session.pop('name', None)
flash('Goodbye!')
return redirect(url_for('users.login'))
232
New Features
Next, update the tasks() function within tasks/views.py to pass in the username to the
template:
@tasks_blueprint.route('/tasks/')
@login_required
def tasks():
return render_template(
'tasks.html',
form=AddTaskForm(request.form),
open_tasks=open_tasks(),
closed_tasks=closed_tasks(),
username=session['name']
)
233
New Features
So, here we test whether the logged_in key is in the session object - {% if
session.logged_in %} - then we display the username like so: <li><a>Welcome,
{{username}}.</a></li> .
Test
234
New Features
def test_users_cannot_see_task_modify_links_for_tasks_not_created_by_them(self)
:
self.register('Michael', '[email protected]', 'python', 'python')
self.login('Michael', 'python')
self.app.get('tasks/', follow_redirects=True)
self.create_task()
self.logout()
self.register(
'Fletcher', '[email protected]', 'python101', 'python101'
)
response = self.login('Fletcher', 'python101')
self.app.get('tasks/', follow_redirects=True)
self.assertNotIn(b'Mark as complete', response.data)
self.assertNotIn(b'Delete', response.data)
def test_users_can_see_task_modify_links_for_tasks_created_by_them(self):
self.register('Michael', '[email protected]', 'python', 'python')
self.login('Michael', 'python')
self.app.get('tasks/', follow_redirects=True)
self.create_task()
self.logout()
self.register(
'Fletcher', '[email protected]', 'python101', 'python101'
)
self.login('Fletcher', 'python101')
self.app.get('tasks/', follow_redirects=True)
response = self.create_task()
self.assertIn(b'complete/2/', response.data)
self.assertIn(b'complete/2/', response.data)
def test_admin_users_can_see_task_modify_links_for_all_tasks(self):
self.register('Michael', '[email protected]', 'python', 'python')
self.login('Michael', 'python')
self.app.get('tasks/', follow_redirects=True)
self.create_task()
self.logout()
self.create_admin_user()
self.login('Superman', 'allpowerful')
self.app.get('tasks/', follow_redirects=True)
response = self.create_task()
self.assertIn(b'complete/1/', response.data)
self.assertIn(b'delete/1/', response.data)
self.assertIn(b'complete/2/', response.data)
self.assertIn(b'delete/2/', response.data)
Look closely at these tests before you run them. Will they both fail? Run them. You
should only get one failure because the links already exist for all tasks. Essentially, the
last two tests are to ensure that no regressions occur when we write the code to make
235
New Features
Code
As for the code, we simply need to make a few changes in the
project/templates/tasks.html template:
and
236
New Features
Essentially, we're testing to see if the name of the poster matches the name of the user
in the session and whether the user's role is admin . If either test evaluates to True ,
then we display the links to modify tasks.
That's it for the added features. What else would you like to see? Implement it utilizing
test driven development.
237
Password Hashing
Password Hashing
Right now our passwords are saved as plain text in the database, we need to securely
hash them before they are added to the database to prevent them from being recovered
by a hostile outsider. Keep in mind that unless you are a cryptographer with an
advanced degree in computer science or mathematics you should never write your own
cryptographic hasher. There's no need to reinvent the wheel. The problem of securely
storing passwords has already been solved.
SEE ALSO: Want more information regarding securely storing passwords? The
Open Web Application Security Project (OWASP), one of the application security
industry’s most trusted resource, provides a number of recommendations for
secure password storage. Highly recommended.
Setup Flask-Bcrypt
Let's use a Flask extension called Flask-Bcrypt:
To set up the extension, simply import the class wrapper and pass the Flask app object
into it:
238
Password Hashing
# project/__init__.py
app = Flask(__name__)
app.config.from_pyfile('_config.py')
bcrypt = Bcrypt(app)
db = SQLAlchemy(app)
if form.validate_on_submit():
new_user = User(
form.name.data,
form.email.data,
bcrypt.generate_password_hash(form.password.data)
)
239
Password Hashing
Manual Test
Delete the database. Then update the db_create.py script:
# db_create.py
Unit Test
Run the test suite:
240
Password Hashing
FAILED (errors=9)
Take a look at the errors: ValueError: Invalid salt . Essentially, since we are manually
hashing the password in the view, we need to do the same in every other place we
create a password. Update the following helper methods within both of the test files:
and
def create_admin_user(self):
new_user = User(
name='Superman',
email='[email protected]',
password=bcrypt.generate_password_hash('allpowerful'),
role='admin'
)
db.session.add(new_user)
db.session.commit()
241
Password Hashing
def test_users_can_register(self):
new_user = User("michael", "[email protected]", bcrypt.generate_password_has
h('michaelherman'))
db.session.add(new_user)
db.session.commit()
test = db.session.query(User).all()
for t in test:
t.name
assert t.name == "michael"
That's it. Run the tests again. They all should pass.
242
Custom Error Pages
Test
Let's add these to a new test file called test_main.py:
# project/test_main.py
import os
import unittest
TEST_DB = 'test.db'
class MainTests(unittest.TestCase):
############################
#### setup and teardown ####
############################
243
Custom Error Pages
########################
#### helper methods ####
########################
###############
#### tests ####
###############
def test_404_error(self):
response = self.app.get('/this-route-does-not-exist/')
self.assertEquals(response.status_code, 404)
def test_500_error(self):
bad_user = User(
name='Jeremy',
email='[email protected]',
password='django'
)
db.session.add(bad_user)
db.session.commit()
response = self.login('Jeremy', 'django')
self.assertEquals(response.status_code, 500)
if __name__ == "__main__":
unittest.main()
Now when you run the tests, only the test_500_error() error fails: ValueError: Invalid
salt . Remember: It's a good practice to not only check the status code, but some text
on the rendered HTML as well. In the case of the 404 error, we have no idea what the
end user sees on their end.
Is that really want we want the end user to see? Probably not. Let's handle that more
gracefully. Think about what you'd like the user to see and then update the test:
def test_404_error(self):
response = self.app.get('/this-route-does-not-exist/')
self.assertEquals(response.status_code, 404)
self.assertIn(b'Sorry. There\'s nothing here.', response.data)
244
Custom Error Pages
So, what do you think the end user sees when the exception, ValueError: Invalid salt ,
is thrown? Let's assume the worst: They probably see that exact same ugly error.
Update the test to ensure that (a) the client does not see that error and (b) that the text
we do want them to see is visible in the response:
def test_500_error(self):
bad_user = User(
name='Jeremy',
email='[email protected]',
password='django'
)
db.session.add(bad_user)
db.session.commit()
response = self.login('Jeremy', 'django')
self.assertEquals(response.status_code, 500)
self.assertNotIn(b'ValueError: Invalid salt', response.data)
self.assertIn(b'Something went terribly wrong.', response.data)
View
The error handlers are set just like any other view. Update project/__init__.py :
@app.errorhandler(404)
def not_found(error):
return render_template('404.html'), 404
This function catches the 404 error, and replaces the default template with 404.html
(which we will create next). Notice how we also have to specify the status code, 404 ,
we want thrown as well.
Template
Create a new template in project/templates called 404.html:
245
Custom Error Pages
{% extends "_base.html" %}
{% block content %}
<h1>404</h1>
<p>Sorry. There's nothing here.</p>
<p><a href="{{url_for('users.login')}}">Go back home</a></p>
{% endblock %}
Test
test_404_error should now pass. Manually test it again as well: Navigate to
http://localhost:5000/this-route-does-not-exist/. Nice.
View
pdate *poect/\\init\_\_.py :
@app.errorhandler(500)
def internal_error(error):
return render_template('500.html'), 500
Based on the last 404 errorhandler() can you describe what's happening here?
Template
Create a new template in project/templates called 500.html:
{% extends "_base.html" %}
{% block content %}
<h1>500</h1>
<p>Something went terribly wrong. Fortunately we are working to fix it right now!
</p>
<p><a href="{{url_for('users.login')}}">Go back home</a></p>
{% endblock %}
246
Custom Error Pages
Test
If you run the tests, you'll see we still have an that invalid salt error. Update the the
test_500_error :
def test_500_error(self):
bad_user = User(
name='Jeremy',
email='[email protected]',
password='django'
)
db.session.add(bad_user)
db.session.commit()
self.assertRaises(ValueError, self.login, 'Jeremy', 'django')
try:
response = self.login('Jeremy', 'django')
self.assertEquals(response.status_code, 500)
except ValueError:
pass
Now, let's manually test this to see what's actually happening for the user. Temporarily
update the part of the register() function, in users/views.py, where the new user is
created.
Change:
new_user = User(
form.name.data,
form.email.data,
bcrypt.generate_password_hash(form.password.data)
)
To:
new_user = User(
form.name.data,
form.email.data,
form.password.data
)
247
Custom Error Pages
Now when a new user is registered, the password will not be hashed. Test this out. Fire
up the server. Register a new user, and then try to log in. You should see the Flask
debug page come up, with the ValueError: Invalid salt . Why is this page populating?
Simple: debug mode is on in the run.py file: debug=True .
register another new user, and then try to log in. Now you should see our custom 500
error page.
new_user = User(
form.name.data,
form.email.data,
bcrypt.generate_password_hash(form.password.data)
)
Also, notice that when we run the tests again the test still doesn't pass. Why? Part of the
problem has to do, again, with debug mode set to True . Let's fix that since tests should
always run with debug mode off.
# run.py
Fire up the server. It should still run in debug mode. Now let's update setUp() for all
test files:
248
Custom Error Pages
def setUp(self):
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
app.config['DEBUG'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
os.path.join(basedir, TEST_DB)
self.app = app.test_client()
db.create_all()
self.assertEquals(app.debug, False)
Here, we explicitly turn off debug mode - app.config['DEBUG'] = False - and then also
run a test to ensure that for each test ran, it's set to False -
self.assertEquals(app.debug, False) . Run your tests again. The test_500_error test
still doesn't pass. Go ahead and remove it for now. We'll discuss why later and how to
set this test up right. For now, let's move on to logging all errors in our app.
Let's face it, your code will never be 100% error free, and you will never be able to write
tests to cover everything that could potentially happen. Thus, it's vital that you set up a
means of capturing all errors so that you can spot trends, setup additional error
handlers, and, of course, fix errors that occur.
It's very easy to setup logging at the server level or within the Flask application itself. In
fact, if your app is running in production, by default errors are added to the logger. You
can then grab those errors and log them to a file as well as have the most critical errors
sent to you via email. There are also numerous third party libraries that can manage the
logging of errors on your behalf.
At the very least, you should set up a means of emailing critical errors since those
should be addressed immediately. Please see the official documentation for setting this
up. It's easy to setup, so we won't cover it here. Instead, let's build our own custom
solution to log errors to a file.
Here's a basic logger that uses the logging library. Update the error handler views in
project/__init__.py :
249
Custom Error Pages
@app.errorhandler(404)
def page_not_found(error):
if app.debug is not True:
now = datetime.datetime.now()
r = request.url
with open('error.log', 'a') as f:
current_timestamp = now.strftime("%d-%m-%Y %H:%M:%S")
f.write("\n404 error at {}: {} ".format(current_timestamp, r))
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_error(error):
db.session.rollback()
if app.debug is not True:
now = datetime.datetime.now()
r = request.url
with open('error.log', 'a') as f:
current_timestamp = now.strftime("%d-%m-%Y %H:%M:%S")
f.write("\n500 error at {}: {} ".format(current_timestamp, r))
return render_template('500.html'), 500
import datetime
from flask import Flask, render_template, request
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.bcrypt import Bcrypt
The code is simple: When an error occurs we add the timestamp and the request to an
error log, error.log. Notice how these errors are only logged when debug mode is off.
Why do you think we would want that? Well, as you know, when the debug mode is on,
errors are caught by the Flask debugger and then handled gracefully by displaying a
nice formatted page with info on how to correct the issue. Since this is caught by the
debugger, it will not throw the right errors. Further, since debug mode will always be off
in production, these errors will be caught by the custom error logger. Make sense?
Save the code. Turn debug mode off. Fire up the server, then navigate to
http://localhost:5000/does_not_exist. You should see the error.log file with the following
error logged:
250
Custom Error Pages
Let's try a 500 error. Again, temporarily update the part of the register() function, in
users/views.py:
Change:
new_user = User(
form.name.data,
form.email.data,
bcrypt.generate_password_hash(form.password.data)
)
To:
new_user = User(
form.name.data,
form.email.data,
form.password.data
)
Kill the server. Fire it back up. Register a new user, and then try to login. You should see
the 500 error page the following error in the error log:
That's it. Make sure to add bcrypt back to users/views.py and turn debug mode back
on.
251
Flask: FlaskTaskr, Part 7 - Deployment
Task Complete
Testing Yes
Styling Yes
Permissions Yes
Blueprints Yes
Deployment Options No
Automated Deployments No
252
Flask: FlaskTaskr, Part 7 - Deployment
253
Deployment
Deployment
As far as deployment options go, PythonAnywhere and Heroku are great. We'll use
PythonAnywhere throughout the web2py sections, so let's use Heroku with this app.
SEE ALSO: Check out the official Heroku docs for deploying an app if you need
additional help.
Setup
Deploying an app to Heroku is ridiculously easy:
extension). The word "web" indicates to Heroku that the application will be attached
to HTTP once deployed to Heroku.
On your local machine, the application runs on port 5000 by default. On Heroku, the
application must run on a random port specified by Heroku. We will identify this port
number by reading the environment variable 'PORT' and passing it to app.run :
# run.py
import os
from project import app
254
Deployment
Set debug to False within the config file. Why? Again, debug mode provides a handy
debugger for when errors occur, which is great during development, but you never want
end users to see this. It's a security vulnerability, as it is possible to execute commands
through the debugger.
Deploy
When you PUSH to Heroku, you have to update your local Git repository. Commit your
updated code.
$ heroku create
$ heroku ps
$ heroku open
If you see errors, open the Heroku log to view all errors and output:
$ heroku logs
255
Deployment
OK
That's it. Make sure that you also PUSH your local repository to Github.
256
Automated Deployments
Automated Deployments
In the last lesson we manually uploaded our code to Heroku. When you only need to
deploy code to Heroku and GitHub, you can get away with doing this manually.
However, if you are working with multiple servers where a number of developers are
sending code each day, you will want to automate this process. We can use Fabric for
such automation.
WARNING: As of writing (October 18, 2016), Fabric only works with Python 2.x.
Please review the Development roadmap for more information.
Setup
As always, start by installing the dependency with Pip:
Fabric is controlled by a file called a fabfile, fabfile.py. You define all of the actions (or
commands) that Fabric takes in this file. Create the file within your app's root directory.
The file itself takes a number of commands. Here's a brief list of some of the most
common commands:
prompt prompts a user with text and returns the user input
Preparing
Add the following code to fabfile.py:
257
Automated Deployments
def test():
local("nosetests -v")
def commit():
message = raw_input("Enter a git commit message: ")
local("git add . && git commit -am '{}'".format(message))
def push():
local("git push origin master")
def prepare():
test()
commit()
push()
Essentially, we imported the local method from Fabric, then ran the basic shell
commands for testing and PUSHing to GitHub as you've seen before.
$ fab prepare
If all goes well, this should run the tests, commit the code to your local repo, and then
deploy to GitHub.
Testing
What happens if your tests fail? Wouldn't you want to abort the process? Probably.
Update your test() function to:
def test():
with settings(warn_only=True):
result = local("nosetests -v", capture=True)
if result.failed and not confirm("Tests failed. Continue?"):
abort("Aborted at user request.")
258
Automated Deployments
Here, if a test fails, then the user is asked to confirm whether or not the script should
continue running.
Deploying
Finally, let's deploy to Heroku as well:
def pull():
local("git pull origin master")
def heroku():
local("git push heroku master")
def heroku_test():
local("heroku run nosetests -v")
def deploy():
pull()
test()
commit()
heroku()
heroku_test()
Now when you run the deploy() function, you PULL the latest code from GitHub, test
the code, commit it to your local repo, PUSH to Heroku, and then test on Heroku. The
commands should all look familiar.
def rollback():
local("heroku rollback")
Updated file:
# prep
def test():
with settings(warn_only=True):
result = local("nosetests -v", capture=True)
if result.failed and not confirm("Tests failed. Continue?"):
abort("Aborted at user request.")
259
Automated Deployments
def commit():
message = raw_input("Enter a git commit message: ")
local("git add . && git commit -am '{}'".format(message))
def push():
local("git push origin master")
def prepare():
test()
commit()
push()
# deploy
def pull():
local("git pull origin master")
def heroku():
local("git push heroku master")
def heroku_test():
local("heroku run nosetests -v")
def deploy():
# pull()
test()
# commit()
heroku()
heroku_test()
# rollback
def rollback():
local("heroku rollback")
If you do run into an error on Heroku, you want to immediately load a prior commit to get
it working. You can do this quickly now by running the command:
$ fab rollback
260
Automated Deployments
Keep in mind that this is just a temporary fix. After you rollback, make sure to fix the
issue locally and then PUSH the updated code to Heroku.
261
Flask: FlaskTaskr, Part 8 - RESTful API
Task Complete
Testing Yes
Styling Yes
Permissions Yes
Blueprints Yes
262
Flask: FlaskTaskr, Part 8 - RESTful API
263
Building a RESTful API
What's an API?
Put simply, an API is collection of functions that other programs can use to access or
manipulate data from a determine database. Each function has an associated endpoint
(also called a resource). One can make changes to a resource via the HTTP
methods/verbs:
WARNING: When you set up a RESTful API, you expose much of your app's
internal system to the rest of the world (or even other areas within your own
organization). Keep this in mind. Only allow users or programs access to a limited
set of data, using only the needed HTTP methods. Never expose more than
necessary.
For example, in the FlaskTaskr app, the endpoint /tasks/ could be for the collection,
while /tasks/<id>/ could be for a specific element (e.g., a single task) from the
collection.
NOTE: If you're using an ORM, like SQLAlchemy, more often than not, resources
are generally your models.
264
Building a RESTful API
/tasks/
View all Add Update all Delete all
tasks task tasks tasks
NOTE: In our API, we are only going to be working with the GET request since it
is read-only. In other words, we do not want external users to add or manipulate
data.
Workflow
Workflow for creating an API via Flask:
NOTE: This workflow is just for the backend. We'll be responding with JSON
based on a user request - but that's it. The frontend of the app could also utilize
this data as well. For example, you could use jQuery or a front-end library or
framework such as React, Ember, or Angular to consume the data from the API
and display it to the end user.
We already have the first four steps done, so let's start with creating our actual
endpoints. Also, as of right now, we're not going to have the ability to add specific
operations with query strings.
First Endpoint
From the design above, the two URLs we want our app to support are /tasks/ and
/tasks/<id> . Let's start with the former.
Test
265
Building a RESTful API
First, let's add a new test file to test our API code. Name the file test_api.py.
Pull up the test_api.py file from the inside assests folder within the exercises repo and
cut and paste it into you project.
1. Like our previous tests, we first set up a test client along with the setUp() and
tearDown() methods.
2. The helper method, add_tasks() , is used to add dummy tasks to the test database.
3. Then when we run the test, we add the tasks to the DB, hit the endpoint, then test
that the appropriate response is returned.
What should happen when you run the tests? It will fail, of course.
Code
Start by creating a new Blueprint called "api". You just need two files - __init__.py and
views.py.
Pull up the api/views.py file from the inside assests folder within the exercises repo and
cut and paste it into you project.
1. We map the URL '/api/v1/tasks/' to the api_tasks() function so once that URL is
requested via a GET request, we query the database to grab the first 10 records
from the tasks table.
2. Next we create a dictionary out of each returned record from the database.
3. Finally, since our API supports JSON, we pass in the dictionary to the jsonify()
function to render a JSON response back to the browser.
SEE ALSO: Take a minute to read about the jsonify() function from the official
Flask documentation. This is a powerful function, used to simplify and beautify
your code. It not only takes care of serialization, but it also validates the data itself
and adds the appropriate status code and header. Without it, your response object
would need to look something like this: return json.dumps(data), 200, { "Content-
Type" : "application/json"}
266
Building a RESTful API
Test Again
Does the new test pass?
$ nosetests tests/test_api.py
.
----------------------------------------------------------------------
Ran 1 test in 0.050s
OK
Of course!
NOTE: Notice how we isolated the tests to just test test_api.py. This speeds up
development as we do not have to run the entire test suite each time. That said,
be sure to run all the tests after you finish implementing a feature to make sure
there are no regressions.
Second Endpoint
Now, let's add the next endpoint.
Test
267
Building a RESTful API
def test_resource_endpoint_returns_correct_data(self):
self.add_tasks()
response = self.app.get('api/v1/tasks/2', follow_redirects=True)
self.assertEquals(response.status_code, 200)
self.assertEquals(response.mimetype, 'application/json')
self.assertIn(b'Purchase Real Python', response.data)
self.assertNotIn(b'Run around in circles', response.data)
@api_blueprint.route('/api/v1/tasks/<int:task_id>')
def task(task_id):
result = db.session.query(Task).filter_by(task_id=task_id).first()
json_result = {
'task_id': result.task_id,
'task name': result.name,
'due date': str(result.due_date),
'priority': result.priority,
'posted date': str(result.posted_date),
'status': result.status,
'user id': result.user_id
}
return jsonify(items=json_result)
This is very similar to our last endpoint. The difference is that we are using a dynamic
route to grab a query and render a specific task_id . Manually test this out. Navigate to
http://localhost:5000/api/v1/tasks/1. You should see a single task.
Test Again
$ nosetests tests/test_api.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.068s
OK
268
Building a RESTful API
Wrong. What happens if the URL you hit is for a task_id that does not exist? Try it.
Navigate to http://localhost:5000/api/v1/tasks/209 in your browser. You should see a 500
error. It's okay for the element not to exist, but we need to return a user-friendly error.
Let's update the code.
Updated Endpoints
Test
def test_invalid_resource_endpoint_returns_error(self):
self.add_tasks()
response = self.app.get('api/v1/tasks/209', follow_redirects=True)
self.assertEquals(response.status_code, 404)
self.assertEquals(response.mimetype, 'application/json')
self.assertIn(b'Element does not exist', response.data)
@api_blueprint.route('/api/v1/tasks/<int:task_id>')
def task(task_id):
result = db.session.query(Task).filter_by(task_id=task_id).first()
if result:
json_result = {
'task_id': result.task_id,
'task name': result.name,
'due date': str(result.due_date),
'priority': result.priority,
'posted date': str(result.posted_date),
'status': result.status,
'user id': result.user_id
}
return jsonify(items=json_result)
else:
result = {"error": "Element does not exist"}
return jsonify(result)
Before you test it out, open the Network tab within Chrome Developer Tools to confirm
the status code.
269
Building a RESTful API
@api_blueprint.route('/api/v1/tasks/<int:task_id>')
def task(task_id):
result = db.session.query(Task).filter_by(task_id=task_id).first()
if result:
result = {
'task_id': result.task_id,
'task name': result.name,
'due date': str(result.due_date),
'priority': result.priority,
'posted date': str(result.posted_date),
'status': result.status,
'user id': result.user_id
}
code = 200
else:
result = {"error": "Element does not exist"}
code = 404
return make_response(jsonify(result), code)
270
Building a RESTful API
Did you notice the new method make_response() ? Read more about it here. Be sure to
import it as well.
Test Again
And manually test it again. You should see a 200 status code for a task_id that exists,
as well as the appropriate JSON data, and a 404 status code for a task_id that does
not exist. Good.
$ nosetests tests/test_api.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.100s
OK
Let's also test the entire test suite to ensure we didn't break anything anywhere else in
the codebase:
OK
Perfect.
271
Building a RESTful API
Deploy
Now deploy to Heroku!
Homework
Now that you've seen how to create a REST API from scratch, check out the Flask-
RESTful extension that simplifies the process. Want a challenge? See if you can set
up this extension, then add the ability for all users to POST new tasks. Once
complete, give logged in users the ability to PUT (update) and DELETE individual
elements within the collection - e.g., individual tasks.
272
Interlude: Flask Boilerplate Template and Workflow
Setup
First, navigate to a directory outside of your "realpython" directory. The "desktop" or
"documents" directories are good choices since they are easy to access. Then clone the
boilerplate template from Github:
273
Interlude: Flask Boilerplate Template and Workflow
├── LICENSE
├── README.md
├── manage.py
├── project
│ ├── __init__.py
│ ├── client
│ │ ├── static
│ │ │ ├── main.css
│ │ │ └── main.js
│ │ └── templates
│ │ ├── _base.html
│ │ ├── errors
│ │ │ ├── 401.html
│ │ │ ├── 403.html
│ │ │ ├── 404.html
│ │ │ └── 500.html
│ │ ├── footer.html
│ │ ├── header.html
│ │ ├── main
│ │ │ ├── about.html
│ │ │ └── home.html
│ │ └── user
│ │ ├── login.html
│ │ ├── members.html
│ │ └── register.html
│ ├── server
│ │ ├── __init__.py
│ │ ├── config.py
│ │ ├── dev.sqlite
│ │ ├── main
│ │ │ ├── __init__.py
│ │ │ └── views.py
│ │ ├── models.py
│ │ └── user
│ │ ├── __init__.py
│ │ ├── forms.py
│ │ └── views.py
│ └── tests
│ ├── __init__.py
│ ├── base.py
│ ├── helpers.py
│ ├── test__config.py
│ ├── test_main.py
│ └── test_user.py
└── requirements.txt
Activate a virtualenv, and then install the various libraries and dependencies:
274
Interlude: Flask Boilerplate Template and Workflow
You can also view the dependencies by running the command pip freeze :
alembic==0.8.9
bcrypt==3.1.1
blinker==1.4
cffi==1.9.1
click==6.6
coverage==4.2
dominate==2.3.1
Flask==0.11.1
Flask-Bcrypt==0.7.1
Flask-Bootstrap==3.3.7.0
Flask-DebugToolbar==0.10.0
Flask-Login==0.4.0
Flask-Migrate==2.0.2
Flask-Script==2.0.5
Flask-SQLAlchemy==2.1
Flask-Testing==0.6.1
Flask-WTF==0.13.1
itsdangerous==0.24
Jinja2==2.8
Mako==1.0.6
MarkupSafe==0.23
pycparser==2.17
python-editor==1.0.3
six==1.10.0
SQLAlchemy==1.1.4
visitor==0.1.3
Werkzeug==0.11.11
WTForms==2.1
Development workflow
Now that you have your skeleton app up, it's time to start developing locally.
NOTE: Be sure to review the README to learn how to set up the database and
run the app.
275
Interlude: Flask Boilerplate Template and Workflow
That's it. You now have a skeleton app to work with to build your own applications.
Cheers!
276
Flask: FlaskTaskr, Part 9: Continuous Integration and Delivery
CI is a practice used by developers to help ensure that they are not introducing bugs into
new code or causing old code to break. In practice, members of a development team
integrate their code frequently, using a version control system like Git, which is then
validated through the use of automated tests. Often times, such integrations happen
many times a day. Plus, once the code is ready to be deployed (after many integrations),
CD is used to reduce deployment times as well as errors by automating the process.
If you're new to Git, Github, or this workflow, check out the Git Branching at a Glance
chapter in the third Real Python course as well as this excellent tutorial. We will be using
this workflow in this chapter.
277
Workflow
Workflow
The workflow that we will use is fairly simple:
Before jumping in, let's quickly look at a few CI tools used to facilitate this process.
278
Continuous Integration Tools
Most of the hosted services are either free for open source projects or have a free tier
plan where you can run automated tests against a limited number of open Github
repositories. There are a plethora of services out there - such as Travis CI, CircleCI,
Codeship, Shippable, Drone, Snap, to name a few.
Bottom line: The CI projects are generally more powerful than the hosted services since
you have full control. However, they take longer to set up and you have to continue to
maintain them. The hosted services, on the other hand, are super easy to set up - most
integrate with Github with a simple click of a button. Plus, you don't have to worry about
whether or not your CI is setup correctly. It's not a good situation when you have to run
tests against your CI tool to ensure it's running the tests against your app correctly.
For this project, we'll use Travis CI since it's battled tested, easy to integrate with Github,
and free for open source projects.
279
Travis CI Setup
Travis CI Setup
At this point you, you need to have accounts set up on Github and Heroku, a local Git
repository, and a repository set up on Github for this project.
NOTE: The repository for this is outside the normal exercise repo. You can find it
here - https://github.com/realpython/flasktaskr_project.
Quickly read over the Travis-CI Getting Started guide so you have a basic understanding
of the process before following the steps in this tutorial.
Step 1: Sign up
Navigate to https://travis-ci.org/, and then click "Sign in with GitHub".
You will be asked if you want to authorize Travis CI to access your account. Grant them
access.
SEE ALSO: You can read more about the permissions asked for here when you
grant Travis CI to access your Github account.
280
Travis CI Setup
Once logged in, navigate to your profile page. There is a 'plus' button at the top left
corner of the page. Click on it. You should be able to see all repositories that you have
admin access to. Find the repository associated with this project and flip the switch to on
to enable CI on it.
A Git Hook has been added to the repository so that Travis is triggered to run tests when
new code is pushed to the repository.
281
Travis CI Setup
# specify language
language:
- python
# install dependencies
install:
- pip install -r requirements.txt
# run tests
script:
- nosetests
Add this to a file called .travis.yml within the main directory. So we specified the
language, the versions of Python we want to use to test against, and then the
commands to install the project requirements and run the tests.
NOTE: This must be a valid YAML file. Copy and paste the contents of the file to
the form on Travis WebLint to ensure that it is in proper YAML format.
After the tests run - and pass - it's common to place the status within the README file
on Github so that visitors can immediately see the build status. Read more about it here.
You can simply grab the markdown and add it to a README.md file.
Running Windows?:
Running OS X or Linux?:
282
Travis CI Setup
Homebrew is a package manager for macOS and it is super useful for all kinds of
installations and updates. If you don't already have homebrew installed, follow the
instructions on their homepage.
$ brew update
$ brew install ruby
Once you have Ruby installed, run gem install travis to install the Travis Command
Line Client. You may have to run sudo gem install travis to get around some
permissions settings.
This command automatically adds the necessary configuration for deploying to Heroku
to the deploy section in the .travis.yml file, which should look something like this:
deploy:
provider: heroku
api_key:
secure: Dxdd3y/i7oTTsC5IHUebG/xVJIrGbz1CCHm9KwE56
app: flasktaskr_project
on:
repo: realpython/flasktaskr_project
Commit your changes, then push to Github. The build should pass on Travis CI and then
automatically push the new code to Heroku. Make sure your app is still running on
Heroku after the push is complete.
SEE ALSO: For more information on setting up the Heroku configuration, please
see the official Travis CI documentation.
283
Travis CI Setup
284
Intermission
Intermission
Let's look what we've accomplished thus far in terms of CI and CD:
Travis CI triggers the automated tests after we push code to the Master branch on
Github.
Then, If the tests pass, the code is deployed to Heroku.
With everything set up, let's jump back to the workflow to see what changes we need to
make.
Workflow
Remember: The end goal is-
What changes need to be made based on this workflow vs. what we've developed thus
far? Well, let's start with implementing the Feature Branch Workflow.
285
Feature Branch Workflow
# specify language
language:
- python
# install dependencies
install:
- pip install -r requirements.txt
# run tests
script:
- nosetests
# deploy!
deploy:
provider: heroku
api_key:
secure: Kq95wr8v6rGlspuTt3Y8NdPse2eandSAF0DGmQm
app: app_name
on:
branch: master
python: '3.5'
repo: github_username/repo_name
286
Feature Branch Workflow
Update this, commit, and then push to Github. Since we're still working off the Master
branch, this will trigger a deploy.
Testing Branch
Let's test out the Feature Branch workflow.
This command creates a new branch called unit-tests based on the code in the
current Master branch.
NOTE: You can tell which branch you're currently working on by running: git
branch .
Since this is the testing branch, let's add another unit test to test_main.py:
def test_index(self):
""" Ensure flask was set up correctly. """
response = self.app.get('/', content_type='html/text')
self.assertEqual(response.status_code, 200)
Run the tests. Commit the changes locally. Then push to Github.
$ git add -A
$ git commit -am "added tests"
$ git push origin unit-tests
Notice how we pushed the code to the Feature branch rather than to Master. Now after
Travis CI runs, and the tests pass, the process ends. The code is not deployed since we
are not working off the Master branch.
Add more tests. Commit. Push. Repeat as much as you'd like. Try committing your code
in logical batches - e.g., after you are finished testing a specific function, commit your
code, and push to the Feature branch on Github. This is exactly what we talked about in
the beginning of the chapter - integrating your code many times a day.
When you're done testing, you're now ready to merge the Feature branch into Master.
287
Feature Branch Workflow
Be sure to review the steps that you went through for the Feature Branch Workflow
before moving on.
How about a quick sanity check! Comment out the following code:
Commit locally. Push to Github. Create then merge the pull request. What happened?
Although Travis CI ran the tests against the Master branch, since the build failed it did
not deploy the code, which is exactly what we want to happen. Fix the code before
moving on.
What's next?
288
Fabric
Fabric
Remember our Fabric script? We can still use it here to automate the CI/CD processes.
Update the push() function like so:
def push():
local("git branch")
branch = raw_input("Which branch do you want to push to? ")
local("git push origin {}".format(branch))
$ fab prepare
fab prepare
[localhost] local: nosetests -v
Enter a git commit message: fabric test
[localhost] local: git add -A && git commit -am 'fabric test'
[unit-tests 5142eff] fabric test
3 files changed, 8 insertions(+), 29 deletions(-)
[localhost] local: git branch
master
* unit-tests
Which branch do you want to push to? unit-tests
[localhost] local: git push origin unit-tests
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 611 bytes, done.
Total 5 (delta 4), reused 0 (delta 0)
To [email protected]:realpython/flasktaskr_project.git
c1d508c..5142eff unit-tests -> unit-tests
Done.
This committed our code locally, then pushed the changes up to Github on the Feature
branch. As you know, this triggered Travis-CI, which ran our tests. Next, manually go to
the Github repository and create the pull request, wait for the tests to pass on Travis CI,
and then merge the pull request into the Master branch. This, in turn, will trigger another
round of automated tests. Once these pass, the code will be deployed to Heroku.
289
Recap
Recap
Let's look at workflow (again!) in terms of our current solution:
Triggered automatically by
4 Run the automated tests
Travis CI
Triggered automatically by
7 Run the automated tests
Travis CI
Triggered automatically by
8 If the tests pass, deploy to Heroku
Travis CI
290
Conclusion
Conclusion
And with that, we are done with Flask for now.
Task Complete
Testing Yes
Styling Yes
Permissions Yes
Blueprints Yes
291
Conclusion
Upgrade to Postgres,
Add jQuery and AJAX,
Update the app configuration,
Utilize Flask-Script and Flask-Migrate,
Expand the RESTful API, and
Add integration tests.
292
Flask: Behavior-Driven Development with Behave
As you probably know, Flaskr - a mini-blog-like-app - is the app you build for the official
tutorial for Flask. Well, we'll be taking the tutorial a step further by developing it using the
BDD paradigm.
Enjoy.
293
Behavior-Driven Development
Behavior-Driven Development
BDD is a process used to drive out the functionality of your application by focusing on
user behavior - or how the end user wants the application to behave.
1. Write a test
2. Run all the tests (the new test should fail)
3. Write just enough code to get the new test to pass
4. Refactor the code (if necessary)
5. Repeat steps 1 through 4
294
Behavior-Driven Development
TDD can often lead to more modularized, flexible, and extensible code; the frequent
nature of testing helps to catch defects early in the development cycle, preventing them
from becoming large, expensive problems.
NOTE: Remember that theory is much, much different than practice. In most
production environments, there are simply not enough resources to test
everything and often testing is pushed off until the last minute - e.g., when things
break.
That said, the BIG difference between TDD and BDD is that before writing any tests, you
first write the actual feature that is being tested in natural language.
For example:
Once that feature is defined, you then write tests based on actions and the expected
behaviors of the feature. In this case, we'd test that the application (Flaskr) is set up and
that after a successful log in, the text "You were logged in" is displayed.
Done right, using BDD, you can answer some of the hardest questions that plague
developers of all skill levels:
Homework
295
Behavior-Driven Development
296
Project Setup
Project Setup
Test your skills by setting up basic a "Hello World" application. Try to do as much of this
as you can without looking back at the Flask Quick Start chapter. Don't worry if you can't
figure out everything on your own. As long as the process is a little easier and you
understand it a little better, then you are learning. One step at a time.
├── app.py
├── static
│ ├── css
│ ├── img
│ └── js
└── templates
Now build your app. Although there are a number of ways to code this out, once
complete, make sure to copy my code over from the "assets" directory in the Real
Python Exercises repo.
297
Introduction to Behave
Introduction to Behave
Behave is a fun (yes, fun) tool used for writing automated acceptance tests to support
development in the BDD style. Not only is it one of the most popular Python-based
behavior-driven tools, it also is easy to learn and use. It utilizes a domain-specific,
human readable language named Gherkin to allow the execution of feature
documentation written in natural language text. These features are then turned into test
code written in Python.
Homework
Complete the basic tutorial from the Behave documentation, making sure that you
understand the relationship between Features, Scenarios, and Steps.
298
Feature Files
Feature Files
Feature files, which are written in natural language, define where in your application the
tests are supposed to cover. This not only helps with thinking through your app, but it
also makes documenting much easier.
Flaskr
Start by looking at the features for the Flaskr App:
1. Let the user sign in and out with credentials specified in the configuration. Only one
user is supported.
2. When the user is logged in, they can add new entries to the page consisting of a
text-only title and some HTML for the text. This HTML is not sanitized because we
trust the user here.
3. The page shows all entries so far in reverse order (newest on top), and the user can
add new ones from there if logged in.
We can use each of the above features to define our feature files for Behave.
Step back for a minute and pretend you're working with a client on a project. If you're
given a list of high-level features, you can immediately build your feature files, tying in
scenarios to help drive out the main functions of the application. Since this is all
completed in natural language, you can return to your client to work through each
feature file and make changes as needed. This accomplishes several things, most
notability - accountability.
You're working closer with your client (or project manager, etc.) to define through user
behavior the functions of the application. Both parties are on the same page, which
helps to decrease the gap between what's expected and what's ultimately delivered.
299
First Feature
First Feature
Let the user sign in and out with credentials specified in the configuration. Only one user
is supported.
First, create a new directory called "features", which will eventually house all of our
features files as well the subsequent tests, called steps. Within that folder, create a file
called auth.feature (no extra file extension) and add the following text, which is written in
a language/style called Gherkin:
Feature: flaskr is secure in that users must log in and log out to access certain
features
The actual feature is the main story, then each scenario is like a separate chapter. We
now need to write our tests, which are called steps, based on the scenarios.
Create a "steps" directory within the "features" directory, then add a file called
auth_steps.py. Let's add our first test:
300
First Feature
First, notice that flaskr is set up matches the text within the Feature file. Next, do you
know what we're testing here? Do you know what a request context is? If not, please
read this article.
Run Behave:
$ behave
Search through the output until you find the overall stats on what features and scenarios
fail:
Failing scenarios:
features/auth.feature:3 successful login
features/auth.feature:8 incorrect username
features/auth.feature:13 incorrect password
features/auth.feature:18 successful logout
Since this test failed, we now want to write just enough code to get the new test to pass.
Also, did you notice that only four steps failed - 0 steps passed, 4 failed, 0 skipped, 9
undefined . How can that be if we only defined one step? Well, that step actually appears
four times in our feature file. Open the file and find them. Hint: Look for all instances of
flaskr is set up .
Environment Control
If you went through the Behave tutorial, which you should have already done, then you
know that you had to set up an environment.py file to specify certain tasks to run before
and after each test. We need to do the same thing.
301
First Feature
Create an environment.py file within your "features" folder, and then add the following
code:
import os
import sys
try:
from flaskr import app
except ImportError:
sys.path.append(full_path)
from flaskr import app
In order for this function to run correctly - e.g., in order to mock our app - we need to
provide an instance of our actual Flask app. How do we do that? Well, first ask yourself:
"Where did we establish an instance of Flask to create our app?" Of course! It's within
flaskr.py:
app = Flask(__name__)
We can't just import that app because right now we do not have access to that script in
the current directory. Thus, we need to add that script to our PATH, using
sys.path.append :
302
First Feature
sys.path.append(full_path)
Yes, this is confusing. Basically, when the Python interpreter sees that a module has
been imported, it tries to find it by searching in various locations - and the PATH is one
of those locations. Read more about this from the official Python documentation.
Failing scenarios:
features/auth.feature:3 successful login
features/auth.feature:8 incorrect username
features/auth.feature:13 incorrect password
features/auth.feature:18 successful logout
This time, all of our features and scenarios failed, but four steps passed. Based on that,
we know that the first step passed. Why? Remember that four steps failed when we first
ran the tests. You can also check the entire stack trace to see the steps that passed
(highlighted in green).
Next steps
Update the auth_steps.py file:
303
First Feature
Compare this to your feature file to see the relationship between scenarios and steps.
Run Behave. Take a look at the entire stack trace in your terminal. Notice how the
variables in the step file - i.e., {username} , are replaced with the provided username
from the feature file.
NOTE: We are breaking a rule by writing more than one test in a single iteration of
the BDD cycle. We're doing this for time's sake. Each of these tests are related
and some even overlap, as well. That said, when you go through this process on
your own, I highly recommend sticking with one test at a time. In fact, if you are
feeling ambitious, deviate from this guide and try writing a single step at a time,
going through the BDD process, until all steps, scenarios, and features pass.
Then move on to the Second Feature...
What's next? Building the log in and log out functionality into our code base. Remember:
Write just enough code to get this it pass. Want to try on your own before looking at my
answer?
Update flaskr.py
304
First Feature
@app.route('/logout')
def logout():
"""User logout/authentication/session management."""
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('index'))
Based on the route decorator, the login() function can either accept a GET or POST
request. If the method is a POST, then the provided username and password are
checked. If both are correct, the user is logged in then redirected to the main, / , route;
and a key is added the session as well. But if the credentials are incorrect, the login
template is re-rendered along with an error message.
# imports
from flask import Flask, render_template, request, session
# configuration
DATABASE = ''
USERNAME = 'admin'
PASSWORD = 'admin'
SECRET_KEY = 'change me'
305
First Feature
<div class="container">
<h1>Flask - BDD</h1>
{% for message in get_flashed_messages() %}
<div class="flash">{{ message }}</div>
{% endfor %}
<h3>Login</h3>
{% if error %}<p class="error"><strong>Error:</strong> {{ error }}{% endif %}
<form action="{{ url_for('login') }}" method="post">
<dl>
<dt>Username:
<dd><input type="text" name="username">
<dt>Password:
<dd><input type="password" name="password">
<br>
<br>
<dd><input type="submit" class="btn btn-default" value="Login">
<span>Use "admin" for the username and password</span>
</dl>
</form>
</div>
While we're at it, let's update the styles; update the head of base.html:
<head>
<title>Flask - Behavior Driven Development</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="http://netdna.bootstrapcdn.com/
bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/
bootstrap/3.1.1/js/bootstrap.min.js"></script>
</head>
Yes, we broke another rule. We are writing a bit more code than necessary. It's okay,
though. This happens in the development process. TDD and BDD are just guides to
follow, providing you with a path to (hopefully) better the development process as a
whole. Every now and then you'll have to bend or break the rules. Now is a good time.
306
First Feature
{% extends "base.html" %}
{% block content %}
<div class="container">
<h1>Flask - BDD</h1>
{% if not session.logged_in %}
<a href="{{ url_for('login') }}">log in</a>
{% else %}
<a href="{{ url_for('logout') }}">log out</a>
{% endif %}
{% for message in get_flashed_messages() %}
<div class="flash">{{ message }}</div>
{% endfor %}
{% if session.logged_in %}
<h1>Hi!</h1>
{% endif %}
</div>
{% endblock %}
Sanity check
Now, before we run Behave, let's do some manual testing. Run the server. Test logging
in and logging out. Did you remember to add the right imports? Are the right messages
flashed? Compare the actual behavior with the expected behavior in the feature and
step files. Are you confident that they align? If so, run Behave:
307
Second Feature
Second Feature
When the user is logged in, they can add new entries to the page consisting of a text-
only title and some HTML for the text. This HTML is not sanitized because we trust the
user here.
This should be fairly straightforward. Although, given these scenarios, it can be hard to
write the steps. Let's focus on one at a time.
308
Second Feature
NOTE: We could also create an entire new feature file for this. However, since
there is much overlap in the code and the auth features are really testing all of the
user behavior for when a user is logged in vs. logged out, it's okay to add this to
the auth feature file. If there were a number of actions or behaviors that a logged
in user could perform, then, yes, it would be best to separate this out into a new
feature file. Just be aware of when you are writing the same code over and over
again; this should trigger a code smell.
First Step
Add the following step to auth.steps:
@when(u'we add a new entry with "{title}" and "{text}" as the title and text')
def add(context, title, text):
context.page = context.client.post(
'/add',
data=dict(title=title, text=text),
follow_redirects=True
)
assert context.page
Run Behave
Failing scenarios:
features/auth.feature:24 successful post
features/auth.feature:30 unsuccessful post
Update flaskr.py
309
Second Feature
@app.route('/add', methods=['POST'])
def add_entry():
flash('New entry was successfully posted')
return redirect(url_for('index'))
Update index.html
{% extends "base.html" %}
{% block content %}
<div class="container">
<h1>Flask - BDD</h1>
{% if not session.logged_in %}
<a href="{{ url_for('login') }}">log in</a>
{% else %}
<a href="{{ url_for('logout') }}">log out</a>
{% endif %}
{% for message in get_flashed_messages() %}
<div class="flash">{{ message }}</div>
{% endfor %}
{% if session.logged_in %}
<br><br>
<form action="{{ url_for('add_entry') }}" method="post" class="add-entry">
<dl>
<dt>Title:
<dd><input type="text" size="30" name="title">
<dt>Text:
<dd><textarea name="text" rows="5" cols="40"></textarea>
<br>
<br>
<dd><input type="submit" class="btn btn-default" value="Share">
</dl>
</form>
{% endif %}
</div>
{% endblock %}
Run Behave
310
Second Feature
Failing scenarios:
features/auth.feature:30 unsuccessful post
Nice. The successful post scenario passed. Moving on to the next scenario,
unsuccessful post ...
Update flaskr.py
We just need to add a conditional to account for users not logged in:
@app.route('/add', methods=['POST'])
def add_entry():
if not session.get('logged_in'):
abort(401)
flash('New entry was successfully posted')
return redirect(url_for('index'))
# imports
from flask import (Flask, render_template, request,
session, flash, redirect, url_for, abort)
Run Behave
Looks good!
311
Third Feature
Third Feature
The page shows all entries so far in reverse order (newest on top) and the user can add
new ones from there if logged in.
Remember:
First Step
@then(u'we should see the post with "{title}" and "{text}" as the title and text')
def entry(context, title, text):
assert title and text in context.page.data
This should be clear by now. We're simply checking that the posted title and text are
found on the page after the logged in user submits a new post.
Run Behave
312
Third Feature
Failing scenarios:
features/auth.feature:24 successful post
Here we are setting up a single table with three fields - "id", "title", and "text".
Update flaskr.py
Add two new imports, sqlite3 and g :
import sqlite3
from flask import (Flask, render_template, request,
session, flash, redirect, url_for, abort, g)
# configuration
DATABASE = 'flaskr.db'
USERNAME = 'admin'
PASSWORD = 'admin'
SECRET_KEY = 'change me'
313
Third Feature
# connect to database
def connect_db():
rv = sqlite3.connect(app.config['DATABASE_PATH'])
rv.row_factory = sqlite3.Row
return rv
@app.route('/')
def index():
db = get_db()
cur = db.execute('select title, text from entries order by id desc')
entries = cur.fetchall()
return render_template('index.html', entries=entries)
@app.route('/add', methods=['POST'])
def add_entry():
if not session.get('logged_in'):
abort(405)
db = get_db()
db.execute('insert into entries (title, text) values (?, ?)',
[request.form['title'], request.form['text']])
db.commit()
flash('New entry was successfully posted')
return redirect(url_for('index'))
314
Third Feature
The index() function queries the database, which we still need to create, grabbing all
the entries, displaying them in reverse order so that the newest post is on top. Do we
have a step for testing to ensure that new posts are displayed first? The add_entry()
function now adds the data from the form to the database.
$ python
>>> from flaskr import init_db
>>> init_db()
>>>
Update index.html
315
Third Feature
{% extends "base.html" %}
{% block content %}
<div class="container">
<h1>Flask - BDD</h1>
{% if not session.logged_in %}
<a href="{{ url_for('login') }}">log in</a>
{% else %}
<a href="{{ url_for('logout') }}">log out</a>
{% endif %}
{% for message in get_flashed_messages() %}
<div class="flash">{{ message }}</div>
{% endfor %}
{% if session.logged_in %}
<br><br>
<form action="{{ url_for('add_entry') }}" method="post" class="add-entry">
<dl>
<dt>Title:
<dd><input type="text" size="30" name="title">
<dt>Text:
<dd><textarea name="text" rows="5" cols="40"></textarea>
<br>
<br>
<dd><input type="submit" class="btn btn-default" value="Share">
</dl>
</form>
{% endif %}
<ul>
{% for entry in entries %}
<li><h2>{{ entry.title }}</h2>{{ entry.text|safe }}
{% else %}
<li><em>No entries yet. Add some!</em>
{% endfor %}
</ul>
</div>
{% endblock %}
This adds a loop to loop through each entry, posting the title and text of each.
Environment Control
Update:
316
Third Feature
import os
import sys
import tempfile
try:
from flaskr import app, init_db
except ImportError:
sys.path.append(full_path)
from flaskr import app, init_db
Now we're setting up our database for test conditions by creating a completely new
database and assigning it to a temporary file using the tempfile.mkstemp() function.
After each feature has run, we close the database context and then switch the primary
database used by our app back to the one defined in flaskr.py.
Run Behave
All clear!
317
Third Feature
318
Conclusion
Update Steps
There's still plenty of functionality that we missed. In some cases we need to update the
scenarios, while in others we can just update the steps. For example, we should
probably update the following step so that we're ensuring the database is set up as well
as the app itself:
@given(u'flaskr is setup')
def flask_is_set_up(context):
assert context.client and context.db
Other changes:
319
Update Steps
Conclusion
Besides Behave, there's only a few other BDD frameworks for Python, all of which are
immature (even Behave, for that matter). If you're interested in trying out other
frameworks, the people behind Behave put an excellent article together that compares
each BDD framework. It's worth a read.
Despite the immature offerings, BDD frameworks provide a means of delivering useful
acceptance, unit, and integration tests. Integration tests are designed to ensure that all
units (or scenarios) work well together. This, coupled with the focus on end user
behavior, helps guarantee that the final product bridges the gap between business
requirements and actual development to meet user expectations.
Homework
Take the quick Python Developer Test.
Want even more Flask? Watch all videos from the Discover Flask series. Note: New
videos are still being added.
320
Interlude: Web Frameworks, Compared
NOTE: Take note of the concept "Don’t Repeat Yourself" (or DRY). Always avoid
reinventing the wheel. This is exactly what web frameworks excel at. The majority
of the low-level tasks (listed above) are common to every web application. Since
frameworks automate much of these tasks, you can get up and running quickly, so
you can focus your development time on what really matters: focusing on the
product and making your application stand out from the crowd.
Most frameworks also include a development web server, which is a great tool used not
only for rapid development but automating testing as well.
The majority of web frameworks can be labeled as either a full (high-level), or micro
(low-level) framework, depending on the amount and level of automation it performs and
the number of pre-installed components (batteries) it comes with. Full frameworks come
with many pre-installed batteries and a number of low-level task automation, while micro
frameworks come with few batteries and less automation.
Since all web frameworks offer some automation to help speed up web development, in
the end, it's up to the developer to decide how much control s/he wants. Beginning
developers should first focus on demystifying much of the magic, (commonly referred to
as automation), to help understand the differences between the various web frameworks
and avoid later confusion.
Keep in mind that while there are plenty of upsides to using web frameworks, most
notably rapid development, there is also a huge downside: lock-in. Put simply, by
choosing a specific framework, you lock your application into that framework's
321
Interlude: Web Frameworks, Compared
In general, problems are more likely to arise with high-level frameworks due to the
automation and features they provide; you have to do things their way. However, with
low-level frameworks, you have to write more code up front to make up for the missing
features, which slows the development process in the beginning. There's no right
answer, but you do not want to have to change a mature application's framework; this is
a daunting task to say the least. Choose wisely.
322
Overview
Overview
Popular Frameworks
1. web2py, Django, and Turbogears are all full frameworks, which offer a number of
pre-installed utilities and automate many tasks beneath the hood. They all have
excellent documentation and community support. The high-level of automation,
though, can make the learning curve for these quite steep.
3. Both Pyramid and Flask, which are still considered micro-frameworks, have quite a
few pre-installed 'batteries' and a number of additional pre-configured 'batteries'
available as well, which are very easy to install. Again, documentation and
community support are excellent, and both are very easy to use.
Components
Before starting development with a new framework, learn what pre-installed and
available 'batteries'/libraries it offers. At the core, most of the components work in a
similar manner; however, there are subtle differences. Take Flask and web2py, for
example; Flask uses an ORM for database communication and a type of template
engine called Jinja2. web2py, on the other hand, uses a DAL for communicating with a
database and has its own brand of templates.
Don't just jump right in, in other words, thinking that once you learn to develop in one,
you can use the same techniques to develop in another. Take the time to learn the
differences between frameworks to avoid later confusion.
323
Overview
Don't let other developers dictate what you do or try. Find out for yourself!
324
web2py: Quick Start
In this section, we'll be exploring the fundamentals of web2py - from installation to the
basic development process. You’ll see how web2py automates much of the low-level,
routine tasks of development, resulting in a smoother process, and one which allows you
to focus on high-level issues. web2py also comes pre-installed with many of the
components we had to manually install for Flask.
Homework
Watch this excellent speech by Di Pierro from PyCon US 2012. Don't worry if the
concepts don't make sense right now. They will soon enough. Pause the video at
times and look up any concepts that you don't understand. Take notes.
325
Installation
Installation
Quick Install
NOTE: This course utilizes web2py version 2.14.6.
If you want to get started quickly, you can download the binary archive, unzip, and run
either web2py.exe (Windows) or web2py.app (Unix). You must set an administrator
password to access the administrative interface. The Python Interpreter is included in
the archive, as well as many third party libraries and packages. You will then have a
development environment set up, and be ready to start building your application - all in
less than a minute, and without even having to touch the terminal.
We'll be utilizing a different workflow by developing from the command line, so follow the
full installation guide below.
Full Install
Create a directory called "web2py". To clone the git repo from the web2py repository run
these commands from you real-python directory:
326
Installation
NOTE: The --recursive flag is important in this command. When this command
executes, it will automatically install the dependencies we need to run web2py.
$ cd web2py
NOTE: Both apps within this chapter will be developed within this same instance
of web2py.
Ok great! We have web2py on our machine and it is accessible through the command
line.
As of writing (October 18, 2016), web2py only works with Python 2.7.x. Because of this,
we will need a new command line tool to create our environment, as 'pyvenv' does not
support anything before Python 3.0.
We will use pyenv. For a great tutorial on virtual environments in python checkout this
blog post. The post talks about 'pyenv' toward the end.
NOTE: Wow! These two virtual environment programs have really similar names.
We just want to reassure you, these are not typos. We were working with pyvenv
and now we will be working with pyenv.
327
Installation
NOTE: If you don't have Homebrew installed yet go here. Depending on where
(what directory) you have Python installed on your machine you may need to also
install both Python 2.7 and Python 3.0 via Homebrew so that everything is wired
up properly.
$ pyenv versions
NOTE: All of this installation has been global on your computer. That means you
will have access to 'pyenv' in any of you directories. What we are going to do next
will be local to the your current working directory. In our case web2py/.
So now, set the Python version that will be specific to this directory:
This command runs the web2py.py file using the 'local' version of python that we just set,
2.7.
328
Installation
After web2py loads, set an admin password, and you're good to go. FYI, there is no
need to activate and deactivate your virtual environments when using pyenv. But you do
need to run your shell commands with pyenv exec in order to maintain your project
environment.
Regardless of how you installed web2py (Quick vs Full), a number of libraries are pre-
imported. These provide a functional base to work with so you can start programming
dynamic websites as soon as web2py is installed. We'll be addressing such libraries as
we go along.
329
Hello World
Hello World
Fire up the server:
Input your admin password taking you to the web2py application home page.
Click the link labeled admin and then input your admin password.
You're now on the Sites page. This is the main administration page where you create
and modify your applications.
330
Hello World
To create a new application, type the name of the app, "hello_world", in the text field
below "New simple application". Click create to be taken to the Edit page. All new
applications are just copies of the "welcome" app:
331
Hello World
NOTE You'll soon find out that web2py has a default for pretty much everything
(but it can be modified). In this case, if you don't make any changes to the views,
for example, your app will have the basic styles and layout taken from the default,
"welcome" app.
Think about some of the pros and cons to having a default for everything. You
obviously can get an application set up quickly. Perhaps if you aren't adept at a
particular part of development, you could just rely on the defaults. However, if you
do go that route you probably won't learn anything new - and you could have a
difficult transition to another framework that doesn't rely as much (or at all) on
defaults. Thus, I urge you to practice. Break things. Learn the default behaviors.
Make them better. Make them your own.
The Edit page is logically organized around the Model-View-Controller (MVC) design
workflow:
MVC provides a means of splitting the back-end business logic from the front-end views,
and it's used to simplify the development process by allowing you to develop your
application in phases or chunks.
Next we need to modify the default controller, default.py, so click the "edit" button next to
the name of the file. Now we're in the web2py IDE. Replace the index() function with
the following code:
def index():
return dict(message="Hello from web2py!")
Save the file, then hit the Back button to return to the Edit page. Now let's update the
view. Edit the default/index.html file, replacing the existing code with:
332
Hello World
<html>
<head>
<title>Hello App!</title>
</head>
<body>
<br/>
<h1>{{=message}}</h1>
</body>
</html>
Save the file, then return to the Edit page again. Click the "hello_world" title link at the
top of the page. You should see the greeting, "Hello from web2py!" staring back at you.
Easy right?
So what happened?
The controller returned a dictionary with the key/value pair {message="Hello from
web2py"} . Then, in the view, we defined how we wanted the greeting to be displayed by
the browser. In other words, the functions in the controller return dictionaries, which are
then converted into the outputs within the view when surrounded by {{...}} tags. The
values from the dictionary are used as variables. In the beginning, you won't need to
worry about the views since web2py has so many pre-made views already built in
(again, defaults). So, you can focus solely on back-end development.
When a dictionary is returned, web2py looks to associate the dictionary with a view that
matches the following format: [controller_name]/[function_name].[extension]. If no
extension is specified, it defaults to .html, and if web2py cannot find the view, it defaults
to using the generic.html view:
333
Hello World
Controller = run.py
Function = hello()
Extension .html
In the hello_world app, since we used the default.py controller with the index() function,
web2py looked to associate this view with default/index.html.
You can also easily render the view in different formats, like JSON or XML, by simply
updating the extension:
Go to http://localhost:8000/hello_world/default/index.html
Change the extension to render the data in a different format:
XML: http://localhost:8000/hello_world/default/index.**xml**
JSON: http://localhost:8000/hello_world/default/index.**json**
Try this out. When done, hit CRTL-C from your terminal to stop the server.
334
Deploying on PythonAnywhere
Deploying on PythonAnywhere
As its name suggests, PythonAnywhere is a Python-hosting platform that allows you to
develop within your browser from anywhere in the world where there’s an Internet
connection.
Simply invite a friend or colleague to join your session, and you have a collaborative
environment for pair programming projects - or for getting help with a certain script you
can't get working. It has a lot of other useful features as well, such as Drop-box
integration and Python Shell access, among others.
Start by creating an account and login in. Once logged in, click "Web", then "Add a new
web app". Your new PythonAnywhere app url will be
https://your_username.pythonanywhere.com. Click "Next" and then on this page click
the button for web2py. "Next" again. Set an admin password and then click "Next" one
last time to set up the web2py project.
335
Deploying on PythonAnywhere
336
Deploying on PythonAnywhere
337
Deploying on PythonAnywhere
NOTE: If you really wanted, you could develop your entire app on
PythonAnywhere. Cool, right?
Before we deploy, we need to get the pydal package (needed to run web2py) into our
web2py/site-packages directory. Run this:
Return to the admin page, click the "Manage" drop down button next to your hello_world
project, then select "Pack All" to save the w2p-package to your computer.
338
Deploying on PythonAnywhere
Homework
Python Anywhere is considered a Platform as a Service (PaaS). Find out exactly
what that means. What's the difference between a PaaS hosting solution vs. shared
hosting?
Learn more about other Python PaaS options:
http://www.slideshare.net/appsembler/pycon-talk-deploy-python-apps-in-5-min-with-
a-paas
339
seconds2minutes App
seconds2minutes App
Let's create a new app that converts, well, seconds to minutes. Activate your virtualenv,
start the server, set a password, enter the Admin Interface, and create a new app called
"seconds2minutes".
NOTE: We'll be developing this locally. However, feel free to try developing it
directly on PythonAnywhere. Or: Do both.
In this example, the controller will define two functions. The first function, index() , will
return a form to index.html, which will then be displayed for users to enter the number of
seconds they want converted over to minutes. Meanwhile, the second function,
convert() , will take the number of seconds, converting them to the number of minutes.
def index():
form=FORM('# of seconds: ',
INPUT(_name='seconds', requires=IS_NOT_EMPTY()),
INPUT(_type='submit')).process()
if form.accepted:
redirect(URL('convert',args=form.vars.seconds))
return dict(form=form)
def convert():
seconds = request.args(0,cast=int)
return dict(seconds=seconds, minutes=seconds/60, new_seconds=seconds%60)
<center>
<h1>seconds2minutes</h1>
<h3>Please enter the number of seconds you would like converted to minutes.</h3>
<p>{{=form}}</p>
</center>
Create a new view called default/convert.html, replacing the default code with:
340
seconds2minutes App
<center>
<h1>seconds2minutes</h1>
<p>{{=seconds}} seconds is {{=minutes}} minutes and {{=new_seconds}} seconds.</p>
<br/>
<p><a href="/seconds2minutes/default/index">Try again?</a><p>
</center>
def index():
form=FORM('# of seconds: ',
INPUT(_type='integer', _name='seconds', requires=IS_INT_IN_RANGE(0,1000000
)),
INPUT(_type='submit')).process()
if form.accepted:
redirect(URL('convert',args=form.vars.seconds))
return dict(form=form)
Test it out. You should get an error, unless you enter an integer between 0 and 999,999:
Again, the convert() function takes the seconds and then runs the basic expressions to
convert the seconds to minutes. These variables are then passed to the dictionary and
are used in the convert.html view.
Homework
341
seconds2minutes App
342
Interlude: APIs
Interlude: APIs
This chapter focuses on client-side programming. Clients are simply web browsers that
access documents from other servers. Web server programming, on the other hand -
covered in the next chapter - deals with, well, web servers. Put simply, when you browse
the Internet, your web client (i.e., browser) sends a (GET) request to a remote server,
which responds back to the web client with the requested information (usually HTML,
CSS, and JavaScript).
In this chapter, we will navigate the Internet using Python programs to:
gather data
access and consume web services
scrape web pages
interact with web pages
This chapter assumes that you have some familiarity with HTML (HyperText Markup
Language), the primary language of the Internet. If you need a quick brush up, the first
17 chapters of W3schools.com's Basic HTML Tutorial will get you up to speed quickly.
They shouldn't take more than an hour to review. Or simply review the chapter in this
course on HTML and CSS.
Make sure that at a minimum, you understand the basic elements of an HTML page
such as the <head> and <body> as well as various HTML tags like <a> , <div> , <p> ,
<h1> , <img> , <center> , and <br> .
Finally, to fully explore this topic, client-side programming, you can gain an
understanding of everything from sockets to various web protocols and become a real
expert on how the Internet works. We will not be going anywhere near that in-depth in
this course. Our focus, rather, will be on the higher-level concepts, which are practical in
nature and which can be immediately useful to a web development project. We will
provide the required concepts, but it's more important to concern yourself with the actual
programs and coding.
Homework
Do you know the difference between the Internet and the Web? Did you know that
there is a difference?
343
Interlude: APIs
The Web is what most people think of as the Internet, which, now you know is
actually incorrect.
Read more about the differences between the Internet and the Web via Google.
Look up any terminology that you have questions about, some of which we will be
covering in this Chapter.
344
Retrieving Web Pages
Let's start with a basic example. But first, create a new folder within the "realpython"
directory called "client-side" Don't forget to create and activate a new virtualenv.
Install:
Code:
import requests
print(r.content)
As long as you are connected to the Internet this script will pull the HTML source code
from the Python Software Foundation's website and output it to the screen. It's a mess,
right? Can you recognize the header ( <head> </head> )? How about some of the other
basic HTML tags?
345
Retrieving Web Pages
import requests
r = requests.get("http://www.python.org/")
Save the file as clientb.py and run it. If you don't see an error, you can assume that it ran
correctly. Open the new file, test_requests.html in your text editor. Now it's much easier
to examine the actual HTML. Go through the file and find tags that you recognize.
Google any tags that you don't. This will help you later when we start web scraping.
NOTE: "wb" stands for write binary, which downloads the raw bytes of the file. In
other words, the file is downloaded in its exact format.
Did you notice the get() function in those last two programs? Computers talk to one
another via HTTP methods. The two methods you will use the most are GET and POST.
When you view a web page, your browser uses GET to fetch that information. When you
submit a form online, your browser will POST information to a web server. Make them
your new best friends. More on this later.
import requests
url = 'http://httpbin.org/post'
data = {'fname': 'Michael', 'lname': 'Herman'}
Output:
<Response [200]>
346
Retrieving Web Pages
We created a dictionary with the field names as the keys fname and lname , associated
with values Michael and Herman respectively.
requests.post initiates the POST request. In this example, you used the website
200 OK
300 Multiple Choices
301 Moved Permanently
302 Found
304 Not Modified
307 Temporary Redirect
400 Bad Request
401 Unauthorized
403 Forbidden
404 Not Found
410 Gone
500 Internal Server Error
501 Not Implemented
503 Service Unavailable
550 Permission denied
There are actually many more status codes that mean various things, depending on the
situation. However, the codes in bold above are the most common.
You should have received a "200" response to the POST request above.
Modify the script to see the entire response by appending .content to the end of the
print statement:
print(r.content)
You should see the data you sent within the response:
347
Retrieving Web Pages
"form": {
"lname": "Herman",
"fname": "Michael"
},
348
Web Services Defined
APIs
An API (Application Programming Interfaces) is a type of protocol used as a point of
interaction between two independent applications with the goal of exchanging data.
Protocols define the type of information that can be exchanged. In other words, APIs
provide a set of instructions, or rules, for applications to follow while accessing other
applications.
One example that you've already seen is the SQLite API, which defines the SELECT,
INSERT, UPDATE, and DELETE requests. The SQLite API allows the end user to
perform certain tasks, which, in general, are limited to those four functions.
HTTP APIs
HTTP APIs, also called web services, are simply APIs made available over the Internet,
used for reading (GET) and writing (POST) data as well as updating existing data
(UPDATE) and deleting data (DELETE).
So, the SELECT command, for example, is equivalent to the GET HTTP method, which
corresponds to the Read CRUD Operation.
349
Web Services Defined
Anytime you browse the Internet, you are constantly sending HTTP requests. For
example, Real Python reads (GETs) data from Facebook to show how many people
have "liked" a specific blog article. Then, if you "like" an article, data is sent (POST) to
Facebook (if you allow it, of course), showing that you like that article. Without web
services, these interactions between two independent applications would be impossible.
Let's look at an example in Chrome Developer Tools. Start by opening Chrome and
navigating to https://realpython.com. Right click anywhere on the screen and within the
window, click "Inspect Element". You are now looking at the main Developer Tools pane:
Click the Network panel. Right click on the column headers and be sure 'method' is
selected. If you are not sure what I mean, click around, explore this interface.
NOTE: Being very familiar with the Chrome Dev Tools console will come in handy
in the future. If you have trouble replicating the views in the following pictures,
don't be afraid to click around and try things out. Google what you need if you still
can't get them to look like the following images.
Now navigate in your browser to a site you need to login to view. Watch the activity in
your console. Do you see the GET requests? You basically sent a request asking the
server to display information for you.
350
Web Services Defined
Now log in to the site. Enter your login credentials. Pay close attention to the console.
Did you see the POST request? Basically, you sent a POST request with your login
credentials to the server.
Check out some other web pages. Try logging in to some of your favorite sites to see
more POST requests. Perhaps POST a comment on a blog or message forum.
351
Web Services Defined
Applications can access APIs either directly, through the API itself, or indirectly, through
a client library. The best means of access depends on a number of factors. Access
through client libraries can be easier, especially for beginners, as the code to directly
access the API has already been written. However, you still have to learn how the client
library works and integrate the library's code base into your overall code. Also, if you do
not first take the time to learn how the client library works, it can be difficult to debug or
troubleshoot. Direct access provides greater control, but beginners may encounter more
problems understanding and interpreting the rules of the specific API.
NOTE: Not all web services rely on HTTP requests to govern the allowed
interaction. Only RESTful APIs use POST, GET, PUT, and DELETE. This
confuses a lot of developers. Just remember that RESTful APIs are just one type
of web service. Also, it's not really important to understand the abstract
principles of RESTful design. Simply being able to recognize at a high-level
what it is and the associated four HTTP methods is sufficient.
Summary
In summary, APIs:
Although web services have brought much order to the Internet, the services themselves
are still fairly chaotic. There are no standards besides a few high-level best practices
(RESTful APIs) associated with HTTP requests. Documentation is a big problem too, as
it is left to the individual developers to document how their web services work. If you
start working more with web services, which we encourage you to do so, you will begin
to see not only just how different each and every API is but also how terribly
documented many of them are.
If you'd like more information on Web APIs, check out the How to use APIs with Python
crash course from Codeacademy.
Fortunately, data exchanged via web services is standardized in text-based formats and
thus, are both human and machine-readable. Two popular formats used today are XML
and JSON, which we will address shortly.
352
Web Services Defined
Before moving on, let's look at a fun example. http://placekitten.com is an API that
returns a picture of a kitten given a width an height -
http://placekitten.com/WIDTH/HEIGHT. You can test it out right in your browser; just
navigate to these URLs:
http://placekitten.com/200/300
http://placekitten.com/300/450
http://placekitten.com/700/600
Homework
Read this article providing a high-level overview of standards associated with
RESTful APIs.
If you're still struggling with understanding what Web Services as well as RESTful
APIs, check out Teach a Dog to REST. After about 8:30 minutes it starts to get
pretty technical, but before that it's pretty accessible to everyone.
353
Working with XML
NOTE: Although, JSON continues to push XML out of the picture in terms of web
services, XML is still widely used and can be easier to parse.
<?xml version="1.0"?>
<CARS>
<CAR>
<MAKE>Ford</MAKE>
<MODEL>Focus</MODEL>
<COST>15000</COST>
</CAR>
<CAR>
<MAKE>Honda</MAKE>
<MODEL>Civic</MODEL>
<COST>20000</COST>
</CAR>
<CAR>
<MAKE>Toyota</MAKE>
<MODEL>Camry</MODEL>
<COST>25000</COST>
</CAR>
<CAR>
<MAKE>Honda</MAKE>
<MODEL>Accord</MODEL>
<COST>22000</COST>
</CAR>
</CARS>
There's a declaration at the top, and the data is surrounded by opening and closing tags.
One useful thing to remember is that the purpose of XML is much different than HTML.
While HTML is used for displaying data, XML is used for transferring data. In itself, an
XML document is purposeless until it is read, understood, and parsed by an application.
It's about what you do with the data that matters.
354
Working with XML
With that in mind, let's build a quick parser. There are quite a few libraries you can use to
read and parse XML files. One of the easiest libraries to work with is the ElementTree
library, which is part of Python's standard library.
Use the cars.xml file found in the exercises repo for this example.
Code:
# XML Parsing 1
Output:
Focus
In this program we read and parsed the file using the find function and then outputted
the data between the first <MODEL> </MODEL> tags. These tags are called element
nodes, and are organized in a tree-like structure and further classified into parent and
child relationships.
In the example above, the parent is <CARS> , and the child elements are <CAR> ,
<MAKE> , <MODEL> , and <COST> . The find function begins looking for elements that
are children of the parent node, which is why we started with the first child when we
outputted the data, rather than the parent element:
print(doc.find("CAR/MODEL").text)
print(doc.find("CAR[1]/MODEL").text)
See what happens when you change the code in the program to:
355
Working with XML
print(doc.find("CAR[2]/MODEL").text)
Civic
See how easy that was. That's why XML is both machine and human readable. Let's
take it a step further and add a loop to extract all the data:
# XML Parsing 2
doc = et.parse("cars.xml")
# outputs the make, model and cost of each car to the screen
for element in doc.findall("CAR"):
print(element.find("MAKE").text + " " +
element.find("MODEL").text +
", $" + element.find("COST").text)
This program follows the same logic as the previous one, but we just added a for loop
to iterate through the XML file, pulling all the data and then outputting it.
Finally, in this last example, we will use a GET request to access XML found on the web:
356
Working with XML
# XML Parsing 3
doc = et.parse("test.xml")
Again, this program follows the same logic. You just added an additional step by
importing the requests library and downloading the XML file before reading and parsing
the XML.
357
Working with JSON
{
"CARS":[
{
"MAKE":"Ford",
"MODEL":"Focus",
"COST":"15000"
},
{
"MAKE":"Honda",
"MODEL":"Civic",
"COST":"20000"
},
{
"MAKE":"Toyota",
"MODEL":"Camry",
"COST":"25000"
},
{
"MAKE":"Honda",
"MODEL":"Accord",
"COST":"22000"
}
]
}
Although the data looks very similar to XML, there are many noticeable differences.
There's less code, no start or end tags, and it's easier to read. Also, because JSON
operates much like a Python dictionary, it is very easy to work with with Python.
358
Working with JSON
3. The curly brackets contain dictionaries, while the square brackets hold lists.
JSON decoding is the act of taking a JSON file, parsing it, and turning it into something
usable.
Code:
# JSON Parsing 1
import json
Output:
You see we have four dictionaries inside a list, enclosed within another dictionary, which
is finally enclosed within another list. Repeat that to yourself a few times. Can you see
that in the output?
If you're having a hard time, try changing the print statement to:
359
Working with JSON
[
{
"CAR": [
{
"COST": "15000",
"MAKE": "Ford",
"MODEL": "Focus"
},
{
"COST": "20000",
"MAKE": "Honda",
"MODEL": "Civic"
},
{
"COST": "25000",
"MAKE": "Toyota",
"MODEL": "Camry"
},
{
"COST": "22000",
"MAKE": "Honda",
"MODEL": "Accord"
}
]
}
]
If you want to print just the value "Focus" of the "MODEL" key within the first dictionary in
the list, you could run the following code:
# JSON Parsing 2
import json
1. [0]["CAR"] - indicates that we want to find the first car dictionary. Since there is
360
Working with JSON
and then extract the value associated with that key. If we changed the number to
1 , it would find the second instance of model and return the associated value:
Civic .
import json
import requests
url = "http://httpbin.org/post"
payload = {"colors":[
{"color": "red", "hex":"#f00"},
{"color": "green", "hex":"#0f0"},
{"color": "blue", "hex":"#00f"},
{"color": "cyan", "hex":"#0ff"},
{"color": "magenta", "hex":"#f0f"},
{"color": "yellow", "hex":"#ff0"},
{"color": "black", "hex":"#000"}
]}
headers = {"content-type": "application/json"}
Output:
200 OK
In some cases you will have to send data (a payload) to be interpreted by a remote
server to perform some action on your behalf. For example, you could send a JSON
Payload to Twitter with a number Tweets to be posted.
Some tech companies require you to send a JSON payload with your name, telephone
number, email address, and a link to your online resume to apply to ensure you know
the basics of sending a POST request. If you ever run into a similar situation, make sure
to test the Payload before actually sending it to the real URL. You can use sites like
JSON Test for testing purposes.
361
Working with JSON
362
Working with Web Services
Twitter
Like Google, Twitter provides a very open API. We use the Twitter API extensively for
pulling in tweets on specific topics, which we then parse and extract into a CSV file for
analysis. One of the best client libraries to use with the Twitter API is Tweepy.
Install:
363
Working with Web Services
Once complete, you will be taken to your application where you will need the following
access codes:
consumer_key
consumer_secret
access_token
access_secret
import tweepy
consumer_key = "<get_your_own>"
consumer_secret = "<get_your_own>"
access_token = "<get_your_own>"
access_secret = "<get_your_own>"
tweets = api.search(q='#python')
NOTE: Add your keys and tokens in the above code before running.
WARNING: Make sure you change your keys and token variables back to "
<get_your_own>" before you PUSH to Github. You do not want anyone else but
you to get a hold of those keys and make requests on your behalf.
If done correctly, this should output the tweets and the dates and times they were
created:
今回の記事は#wxPython でメニューバーを作成する⽅法です。
結構⼤変なんですよね('Д')
364
Working with Web Services
https://t.co/5SEDtu3W0s
#python https://t.co/ToZkwyTYc0
2016-11-12 23:15:03 It's like Sesame Street for the STEM set https://t.co/kIO0n5U5
3P #edtech #edchat #linux #python #puppets #firefox #html5 #blender #lwks #diy
2016-11-12 23:15:00 Recruit tech talent for #Java, #Ruby, #Python, #Scala & #P
HP #Devops #Mobile #Apps #BigData #Hadoop & more. Visit https://t.co/zjWuBvQSx
Q
2016-11-12 23:06:02 It was create to help you #python #ssh #Yii #SearchEngineOptim
ization #Joomla #ReactJS #googleanalytics #git… https://t.co/xYHaMcFU0h
2016-11-12 23:06:01 Python: import a file from a subdirectory #python #module #sub
directory #python-import https://t.co/pzBCLOOCkk
Essentially, the search results were returned to a list, and then we iterated through that
list to extract the desired information.
365
Working with Web Services
How did we know we wanted the keys created at and text ? Again, trial and error.
Start with the documentation, then turn to Google. You will soon become quite adept at
knowing the types of reliable sources to use when trying to obtain information about an
API.
Try this on your own. See what other information you can extract. Have fun!
WARNING: REMEMBER!!! Make sure you change your keys and token variables
back to "<get_your_own>" before you PUSH to Github.
Let's get walking directions from Central Park to Times Square. Use the following URL to
call (or invoke) the API:
https://maps.googleapis.com/maps/api/directions/output?parameters
You then must specify the output. We'll use JSON since it's easier to work with. Also,
you must specify some parameters. Notice in the documentation how some parameters
are required while others are optional. Let's use these parameters:
origin=Central+Park
destination=Times+Square
sensor=false
mode=walking
Now you could simply append the output as well as the parameters to the end of the
URL like so-
https://maps.googleapis.com/maps/api/directions/json?
origin=Central+Park&destination=Times+Square&sensor=false&mode=walking
366
Working with Web Services
However, there's a lot more information there than we need. Let's call the API directly
from the Python Shell, and then extract the actual driving directions. In the python shell
run these commands:
url = "https://maps.googleapis.com/maps/api/directions/json?origin=Central+Park&de
stination=Times+Square&sensor=false&mode=walking"
data = requests.get(url)
binary = data.content
output = json.loads(str(binary, 'utf-8'))
print(output['status'])
367
Working with Web Services
Compare the loops to the entire output. You can see that for each loop we're just moving
in (or down) one level:
So, if we wanted to print the start_address and end address , we would just need two
for loops:
368
Working with Web Services
Homework
Using the Google Direction API, pull driving directions from San Francisco to Los
Angeles in XML. Then extract the step-by-step directions.
369
My API Films
My API Films
Before moving on to web scraping, let's look at an extended example of how to use web
services to obtain information. In the last lesson we used client libraries to connect with
APIs; in this lesson we'll establish a direct connection. You'll grab data - e.g., make a
GET request - from the My API Films, parse the relevant info, then upload the data to a
SQLite database.
Whenever you start working with a new API, you always want to start with the
documentation. Again, all APIs work differently because few universal standards or
practices have been established. Fortunately, the My API Films' API is not only well
documented - but also easy to read and follow.
In this example, let's grab a list of all movies currently playing in theaters, which we can
grab from this endpoint:
Endpoints are the actual connection points for accessing the data. In other words, they
are the specific URLs used for calling an API. Each endpoint is generally associated with
a different type of data, which is why endpoints are often associated with groupings
(often called resources) of data (e.g., movies playing in the theater, movies opening on a
certain date, top rentals, and so on).
Did you notice how you need a token to make the actual API call?
The majority of web APIs (or web services) require users to go through some form of
authentication in order to access their services. There are a number of different means
of going through authentication.
In this particular case, we just need to request a token from here. Once you obtain the
token, DO NOT share it with anyone. You do not want someone else using that key to
obtain information and possibly use it in an illegal or unethical manner.
http://api.myapifilms.com/imdb/inTheaters?
token=ADD_YOUR_TOKEN_HERE&format=json&language=en-us
370
My API Films
Use the URL from above and replace "ADD_YOUR_TOKEN_HERE" with the generated
token. Now test it in your browser. You should see a large JSON file full of data. If not,
there may be a problem with your token. Make sure you copied and pasted the entire
token and appended it correctly to the URL.
Now comes the fun part: Building the program to actually GET the data, parse the
relevant data, and then dump it into a database. We'll do this in iterations.
import sqlite3
# create a table
c.execute("""CREATE TABLE new_movies
(title TEXT, year INT, votes text,
release_date text, rating INT, metascore INT)""")
$ python create_db.py
We need one more script now to pull the data and dump it directly to the database:
371
My API Films
import json
import requests
import sqlite3
# retrieve data
c.execute("SELECT * FROM new_movies ORDER BY title ASC")
Make sure you add your API token into the value for the variable TOKEN .
What happened?
372
My API Films
We:
1. Grabbed the data from the endpoint URL with a GET request.
2. Converted the data to binary.
3. Decoded the JSON feed.
4. Used a for loop to write the data to the database.
5. Selected the data from the database and outputted it.
Nice, right?
Were you able to follow this code? Go through it a few more times. See if you can grab
data from a different endpoint. Practice!
373
web2py: Sentiment Analysis
374
Sentiment Analysis
Sentiment Analysis
What is Sentiment Analysis?
Essentially, sentiment analysis measures the sentiment of something - a feeling rather
than a fact. The aim is to break down natural language data, analyze each word, and
then determine if the data, as a whole, is positive, negative, or neutral.
Twitter is a great resource for sourcing data for sentiment analysis. You could use the
Twitter API to pull hundreds of thousands of tweets on topics such as Obama, abortion,
gun control, etc. to get a sense of how Twitter users feel about a particular topic.
Companies often use sentiment analysis to gain a deeper understanding about
marketing campaigns, product lines, and the company itself.
NOTE: Sentiment analysis works best when it's conducted on a popular topic that
people have strong opinions about.
In this example, we'll be using a natural language classifier to power a web application
that allows you to enter data for analysis via an html form. The focus is not on the
classifier but on the development of the application. For more information on how to
develop your own classifier using Python, read Twitter sentiment analysis using Python
and NLTK.
Steps
Start by reading the API documentation for the natural language classifier we'll be using
for our app. Are the docs clear? What questions do you have? Write them down. You
should be able to answer them by the end of this lesson.
First, what the heck is cURL? For simplicity, cURL is a utility used for transferring data
across numerous protocols. We will be using it to test HTTP requests.
NOTE: Traditionally, you would access cURL from the bash terminal in Unix
environments. Unfortunately, for Windows users, prior to Windows 10, it does not
come with the utility. Fortunately, there is an advanced command line tool called
Cygwin available that provides a Unix-like terminal for Windows. Please follow the
steps here to install. Make sure to add the cURL package when you get to the
"Choosing Packages" step. Or scroll down to step 5 and use Hurl instead.
375
Sentiment Analysis
$ curl -d "text=i usually like ice cream but this place is terrible" http://text-p
rocessing.com/api/sentiment/
$ curl -d "text=i really really like you, but today you just smell." http://text-p
rocessing.com/api/sentiment/
So, you can see the natural language, the probability of the sentiment being positive,
negative, or neutral, and then the final sentiment. Did you notice the last two text
statements are more neutral than negative but were classified as negative? Why do you
think that is? How can a computer analyze sarcasm?
376
Sentiment Analysis
Steps:
Requests
Alright, let's build the app. Before we begin, though, we will be using the requests library
for initiating the POST request. The cURL command is equivalent to the following code:
import requests
url = 'http://text-processing.com/api/sentiment/'
data = {'text': 'great'}
r = requests.post(url, data=data)
print(r.content)
Go ahead and install the requests library. Wait. Didn't we already do that? Remember:
Since we're in a different virtualenv, we need to install it again. Use pip install
requests .
Now, let's build the app for easily testing sentiment analysis. It's called "Pulse".
Pulse
You know the drill: fire up the server using pyenv exec , enter the Admin Interface, and
create a new app called "Pulse".
377
Sentiment Analysis
Like the last app, the controller will define two functions, index() and pulser() .
index() , will return a form to index.html, so users can enter the text for analysis.
pulser() , meanwhile, handles the POST request to the API and outputs the results of
import requests
def index():
form=FORM(
TEXTAREA(_name='pulse', requires=IS_NOT_EMPTY()),
INPUT(_type='submit')).process()
if form.accepted:
redirect(URL('pulser',args=form.vars.pulse))
return dict(form=form)
def pulser():
text = request.args(0)
text = text.split('_')
text = ' '.join(text)
url = 'http://text-processing.com/api/sentiment/'
data = {'text': text}
r = requests.post(url, data=data)
return dict(text=text, r=r.content)
default/index.html:
{{extend 'layout.html'}}
<center>
<br/>
<br/>
<h1>check a pulse</h1>
<h4>Just another Sentiment Analysis tool.</h4>
<br/>
<p>{{=form}}</p>
</center>
default/pulser.html:
378
Sentiment Analysis
{{extend 'layout.html'}}
<center>
<p>{{=text}}</p>
<br/>
<p>{{=r}}</p>
<br/>
<p><a href="/pulse/default/index">Another Pulse?</a><p>
</center>
Test this out. Compare the results to the results using either cURL or Hurl to make sure
all is set up correctly. If you did everything right you should have received an error that
the requests module is not installed. Kill the server, and then install requests from the
terminal:
Now, let's finish cleaning up pulser.html. We need to parse the JSON file. What do you
think the end user wants to see? Do you think they care about the probabilities? Or just
the end results? What about a graph? It all depends on your (intended) audience. Let's
just parse out the end results for now.
Update default.py:
379
Sentiment Analysis
import requests
import json
def index():
form=FORM(
TEXTAREA(_name='pulse', requires=IS_NOT_EMPTY()),
INPUT(_type='submit')).process()
if form.accepted:
redirect(URL('pulser',args=form.vars.pulse))
return dict(form=form)
def pulser():
text = request.args(0)
text = text.split('_')
text = ' '.join(text)
url = 'http://text-processing.com/api/sentiment/'
data = {'text': text}
r = requests.post(url, data=data)
binary = r.content
output = json.loads(binary)
label = output["label"]
NOTE: Notice how we did not change the 'binary' data into a string using "utf-8"
encoding like we did in previous chapters? Can you think of why? Remember that
we are using Python 2.7 for all of our web2py projects, and there are some slight
differences.
Update default/pulser.html:
{{extend 'layout.html'}}
<center>
<br/>
<br/>
<h1>your pulse</h1>
<h4>{{=text}}</h4>
<p>is</p>
<h4>{{=label}}</h4>
<br/>
<p><a href="/pulse/default/index">Another Pulse?</a><p>
</center>
380
Sentiment Analysis
<!DOCTYPE html>
<html>
<head>
<title>check your pulse</title>
<meta charset="utf-8" />
<style type="text/css">
body {font-family: Arial, Helvetica, sans-serif; font-size:x-large;}
</style>
{{
middle_columns = {0:'span12',1:'span9',2:'span6'}
}}
{{block head}}{{end}}
</head>
<body>
<div class="{{=middle_columns}}">
{{block center}}
{{include}}
{{end}}
</div>
</body>
</html>
381
Sentiment Analysis
382
Sentiment Analysis
383
Sentiment Analysis
Better?
Next steps
What else could you do with this? Well, you could easily add a database to the
application to save the inputed text as well as the results. With sentiment analysis, you
want your algorithm to get smarter over time. Right now, the algorithm is static. Try
entering the term "i like milk". It's negative, right?
"i":
384
Sentiment Analysis
"like":
"milk":
All negative. Doesn't seem right. The algorithm needs to be updated. Unfortunately,
that's beyond the scope of this course. By saving each result in a database, you can
begin analyzing the results to at least find errors and spot trends. From there, you can
begin to update the algorithm. Good luck.
385
Sentiment Analysis Expanded
Let's begin.
{{extend 'layout.html'}}
<h1>AJAX Test</h1>
<form>
<input type="number" id="key" name ="key">
<input type="button" value="submit"
onclick="ajax('{{=URL('data')}}',['key'],'target')">
</form>
<br>
<div id="target"></div>
Add this code to a new view in your "Pulse" app, which will be used just for testing. Call
the view test/index.html.
This is essentially a regular form, but you can see the ajax() function within the button
input - onclick="ajax('{{=URL('data')}}',['key'],'target') . The url will call a function
within our controller (which we have yet to define), the data is grabbed from the input
box, then the final results will be appended to <div id="target"></div> . Essentially, on
the button click, we grab the value from the input box, send it to the data() function for
something to happen, then it is added to the page between the <div> tag with the
selector id=target .
Make sense? Let's get our controller setup. Create a new one called test.py:
386
Sentiment Analysis Expanded
def index():
return dict()
def data():
return (int(request.vars.key)+10)
So when the input data is sent to the data() function, we are simply adding 10 to the
number and then returning it.
<!DOCTYPE html>
<head>
<title>AJAX Test</title>
<script src="{{=URL('static','js/modernizr.custom.js')}}"></script>
<!-- include stylesheets -->
{{
response.files.insert(0,URL('static','css/web2py.css'))
response.files.insert(1,URL('static','css/bootstrap.min.css'))
response.files.insert(2,URL('static','css/bootstrap-responsive.min.css'))
response.files.insert(3,URL('static','css/web2py_bootstrap.css'))
}}
{{include 'web2py_ajax.html'}}
{{
middle_columns = {0:'span12',1:'span9',2:'span6'}
}}
<noscript><link href="{{=URL('static', 'css/web2py_bootstrap_nojs.css')}}" rel="
stylesheet" type="text/css" /></noscript>
{{block head}}{{end}}
</head>
<body>
<div class="container">
<section id="main" class="main row">
<div class="{{=middle_columns}}">
{{block center}}
{{include}}
{{end}}
</div>
</section><!--/main-->
</div> <!-- /container -->
<script src="{{=URL('static','js/bootstrap.min.js')}}"></script>
<script src="{{=URL('static','js/web2py_bootstrap.js')}}"></script>
</body>
</html>
For more information on what is happening in this HTML, checkout the web2py docs on
including jQuery and AJAX.
387
Sentiment Analysis Expanded
Test it out. Make sure to enter an integer. You should see something like this:
Did you notice that when you click the button the page does not refresh? This is what
makes AJAX, well, AJAX: To the end user, the process is seamless. Web apps send
data from the client to the server, which is then sent back to the client without interfering
with the default behavior of the browser page (no page refresh).
Adding AJAX
Update the default controller:
388
Sentiment Analysis Expanded
import requests
def index():
return dict()
def pulse():
session.m=[]
if request.vars.sentiment:
text = request.vars.sentiment
text = text.split('_')
text = ' '.join(text)
url = 'http://text-processing.com/api/sentiment/'
data = {'text': text}
r = requests.post(url, data=data)
session.m.append(r.content)
session.m.sort()
return text, TABLE(*[TR(v) for v in session.m]).xml()
Here, we simply grab the text from the input, run the analysis, append the results to a
list, and then return the original text along with the results.
{{extend 'layout.html'}}
<h1>check your pulse</h1>
<form>
<input type="text" id="sentiment" name ="sentiment">
<input type="button" value="submit" onclick="ajax('{{=URL('pulse')}}',['sentimen
t'],'target')">
</form>
<br>
<div id="target"></div>
Nothing new here. Test it out. You should see something like:
389
Sentiment Analysis Expanded
Additional Features
Now, let's expand our app so that we can enter two text inputs for comparison.
import requests
def index():
return dict()
def pulse():
session.m=[]
url = 'http://text-processing.com/api/sentiment/'
# first item
text_first = request.vars.first_item
text_first = text_first.split('_')
text_first = ' '.join(text_first)
data_first = {'text': text_first}
r_first = requests.post(url, data=data_first)
session.m.append(r_first.content)
# second_item
text_second = request.vars.second_item
text_second = text_second.split('_')
text_second = ' '.join(text_second)
data_second = {'text': text_second}
r_second = requests.post(url, data=data_second)
session.m.append(r_second.content)
session.m.sort()
return text_first, text_second, TABLE(*[TR(v) for v in session.m]).xml()
This performs the exact same actions as before, only with two inputs instead of one.
390
Sentiment Analysis Expanded
{{extend 'layout.html'}}
<h1>pulse</h1>
<p>comparisons using sentiment</p>
<br>
<form>
<input type="text" id="first_item" name ="first_item" placeholder="enter first i
tem...">
<br>
<input type="text" id="second_item" name ="second_item" placeholder="enter secon
d item...">
<br>
<input type="button" value="submit" onclick="ajax('{{=URL('pulse')}}',['first_it
em','second_item'],'target')">
</form>
<br>
<div id="target"></div>
Notice how we're passing two items to the pulse URL, ['first_item','second_item'] .
Test.
Only display the item that has the higher (more positive) sentiment. To do this, update
the controller. Go to the assets file in the book-2 exercises repo.
Update, the controller, default.py, with the code from the repo.
Although there is quite a bit more code here, the logic is simple. Walk through the
program, slowly, stating what each line is doing. Do this out loud. Keep going through it
until it makes sense. Also, make sure to test it to ensure it's returning the right values.
You can compare the results with the results found here.
Next, let's clean up the output. Go to the assets file in the book-2 exercises repo.
391
Sentiment Analysis Expanded
First, update the layout template with a Bootstrap stylesheet as well as some custom
styles.
392
Movie Suggester
Movie Suggester
Let's take this to the next level and create a movie suggester app. Essentially, we'll pull
data from the My API Films API that we used earlier to grab movies that are playing then
display the sentiment of each.
Setup
Create a new app called "movie_suggest". Replace the code in the controller, default.py
with:
import requests
import json
def index():
return dict()
def grab_movies():
session.m = []
TOKEN = 'GET_YOUR_OWN_KEY'
requests.get('http://api.myapifilms.com/imdb/inTheaters?token=' +
'{0}&format=json&language=en-us'.format(TOKEN))
binary = url.content
output = json.loads(binary)
movies = output['data']['inTheaters']
for movie in movies:
all_movies = movie['movies']
for meta in all_movies:
if(meta['title']):
session.m.append(meta["title"])
session.m.sort()
return TABLE(*[TR(v) for v in session.m]).xml()
NOTE: Make sure you add your API key into the value for the variable TOKEN .
In this script, we're using the My API Films to grab the movies currently playing. Test this
out in your shell to see exactly what's happening:
393
Movie Suggester
{{extend 'layout.html'}}
<h1>suggest-a-movie</h1>
<p>use sentiment to find that perfect movie</p>
<br>
<form>
<input type="button" value="Get Movies" onclick="ajax('{{=URL('grab_movies')}}',
[],'target')">
</form>
<br>
<div id="target"></div>
394
Movie Suggester
<!DOCTYPE html>
<head>
<title>suggest-a-movie</title>
<script src="{{=URL('static','js/modernizr.custom.js')}}"></script>
<!-- include stylesheets -->
{{
response.files.insert(0,URL('static','css/web2py.css'))
response.files.insert(1,URL('static','css/bootstrap.min.css'))
response.files.insert(2,URL('static','css/bootstrap-responsive.min.css'))
response.files.insert(3,URL('static','css/web2py_bootstrap.css'))
}}
{{include 'web2py_ajax.html'}}
{{
middle_columns = {0:'span12',1:'span9',2:'span6'}
}}
<link href="http://maxcdn.bootstrapcdn.com/
bootswatch/3.2.0/yeti/bootstrap.min.css" rel="stylesheet">
<noscript><link href="{{=URL('static', 'css/web2py_bootstrap_nojs.css')}}" rel="st
ylesheet" type="text/css" /></noscript>
{{block head}}{{end}}
</head>
<body>
<div class="container">
<div class="jumbotron">
<div class="{{=middle_columns}}">
{{block center}}
{{include}}
{{end}}
</div>
</div>
</div> <!-- /container -->
<script src="{{=URL('static','js/bootstrap.min.js')}}"></script>
<script src="{{=URL('static','js/web2py_bootstrap.js')}}"></script>
</body>
</html>
Test it out. You should see something similar to this (depending on which movies are in
the theater, of course):
395
Movie Suggester
Add Sentiment
Let's now determine the sentiment of each movie. Update the controller:
396
Movie Suggester
import requests
import json
def index():
return dict()
def grab_movies():
session.m = []
TOKEN = 'GET_YOUR_OWN_KEY'
requests.get('http://api.myapifilms.com/imdb/inTheaters?token=' +
'{0}&format=json&language=en-us'.format(TOKEN))
binary = url.content
output = json.loads(binary)
movies = output['data']['inTheaters']
for movie in movies:
all_movies = movie['movies']
for meta in all_movies:
if(meta['title']):
session.m.append(pulse(meta["title"]))
session.m.sort()
return TABLE(*[TR(v) for v in session.m]).xml()
def pulse(movie):
text = movie.replace('_', ' ')
url = 'http://text-processing.com/api/sentiment/'
data = {'text': text}
r = requests.post(url, data=data)
binary = r.content
output = json.loads(binary)
label = output["label"]
pos = output["probability"]["pos"]
neg = output["probability"]["neg"]
neutral = output["probability"]["neutral"]
return text, label, pos, neg, neutral
Here we are simply taking each name and passing them as an argument into the
pulse() function where we are calculating the sentiment.
397
Movie Suggester
Update Styles
Update the child view with some bootstrap styles:
398
Movie Suggester
{{extend 'layout.html'}}
<h1>suggest-a-movie</h1>
<p>use sentiment to find that perfect movie</p>
<br>
<form>
<input type="button" class="btn btn-primary" value="Get Movies" onclick="ajax('{
{=URL('grab_movies')}}',[],'target')">
</form>
<br>
<table class="table table-hover">
<thead>
<tr>
<th>Movie Title</th>
<th>Label</th>
<th>Positive</th>
<th>Negative</th>
<th>Neutral</th>
</tr>
</thead>
<tbody id="target"></tbody>
</table>
399
Movie Suggester
Think about what else you could do? Perhaps you could highlight movies that are
positive. Check the web2py documentation for help.
Deploy to Heroku
Open setup-web2py-heroku.sh and comment out the line:
Then in the .gitignore, which is in the root directory, comment out this line:
applications/*/private/*
400
Movie Suggester
As you can see web2py is using virtualenv to manage it's environment. Because we are
using pyenv we have to make sure that this deploy process installs the proper
requirements for our applications. So, now we need to run:
This will write all of our needed packages to the requirements file allowing the web2py
deploy script to install them for us.
Finally to deploy, we will run the script provided by web2py, it should work like a charm
with those fews modifications. Run this:
$ sudo sh scripts/setup-web2py-heroku.sh
This will perform all the steps necessary to create, install and fire up your app, just sit
back and watch!
Cheers!
401
Movie Suggester
402
web2py: py2manager
web2py: py2manager
In the last chapters we built several small applications to illustrate the power of web2py.
Those applications were meant more for learning. In this chapter we will develop a much
larger application - a task manager, similar to FlaskTaskr, called py2manager.
This application will be developed from the ground up to not only show you all that
web2py has to offer - but to also dig deeper into modern web development and the
Model View Controller pattern.
1. Users must sign in (and register, if necessary) before hitting the landing page,
index.html.
2. Once signed in, users can add new companies, notes, and other information
associated with a particular project and view other employees' profiles.
1. Each company consists of a company name, email, phone number, and URL.
2. Each project consists of a name, employee name (person who logged the
project), description, start date, due date, and completed field that indicates whether
the project has been completed.
3. Finally, the notes reference a project and include a text field for the actual note,
created date, and a created by field.
Up to this point, you have developed a number of different applications using the Model
View Controller (MVC) architecture pattern:
Again, a user sends a request to a web server. The server, in turn, passes that request
to the controller. The controller then performs an action, such as querying (GET) or
modifying (POST, PUT, DELETE) the database. Once the data is found or modified, the
controller then sends a response back to the views, which are rendered into HTML for
the end the user to see.
403
web2py: py2manager
Most modern web frameworks utilize the MVC-style architecture, offering similar
components. Each framework implements the various components slightly different, due
to the choices made by the developers of the framework. Learning about such
differences is vital for sound development.
404
Setup
Setup
Lets try yet another way of installing the web2py app on our local machine:
Text Editor
Instead of using the internal web2py IDE for development, like in the previous examples,
let's jump back to traditional development and use a text editor, like Sublime Text. Load
the entire project into your editor.
You should already have Sublime Text installed from previous work we have done.
Checkout the Getting Started chapter of this book for instructions on installing and
configuring Sublime Text.
$ subl .
If that command is not found, run this snippet to create enable this short cut:
Try again, this should load all of the files into Sublime text and open a sublime text
window. For more information on setting up Sublime Text for Python web development
checkout this blog post.
405
Setup
You should now have the entire application structure (files and folders) in Sublime. Take
a look around. Open the "Models", "Views", and "Controllers" folders. Simply double-
click to open a particular file to load it in the editor window. Files appear as tabs on the
top of the editor window, allowing you to move between them quickly.
406
Database
Database
As you saw in the previous chapter, web2py uses an API called a Database Abstraction
Layer (DAL) to map Python objects to database objects. Like an ORM, a DAL hides the
complexity of the underlying SQL code. The major difference between an ORM and a
DAL, is that a DAL operates at a lower level. In other words, its syntax is somewhat
closer to SQL. If you have experience with SQL, you may find a DAL easier to work with
than an ORM. If not, learning the syntax is no more difficult than an ORM.
ORM:
class User(db.Model):
__tablename__ = 'users'
name = db.Column(db.String, unique=True, nullable=False)
email = db.Column(db.String, unique=True, nullable=False)
password = db.Column(db.String, nullable=False)
DAL:
db.define_table('users',
Field('name', 'string', unique=True, notnull=True),
Field('email', 'string', unique=True, notnull=True),
Field('password', 'string', 'password', readable=False, label='Password'))
The above examples create the exact same "users" table. ORMs generally use classes
to declare tables, while the web2py DAL flavor uses functions. Both are portable among
many different relational database engines. Meaning they are database agnostic, so you
can switch your database engine without having to re-write the code within your Model.
web2py is integrated with a number of popular databases, including SQLite,
PostgreSQL, MySQL, SQL Server, FireBird, Oracle, MongoDB, among others.
Shell
If you prefer the command line, you can work directly with your database from the
web2py Shell. The following is a quick, unrelated example...
In your terminal navigate to the project root directory, "/web2py/py2manager", and then
run the following command:
407
Database
>>> db = DAL('sqlite://storage.sqlite',pool_size=1,check_reserved=['all'])
>>> db.define_table('special_users', Field('name'), Field('email'))
<Table special_users (id,name,email)>
>>> db.special_users.insert(id=1, name="Alex", email="[email protected]")
1L
>>> db.special_users.bulk_insert([{'name':'Alan', 'email':'[email protected]'}, {'name':'Joh
n', 'email':'[email protected]'}, {'name':'Tim', 'email':'[email protected]'}])
[2L, 3L, 4L]
>>> db.commit()
>>> for row in db().select(db.special_users.ALL):
... print row.name
...
Alex
Alan
John
Tim
>>> for row in db().select(db.special_users.ALL):
... print row
...
<Row {'name': 'Alex', 'email': '[email protected]', 'id': 1}>
<Row {'name': 'Alan', 'email': '[email protected]', 'id': 2}>
<Row {'name': 'John', 'email': '[email protected]', 'id': 3}>
<Row {'name': 'Tim', 'email': '[email protected]', 'id': 4}>
>>> db.special_users.drop()
>>> exit()
Here, we created a new table called "special_users" with the fields "name" and "email".
We then inserted a single row of data, then multiple rows. Finally, we printed the data to
the screen using for loops before dropping (deleting) the table and exiting the Shell.
web2py Admin
Now, as we mentioned before, web2py has a default for everything. These are the
default values for each table field:
408
Database
So, our company_name field would by default be a string value, it is not required (meaning
it is not mandatory to enter a company name), and, finally, it does not have to be a
unique value. Keep these defaults in mind when you are creating your database tables,
as you will often need to override them.
Let's create the model for our application. Navigate into the "applications" directory, and
then to the "py2manager" directory. Create a new file to define your database schema
called db_tasks.py within the "Models" directory.
db.define_table('company',
Field('company_name', notnull=True, unique=True),
Field('email'),
Field('phone', notnull=True),
Field('url'),
format = '%(company_name)s')
db.company.email.requires=IS_EMAIL()
db.company.url.requires=IS_EMPTY_OR(IS_URL())
db.define_table('project',
Field('name', notnull=True),
Field('employee_name', db.auth_user, default=auth.user_id),
Field('company_name', 'reference company', notnull=True),
Field('description', 'text', notnull=True),
Field('start_date', 'date', notnull=True),
Field('due_date', 'date', notnull=True),
Field('completed', 'boolean', notnull=True),
format = '%(company_name)s')
We defined two tables tables: company and project . You can see the foreign key in
the project table, reference company . The auth_user table is an auto-generated table.
Also, the employee_name field in the project table references the logged in user. So
409
Database
when a user posts a new project, the user information will automatically be added to the
database. Save the file.
Navigate to http://localhost:8000/ in your browser. Make your way to the Edit page and
click the "database administration" button to execute the DAL commands. Take a look at
the sql.log file within the "databases" directory in Sublime to verify exactly which tables
and fields were created.
Have look in the assets folder of the book 2 exercises repo and compare the sql.log to
your file and make sure they match up.
SEE ALSO: You can also read the documentation on all the auto-generated
tables in the web2py official documentation.
Notice the format attribute. All references are linked to the Primary Key of the
associated table, which is the auto-generated ID. By using the format attribute,
references will not show up by the id - but by the preferred field. You'll see exactly what
that means in a second.
One less thing you have to worry about. For more information, please check out
the web2py documentation.
410
Database
Homework
Download the web2py cheatsheet. Read it.
411
URL Routing
URL Routing
Controllers describe the application/business logic and workflow in order to link the user
with the application through the request/response cycle. More precisely, the controller
controls the requests made by the users, obtains and organizes the desired information,
and then responds back to the user via views and templates.
For example, navigate to the login page within your app and log in with the user that you
created. When you clicked the "Login" button after you entered your credentials, a POST
request was sent to the controller. The controller then took that information, and
compared it with the users in the database via the model. Once your user credentials
were found, this information was sent back to the controller. Then the controller
redirected you to the appropriate view.
def index():
return dict(message="Hello!")
This is just a simple function used to output the string "Hello!" to the screen. The function
name is index() . You can't tell from the above info, but the application name is "hello"
and the controller used for this function is default.py.
http://www.yoursite.com/hello/default/index.html
http://www.yoursite.com/application_name/controller_name/function_name.html
412
URL Routing
You can customize the URL routing methods by adding a routes.py file to
"web2py/py2manager" (the outer "py2manager" directory). For example, to remove the
"controller_name" from the URL, add the following code to the newly created file:
routers = dict(
BASE = dict(
default_application='py2manager',
)
)
Test this out. Restart the server. Navigate to the login page again:
http://localhost:8000/py2manager/default/user/login. Well, since we made those
changes, we can now access the same page from this url
http://localhost:8000/user/login.
SEE ALSO: For more information on URL routing, please see the official web2py
documentation.
Let's configure the logic and URL routing in the py2manager app. Add the following code
to default.py:
@auth.requires_login()
def index():
project_form = SQLFORM(db.project).process()
projects = db(db.project).select()
users = db(db.auth_user).select()
companies = db(db.company).select()
return locals()
Here we displayed the data found in the project , auth_user , and company tables, as
well as added a form for adding projects. With that, most of the functionality is in place.
We just need to update the views, organize the index.html page, and update the layout
and styles.
413
Initial Views
Initial Views
Views (or templates) describe how the subsequent response, from a request, should be
translated to to the user using mostly a combination of a templating engine, HTML,
Javascript, and CSS.
Template Engine
Template engines are used for embedding Python code directly into standard HTML.
web2py uses a slightly modified Python syntax to make the code more readable. You
can also define control statements such as for and while loops as well as if
statements.
def tester():
return locals()
<html>
<body>
{{numbers = [1, 2, 3]}}
<ul>
{{for n in numbers:}}<li>{{=n}}</li>{{pass}}
</ul>
</body>
</html>
Template Composition
Like most templating languages, the web2py flavor can extend and include a set of sub
templates. For example, you could have the base or child template, index.html, that
extends from a parent template, default.html. Meanwhile, default.html could include two
414
Initial Views
{{extend 'layout.html'}}
<html>
<body>
{{numbers = [1, 2, 3]}}
<ul>
{{for n in numbers:}}<li>{{=n}}</li>{{pass}}
</ul>
</body>
</html>
JavaScript Libraries
As you have seen, web2py includes a number of JavaScript libraries, many of which are
pre-configured. Refer to the web2py documentation for more information on JavaScript,
jQuery, and other components of the views.
415
Templates
Templates
default/index.html:
{{extend 'layout.html'}}
<h2>Welcome to py2manager</h2>
<br/>
{{=(project_form)}}
<br/>
<h3> All Open Projects </h3>
<ul>{{for project in projects:}}
<li>
{{=(project.name)}}
</li>
{{pass}}
</ul>
This file has a form at the top to add new projects to the database. It also lists out all
open projects using a for loop. You can view the results here:
http://localhost:8000/index. Notice how this template extends from layout.html.
default/user.html:
Open up this file. This template was created automatically to make the development
process easier and quicker. If you go back to the default.py file, you can see a
description of the main functionalities of the user function:
"""
exposes:
http://..../[app]/default/user/login
http://..../[app]/default/user/logout
http://..../[app]/default/user/register
http://..../[app]/default/user/profile
http://..../[app]/default/user/retrieve_password
http://..../[app]/default/user/change_password
use @auth.requires_login()
@auth.requires_membership('group name')
@auth.requires_permission('read','table name',record_id)
to decorate functions that need access control
"""
416
Templates
Let's edit the main layout to replace the generic template. Start with models/menu.py.
Update the following code:
response.logo = A(B('py',SPAN(2),'manager'),_class="brand")
response.title = "py2manager"
response.subtitle = T('just another project manager')
DEVELOPMENT_MENU = False
#########################################################################
## Customize your APP title, subtitle and menus here
#########################################################################
response.logo = A(B('py',SPAN(2),'manager'),_class="brand")
response.title = "py2manager"
response.subtitle = T('just another project manager')
#########################################################################
## this is the main application menu add/remove items as required
#########################################################################
DEVELOPMENT_MENU = False
Now that we've gone over the Model View Controller architecture, let's shift to focus on
the main functionality of the application.
417
Profile Page
Profile Page
Remember the auto-generated auth_user table? Take a look at the sql.log for a quick
reminder. In short, the auth_user table is part of a larger set of auto-generated tables
aptly called the Auth tables.
It's easy to add fields to any of the Auth tables. Open up db.py and place the following
code after auth = Auth(db) and before auth.define_tables) (line 90, at time of writing -
11/26/2016):
auth.settings.extra_fields['auth_user']= [
Field('address'),
Field('city'),
Field('zip'),
Field('image', 'upload')]
418
Profile Page
419
Add Projects
Add Projects
To clean up the homepage, let's move the form to add new projects to a separate page.
Open your controllers/default.py file, and add a new function:
@auth.requires_login()
def add():
project_form = SQLFORM(db.project).process()
return dict(project_form=project_form)
@auth.requires_login()
def index():
projects = db(db.project).select()
users = db(db.auth_user).select()
companies = db(db.company).select()
return locals()
{{extend 'layout.html'}}
<h2>Add a new project:</h2>
<br/>
{{=project_form.custom.begin}}
<strong>Project name</strong><br/>{{=project_form.custom.widget.name}}<br/>
<strong>Company name</strong><br/>{{=project_form.custom.widget.company_name}}<br/>
<strong>Description</strong><br/>
{{=project_form.custom.widget.description}}<br/>
<strong>Start Date</strong><br/>{{=project_form.custom.widget.start_date}}<br/>
<strong>Due Date<strong><br/>{{=project_form.custom.widget.due_date}}<br/>
{{=project_form.custom.submit}}
{{=project_form.custom.end}}
In the controller, we used web2py's SQLFORM to generate a form automatically from the
database. We then customized the look of the form using the following syntax:
form.custom.widget[fieldname] .
420
Add Projects
{{extend 'layout.html'}}
<h2>Welcome to py2manager</h2>
<br>
<h3> All Open Projects </h3>
<ul>{{for project in projects:}}
<li>
{{=(project.name)}}
</li>
{{pass}}
</ul>
421
Add Companies
Add Companies
We need to add a form for adding new companies, which follows nearly identical pattern
as adding a form for projects. Try working on it on your own before looking at the code.
default.py:
@auth.requires_login()
def company():
company_form = SQLFORM(db.company).process()
return dict(company_form=company_form)
{{extend 'layout.html'}}
<h2>Add a new company:</h2>
<br/>
{{=company_form.custom.begin}}
<strong>Company Name</strong><br/>{{=company_form.custom.widget.company_name}}<br/>
<strong>Email</strong><br/>{{=company_form.custom.widget.email}}<br/>
<strong>Phone</strong><br/>{{=company_form.custom.widget.phone}}<br/>
<strong>URL</strong><br/>{{=company_form.custom.widget.url}}<br/>
{{=company_form.custom.submit}}
{{=company_form.custom.end}}
422
Homepage
Homepage
Now, let's finish organizing the homepage to display all projects. We'll be using the
SQLFORM.grid to display all projects. Essentially, the SQLFORM.grid is a high-level table
that creates complex CRUD controls. It provides pagination, the ability to browse,
search, sort, create, update and delete records from a single table.
@auth.requires_login()
def index():
response.flash = T('Welcome!')
grid = SQLFORM.grid(db.project)
return locals()
return locals() is used to return a dictionary to the view, containing all the variables.
It's equivalent to return dict(grid=grid) , in the above example. We also added a flash
greeting.
{{extend 'layout.html'}}
<h2>All projects:</h2>
<br/>
{{=grid}}
db.project.start_date.requires = IS_DATE(format=T('%m-%d-%Y'),
error_message='Must be MM-DD-YYYY!')
db.project.due_date.requires = IS_DATE(format=T('%m-%d-%Y'),
error_message='Must be MM-DD-YYYY!')
423
Homepage
This changes the date format from YYYY-MM-DD to MM-DD-YYYY. What happens if
you use a lowercase y instead? Try it and see.
To:
Now let's update the grid in the index() function within the controller:
What does this do? Take a look at the documentation here. It's all self-explanatory.
Compare the before and after output for additional help.
424
More Grids
More Grids
First, let's add a grid to the company view.
default.py:
@auth.requires_login()
def company():
company_form = SQLFORM(db.company).process()
grid = SQLFORM.grid(db.company, create=False, deletable=False, editable=False,
maxtextlength=50, orderby=db.company.company_name)
return locals()
company.html:
{{extend 'layout.html'}}
<h2>Add a new company:</h2>
<br/>
{{=company_form.custom.begin}}
<strong>Company Name</strong><br/>{{=company_form.custom.widget.company_name}}<br/>
<strong>Email</strong><br/>{{=company_form.custom.widget.email}}<br/>
<strong>Phone</strong><br/>{{=company_form.custom.widget.phone}}<br/>
{{=company_form.custom.submit}}
{{=company_form.custom.end}}
<br/>
<br/>
<h2>All companies:</h2>
<br/>
{{=grid}}
default.py:
@auth.requires_login()
def employee():
employee_form = SQLFORM(db.auth_user).process()
grid = SQLFORM.grid(db.auth_user, create=False, fields=[db.auth_user.first_nam
e, db.auth_user.last_name, db.auth_user.email], deletable=False, editable=False, m
axtextlength=50)
return locals()
425
More Grids
employee.html:
{{extend 'layout.html'}}
<h2>All employees:</h2>
<br/>
{{=grid}}
426
Notes
Notes
Next, let's add the ability to add notes to each project. Add a new table to the database
in db_task.py:
db.define_table('note',
Field('post_id', 'reference project', writable=False),
Field('post', 'text', notnull=True),
Field('created_on', 'datetime', default=request.now, writable=False),
Field('created_by', db.auth_user, default=auth.user_id))
Update the index() function and add a note() function in the controller:
@auth.requires_login()
def index():
response.flash = T('Welcome!')
notes = [lambda project: A('Notes',_href=URL("default","note",args=[project.id
]))]
grid = SQLFORM.grid(db.project, create=False, links=notes, fields=[db.project.
name, db.project.employee_name, db.project.company_name, db.project.start_date, db
.project.due_date, db.project.completed], deletable=False, maxtextlength=50)
return locals()
@auth.requires_login()
def note():
project = db.project(request.args(0))
db.note.post_id.default = project.id
form = crud.create(db.note) if auth.user else "Login to Post to the Project"
allnotes = db(db.note.post_id==project.id).select()
return locals()
Take a look. Add some notes. Now let's add a new view called default/note.html:
427
Notes
{{extend 'layout.html'}}
<h2>Project Notes</h2>
<br/>
<h4>Current Notes</h4>
{{for n in allnotes:}}
<ul>
<li>{{=db.auth_user[n.created_by].first_name}} on {{=n.created_on.strftim
e("%m/%d/%Y")}}
- {{=n.post}}</li>
</ul>
{{pass}}
<h4>Add a note</h4>
{{=form}}<br>
We also need to give our application access to the web2py crud tools.
db.py
Change this:
# host names must be a list of allowed host names (glob syntax allowed)
auth = Auth(db, host_names=myconf.get('host.names'))
service = Service()
plugins = PluginManager()
To this:
# host names must be a list of allowed host names (glob syntax allowed)
auth = Auth(db, host_names=myconf.get('host.names'))
service = Service()
plugins = PluginManager()
crud = Crud(db)
428
Notes
@auth.requires_login()
def index():
response.flash = T('Welcome!')
notes = [lambda project: A('Notes', _class="btn", _href=URL("default","note",arg
s=[project.id]))]
grid = SQLFORM.grid(db.project, create=False, links=notes,
fields=[db.project.name, db.project.employee_name, db.project.company_name,
db.project.start_date, db.project.due_date, db.project.completed], deletable=F
alse, maxtextlength=50)
return locals()
429
Error Handling
Error Handling
web2py handles errors much differently than other frameworks. Tickets are automatically
logged, and web2py does not differentiate between the development and production
environments.
Have you seen an error yet? Remove the closing parenthesis from the following
statement in the index() function: response.flash = T('Welcome!' . Now navigate to the
homepage. You should see that a ticket number was logged. When you click on the
ticket number, you get the specific details regarding the error.
You do not want users seeing errors, create routes.py in the root of the project
(web2py/). Add the following code:
routes_onerror = [
('*/*', '/py2manager/static/error.html')
]
Refresh the homepage to see the new error message. Now errors are still logged, but
end users won't see them. Correct the error.
Homework
Please read the web2py documentation regarding error handling.
430
Interlude: Web Scraping and Crawling
Web scraping is an automated means of retrieving data from a web page. Essentially,
we grab unstructured HTML and parse it into usable data format that Python can work
with. Most web page owners and many developers do not view scraping in the highest
regard. The question of whether it's illegal or not often depends on what you do with the
data, not the actual act of scraping. If you scrape data from a commercial website, for
example, and resell that data, there could be serious legal ramifications. The scraping, if
done ethically, is generally not illegal, if you use the data for your own personal use.
That said, most developers will tell you to follow these two principles:
It's absolutely vital to adhere to ethical scraping. You could very well get yourself
banned from a website if you scrape millions of pages using a loop. With regard to the
second principle, there is much debate about whether accepting a website's terms of use
431
Interlude: Web Scraping and Crawling
is a binding contract or not. This is not a course on ethics or law, though. So, the
examples covered will adhere to both principles.
Finally, it's also a good idea to check the robots.txt file before scraping or crawling.
Usually found in the root directory of a web site, robots.txt establishes a set of rules that
web crawlers or robots should adhere to.
The User-Agent is the robot, or crawlier, itself. Nine times out of ten you will see a
wildcard, * , used as the argument, specifying that robots.txt applies to all robots.
Disallow parameters establish the directories or files - "Disallow: /folder/" or
"Disallow: /file.html" - that robots must avoid.
The Crawl-delay parameter is used to indicate the minimum delay (in seconds)
between successive server requests. So, in the HackerNews' example, after
scraping the first page, a robot must wait thirty seconds before moving on to the
next page and scraping it, and so on.
432
Libraries
Libraries
There are a number of great libraries you can use for extracting data from websites. If
you are new to web scraping, start with Beautiful Soup. It's easy to learn, simple to use,
and the documentation is great. That being said, there are plenty of examples of using
Beautiful Soup in the first Real Python course. Start there. We're going to be looking at a
more advanced library called Scrapy.
NOTE: If you are on a Windows machine, there are additional steps and
dependencies that you need to install. Please follow this video for details. Just
make sure you install the correct version of Scrapy - 1.2.1.
433
HackerNews (scrapy.Spider)
HackerNews (scrapy.Spider)
In this first example, let's scrape HackerNews.
Once Scrapy is installed, open your terminal and create a new directory called
"scraping", activate a new pyvenv environment, and then start a new Scrapy project:
├── hackernews
│ ├── __init__.py
│ ├── items.py
│ ├── pipelines.py
│ ├── settings.py
│ └── spiders
│ └── __init__.py
└── scrapy.cfg
In this basic example, we only need to worry about the creation of the actual spider,
which is the Python script used for scraping.
$ cd hackernews
$ scrapy genspider basic news.ycombinator.com
Open up the items.py file in your text editor and edit it to define the fields that you want
extracted. Let's grab the title and url from each posting on HackerNews:
434
HackerNews (scrapy.Spider)
import scrapy
class HackernewsItem(scrapy.Item):
title = scrapy.Field()
url = scrapy.Field()
Now, let's create the actual spider, which is already partially started for us in the basic.py
file found in the "spiders" directory
("/scraping/hackernews/hackernews/spiders/basic.py"):
class BasicSpider(Spider):
# name the spider
name = "basic"
Essentially, we used XPath to parse and extract the data using HTML tags:
435
HackerNews (scrapy.Spider)
class="athing" .
2. a[@href]/text() - finds all <a> tags within each <td> tag, then extracts the text
3. a/@href - again finds all <a> tags within each <td> tag, but this time it extracts
Open the start url in Chrome: https://news.ycombinator.com/. Right click on the first
article link and select "Inspect Element". In the Chrome Developer Tools console, you
can see the HTML that's used to display the first link:
<tr class="athing">
<td align="right" valign="top" class="title">
<span class="rank">1.</span>
</td>
<td valign="top" class="votelinks">
<center>
<a id="up_10606355" href="vote?for=10606355&dir=up&
auth=2febc45204aa28f15f87c085df8fcfe96a99d85d&goto=news"><div class="vot
earrow" title="upvote"></div></a>
</center>
</td>
<td class="title">
<a href="https://mikeash.com/pyblog/
friday-qa-2015-11-20-covariance-and-contravariance.html">Covariance and Contra
variance</a>
<span class="sitebit comhead"> (<a href="from?site=mikeash.com"><span class="s
itestr">mikeash.com</span></a>)</span>
</td>
</tr>
You can see that everything we need, text and url, is located between the <td
class="title"> </td> tag:
<td class="title">
<span class="deadmark"></span>
<a href="https://mikeash.com/pyblog/
friday-qa-2015-11-20-covariance-and-contravariance.html">Covariance and Contrava
riance</a>
<span class="sitebit comhead"> (<a href="from?site=mikeash.com"><span class="sit
estr">mikeash.com</span></a>)</span>
</td>
And if you look at the rest of the document, all other postings fall within the same tag.
436
HackerNews (scrapy.Spider)
titles = Selector(response).xpath('//tr[@class="athing"]/td[3]')
Now, we just need to establish the XPath for the title and url. Take a look at the HTML
again:
<a href="https://mikeash.com/pyblog/
friday-qa-2015-11-20-covariance-and-contravariance.html">Covariance and Contravari
ance</a>
Both the title and url fall within the <a> tag. So our XPath must begin with those tags.
Then we just need to extract the right attributes, text and @href respectively.
Need more help testing XPath expressions? Try the Scrapy Shell...
437
Scrapy Shell
Scrapy Shell
Scrapy comes with an interactive tool called Scrapy Shell which easily tests XPath
expressions. It's already included with the standard Scrapy installation.
The basic format for entering the shell is scrapy shell <url> :
Assuming there are no errors in the URL, you can now test your XPath expressions.
Start by using Developer Tools to get an idea of what to test. Based on the analysis we
conducted a few lines up, we know that //tr[@class="athing"]/td[3] is part of the
XPath used for extracting the title and URL. If you didn't know that, you could test it out
in Scrapy Shell.
sel.xpath('//tr[@class="athing"]/td[3]/a').extract()[0]
NOTE: By adding [0] to the end, we are just returning the first result.
Now you can see that just the title and URL are part of the results. Just extract the text
and then the href:
sel.xpath('//tr[@class="athing"]/td[3]/a/text()').extract()[0]
and
sel.xpath('//tr[@class="athing"]/td[3]/a/@href').extract()[0]
Scrapy Shell is a valuable tool for testing whether your XPath expressions are targeting
the correct data within the scraper. Try some more XPath expressions...
438
Scrapy Shell
sel.xpath('//span[@class="yclinks"]/a[3]/@href').extract()
sel.xpath('//td[@class="subtext"]/a/@href').extract()[0]
and
sel.xpath('//td[@class="subtext"]/a/text()').extract()[0]
See what else you can extract. Play around with this!
NOTE: If you need a quick primer on XPath, check out the W3C tutorial. Scrapy
also has some great documentation. Also, before you start the next section, read
this part of the Scrapy documentation. Make sure you understand the difference
between the scrapy.Spider and CrawlSpider .
439
Wikipedia (scrapy.Spider)
Wikipedia (scrapy.Spider)
In this next example, we'll scrape a listing of movies from Wikipedia:
http://en.wikipedia.org/wiki/Category:2016_films
First, check the terms of use and the robots.txt file and answer the following questions:
Start by building a scraper to scrape just the first page. Grab the movie title and URL.
This is a slightly more advanced example than the previous one. Please try it on your
own before looking at the code.
import scrapy
class WikipediaItem(scrapy.Item):
title = scrapy.Field()
url = scrapy.Field()
Setup your crawler. Again, you can setup a skeleton crawler using the following
command:
$ cd wikipedia
$ scrapy genspider wiki en.wikipedia.org
440
Wikipedia (scrapy.Spider)
class BasicSpider(Spider):
# name the spider
name = "wiki"
titles = Selector(response).xpath('//div[@id="mw-pages"]//li')
Save this to your "spiders" directory as wiki.py. Did you notice the XPath?
hxs.select('//div[@id="mw-pages"]//li')
hxs.select('//div[@id="mw-pages"]/td/ul/li')
Since <li> is a child element of <div id="mw-pages"> , you can bypass the elements
between them by using two forward slashes, // .
441
Wikipedia (scrapy.Spider)
Take a look at the results. We now need to change the relative URLs to absolute by
appending http://en.wikipedia.org to the front of the URLs. First, import the urlparse
library - from urlparse import urljoin - then update the for loop:
Also, notice how there are some links without titles. These are not movies. We can
easily eliminate them by adding a simple 'if' statement:
442
Wikipedia (scrapy.Spider)
class BasicSpider(Spider):
# name the spider
name = "wiki"
titles = Selector(response).xpath('//div[@id="mw-pages"]//li')
Delete the JSON file and run the scraper again. You should now have the full URL.
443
Socrata (CrawlSpider and Item Pipeline)
scrapy.Spider
Start with the basic spider ( scrapy.Spider ). We want the title, URL, and the number of
views for each listing. Do this on your own.
items.py:
import scrapy
class SocrataItem(scrapy.Item):
text = scrapy.Field()
url = scrapy.Field()
views = scrapy.Field()
$ cd socrata
$ scrapy genspider opendata opendata.socrata.com
opendata.py:
444
Socrata (CrawlSpider and Item Pipeline)
class OpendataSpider(Spider):
name = "opendata"
allowed_domains = ["opendata.socrata.com"]
start_urls = ['https://opendata.socrata.com/']
CrawlSpider
Moving on, let's now look at how to crawl a website as well as scrape it. Basically, we'll
start at the same starting URL, scrape the page, follow the first link in the pagination
links at the bottom of the page. Then we'll start over on that page. Scrape. Crawl.
Scrape. Crawl. Scrape. Etc.
Earlier, when you looked up the difference between the scrapy.Spider and
CrawlSpider , what did you find? Do you feel comfortable setting up the CrawlSpider?
Give it a try.
First, there's no change to items.py. We will be scraping the same data on each page.
Make a copy of opendata.py. Save it as opendata_crawl.py.
445
Socrata (CrawlSpider and Item Pipeline)
Update the name: name = "opendatacrawl" Add the rules just below the start_urls :
rules = [
Rule(LinkExtractor(allow='browse\?utf8=%E2%9C%93&page=\d*'),
callback='parse_item', follow=True)
]
What else do you have to update? First, the class must inherent from CrawlSpider , not
Spider . Anything else?
class OpendataSpider(CrawlSpider):
name = "opendata_crawl"
allowed_domains = ["opendata.socrata.com"]
start_urls = ['https://opendata.socrata.com/']
rules = [
Rule(LinkExtractor(allow='browse\?utf8=%E2%9C%93&page=\d*'),
callback='parse_item', follow=True)
]
As you can see, the only new parts of the code, besides the imports, are the rules, which
define the crawling portion of the spider and the name of the parsing method:
446
Socrata (CrawlSpider and Item Pipeline)
rules = [
Rule(LinkExtractor(allow='browse\?utf8=%E2%9C%93&page=\d*'),
callback='parse_item', follow=True)
]
NOTE: Please read over the documentation regarding rules quickly before you
read the explanation. Also, it's important that you have a basic understanding of
regular expressions. Please refer to the first Real Python course for a high-level
overview.
So, the LinkExtractor is used to specify the links that should be crawled. The allow
parameter is used to define the regular expressions that the URLs must match in order
to be crawled.
https://opendata.socrata.com/browse?utf8=%E2%9C%93&page=2
https://opendata.socrata.com/browse?utf8=%E2%9C%93&page=3
https://opendata.socrata.com/browse?utf8=%E2%9C%93&page=4
What differs between them? The numbers on the end, right? So, we need to replace the
number with an equivalent regular expression, which will recognize any number. The
regular expression \d represents any number, 0 - 9. Then the * operator is used as a
wildcard. Thus, any number will be followed, which will crawl every page in the
pagination list.
We also need to escape the question mark (?) from the URL since question marks have
special meaning in regular expressions. In other words, if we don't escape the question
mark, it will be treated as a regular expression as well, which we don't want because it is
part of the URL.
browse\?utf8=%E2%9C%93&page=\d*
Make sense?
Remember how we said that we need to crawl "ethically"? Well, let's put a 10-second
delay between each request.
447
Socrata (CrawlSpider and Item Pipeline)
WARNING: I cannot urge you enough to be careful. Only crawl sites where it is
100% legal at first. If you start venturing into gray area, do so at your own risk.
These are powerful tools you are learning. Act responsibly. If you don't, getting
banned from a site will be the least of your worries. Speaking of which, did you
check the terms of use and the robots.txt file? If not, do so now.
To add a delay, open up the settings.py file, and then add the following code:
DOWNLOAD_DELAY = 10
Item Pipeline
Finally, instead of dumping the data into a JSON file, let's feed it to a database.
Create the database within the first "socrata" directory from your shell:
$ python
>>> import sqlite3
>>> conn = sqlite3.connect("project.db")
>>> cursor = conn.cursor()
>>> cursor.execute("CREATE TABLE data(text TEXT, url TEXT, views TEXT)")
<sqlite3.Cursor object at 0x1029db730>
import sqlite3
class SocrataPipeline(object):
def __init__(self):
self.conn = sqlite3.connect('project.db')
self.cur = self.conn.cursor()
448
Socrata (CrawlSpider and Item Pipeline)
Look good? Go ahead and delete the data using the SQLite Browser. Save the
database.
This will take a while. In the meantime, send a tweet about how awesome Real Python
is! Is it still running? Take a break. Stretch. Once complete, open the database with the
SQLite Browser. You should have about ~20,000 rows of data. Make sure to hold on to
this data as we'll be using it later.
Homework
Use your knowledge of Beautiful Soup, which, again, was taught in the first Real
Python course, as well as the requests library, to scrape and parse all links from the
web2py homepage. Use a for loop to output the results to the screen. Refer back to
the first course or the Beautiful Soup documentation for assistance.
Use the following command to install Beautiful Soup: pip install beautifulsoup4
NOTE: Want some more fun? We need web professional web scrapers.
Practice more with Scrapy. Make sure to upload everything to Github. Email
us the link to [email protected]. We pay well.
449
Web Interaction
Web Interaction
Web interaction and scraping go hand in hand. Sometimes, you need to fill out a form to
access data or log in to a restricted area of a website. In such cases, Python makes it
easy to interact in real-time with web pages. Whether you need to fill out a form,
download a CSV file on a weekly basis, or extract a stock price each day when the stock
market opens, Python can handle it. This basic web interaction combined with the data
extracting methods we learned in the last lesson can create powerful tools.
import requests
import time
i = 0
i += 1
Save this file as stock_download.py in the "scraping" directory and run it. Then load up
the CSV file after the program ends to see the stock prices. You could change the sleep
time to 60 seconds so it pulls the stock price every minute or 360 to pull it every hour.
Just leave it running in the background.
450
Web Interaction
Let's look at how we got the parameters: params={'s': 'GOOG', 'f': 'sl1d1t1c1ohgv',
'e': '.csv'})
http://download.finance.yahoo.com/d/quotes.csv?s=goog&f=sl1d1t1c1ohgv&e=.csv
So to download the CSV, we need to input parameters for s , f , and e , which you
can see in the above URL. The parameters for f and e are constant, which means
you could include them in the base_url . So it's just the actual stock quote that changes.
How would you then pull prices for a number of quotes using a loop? Think about this
before you look at the answer.
import requests
import time
i = 0
i += 1
451
web2py: REST Redux
1. The data could be in high demand but the Socrata website is unreliable. By scraping
the data and providing it via a RESTful API, you can ensure the data is always
available to you or your clients.
2. Again, the data could be in high demand but it's poorly organized. You can cleanse
the data after scraping and offer it in a more human and machine readable format.
3. You want to create a mashup. You could scrape data from other sites and combine
that data with the data from Socrata and create an API.
Whatever the reason, let's look at how to quickly set up a RESTful web service via
web2py by to expose the data we pulled from Socrata.
Remember:
Let's start with a basic example before using the scraped data from Socrata.
452
Basic REST
Basic REST
Remember how to set up a web2py project?
API
Create a new database table in in db.py:
db.define_table('fam',Field('role'),Field('name'))
Within the web2py admin, enter some dummy data into the newly created table. Add the
RESTful functions to the default.py controller):
453
Basic REST
@request.restful()
def api():
response.view = 'generic.'+request.extension
def GET(*args,**vars):
patterns = 'auto'
parser = db.parse_as_rest(patterns,args,vars)
if parser.status == 200:
return dict(content=parser.response)
else:
raise HTTP(parser.status,parser.error)
def POST(table_name,**vars):
return db[table_name].validate_and_insert(**vars)
def PUT(table_name,record_id,**vars):
return db(db[table_name]._id==record_id).update(**vars)
def DELETE(table_name,record_id):
return db(db[table_name]._id==record_id).delete()
return dict(GET=GET, POST=POST, PUT=PUT, DELETE=DELETE)
These functions expose any field in the database to the outside world. If you want to limit
the resources exposed, you'll need to define various patterns. For example:
def GET(*args,**vars):
patterns = [
"/test[fam]",
"/test/{fam.name.startswith}",
"/test/{fam.name}/:field",
]
parser = db.parse_as_rest(patterns,args,vars)
if parser.status == 200:
return dict(content=parser.response)
else:
raise HTTP(parser.status,parser.error)
Sanity Check
454
Basic REST
http://127.0.0.1:8000/basic_rest/default/api/fam.json
http://127.0.0.1:8000/basic_rest/default/api/fam/id/1.json
Test out the following requests in the Shell and look at the results in the database:
Auth
In most cases, you do not want just anybody having access to your API like this.
Besides, limiting the data points as described above, you also want to have user
authentication in place.
455
Basic REST
auth.settings.allow_basic_login = True
@auth.requires_login()
@request.restful()
def api():
response.view = 'generic.'+request.extension
def GET(*args,**vars):
patterns = 'auto'
parser = db.parse_as_rest(patterns,args,vars)
if parser.status == 200:
return dict(content=parser.response)
else:
raise HTTP(parser.status,parser.error)
def POST(table_name,**vars):
return db[table_name].validate_and_insert(**vars)
def PUT(table_name,record_id,**vars):
return db(db[table_name]._id==record_id).update(**vars)
def DELETE(table_name,record_id):
return db(db[table_name]._id==record_id).delete()
return dict(GET=GET, POST=POST, PUT=PUT, DELETE=DELETE)
Homework
Watch this short video on REST.
456
Basic REST
457
Advanced REST
Advanced REST
Now that you've seen the basics of creating a RESTful web service, let's build a more
advanced example using the Socrata data.
Setup
First, create a new app called "socrata" within "web2py-rest", then register a new user -
http://127.0.0.1:8000/socrata/default/user/register, and then create a new table with the
following schema:
db.define_table('socrata',Field('name'),Field('url'),Field('views'))
Now we need to extract the data from the projects.db and import it to the new database
table you just created. There are a number of different ways to handle this. We'll export
the data from the old database in CSV format and then import it directly into the new
web2py table.
Open projects.db in your SQLite Browser. Then click "File" -> "Export" -> "Table" as CSV
file. Save the file in the following directory as socrata.csv: "../web2py-
rest/applications/socrata".
458
Advanced REST
NOTE: Be sure to use the settings for the export that are shown in the picture
above. This will create a new line for each row as well as include the field names.
You need to rename the "text" field since it's technically a restricted name. Change this
to "name".
To upload the CSV file, return to the Edit page on web2py, click the button for "database
administration", then click the "db.socrata" link. Scroll to the bottom of the page and click
"choose file" select socrata.csv. Now click import.
NOTE: In the future, when you set up your Scrapy Items Pipeline, you can dump
the data right to the web2py database. The process is the same as outlined.
Again, make sure to only grab the view count, not the word "views".
API Design
When designing your RESTful API, you should follow these best practices:
459
Advanced REST
@request.restful()
def api():
response.view = 'generic.json'+request.extension
def GET(*args,**vars):
patterns = 'auto'
parser = db.parse_as_rest(patterns,args,vars)
if parser.status == 200:
return dict(content=parser.response)
else:
raise HTTP(parser.status,parser.error)
def POST(table_name,**vars):
return db[table_name].validate_and_insert(**vars)
def PUT(table_name,record_id,**vars):
return db(db[table_name]._id==record_id).update(**vars)
def DELETE(table_name,record_id):
return db(db[table_name]._id==record_id).delete()
return dict(GET=GET, POST=POST, PUT=PUT, DELETE=DELETE)
Sanity Check
GET
Navigate to the following URL to see the endpoints that are available via GET -
http://127.0.0.1:8000/socrata/default/api/patterns.json
Output:
{
content: [
"/socrata[socrata]",
"/socrata/id/{socrata.id}",
"/socrata/id/{socrata.id}/:field"
]
}
http://127.0.0.1:8000/socrata/default/api/socrata.json
http://127.0.0.1:8000/socrata/default/api/socrata/id/ID.json
http://127.0.0.1:8000/socrata/default/api/socrata/id/ID/FIELD_NAME.json
460
Advanced REST
http://127.0.0.1:8000/socrata/default/api/socrata/id/ID.json
>>> r = requests.get("http://127.0.0.1:8000/socrata/
default/api/socrata/id/1.json")
>>> r
<Response [200]>
>>> r.content
'{"content": [{"name": "Drug and Alcohol Treatments Florida",
"views": "6", "url": "https://opendata.socrata.com/
dataset/Drug-and-Alcohol-Treatments-Florida/uzmv-9jrm", "id": 100}]}\n'
http://127.0.0.1:8000/socrata/default/api/socrata/id/ID/FIELD_NAME.json
>>> r = requests.get("http://127.0.0.1:8000/socrata/
default/api/socrata/id/100/name.json")
>>> r
<Response [200]>
>>> r.content
'{"content": [{"name": "Drug and Alcohol Treatments Florida"}]}\n'
>>> r = requests.get("http://127.0.0.1:8000/
socrata/default/api/socrata/id/100/views.json")
>>> r
<Response [200]>
>>> r.content
'{"content": [{"views": "6"}]}\n'
POST
http://127.0.0.1:8000/socrata/default/api/socrata.json
PUT
http://127.0.0.1:8000/socrata/default/api/socrata/ID.json
461
Advanced REST
DELETE
http://127.0.0.1:8000/socrata/default/api/socrata/ID.json
>>> r = requests.delete("http://127.0.0.1:8000/
socrata/default/api/socrata/3.json")
>>> r
<Response [200]>
>>> r = requests.get("http://127.0.0.1:8000/
socrata/default/api/socrata/id/3/name.json")
>>> r
<Response [404]>
>>> r.content
'no record found'
Authentication
Finally, make sure to add the login required decorator to the api() function, so that
users have to be registered to make API calls:
auth.settings.allow_basic_login = True
@auth.requires_login()
Authorized:
>>> r = requests.get("http://127.0.0.1:8000/socrata/default/api/socrata/id/1.json"
, auth=('[email protected]', 'admin'))
>>> r.content
'{"content": [{"name": "2010 Report to Congress on
White House Staff", "views": "508,705",
"url": "https://opendata.socrata.com/Government/
2010-Report-to-Congress-on-White-House-Staff/vedg-c5sb", "id": 1}]}\n'
462
Advanced REST
463
Django: Quick Start
WARNING: Django has a high learning curve due to much of the implicit
automation that happens behind the scenes. It's much more important to
understand the basics - e.g., the Python syntax and language, web client and
server fundamentals, request/response cycle, etc. - and then move on to one of
lighter frameworks (like Flask or bottle.py) so that when you do start developing
with Django, it will be much easier to obtain a deeper understanding of the
automation that happens and its integrated functionality. Even web2py, which is
slightly more automated, is easier to learn because it was specifically designed as
a learning tool.
Models represent your data model, traditionally in the form of a relational database.
Django uses the Django ORM to organize and manage databases, which functions
in relatively the same manner, despite a much different syntax, as SQLAlchemy and
web2py's DAL.
Templates visually represent the data model (like the views in the MVC
architecture). This is the presentation layer and defines how information is displayed
to the end user.
464
Django: Quick Start
Views define the business logic (like the controllers in the MVC architecture), which
logically link the templates and models.
This can be a bit confusing, but just remember that the MTV and MVC architectures
work relatively the same.
In this chapter you'll see how easy it is to get a project up and running due to the
automation of common web development tasks and integrated functionality. As long as
you are aware of the inherent structure and organization that Django uses, you can
focus less on monotonous tasks, inherent in web development, and more on developing
the higher-level portions of your application.
Brief History
Django grew organically from a development team at the Lawrence Journal-World
newspaper in Lawrence, Kansas (home of the University of Kansas) in 2003. The
developers realized that web development up to that point followed a similar pattern
resulting in much redundancy:
The developers found that the commonalities shared between most applications could
(and should) be automated. Django came to fruition from this rather simple realization,
changing the state of web development as a whole.
Homework
Read Django's Design Philosophies
Optional: To gain a deeper understanding of the history of Django and the current
state of web development, read the Introducing Django of the Django Book.
465
Installation
Installation
Make a new directory called "django-quick-start", and then create and activate a
virtualenv.
Install Django:
Want to check the Django version currently installed within your virtualenv? Open the
Python shell and run the following commands:
If you see a different version or an ImportError , make sure to uninstall Django, pip
uninstall Django , and then try installing Django again. Double-check that your virtualenv
466
Hello, World!
Hello, World!
As always, let's start with a basic "Hello World" app.
Basic Setup
Start a new Django project:
This command created the basic project layout, containing one directory and five files:
├── hello_world_project
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
NOTE: If you just type django-admin.py , you'll be taken to the help section, which
displays all the available commands that can be performed with django-admin.py .
Use this if you forget the name of a command.
For now you just need to worry about the manage.py, settings.py, and urls.py files:
manage.py: This file is a command-line utility, used to manage and interact with the
Django project and database. You probably won't ever have to edit the file itself;
however, it is used with almost every process as you develop your project. Run
python manage.py help to learn more about all the commands supported by
manage.py.
settings.py: This is the settings file for your project, where you define your project's
configuration settings, such as database connections, external applications,
template files, and so forth. There are numerous defaults setup in this file, which
often change as you develop and deploy your Project.
urls.py: This file contains the URL mappings, connecting URLs to Views.
Before we start creating our project, let's make sure everything is setup correctly by
running the built-in development server. Navigate into the first "hello_world_project"
directory and run the following command:
467
Hello, World!
NOTE: Don't worry about the migration warning that you get. Keep going!
You can specify a different port with the following command (if necessary):
You have unapplied migrations; your app may not work properly until they are appli
ed.
Run 'python manage.py migrate' to apply them.
Take note of You have unapplied migrations; your app may not work properly until they
are applied. Run 'python manage.py migrate' to apply them. Before we can test the
project, we need to apply the database migrations. We'll discuss this in the next chapter.
For now, kill the development server by pressing Control-C within the terminal, then run
the command:
468
Hello, World!
You should see both directories and all files on the left pane. Now if you exit out of
Sublime and want to start working on the same project, you can just go to "Project" ->
"Open Project", and then find the saved hello-world-project.sublime-project file.
469
Hello, World!
This will create a new directory called "hello_world", which includes the following files:
admin.py: Used to register an app's models with the Django admin panel
models.py: Used to define your data models and map them to database table
tests.py: Houses your test code used for testing your application (don't worry about
this for now)
views.py: This file is your application's controller, defining the business logic in order
to accept HTTP requests and return responses back to the user
├── db.sqlite3
├── hello_world
│ ├── __init__.py
│ ├── admin.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── hello_world_project
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
NOTE: What’s the difference between a Django project and an App? A project is
the main web app, containing the settings, templates, and URL routes for a set of
Django Apps. Meanwhile, a Django app is just an application that has an
individual function such as a blog or message forum. Each app should have a
separate function associated with it, distinct from other Apps. Django Apps are
used to encapsulate common functions. Put another way, the project is your final
product (your entire web app), comprised of separate functions from each app
(each individual feature of your App, like - user authentication, a blog, registration,
and so on.
By breaking Projects into a series of small Apps, you can theoretically reuse a
Django app in a different Django project - so there's no need to reinvent the
wheel.
Next, we need to include the new app in the settings.py file so that Django knows that it
exists. Scroll down to "INSTALLED_APPS" and add the app name, hello_world, to the
end of the tuple:
470
Hello, World!
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello_world',
)
def index(request):
return HttpResponse('<html><body>Hello, World!</body></html>')
This function, index() , takes a parameter, request , which is an object that has
information about the HTTP request.
We named the function index() but as you will see in a second, this doesn't matter
- you can name the function whatever you wish. By convention, though, make sure
the function name represents the main objective of the function itself.
A response object is then instantiated, which returns the text <html><body>Hello,
World!</body></html> to the browser.
Add a urls.py file to the the "hello_world" app, then add the following code to link (or
map) a URL to our specific home view:
urlpatterns = [
url(r'^$', views.index, name='index'),
]
471
Hello, World!
With our app's Views and URLs setup, we now just need to link the project URLs to the
app URLs. To do this, add the following code to the project urls.py in the
"hello_world_project" folder:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/', include('hello_world.urls')),
]
SEE ALSO: Please read the official Django documentation on URLs for further
details and examples.
Let's test it out. Fire up the server - python manage.py runserver , and then open your
browser to http://localhost:8000/hello. It worked! You should see the "Hello, World!" text
in the browser.
Navigate back to the root - http://localhost:8000/. You should see a 404 error since we
have not defined a view that maps to / . Let's change that.
Open up your Project's urls.py file again and update the urlpatterns list:
472
Hello, World!
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/', include('hello_world.urls')),
url(r'^', include('hello_world.urls')),
]
Save the file and refresh the page. You should see the same "Hello, World!" text. So, we
simply assigned or mapped two URLs ( / and /hello ) to that single view.
Homework
Experiment with adding additional text-based views within the app's urls.py file and
assigning them to URLs. For example:
view:
def about(request):
return HttpResponse(
"Here is the About Page. Want to return home? <a href='/'>Back Home</a>"
)
url:
Templates
Django templates are similar to web2py's views, which are used for displaying HTML to
the user. You can also embed Python code directly into the templates using template
tags. Let's modify the example above to take advantage of this feature.
Navigate to the project root and create a new directory called "templates". Your project
structure should now look like this:
473
Hello, World!
├── hello_world
│ ├── __init__.py
│ ├── admin.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── hello_world_project
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── templates
Next, update the settings.py file to add the path to the "templates" directory to the list
associated with the DIRS key so that your Django project knows where to find the
templates:
def index(request):
return HttpResponse('<html><body>Hello, World!</body></html>')
def about(request):
return HttpResponse(
"Here is the About Page. Want to return home? <a href='/'>Back Home</a>"
)
def better(request):
t = loader.get_template('betterhello.html')
c = Context({'current_time': datetime.now(), })
return HttpResponse(t.render(c))
474
Hello, World!
Next, create a new HTML file called betterhello.html in the "template" directory and pass
in the key from the dictionary surrounded by double curly braces {{ }} :
<html>
<head><title>A Better Hello!</title></head>
<body>
<p>Hello, World! This template was rendered on {{current_time}}.</p>
</body>
</html>
475
Hello, World!
Here, we have a placeholder in our view, {{current_time}} , which is replaced with the
current date and time from the views, {'current_time': datetime.now(),} . If you see an
error, double check your code. One of the most common errors is that the templates
directory path is incorrect in your settings.py file. Try adding a print statement to
double check that path is correct to the settings.py file - print(os.path.join(BASE_DIR,
'templates')) .
Notice the time. Is it correct? The time zone defaults to UTC, TIME_ZONE = 'UTC' . If you
would like to change it, open the settings.py file, and then change the COUNTRY/CITY
based on the timezones found in Wikipedia.
For example, if you change the time zone to TIME_ZONE = 'America/Denver' , and refresh
http://localhost:8000/better, it should display the U.S. Mountain Standard Time.
Change the time zone so that the time is correct based on your location.
476
Workflow
Workflow
Before moving on, let's take a look at the basic workflow used for creating a Django
project and App...
Create a Project
1. Run python django-admin.py startproject <name_of_the_project> to create a new
Project. This will create the project in a new directory. Enter that new directory.
2. Migrate the database via python manage.py migrate .
Creating an App
1. Run python manage.py startapp <name_of_the_app> .
2. Add the name of the app to the INSTALLED_APPS tuple within settings.py file so that
Django knows that the new app exists.
3. Link the Application URLs to the main URLs within the Project's urls.py file.
4. Add the views to the Application's views.py file.
5. Add a urls.py file within the new application's directory to map the views to specific
URLs.
6. Create a "templates" directory, update the template path in the "settings.py" file, and
finally add any templates.
477
Interlude: Introduction to JavaScript and jQuery
NOTE: Did you miss part 1? Jump back to Interlude: Introduction to HTML and
CSS.
478
Getting Started
Getting Started
Start by adding a main.js file to the root directory and include the following code:
$(function() {
console.log("whee!")
});
Then add the following files to your index.html just before the closing </body> tag:
<script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/
bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="main.js"></script>
Here we are just including the jQuery and Bootstrap dependencies as well as and our
custom JavaScript file, main.js.
Open the "index.html" file in your web browser. In the JavaScript file there is a
console.log . This is a debugging tool that allows you to post a message to the
Now, insert a word into the input box and click Submit. Nothing happens. We need to
somehow grab that inputted word and do something with it.
479
Handling the Event
NOTE: Please note that jQuery is Javascript, just a set of helper methods
developed in JavaScript. It is possible to perform the exact same functionality
jQuery provides in vanilla JavaScript - it just takes more code.
Update main.js:
$(function() {
console.log("whee!")
// event handler
$("#btn-click").click(function() {
if ($('input').val() !== '') {
var input = $("input").val()
console.log(input)
}
});
});
the code in the remainder of the function. In other words, the remaining JavaScript
will not run until there is a button click.
2. var input = $("input").val() sets a variable called input with the inputted value
480
Handling the Event
Open "index.html" in your browser. Make sure you have your JavaScript console open.
Enter a word in the input box and click the button. This should display the word in the
console
481
Updating the DOM
Updated file:
$(function() {
console.log("whee!")
// event handler
$("#btn-click").click(function() {
if ($('input').val() !== '') {
// grab the value from the input box after the button click
var input = $("input").val()
// display value within the browser's JS console
console.log(input)
// add the value to the DOM
$('ol').append('<li><a href="">x</a> - ' + input + '</li>');
}
$('input').val('');
});
});
Then add the following code to your index.html file, just below the button:
<br>
<br>
<h2>Todos</h2>
<h3>
<ol class="results"></ol>
<h3>
482
Updating the DOM
input variable to the DOM between the <ol class="results"> and <ol> . Further,
we're adding the value from the input plus some HTML, <a href="">x - </a> .
$('input').val(''); clears the input box.
Before moving on, we need to make one last update to main.js to remove todos from the
DOM once complete.
$(function() {
console.log("whee!")
// event handler
$("#btn-click").click(function() {
if ($('input').val() !== '') {
// grab the value from the input box after the button click
var input = $("input").val()
// display value within the browser's JS console
console.log(input)
// add the value to the DOM
$('ol').append('<li><a href="">x</a> - ' + input + '</li>');
}
$('input').val('');
});
});
483
Updating the DOM
Here, on the event, the click of the link, we're removing that specific todo from the DOM.
event.preventDefault() cancels the default action of the click, which is to follow the link.
Try removing this to see what happens. this refers to the current object, a , and we're
removing the parent element, <li >`.
Homework
Ready to test your skills? Check out Practicing jQuery with the Simpsons along with
the other front-end resources.
Go through the Codecademy tracks on JavaScript and jQuery for more practice.
Spend some time with Chrome's Dev Tools. Read the official documentation for
help.
484
Django Bloggy: A blog app (part one)
485
Setup
Setup
Setup a new Django project:
$ mkdir django-bloggy
$ cd django-bloggy
$ pyvenv-3.5 env
$ source env/bin/activate
$ pip install django==1.10.3
$ pip freeze > requirements.txt
$ django-admin.py startproject bloggy_project
486
Model
Model
Database setup
Open up settings.py and navigate to the DATABASES dict. Notice that SQLite is the
default database. Let's change the name to bloggy.db:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'bloggy.db'),
}
}
$ cd bloggy_project
$ python manage.py migrate
This command, migrate , creates the basic tables based on the apps in the
INSTALLED_APPS tuple in your settings.py file. Did it ask you to create a superuser? Use
"admin1234" for both your username and password, and '[email protected]' for the
email. If not, run python manage.py createsuperuser to create one.
You should now see a the bloggy.db file in your project's root directory.
Sanity check
Launch the Django development server:
Open http://localhost:8000/ and you should see the familiar, light-blue "Welcome to
Django" screen. Kill the server.
487
Setup an App
Setup an App
Start a new app
class Post(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100)
content = models.TextField()
This class, Post() , which inherits some of the basic properties from the standard
Django Model() class, defines the database table as well as each field - created_at ,
title , and content , representing a single blog post.
NOTE: Much like SQLAlchemy and web2py's DAL, the Django ORM provides a
database abstraction layer used for interacting with a database via Python
objects.
Note that the primary key - which is a unique id (uuid) that we don't even need to define -
and the created_at timestamp will both be automatically generated for us when new
Post objects are added to the database. In other words, we just need to explicitly add
the title and content when creating new objects (database rows).
settings.py
Add the new app to the settings.py file:
488
Setup an App
INSTALLED_APPS = (
# Django Apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Local Apps
'blog',
)
Migrations
Execute the underlying SQL statements to create the database tables by creating then
applying the migration:
The makemigrations command essentially tells Django that changes have been made to
the models and we want to create a migration. You can see the actual migration by
opening the file 0001_initial.py from the "migrations folder".
Now we need to apply the migration to create the actual database tables:
Operations to perform:
Apply all migrations: admin, blog, contenttypes, auth, sessions
Running migrations:
Applying blog.0001_initial... OK
Just remember whenever you want to make any changes to the database, you must:
489
Setup an App
490
Django Shell
Django Shell
Django provides an interactive Python shell for accessing the Django API. Use the
following command to start the Shell:
Searching
Let's add some data to the table we just created via the database API. Start by importing
the Post model we created in the Django Shell:
If you search for objects in the table (somewhat equivalent to the SQL statement select
* from blog_post ) you should find that it's empty:
>>> Post.objects.all()
<QuerySet []>
Adding data
To add new rows, we can use the following commands:
491
Django Shell
NOTE: Remember that we don't need to add a primary key or the created_at
time stamp as those are auto-generated.
Searching (again)
Now if you search for all objects, three objects should be returned:
>>> Post.objects.all()
<QuerySet [<Post: Post object>, <Post: Post object>, <Post: Post object>]>
Notice how <Post: Post object> returns absolutely no distinguishing information about
the object. Let's change that.
492
Django Shell
def __str__(self):
return self.title
class Post(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100)
content = models.TextField()
def __str__(self):
return self.title
Save your models.py file, exit the Shell, re-open the Shell, import the Post model class
again ( from blog.models import Post ), and now run the query Post.objects.all() .
<QuerySet [<Post: What Am I Good At>, <Post: Understand Your Support System Better
With Sentiment Analysis>, <Post: Charting Best Practices>]>
This should be much easier to read and understand. We know there are three rows in
the table, and we know their titles.
Want more?
Depending on how much information you want returned, you could add all the fields to
the models.py file:
def __str__(self):
return "{0}/{1}/{2}/{3}\n".format(self.id, self.created_at, self.title, self.c
ontent)
Test this out. What does this return? Make sure to update this when you're done so it's
just returning the title again:
def __str__(self):
return self.title
493
Django Shell
Open up your SQLite Browser to make sure the data was added correctly:
>>> Post.objects.filter(id=1)
>> Post.objects.filter(id__gt=1)
>>> Post.objects.filter(title__contains='Charting')
494
Django Shell
If you want more info on querying databases via the Django ORM in the Shell, take a
look at the official Django documentation. And if you want a challenge, add more data
from within the Shell via SQL and practice querying using straight SQL. Then delete all
the data. Finally, see if you can add the same data and perform the same queries again
within the Shell - but with objects via the Django ORM rather than SQL.
Homework
Please read about the Django Models for more information on the Django ORM
syntax.
495
Unit Tests for Models
class PostTests(TestCase):
def test_str(self):
my_title = Post(title='This is a basic title for a basic test case')
self.assertEquals(
str(my_title), 'This is a basic title for a basic test case',
)
NOTE: You can run all the tests in the Django project with the following command
- python manage.py test ; or you can run the tests from a specific app, like in the
command above.
496
Unit Tests for Models
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Destroying test database for alias 'default' (':memory:')...
One thing to to note is that since this test needed to add data to a database to run, it
created a temporary, in-memory database and then destroyed it after the test ran. This
prevents the test from accessing the real database and possibly damaging the database
by mixing test data with real data.
NOTE: Anytime you make changes to an existing model or function, re-run the
tests. If they fail, find out why. You may need to re-write them depending on the
changes made. Or: the code you wrote may have broken the tests, which will
need to be refactored.
497
Django Admin
Django Admin
Depending how familiar you are with the Django ORM and SQL in general, it's probably
much easier to access and modify the data stored within the models using the Django
web-based admin. This is one of the most powerful built-in features that Django has to
offer.
To access the admin, add the following code to the admin.py file in the "blog" directory:
admin.site.register(Post)
Here, we're simply telling Django which models we want to make available to the admin.
Now let's access the Django Admin. Fire up the server and navigate to
http://localhost:8000/admin within your browser. Enter your login credentials
("admin1234" and "admin1234").
Add some more posts, change some posts, delete some posts. Go crazy.
498
Django Admin
499
Custom Admin View
Start by creating a PostAdmin() class that inherits from admin.ModelAdmin from the
admin.py within the "blog" directory:
# blog/admin.py
class PostAdmin(admin.ModelAdmin):
pass
Next, in that new class, add a variable called list_display , and set it equal to a tuple
that includes the fields from the database we want displayed:
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'created_at')
admin.site.register(Post, PostAdmin)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'created_at')
admin.site.register(Post, PostAdmin)
500
Custom Admin View
501
Templates and Views
Before we start, create a new directory in the root called "templates" then within that
directory add a "blog" directory, and then add the correct path to our settings.py file, just
like we did with the 'Hello World' app:
└── bloggy_project
├── blog
│ ├── __init__.py
│ ├── admin.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── bloggy.db
├── bloggy_project
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── templates
└── blog
View
Add the following code to the views.py file:
502
Templates and Views
def index(request):
latest_posts = Post.objects.all().order_by('-created_at')
t = loader.get_template('blog/index.html')
c = Context({'latest_posts': latest_posts, })
return HttpResponse(t.render(c))
Template
Create a new file called index.html within the "templates/blog" directory and add the
following code:
URL
Add a urls.py file to your "bloggy_project/blog" directory, then add the following code:
urlpatterns = [
url(r'^$', views.index, name='index'),
]
503
Templates and Views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('blog.urls')),
]
Sanity Check
Test it out. Fire up the server and navigate to http://localhost:8000/blog/. You should
have something that looks like this:
Refactor
Let's clean this up a bit.
504
Templates and Views
<html>
<head>
<title>Bloggy: a blog app</title>
</head>
<body>
<h2>Welcome to Bloggy!</h2>
{% if latest_posts %}
<ul>
{% for post in latest_posts %}
<h3><a href="/blog/{{ post.id }}">{{ post.title }}</a></h3>
<p><em>{{ post.created_at }}</em></p>
<p>{{ post.content }}</p>
<br>
{% endfor %}
</ul>
{% endif %}
</body>
</html>
If you refresh the page now, you'll see that it's easier to read. Also, each post title is now
a link. Try clicking on the link. You should see a 404 error because we have not set up
the URL route or the template. Let's do that now.
Notice how we used two kinds of curly braces within the template. The first, {% ... %} ,
is used for adding Python logic/expressions such as a if statements or for loops,
and the second, {{ ... }} , is used for inserting variables or the results of an
expression.
Finally, Before moving on, update the timezone to reflect where you live. Flip back to the
Django: Quick Start chapter for more information.
post.html
URL
Update the urls.py file within the "blog" directory:
505
Templates and Views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<post_id>\d+)/$', views.post, name='post'),
]
Take a look at the new URL. The pattern, (?P<slug>\d+) , is made up of regular
expressions.
Turn to the the Python docs to learn more about regular expressions. There's also a
chapter on regular expressions within the first Real Python course. Finally, you can test
out regular expressions using Pythex:
View
Add a new function to views.py:
506
Templates and Views
Template
Create a new template called post.html:
<html>
<head>
<title>Bloggy: {{ single_post.title }}</title>
</head>
<body>
<h2>{{ single_post.title }}</h2>
<ul>
<p><em>{{ single_post.created_at }}</em></p>
<p>{{ single_post.content }}</p>
<br/>
</ul>
<p>Had enough? Return <a href="/blog">home</a>.</p><br/>
</body>
</html>
Back on the development server, test out the links for each post. They should all be
working now.
507
Friendly Views
Friendly Views
Take a look at our current URLs for displaying posts - /blog/1 , /blog/2 , and so forth.
Although this works, it's not the most human readable (or SEO friendly). Instead, let's
update this so that the post title is used in the URL rather than the primary key.
To achieve this, we need to update our views.py, urls.py, and index.html files.
View
Update the index() function within the views.py file:
def index(request):
latest_posts = Post.objects.all().order_by('-created_at')
t = loader.get_template('blog/index.html')
context_dict = {'latest_posts': latest_posts, }
for post in latest_posts:
post.url = post.title.replace(' ', '_')
c = Context(context_dict)
return HttpResponse(t.render(c))
The main difference is that we created a post.url using a for loop to replace the
spaces in a post name with underscores:
Thus, a post title of "test post" will convert to "test_post". This will make our URLs look
better (and, hopefully, more search engine friendly). If we didn't remove the spaces or
add the underscore ( post.url = post.title ), the URL would show up as "test%20post",
which is difficult to read. Try this out if you're curious.
Template
Now update the actual URL in the index.html template...
Replace:
508
Friendly Views
With:
View (again)
The post() function is still searching for a post in the database based on an id . Let's
update that:
Now, this function is searching for the title in the database rather than the primary key.
Notice how we have to replace the underscores with spaces so that it matches exactly
what's in the database.
URL
Finally, let's update the regex in the urls.py file:
Run the server. Now you should see URLs that are a little easier on the eye.
509
Django Migrations
Django Migrations
Before moving on, let's look at how to handle database schema changes via migrations
in Django.
NOTE: This feature came about from a KickStarter campaign just like all of the
Real Python courses. Thank you all, again!
Before Django Migrations you had to use a package called South, which wasn't always
the easiest to work with. But now with migrations built-in, they automatically become part
of your basic, everyday workflow.
NOTE: By setting blank=True , we are indicating that the field is not required and
can be left blank within the form (or whenever data is inputted by the user).
Meanwhile, null=True allows blank values to be stored in the database as NULL .
These options are usually used in tandem.
Since we're going to be working with images, we need to use Pillow. You should have an
error in your terminal telling you to do just that (if the development server is running):
510
Django Migrations
ERRORS:
blog.Post.image: (fields.E210) Cannot use ImageField because Pillow is not install
ed.
HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip in
stall Pillow".
Install Pillow:
Fire up your development server and log in to the admin page, and then try to add a new
row to the Post model. You should see the error table blog_post has no column named
tag because the fields we tried to add didn't get added to the database. That's a
Do you remember what the makemigrations command does? It essentially tells Django
that changes were made to the models and we want to create a migration.
Adding on to that definition, Django scans your models and compares them to the newly
created migration file, which you'll find in your "migrations" folder in the "blog" folder; it
should start with 0002_ .
WARNING: You should always double-check the migration file to ensure that it is
correct. Complex changes to the model may not always turn out the way you
expect within the migration file. You can edit migration files directly, if necessary.
511
Django Migrations
Apply migrations
Now, let's apply those migrations to the database:
Output:
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying blog.0002_auto_20161207_1607... OK
Run the server, enter the Django admin, and you now should be able to add new rows
that include tags, an image, and/or the number of views.
Add a few posts with the new fields. Don't add any images just yet. Then update the
remaining posts with tags and views. Do you remember how to update which fields are
displayed in the admin? Add the number of views to the admin by updating the
list_display tuple in the admin.py file:
Update app
Now let's update the the application so that tags and images are displayed on the post
page.
Update post.html:
512
Django Migrations
<html>
<head>
<title>Bloggy: {{ single_post.title }}</title>
</head>
<body>
<h2>{{ single_post.title }}</h2>
<ul>
<p><em>{{ single_post.created_at }}</em></p>
<p>{{ single_post.content }}</p>
<p>Tag: {{ single_post.tag }}</p>
<br>
<p>
{% if single_post.image %}
<img src="/media/{{ single_post.image }}">
{% endif %}
</p>
</ul>
<p>Had enough? Return <a href="/blog">home</a>.</p><br/>
</body>
</html>
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('blog.urls')),
url(r'^media/(?P<path>.*)$', serve, {'document_root': MEDIA_ROOT}),
]
513
Django Migrations
Add some more posts from the admin. Make sure to include images. Check out the
results at http://localhost:8000/blog/. Also, you should see the images in the
"media/images" folder.
514
View Counts
View Counts
What happens to the view count when you visit a post? Nothing. It should increment,
right?
Super simple.
Let's also add a counter to the post page by adding the following code to post.html:
Save. Navigate to a post and check out the counter. Refresh the page to watch it
increment.
515
Styles
Styles
Before adding any styles, we need to break our templates into base and child templates,
so that the child templates inherent the HTML and styles from the base template. We've
covered this a number of times before so we won't go into great detail. If you would like
more info as to how this is achieved specifically in Django, please see this document.
Parent template
Create a new template file called _base.html in the templates directory. This is the
parent template. Get the code from the assets directory in the book 2 exercises repo.
NOTE: Make sure to put the _base.html template in the main "templates"
directory, while the other templates go in the "blog" directory. By doing so, you
can now use this same base template for all your apps.
Child templates
Update index.html:
{% extends '_base.html' %}
{% block content %}
{% if latest_posts %}
<ul>
{% for post in latest_posts %}
<h3><a href="/blog/{{ post.url }}">{{ post.title }}</a></h3>
<p><em>{{ post.created_at }}</em></p>
<p>{{ post.content }}</p>
<br>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
Update post.html:
516
Styles
{% extends '_base.html' %}
{% block content %}
<h2>{{ single_post.title }}</h2>
<ul>
<p><em>{{ single_post.created_at }}</em></p>
<p>{{ single_post.content }}</p>
<p>Tag: {{ single_post.tag }}</p>
<p>Views: {{ single_post.views }}</p>
<br>
<p>
{% if single_post.image %}
<img src="/media/{{ single_post.image }}">
{% endif %}
</p>
</ul>
<p>Had enough? Return <a href="/blog">home</a>.</p><br/>
{% endblock %}
Take a look at the results. Amazing what five minutes - and Bootstrap - can do.
517
Popular Posts
Popular Posts
Next, let's update the index view to return the top five posts by popularity (most views).
If, however, there are five posts or less, all posts will be returned.
View
Update views.py:
def index(request):
latest_posts = Post.objects.all().order_by('-created_at')
popular_posts = Post.objects.order_by('-views')[:5]
t = loader.get_template('blog/index.html')
context_dict = {
'latest_posts': latest_posts,
'popular_posts': popular_posts,
}
for post in latest_posts:
post.url = post.title.replace(' ', '_')
c = Context(context_dict)
return HttpResponse(t.render(c))
Template
Update the index.html template, so that it loops through the popular_posts :
<h4>Must See:</h4>
<ul>
{% for popular_post in popular_posts %}
<li><a href="/blog/{{ popular_post.title }}">{{ popular_post.title }}</a></li>
{% endfor %}
</ul>
Updated file:
518
Popular Posts
{% extends '_base.html' %}
{% block content %}
{% if latest_posts %}
<ul>
{% for post in latest_posts %}
<h3><a href="/blog/{{ post.url }}">{{ post.title }}</a></h3>
<p><em>{{ post.created_at }}</em></p>
<p>{{ post.content }}</p>
<br>
{% endfor %}
</ul>
{% endif %}
<h4>Must See:</h4>
<ul>
{% for popular_post in popular_posts %}
<li><a href="/blog/{{ popular_post.title }}">{{ popular_post.title }}</a></li>
{% endfor %}
</ul>
{% endblock %}
Bootstrap
Next let's add the Bootstrap Grid System to our index.html template:
519
Popular Posts
{% extends '_base.html' %}
{% block content %}
<div class="row">
<div class="col-md-8">
{% if latest_posts %}
<ul>
{% for post in latest_posts %}
<h3><a href="/blog/{{ post.url }}">{{ post.title }}</a></h3>
<p><em>{{ post.created_at }}</em></p>
<p>{{ post.content }}</p>
<br>
{% endfor %}
</ul>
{% endif %}
</div>
<div class="col-md-4">
<h4>Must See:</h4>
<ul>
{% for popular_post in popular_posts %}
<li><a href="/blog/{{ popular_post.title }}">{{ popular_post.title }}</a></
li>
{% endfor %}
</ul>
</div>
</div>
{% endblock %}
SEE ALSO: For more info on how the Grid System works, please review the
Getting Started With Bootstrap 3 post.
View
If you look at the actual URLs for the popular posts, you'll see that they do not have the
underscores in the URLs. Let's change that. The easiest way to correct this is to just add
another for loop to the index view:
Updated function:
520
Popular Posts
def index(request):
latest_posts = Post.objects.all().order_by('-created_at')
popular_posts = Post.objects.order_by('-views')[:5]
t = loader.get_template('blog/index.html')
context_dict = {
'latest_posts': latest_posts,
'popular_posts': popular_posts,
}
for post in latest_posts:
post.url = post.title.replace(' ', '_')
for popular_post in popular_posts:
popular_post.url = popular_post.title.replace(' ', '_')
c = Context(context_dict)
return HttpResponse(t.render(c))
Finally, let's add a encode_url() helper function to views.py and refactor the index()
function to clean up the code:
# helper function
def encode_url(url):
return url.replace(' ', '_')
def index(request):
latest_posts = Post.objects.all().order_by('-created_at')
popular_posts = Post.objects.order_by('-views')[:5]
t = loader.get_template('blog/index.html')
context_dict = {
'latest_posts': latest_posts, 'popular_posts': popular_posts,
}
for post in latest_posts:
post.url = encode_url(post.title)
for popular_post in popular_posts:
popular_post.url = encode_url(popular_post.title)
c = Context(context_dict)
return HttpResponse(t.render(c))
Test this out in your development server to make sure nothing broke. Try running your
test suite as well:
521
Popular Posts
Next chapter, we will create a form so we can add posts to our blog.
Homework
Update post.html file to show the popular posts.
Notice how we have some duplicate code in both the index() and post()
functions. This stinks. How can you write a helper function for this so that you're
only writing that code once?
522
Django Bloggy: A blog app (part two)
523
Forms
Forms
In this section, we'll look at how to add forms to allow end-users to add posts utilizing
Django's built-in form handling features.
1. Display an HTML form with automatically generated form widgets (like a text field or
date picker)
2. Check submitted data against a set of validation rules
3. Redisplay a form in case of validation errors
4. Convert submitted form data to the relevant Python data types
In short, forms take the inputted user data, validate it, and then convert the data to
Python objects.
To simplify the process of form creation, let's split the workflow into four steps:
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['title', 'content', 'tag', 'image', 'views']
524
Forms
Here, we created a new ModelForm that's mapped to our model via the Meta() inner
class model = Post . Notice how each of our form fields have an associated column in
the database. This is required.
def add_post(request):
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid(): # is the form valid?
form.save(commit=True) # yes? save to database
return redirect(index)
else:
print(form.errors) # no? display errors to end user
else:
form = PostForm()
return render(request, 'blog/add_post.html', {'form': form})
525
Forms
If it's a POST request, we first determine if the supplied data is valid or not.
Essentially, forms have two different types of validation that are triggered when
is_valid() is called on a form - field and form validation:
1. Field validation, which happens at the form level, validates the user inputs against
the arguments specified in the ModelForm - i.e., max_length=100 , required=false ,
etc. Be sure to look over the official Django Documentation on Widgets to see the
available fields and the parameters that each can take.
2. Once the fields are validated, the values are converted over to Python objects and
then form validation occurs via the form's clean method. Read more about this
method here.
Validation ensures that Django does not add any data to the database from a submitted
form that could potentially harm your database.
Again, each of these forms of validation happen implicitly as soon as the is_valid()
method is called. You can, however, customize the process. Read more about the
overall process from the links above or for a more detailed workflow, please see the
Django form documentation here.
After the data is validated, Django either saves the data to the database,
form.save(commit=True) and redirects the user to the index page or outputs the errors to
526
Forms
{% extends '_base.html' %}
{% block content %}
<h2>Add a Post</h2>
<form id="post_form" method="post" action="/blog/add_post/" enctype="multipart/for
m-data">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
Here, we have a <form> tag, which loops through both the visible and hidden fields.
Only the visible fields will produce markup (HTML). Read more about such loops here.
Also, within the visible fields loop, we are displaying the validation errors, {{
field.errors }} , as well as the name of the field, label_tag . You never want to do this
for the hidden fields since this will really confuse the end user. You just want to have
tests in place to ensure that the hidden fields generate valid data each and every time.
We'll discuss this in a bit.
Finally, did you notice the {% csrf_token %} tag? This is a Cross-Site Request Forgery
(CSRF) token, which is required by Django. Please read more about it from the official
Django documentation.
Alternatively, if you want to keep it simple, you can render the form with one tag:
527
Forms
{% extends '_base.html' %}
{% block content %}
<h2>Add a Post</h2>
<form id="post_form" method="post" action="/blog/add_post/" enctype="multipart/for
m-data">
{% csrf_token %}
{{ form }}
When you test this out (which will happen in a bit), try both types of templates. Make
sure to end with the latter one.
Since we're not adding any custom features to the form in the above (simple) template,
we can get away with just this simplified template and get the same functionality as the
more complex template. If you are interested in further customization, check out the
Django documentation.
NOTE: When you want users to upload files via a form, you must set the enctype
to multipart/form-data . If you do not remember to do this, you won't get an error;
the upload just won't be saved - so, it's vital that you remember to do this. You
could even write your templates in the following manner to ensure that you include
enctype="multipart/form-data" in case you don't know whether users will be
{% if form.is_multipart %}
<form enctype="multipart/form-data" method="post" action="">
{% else %}
<form method="post" action="">
{% endif %}
{{ form }}
</form>
528
Forms
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^add_post/', views.add_post, name='add_post'), # add post form
url(r'^(?P<slug>[\w|\-]+)/$', views.post, name='post'),
]
That's it. Just update the Add Post link in our _base.html template:
Styling Forms
Navigate to the form template at http://localhost:8000/blog/add_post/.
Looks pretty bad. We can clean this up quickly with Bootstrap, specifically with the
Django Forms Bootstrap package.
Install:
529
Forms
INSTALLED_APPS = (
# Django Apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Local Apps
'blog',
)
{% extends '_base.html' %}
{% block content %}
{% load bootstrap_tags %}
<h2>Add a Post</h2>
<form id="post_form" method="post" action="/blog/add_post/" enctype="multipart/for
m-data">
{% csrf_token %}
{{ form | as_bootstrap}}
Sanity check
Restart the server. Check out the results:
530
Forms
Well, just remove that specific field from the fields list in forms.py:
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['title', 'content', 'tag', 'image']
Test it out.
Homework
See if you can change the redirect URL after a successful form submission, return
redirect(index) , to redirect to the newly created post.
531
Forms
532
Even Friendlier Views
Not good. Let's fix that by using a unique slug for the URL with the Django Uuslug
package.
Model
Update models.py:
class Post(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100)
content = models.TextField()
tag = models.CharField(max_length=20, blank=True, null=True)
image = models.ImageField(upload_to="images", blank=True, null=True)
views = models.IntegerField(default=0)
slug = models.CharField(max_length=100, unique=True)
def __str__(self):
return self.title
533
Even Friendlier Views
Essentially, we're overriding how the slug is saved by setting up the custom save()
method. For more on this, please check out the official Django Uuslug documentation.
What's next?
However, it's not that easy this time. When you run that command, you'll see:
You are trying to add a non-nullable field 'slug' to post without a default;
we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
Select an option:
Basically, since the field is not allowed to be blank, the existing database columns need
to have a value. Let's provide a default - meaning that all columns will be given the same
value - and then manually fix them later on. After all, this is the exact issue that we're
trying to fix: All post URLs must be unique.
Select an option: 1
Please enter the default value now, as valid Python
The datetime module is available, so you can do e.g. datetime.date.today()
>>> "test"
Migrations for 'blog':
0003_post_slug.py:
- Add field slug to post
Before we can migrate this, we have another problem. Take a look at the new field we're
adding:
The value must be unique. If we try to migrate now, we will get an error since we're
adding a default value of "test". Here's how we get around this: Open the migrations file
and remove the unique constraint so that it looks like this:
534
Even Friendlier Views
class Migration(migrations.Migration):
dependencies = [
('blog', '0002_auto_20140918_2218'),
]
operations = [
migrations.AddField(
model_name='post',
name='slug',
field=models.CharField(default='test', max_length=100),
preserve_default=False,
),
]
slug = models.CharField(max_length=100)
Django Shell
Start the Shell:
Grab all the posts, then loop through them appending the post title to a list:
535
Even Friendlier Views
Model
Update the slug field in the model:
URL
Update the following URL pattern in urls.py:
536
Even Friendlier Views
To:
Here we matched the slug with any number of '-' characters followed by any number of
alphanumeric characters. Again, if you're unfamiliar with regex, check out the chapter on
regular expressions in the first Real Python course and be sure to test out regular
expressions with Pythex.
View
Now we can simplify the views.py file (making sure to replace post_url with slug ).
Head over to the assets folder in the exercises repo for the updated code.
Template
Now update the actual URL in the index.html template...
Replace:
With:
Make sure to update the popular posts URL in both index.html and post.html as well...
Replace:
With:
537
Even Friendlier Views
Sanity Check
What happens now when you try to register a post with a duplicate title, like - "test"? You
should get two unique URLS:
1. http://localhost:8000/blog/test/
2. http://localhost:8000/blog/test-1/
Bonus! What happens if you register a post with a symbol in the title, like - '?', '*', or '!'? It
should drop it from the URL altogether.
Nice.
538
Stretch Goals
Stretch Goals
What else could you add to this app? Comments? User auth? Tests! Add whatever you
feel like. Find tutorials on the web for assistance. Show us the results.
Homework
Optional: You're ready to go through the Django tutorial. For beginners, this tutorial
can be pretty confusing. However, since you now have plenty of web development
experience and have already created a basic Django app, you should have no
trouble. Have fun! Learn something.
539
Django Workflow
Django Workflow
The following is a basic workflow that you can use as a quick reference for developing a
Django 1.10 project.
NOTE: We followed this workflow during the Hello World app and loosely followed
this for our Blog app. Keep in mind that this is just meant as a guide, so alter this
workflow as you see fit for developing your own app.
Setup
1. Within a new directory, create and activate a new virtualenv.
2. Install Django.
3. Create your project: django-admin.py startproject <name>
4. Create a new app: python manage.py startapp <appname>
5. Add your app to the INSTALLED_APPS tuple.
540
Django Workflow
Forms
1. Create a forms.py file in the app directory to define form-related classes; define your
ModelForm classes there.
2. Add or update a view for handling the form logic - e.g., displaying the form, saving
the form data, alerting the user about validation errors, etc.
3. Add or update a template to display the form.
4. Add a urlpattern in the app's urls.py file for the new view.
User Registration
1. Create a UserForm
2. Add a view for creating a new user.
3. Add a template to display the form.
4. Add a urlpattern for the new view.
User Login
1. Add a view for handling user credentials.
2. Create a template to display a login form.
3. Add a urlpattern for the new view.
541
Bloggy Redux: Introducing Blongo
542
MongoDB
MongoDB
MongoDB is an open-source, document-oriented database. Classified as a NoSQL
database, Mongo stores semi-structured data in the form of binary JSON objects
(BSON). It's essentially schema-less and meant to be used for hierarchical data that
does not conform to the traditional relational databases. Typically, if the data you are
tying to store does not easily fit on to a spreadsheet, you could consider using Mongo or
some other NoSQL database. For more on Mongo, check out the official Mongo
documentation.
To download, visit the following site. Follow the instructions for your particular operating
system.
$ mongo --version
MongoDB shell version: 3.4.0
Once complete, be sure to follow the instructions to get mongod running, which,
according to the official documentation, is the primary daemon process for the MongoDB
system. It handles data requests, manages data format, and performs background
management operations.
This must be running in order for you to connect to the Mongo server.
543
Talking to Mongo
Talking to Mongo
In order for Python to talk to Mongo, you need some sort of ODM (Object Document
Module), which is akin to an ORM for relational databases. There's a number to choose
from, but we'll be using MongoEngine, which uses PyMongo to connect to MongoDb.
We'll use PyMongo to write some test scripts to learn how Python and Mongo talk to
each other.
Create a new directory called "django-blongo", enter the newly created directory,
activate a virtualenv, and then install MongoEngine and PyMongo:
NOTE: It is important that you use the versions specified above. Some of the
newer versions of mongoengine and pymongo are not compatible with each other
or do not support django out of the box.
544
Talking to Mongo
$ python
>>> import pymongo
>>> client = pymongo.MongoClient("localhost", 27017)
>>> db = client.test
>>> db.name
'test'
>>> db.my_collection
Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_
aware=False, connect=True), 'test'), 'my_collection')
>>> db.my_collection.save({"django": 10})
ObjectId('53e2e5a03386b7202c8bde61')
>>> db.my_collection.save({"flask": 20})
ObjectId('53e2e5b13386b7202c8bde62')
>>> db.my_collection.save({"web2py": 30})
ObjectId('53e2e5bc3386b7202c8bde63')
>>> db.my_collection
Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_
aware=False, connect=True), 'test'), 'my_collection')
>>> db.my_collection.find()
<pymongo.cursor.Cursor object at 0x10124f810>
>>> for item in db.my_collection.find():
... print(item)
...
{'django': 10, '_id': ObjectId('5848da7425dc9ab950b64086')}
{'flask': 20, '_id': ObjectId('5848da8a25dc9ab950b64087')}
{'web2py': 30, '_id': ObjectId('5848da9325dc9ab950b64088')}
Here, we created a new database called test , and then added a new collection (which
is equivalent to a table in relational databases). Then within that collection we saved a
number of objects, which we queried for at the end. Simple, right?
Try creating a new database and collection and adding in JSON objects to define a
simple blog with a 'title' and a 'body'. Add a number of short blog posts to the collection.
Test Script
545
Talking to Mongo
import pymongo
Save this as mongo.py. This simply connects to your instance of Mongo, then outputs all
database names. It will come in handy.
546
Django Setup
Django Setup
Install Django within "django-blongo", then create a new Project:
Update settings.py:
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
AUTHENTICATION_BACKENDS = (
'mongoengine.django.auth.MongoEngineBackend',
)
SESSION_ENGINE = 'mongoengine.django.sessions'
MONGO_DATABASE_NAME = 'blongo'
NOTE: One thing to take note of here is that by using MongoDB, you cannot use
the syncdb or migrate commands. Thus, you lose out-of-the-box migrations as
well as the Django Admin panel. There are a number of drop-in replacements for
the Admin panel, like django-mongoadmin. There are also a number of example
scripts floating around on Github and Stack Overflow, detailing how to migrate
your data.
547
Setup an App
Setup an App
Start a new app:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blongo',
)
Define the model for the application. Despite the fact that Mongo does not require a
schema, it's still import to define one at the application level so that we can specify
datatypes, force require fields, and create utility methods. Keep in mind that this is only
enforced at the application-level, not within MongoDB itself. For more information,
please see the official MongoEngine documentation. Simply add the following code to
the models.py file:
class Post(Document):
title = StringField(max_length=200, required=True)
content = StringField(required=True)
date_published = DateTimeField(default=datetime.datetime.now, required=True)
Here, we just created a class called Posts() that inherits from Document, then we
added the appropriate fields. Compare this model, to the model from the Bloggy chapter.
There's very few differences.
548
Add Data
Add Data
Let's go ahead and add some data to the Posts collection from the Django Shell:
To access the data we can use the objects attribute from the Document() class:
Now that we’ve got data in MongoDB, let’s finish building the app. You should be pretty
familiar with the process by now; if you have questions that are not addressed, please
refer to the Bloggy chapter.
549
Update Project
Update Project
Project URLs
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('blongo.urls')),
]
App URLs
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Views
def index(request):
latest_posts = Post.objects
t = loader.get_template('index.html')
context_dict = {'latest_posts': latest_posts}
c = Context(context_dict)
return HttpResponse(t.render(c))
As you saw before, we're using the objects attribute to access the posts, then passing
the latest_posts variable to the templates.
550
Update Project
Template Settings
Add the paths for the template directory in settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
.
├── blongo
│ ├── __init__.py
│ ├── admin.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── blongo_project
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── templates
Template
Finally add the template (index.html). Go to the "assets" folder in the Book 2 Exercises
repo for the code.
551
Update Project
552
Test
Test
Fire up the development server, and then navigate to http://localhost:8000/ to see the
blog.
553
Test Script
Test Script
Finally, let's update the test script to tailor it specifically to our app:
import pymongo
def get_collection(conn):
databases = conn.database_names()
if 'blongo' in databases:
# connect to the blongo database
db = conn.blongo
# grab all collections
collections = db.collection_names()
# output all collection
print("Collections")
print("-----------")
for collection in collections:
print(collection)
# pick a collection to view
col = input(
'\nInput a collection name to show its field names: '
)
if col in collections:
get_items(db[col].find())
else:
print("Sorry. The '{}' collection does not exist.".format(col))
else:
print("Sorry. The 'blongo' database does not exist.")
def get_items(collection_name):
if __name__ == '__main__':
# Open the MongoDB connection
conn = pymongo.Connection('mongodb://localhost:27017')
get_collection(conn)
554
Test Script
Run this to connect to MongoDB, grab the 'blongo' database, and then output the items
within a specific collection. It tests out the 'post' collection.
555
Conclusion
Conclusion
Next Steps
Implement all of the features of Bloggy on your own. Good luck! With Mongo now
integrated, you can add in the various features in the exact same way you we did before;
you will just need to tailor the actual DB queries to the MongoEngine syntax. You should
be able to hack your way through this to get most of the features working. If you get
stuck or want to check your answers, just turn back to the Bloggy chapter.
Once done, email us the link to your Github project and we'll look it over.
Summary
There are some problems with how we integrated MongoDB into our Django project.
1. We do not have a basic admin to manage the data. Despite the ease of adding,
updating, and deleting data within a Mongo collection, it's still nice to be able to
have a GUI to manage this. How can you fix this?
Fortunately, we've solved this problem for you in the next course. Cheers!
556
Django: Ecommerce Site
NOTE: The point of this chapter is to get a working application up quickly. This
application is extended throughout Course 3 into an enterprise-level application.
557
Rapid Web Development
As you have seen, development is broken into logical chunks. If you were starting from
complete scratch we'd begin with prototyping to define the major features and design a
mock-up through iterations: Prototype, Review, Refine. Repeat. After each stage you
generally get more and more detailed, from low to high-fidelity, until you've hashed out
all the features and prepared a mock-up complex enough to add a web framework, in
order to apply the MVC-style architecture.
If you are beginning with low-fidelity, start prototyping with a pencil and paper. Avoid the
temptation to use prototyping software until the latter stages, as these can restrict
creativity.
As you define each function and apply a design, put yourself in the end users' shoes.
What can they see? What can they do? Do they really care? For example, if one of your
app's features is to allow users to view a list of search results from an airline aggregator
in pricing buckets, what does this look like? Are the results text or graphic-based? Can
the user drill down on the ranges to see the prices at a more granular level? Will the end
user care? They better. Will this differentiate your product versus the competition?
Perhaps not. But there should be some features that do separate your product from your
competitor's products. Or perhaps your functionally is the same - you just implement it
better?
Finally, rapid web development is one of the most important skills a web developer can
have, especially developers who work within a start-up environment. Speed is the main
advantage that start-ups have over their larger, more established competition. The key is
to understand each layer of the development process, from beginning to end.
SEE ALSO: For more on prototyping, check out this excellent resource.
558
Prototyping
Prototyping
Prototyping is the process of building a working model of your application, from a front-
end perspective, allowing you to test and refine your application's main features and
functions. Again, it's common practice to begin with a low-fidelity prototype.
From there you can start building a storyboard, which traces the users' movements. For
example, if the user clicks the action button and they are taken to a new page, create
that new page. Again, for each main feature, answer these three questions:
If you're building out a full-featured web application, take the time to define in detail
every interaction the user has with the app. Create a storyboard, and then after plenty of
iterations, build a high-fidelity prototype. One of the quickest means of doing this is with
Bootstrap.
Get used to using user stories to describe behavior, as discussed in the Flask BDD
chapter. Break down each of your app's unique pieces of functionality into user stories to
drive out the app's requirements. This not only helps with organizing the work, but
tracking progress as well.
As a <role>
I want <goal>
In order to <benefit>
For example:
As a visitor
I want to sign up
In order to view blog posts
Each of these individual stories are then further broken down until you can transfer them
over to individual functions. This helps answer the question, "Where should I begin
developing?"
559
Prototyping
We won't be getting this granular in this chapter since you now have the skills to do this
on your own. For now, let's assume we already went through the prototyping phase, built
out user stories, and figured out our app's requirements. We have also put this basic
app, with little functionality on the web somewhere and validated our business model.
We know we have something, in other words; we just need to build it. This is where
Django comes in.
NOTE: In many cases, development begins with defining the model and
constructing the database first. Since we're creating a rapid prototype, we'll start
with the front-end first, adding the most important functions, then move to the
back-end. This is vital for when you develop your basic minimum viable
prototype/product (MVP). The goal is to create an app quickly to validate your
product and business model. Once validated, you can finish developing your
product, adding all components you need to transform your prototype into a
project.
560
Project Setup
Project Setup
Create a new directory called "django_ecommerce". Create and activate a virtualenv
within that directory, and then install Django:
$ cd django_ecommerce
$ python manage.py startapp main
NOTE: Due to the various features of this project, we will be creating a number of
different apps. Each app will play a different role within your main project. This is a
good practice: Each app should encompass a single piece of functionality.
├── django_ecommerce
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── main
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── manage.py
561
Project Setup
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main'
]
Create a new directory within the root directory called "templates", and then add the
absolute path to the settings.py file within the TEMPLATES list:
admin.autodiscover()
Don't forget to add your Django project folder to Sublime as a new project.
562
Landing Page
Landing Page
Now that the main app is up, let's quickly add a landing page. Add the following code to
the main app's views.py file:
def index(request):
return render(
request, 'index.html'
)
index() takes a parameter, request , which is an object that has info about the user
requesting the page from the browser. The function's response is to simply render the
index.html template. In other words, when a user navigates to the index.html page (the
request), the Django controller renders the index.html template (the response).
NOTE: Did you notice the render() function? This is simply used to render the
given template.
Finally, we need to create the index.html template. Create a new file called base.html
(the parent template) within the templates directory. Go to the exercises repo and look in
the assets for the code.
By now you should understand the relationship between the parent and child templates
so we won't go over that again. Also, your project structure should now look like this:
563
Landing Page
├── django_ecommerce
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── main
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── manage.py
└── templates
├── base.html
└── index.html
Looking good so far, but let's add some more pages. Before that, though, let's take a
step back and talk about Bootstrap.
564
Landing Page
565
Bootstrap
Bootstrap
We used the Bootstrap front end framework to quickly add a basic design. To avoid
looking too much like, well, a generic Bootstrap site, the design should be customized,
which is not too difficult to do. In fact, as long as you have a basic understanding of CSS
and HTML, you shouldn't have a problem. That said, customizing Bootstrap can be time-
consuming. It takes practice to get good at it. Try not to get discouraged as you build out
your prototype. Work on one single area at a time, then take a break. Remember: It does
not have to be perfect - it just has to give users, a better sense of how your application
works.
Make as many changes as you want. Learning CSS like this is a trial and error process:
You just have to make a few changes, then refresh the browser, see how they look, and
then make more changes or update or revert old changes. Again, it takes time to know
what will look good. Eventually, after much practice, you will find that you will be
spending less and less time on each section, as you know how to get a good base set
up quickly and then you can focus on creating something unique.
In this chapter, we will not focus too much time on the design process. Be sure to check
out the next course where we had a slick Star Wars theme to our application.
SEE ALSO: If you're working on your own application, Jetstrap is a great resource
for creating quick Bootstrap prototypes. Try it out. Also, check out this for info on
how to create a nice sales page with Bootstrap.
566
About Page
About Page
First, let's add the Flatpages App, which will allow us to add basic pages with HTML
content. Add it to the INSTALLED_APPS section in setings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'main'
]
SITE_ID = 1
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
]
Wait. What is middleware? Check the Django docs for a detailed answer specific to
Django's middleware. Read it even though it may not make much sense right now. Then
check the Modern Web Development chapter for a more general definition. Finally, once
you complete this course, go through the third Real Python course, Advanced Web
Development with Django, for a detailed explanation specific to Django, which will tie
everything together.
567
About Page
url(r'^pages/', include('django.contrib.flatpages.urls')),
Then, add a new template folder within the "templates" directory called "flatpages". Add
a default template by creating a new file, default.html, within that new directory. Add the
following code to the file:
{% extends 'base.html' %}
{% block content %}
{{ flatpage.content }}
{% endblock %}
NOTE: If you were not prompted to create a superuser after your migrate, run this
command: 'python manage.py createsuperuser'
<li><a href="/pages/about">About</a></li>
Launch the server and navigate to http://localhost:8000/admin/, log in with the username
and password you just created for the superuser, and then add the following page within
the Flatpages section:
568
About Page
<br>
<p>You can add some text about yourself here. Then when you are done, just add a c
losing HTML paragraph tag.</p>
<ul>
<li>Bullet Point # 1</li>
<li>Bullet Point # 2</li>
<li>Bullet Point # 3</li>
<li>Bullet Point # 4</li>
</ul>
<br/>
Nice, right? Next, let's add a Contact Us page. First, your project structure should look
like this:
569
About Page
├── db.sqlite3
├── django_ecommerce
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── main
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── manage.py
└── templates
├── base.html
├── flatpages
│ └── default.html
└── index.html
570
Contact App
Contact App
Create a new app:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'main',
'contact',
]
class ContactForm(models.Model):
name = models.CharField(max_length=150)
email = models.EmailField(max_length=250)
topic = models.CharField(max_length=200)
message = models.CharField(max_length=1000)
timestamp = models.DateTimeField(
auto_now_add=True
)
def __unicode__(self):
return self.email
class Meta:
ordering = ['-timestamp']
571
Contact App
Which file does this code belong in? What will this code do? Let's talk it out. We are are
creating a new model for our contact app, which exists within our e-commerce project.
This code defines a table that will exist within our project's database, but the model itself
will be used within the contact app. Therefore, we need to add this code to the
models.py file within the "contact" directory. So now that we know how the code is
interacting with our project, let's go deeper.
Does this make sense to you? The DateTimeField and the ordering within the Meta()
class may be new. If so, take a look at the official Django docs:
DateTimeField
ordering
In the latter case, we are simply deviating from the default ordering and defining our own
based on the data added.
Add a view:
def contact(request):
if request.method == 'POST':
form = ContactView(request.POST)
if form.is_valid():
our_form = form.save(commit=False)
our_form.save()
messages.add_message(
request, messages.INFO, 'Your message has been sent. Thank you.'
)
return redirect('/')
else:
form = ContactView()
return render(request, 'contact.html', {'form': form, })
572
Contact App
Here, if the request is a POST (if the form is submitted, in other words) we need to
process the form data. Meanwhile, if the request is anything else, then we just create a
blank form. If it is a POST request and the form data is valid, then save the data, redirect
the user to main page, and display a message.
NOTE: The third course dives more into the inner workings of forms, and how to
properly test them to ensure data is properly being validated. Check it out!
class ContactView(ModelForm):
message = forms.CharField(widget=forms.Textarea)
class Meta:
model = ContactForm
fields = ['name', 'email', 'topic', 'message']
class ContactFormAdmin(admin.ModelAdmin):
class Meta:
model = ContactForm
admin.site.register(ContactForm, ContactFormAdmin)
573
Contact App
{% extends "base.html" %}
{% block content %}
<h3><center>Contact Us</center></h3>
{% endblock %}
See anything new? How about the {% csrf_token %} and {{ form.as_p }} tags? Read
about the former here. Why is it so important? Because you will be subject to CSRF)
attacks if you leave it off. Meanwhile, including the as_p method simply renders each
for field as a separate paragraph. Check out some of the other ways you can render
forms here.
Fire up the server and load your app. Navigate to the main page. Click the link for
"contact". Test it out first with valid data, then invalid data. Then make sure that the valid
data is added to the database table within the Admin page. Pretty cool. As a sanity
check, open the database in the SQLite database browser and ensure that the valid data
was added as a row in the correct table.
Now that you know how the contact form works, go back through this section and see if
you can follow the code. Try to understand how each piece fits into the whole within the
MVC structure.
574
Contact App
See if you can find the code for it in the views.py file. You can read more about the
messages framework here, which provides support for flash messages.
├── contact
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── django_ecommerce
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── main
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── manage.py
└── templates
├── base.html
├── contact.html
├── flatpages
│ └── default.html
└── index.html
575
Contact App
576
User Registration with Stripe
SEE ALSO: Take a look at this brief tutorial on implementing Flask and Stripe to
get a sense of how Stripe works with Python (obviously, it will be slightly different
to integrate with Django).
Update the path to the STATICFILES_DIRS in the settings.py file above STATIC_URL =
'/static/' and add the "static" directory to the root directory:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'main',
'contact',
'payments',
]
Install stripe:
577
User Registration with Stripe
Update models.py:
class User(AbstractBaseUser):
name = models.CharField(max_length=255)
email = models.CharField(max_length=255, unique=True)
# password field defined in base class
last_4_digits = models.CharField(max_length=4, blank=True, null=True)
stripe_id = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
USERNAME_FIELD = 'email'
def __str__(self):
return self.email
SEE ALSO: This model replaces the default Django User model. For more on
this, please visit the official Django docs
# user registration/authentication
url(r'^sign_in$', payment_views.sign_in, name='sign_in'),
url(r'^sign_out$', payment_views.sign_out, name='sign_out'),
url(r'^register$', payment_views.register, name='register'),
url(r'^edit$', payment_views.edit, name='edit'),
Add the remaining templates and all of the static files from the assets directory in the
repository:
578
User Registration with Stripe
Take a look at the templates. These will make more sense after you see the app in
action.
Now in admin.py:
class UserAdmin(admin.ModelAdmin):
class Meta:
model = User
admin.site.register(User, UserAdmin)
Add a new file called forms.py with the file in the assets folder of the exercises repo.
Whew. That's a lot of forms. There isn't too much new happening, so let's move on. If
you want, try to see how these form attributes align to the form fields in the templates.
Update payments/views.py with the file in the assets folder of the exercises repo.
Yes, there is a lot going on here, and guess what - We're not going to go over any of it. It
falls on you this time. Why? We want to see how far you're getting and how badly you
really want to know what's happening in the actual code. Are you just trying to get an
answer or are you taking it a step further and going through the process, working
through each piece of code line by line?
You can charge users either a one time charge or a recurring charge. This script is set
up for the latter. To change to a one time charge, simply make the following changes to
the file:
579
User Registration with Stripe
if form.is_valid():
# update based on your billing method (subscription vs one time)
# customer = stripe.Customer.create(
# email=form.cleaned_data['email'],
# description=form.cleaned_data['name'],
# card=form.cleaned_data['stripe_token'],
# plan="gold",
#
# )
customer = stripe.Charge.create(
description = form.cleaned_data['email'],
card = form.cleaned_data['stripe_token'],
amount="5000",
currency="usd"
)
Yes, this is a bit of a hack. We'll show you an elegant way of doing this in the next
course.
├── contact
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── django_ecommerce
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── main
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── manage.py
├── payments
580
User Registration with Stripe
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── static
│ ├── application.js
│ ├── bootstrap.css
│ ├── jquery.js
│ ├── jquery.min.js
│ └── jquery_ujs.js
└── templates
├── base.html
├── cardform.html
├── contact.html
├── edit.html
├── errors.html
├── field.html
├── flatpages
│ └── default.html
├── home.html
├── index.html
├── register.html
├── sign_in.html
├── test.html
└── user.html
Update main/views.py:
def index(request):
uid = request.session.get('user')
if uid is None:
return render(request, 'index.html')
else:
return render(
request,
'user.html',
{'user': User.objects.get(pk=uid)}
)
581
User Registration with Stripe
Update the base.html template with base_version_2.html from the assests folder in the
repo.
Stripe
Before we can start charging customers, we must grab the API keys and add a
subscription plan, which we called plan="gold" back in our views.py file. This is the
plan ID.
582
User Registration with Stripe
The first time you go to this URL, it will ask you to log in. Don't. Instead, click the link to
sign up, then on the sign up page, click the link to skip this step. Finally, on the next
view, click the link to 'go straight to your dashboard'. You should now be taken to the
dashboard page.
583
User Registration with Stripe
Now, you should be able to access the keys, by going to this URL:
https://dashboard.stripe.com/account/apikeys.
Test payment
Go ahead and test this out using the test keys. Fire up the server. Navigate to the main
page, then click Register.
Fill everything out. Make sure to use the credit card number 4242424242424242 and
any 3 digits for the CVC code. Use any date in the future for the expiration date.
Click submit. You should get an error that says, "No such plan: gold". If you go back to
the view, you will see this code indicating that this is looking for a Stripe plan with the id
of 'gold':
584
User Registration with Stripe
customer = stripe.Customer.create(
email = form.cleaned_data['email'],
description = form.cleaned_data['name'],
card = form.cleaned_data['stripe_token'],
plan="gold",
)
NOTE: As of April of 2016, Stripe has adopted more secure protocols. In this
chapter the TLS upgrade is likely to affect your progress with integrating Stripe
into your application. But have no fear as this is a great learning opportunity,
although I must admit, a bit tricky. It requires you to update your 'OpenSSL'
version to 1.0.1 or greater (currently 1.0.1j) and this newer version must then be
used by Python3 within your development environment. This setup process will be
similar on most computers but exactly the same. Here are a bunch of tutorials and
posts to help you on this journey:
https://support.stripe.com/questions/how-do-i-upgrade-my-openssl-to-
support-tls-1-2
https://support.stripe.com/questions/how-do-i-upgrade-my-stripe-integration-
from-tls-1-0-to-tls-1-2#python Mac OS X
http://apple.stackexchange.com/questions/126830/how-to-upgrade-openssl-
in-os-x
https://github.com/Homebrew/legacy-homebrew/issues/14497
Let's create that plan now. Kill the server. Then, back on Stripe, click Plans and then
Create your first plan. Enter the following data into the form:
585
User Registration with Stripe
ID: gold
Name: Amazing Gold Plan
Amount: 20
Interval: monthly
Trial Period: 10
Statement Description: Gold Plan
586
User Registration with Stripe
SEE ALSO: Refer to the Stripe documentation and API reference docs for more
info.
Go back to your app. Now you should be able to register a new user.
587
User Registration with Stripe
Finally, after you process the test/dummy payment, make sure the subscription was
added to the Stripe dashboard.
Take a look at the 'payments_users' collection in the SQLiteBrowser to make sure your
customer info saved:
588
User Registration with Stripe
It's up to you to figure out what type of product or service you are offering the user.
You can update a user's credit card info within the Member's only page. Try this. Change
the CVC code and expiration data. Then, back on Stripe, click Customers and then the
customer. You should see an event showing that the changes were made. If you click on
the actual event, you can see the raw JSON sent that shows the changes you made.
589
Next Steps
Next Steps
Think about some of the other things that you'd want to allow a user to do. Update their
plan - if you had separate plan tiers, of course. Cancel. Maybe they could get a monthly
credit if they invite a friend to your app.
In the next course, we will show you how to do this and more. See you then. Cheers!
590
Appendix A: Installing Python
591
Check Current Version
$ python
Python 2.7.12 (default, Oct 11 2016, 05:24:00)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
NOTE: If you have a version older than 3.5, please download the latest version
below.
592
Install Python
Install Python
Choose your Operating system...
Mac
You need a Python version 3.5+. So, if you need to download a new version, download
the latest installer for version 3.5.1.
Linux
If you are using Ubuntu, Linux Mint, or another Debian-based system, enter the following
command in your terminal to install Python:
Or you can download the tarball directly from the official Python website. Once
downloaded, run the following commands:
NOTE: If you have problems or have a different Linux distribution, you can always
use your package manager or just do a Google search for how to install Python on
your particular Linux distribution.
Windows
Download
Start by downloading Python 3.5.1 from the official Python website. The Windows
version is distributed as a MSI package. Once downloaded, double-click to start the
installer. Follow the installer instructions to completion. By default this will install Python
to C:\Python35 .
593
Install Python
NOTE: You may need to be logged in as the administrator to run the install.
Test
To test this install open your command prompt, which should open to the C:prompt,
C:/> , then type:
\Python35\python.exe
NOTE: The >>> indicates that you are at the Python interpreter (or prompt)
where you can run Python code interactively.
exit()
Then press Enter. This will take you back to the C:prompt.
Path
You also need to add Python to your PATH environmental variables, so when you want
to run a Python script, you do not have to type the full path each and every time, as this
is quite tedious. In other words, after adding Python to the PATH, we will be able to
simply type python in the command prompt rather than \Python35\python.exe .
Since you downloaded Python version 3.5.1, you need to add the add the following
directories to your PATH:
C:\Python35\
C:\Python35\Scripts\
C:\PYTHON35\DLLs\
C:\PYTHON35\LIB\
594
Install Python
[Environment]::SetEnvironmentVariable("Path",
"$env:Path;C:\Python35\;C:\Python35\Scripts\;
C:\PYTHON35\DLLs\;C:\PYTHON35\LIB\;", "User")
That's it.
Video
Watch the video here for assistance. Note: Even though this is an older version of
Python the steps are the same.
595
Verify Install
Verify Install
Test this new install by opening a new terminal, then type python . You should see the
same output as before except the version number should now be 3.5.1 (or whatever the
latest version of Python is):
$ python
Python 3.5.2 (default, Oct 11 2016, 05:05:28)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
NOTE: You may need to run python3 instead of python if you have multiple
versions of Python installed.
596
Appendix B: Supplementary Materials
597
Working with FTP
In most cases, FTP is used to either upload a file (such as a web page) to a remote
server, or download a file from a server. In this lesson, we will access an FTP server to
view the main directory listing, upload a file, and then download a file.
import ftplib
server = ''
username = ''
password = ''
# Initialize and pass in FTP URL and login credentials (if applicable)
ftp = ftplib.FTP(host=server, user=username, passwd=password)
598
Working with FTP
Let's test this out with a public ftp site, using these server and login credentials:
server = 'ftp.debian.org'
username = 'anonymous'
password = 'anonymous'
Also add the following line just after you create the list:
Keep everything else the same, and save the file again. Now you can run it. If done
correctly your output should look like this:
Next, let's take a look at how to download a file from a FTP server.
599
Working with FTP
import ftplib
import sys
server = 'ftp.debian.org'
username = 'anonymous'
password = 'anonymous'
# Initialize and pass in FTP URL and login credentials (if applicable)
ftp = ftplib.FTP(host=server, user=username, passwd=password)
ftp.cwd('debian')
# Create a local file with the same name as the remote file
with open(file_name, "wb") as f:
When you run the file, make sure you specify a file name from the remote server on the
command line. You can use any of the files you saw in the above script when we
outputted them to the screen.
For example:
600
Working with FTP
import ftplib
import sys
server = ''
username = ''
password = ''
# Initialize and pass in FTP URL and login credentials (if applicable)
ftp = ftplib.FTP(host=server, user=username, passwd=password)
Unfortunately, the public FTP site we have been using does not allow uploads. You will
have to use your own server to test. Many free hosting services offer FTP access. You
can set one up in less than fifteen minutes. Just search Google for "free hosting with ftp"
to find a free hosting service. One good example is http://www.0fees.net.
After you set up your FTP Server, update the server name and login credentials in the
above script, save the file, and then run it. Again, specify the filename as one of the
command line arguments. It should be a file found on your local directory.
For example:
Check to ensure that the file has been uploaded to the remote directory
NOTE: It's best to send files in binary mode, "rb", as this mode sends the raw
bytes of the file. Thus, the file is transferred in its exact original form.
Homework
601
Working with FTP
Write a script to navigate to a specific directory, upload a file, and then run a directory
listing of that directory to see the newly uploaded file.
602
Working with SFTP
Install pysftp :
Now, let's try to list the directory contents from a remote server via SFTP:
import pysftp
server = ''
username = ''
password = ''
# Initialize and pass in SFTP URL and login credentials (if applicable)
sftp = pysftp.Connection(host=server, username=username, password=password)
To test the code, you will have to use your own remote server that supports SFTP.
Unfortunately, most of the free hosting services do not offer SFTP access. But don't
worry, there are free sites that offer free shell accounts which normally include SFTP
access. Just search Google for "free shell accounts".
603
Working with SFTP
After you set up your SFTP Server, update the server name and login credentials in the
above script, save the file, and then run it. If done correctly, all the files and directories of
the current directory of your remote server will be displayed.
Next, let's take a look at how to download a file from an SFTP server:
import pysftp
import sys
server = ''
username = ''
password = ''
# Initialize and pass in SFTP URL and login credentials (if applicable)
sftp = pysftp.Connection(host=server, username=username, password=password)
When you run it, make sure you specify a file name from the remote server on the
command line. You can use any of the files you saw in the above script when you
outputted them to the screen.
For example:
604
Working with SFTP
import pysftp
import sys
server = ''
username = ''
password = 'p@'
# Initialize and pass in SFTP URL and login credentials (if applicable)
sftp = pysftp.Connection(host=server, username=username, password=password)
When you run this, make sure you specify a file name from your local directory on the
command line.
For example:
Check to ensure that the file has been uploaded to the remote directory
605
Sending and Receiving Email
606
Sending and Receiving Email
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# email account info from where we'll be sending the email from
smtp_host = 'smtp.mail.com'
smtp_port = '###'
user = 'username'
password = 'password'
# use encryption
server.starttls()
Since this code is pretty self-explanatory (follow along with the inline code comments),
go ahead and update the the following variables: smtp_host , smtp_port , user ,
password to match your email account's SMTP info and login credentials you wish to
607
Sending and Receiving Email
send from.
Example:
# email account info from where we'll be sending the email from
smtp_host = 'smtp.gmail.com'
smtp_port = 587
user = '[email protected]'
password = "it's a secret - sorry"
Try using a Gmail account to send and receive from the same address at first to test it
out, and then send a message from Gmail to a different email account, on a different
email service. Once complete, run the file. As long as you don't get an error, the email
should have sent correctly. Check your email to make sure.
608
Sending and Receiving Email
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# use encryption
server.starttls()
server.quit()
if __name__ == '__main__':
fromaddr = '[email protected]'
toaddr = '[email protected]'
subject = 'test'
body_text = 'hear me?'
smtp_host = 'smtp.gmail.com'
smtp_port = '587'
user = '[email protected]'
password = "it's a secret"
Meanwhile, IMAP (Internet Message Access Protocol) is the Internet standard for
receiving email on a remote mail server. Python provides the imaplib library as part of
the standard library which is used to define the IMAP client session implementation,
609
Sending and Receiving Email
used for accessing email. Essentially, we will set up our own mail server.
import imaplib
# email account info from where we'll be sending the email from
imap_host = 'imap.gmail.com'
imap_port = '993'
user = '[email protected]'
password = "It's a secret - sorry!"
Notice how we used the same Gmail account from the last example. Make sure you
tailor this to your own account settings. Essentially, this code is used to read the most
recent email in the Inbox. This just so happened to be the email I sent myself in the last
example.
Here we know that server.select returns a tuple of 2 values. This syntax unpacks the
tuple and assigns values to the variables in the order they are listed.
610
Sending and Receiving Email
--===============7968962300591266385==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Also, in the program num[0] it specifies the message we wish to view, while
(BODY[TEXT]) displays the information from the email.
Homework
See if you can figure out how to use a loop to read the first 10 messages in your inbox.
611
Acknowledgements
Acknowledgements
Writing is an intense, solitary activity that requires discipline and repetition. Although
much of what happens in that process is still a mystery to me, I know that the my friends
and family have played a huge role in the development of this course. I am immensely
grateful to all those in my life for providing feedback, pushing me when I needed to be
pushed, and just listening when I needed silent support.
At times I ignored many people close to me, despite their continued support. You know
who you are. I promise I will do my best to make up for it.
For those who wish to write, know that it can be a painful process for those around you.
They make just as many sacrifices as you do - if not more. Be mindful of this. Take the
time to be in the moment with those people, in any way that you can. You and your work
will benefit from this.
Thank you
First and foremost, I'd like to thank Fletcher and Jeremy, authors of the other Real
Python courses, for believing in me even when I did not believe in myself. They both are
talented developers and natural leaders; I'm also proud to call them friends. Thanks to all
my close friends and family (Mom, Dad, Jeff) for all your support and kindness. Derek,
Josh, Danielle, Richard, Lily, John (all three of you), Marcy, and Travis - each of you
helped in a very special way that I am only beginning to understand.
Thank you also to the immense support from the Python community. Despite not
knowing much about me or my abilities, you welcomed me, supported me, and shaped
me into a much better programmer. I only hope that I can give back as much as you
have given me.
Thanks to all who read through drafts, helping to shape this course into something
accurate, readable, and, most importantly, useful. Nina, you are a wonderful technical
writer and editor. Stay true to your passions.
612
Acknowledgements
For those who don't know, this course started as a Kickstarter. To all my original backers
and supporters: You have lead me as much as I hope I am now leading you. Keep
asking for more. This is your course.
Finally, thank you to a very popular yet terrible API that forced me to develop my own
solution to a problem, pushing me back into the world of software development.
Permanently.
Python is his tool of choice. He's founded and co-founded several startups and has
written extensively on his experiences.
He loves libraries and other mediums that provide publicly available data. When not
staring at a computer screen, he enjoys running, writing flash fiction, and making people
feel uncomfortable with his dance moves.
Massimo has a PhD in High Energy Theoretical Physics from the University of
Southampton (UK), and he has previously worked as an associate researcher for Fermi
National Accelerator Laboratory. Massimo is the author of a book on web2py, and more
than 50 publications in the fields of Physics and Computational Finance, and he has
contributed to many open source projects.
He started the web2py project in 2007, and is currently the lead developer.
Python 2 to 3
613
Acknowledgements
Thanks to Pete Jeffryes, in the final months of 2016 the entirety of this book was revised
to make use of the newest versions of all relied upon frameworks and dependencies
including, the shift from Python 2 to 3. Pete is a developer out of Denver, Colorado who
has always enjoyed the art of language learning from jazz and classical music to
Mandarin Chinese, and now the languages of software development. His was first
exposed to Python through an MIT OpenCourseWare offering in 2014 and he has been
hooked ever since.
614