Book Image

Flask By Example

By : Gareth Dwyer
Book Image

Flask By Example

By: Gareth Dwyer

Overview of this book

This book will take you on a journey from learning about web development using Flask to building fully functional web applications. In the first major project, we develop a dynamic Headlines application that displays the latest news headlines along with up-to-date currency and weather information. In project two, we build a Crime Map application that is backed by a MySQL database, allowing users to submit information on and the location of crimes in order to plot danger zones and other crime trends within an area. In the final project, we combine Flask with more modern technologies, such as Twitter's Bootstrap and the NoSQL database MongoDB, to create a Waiter Caller application that allows restaurant patrons to easily call a waiter to their table. This pragmatic tutorial will keep you engaged as you learn the crux of Flask by working on challenging real-world applications.
Table of Contents (20 chapters)
Flask By Example
Credits
About the Author
Acknowledgements
About the Reviewers
www.PacktPub.com
Preface
Index

Introducing PyMongo


PyMongo is a library that implements drivers for MongoDB and will allow us to execute commands on our database from our application code. As usual, install it through pip using the following command (note that, similarly to MongoDB, you only need to install this library on the server):

pip install --user pymongo

Now, we can import this library into our application and build our real DBHelper class, implementing all the methods we used in our MockDBHelper class.

Writing the DBHelper class

The last class that we need is the DBHelper class, which will contain all the functions that are required for our application code to talk to our database. This class will use the pymongo library we just installed in order to run MongoDB commands. Create a file named dbhelper.py in the waiter directory and add the following code:

import pymongo

DATABASE = "waitercaller"


class DBHelper:

  def __init__(self):
    client = pymongo.MongoClient()
    self.db = client[DATABASE]

This code imports...