Lesson 10: Frameworks

Homepage Content Slides Video


This lesson is under construction. Learn from it at your own risk. If you have any feedback, please fill out our General Feedback Survey.



Frameworks are collections of classes, functions, and constants designed to make completing a task easier.

Types of frameworks include:

The job of a framework

To take care of the boring stuff.

Why and When to use a Framework

Use a framework if you are making a cookie cutter application.

If a framework exists for what you're doing, consider using it.

Looking for Frameworks

Things to keep in mind when looking for a framework:

Web Frameworks

The Flask logo

Frameworks for building websites or APIs

Static vs Dynamic Sites

There are two types of websites: Static and Dynamic.

Static Site
Rarely changes, looks the same for all visitors (Blog, News, Document)
Dynamic Site
Changes based on who you are and what you do. (Search Engine, Login)

Popular Web Frameworks

Arguably the most popular web-framework out there. Similar to Django in it's features out of the box.
Analogous to Flask on the Python side, very simple and easy to start with, encourages building up the features you need.
A bare-bones NodeJS application, similar again to Flask.

The Model-View-Controller Pattern

model view controller diagram

URL Routing

@app.route('/accounts/<account_name>', methods=['DELETE'])
def delete_account(account_name):
    if authenticated() and authorized():
        return 'Success', 200
        return 'Failure', 401

Templating Engines (mad-libs!)


        <title>Template Example</title>

        <p>Your lucky number today is {{ number }}!</p>

render_template("template.html", number=random.randint(0, 99))
Your lucky number today is 42!

Templating Engines (mad-libs!)

{% for message in messages %}
    <p>{{ message }}</p>
{% endfor %}
messages = ["Welcome!", "Test Message", "Vim > Emacs"]

render_template("template2.html", messages=messages)
Test Message
Vim > Emacs


GET http://web.site/page.html HTTP/1.1

HTTP/1.1 200 OK
Content-Type: text/html

HTTP Methods


TODO: Dynamic Website

$ git clone https://github.com/DevOpsBootcamp/Bootcamp-Exercises
$ cd Bootcamp-Exercises/2016-2017/frameworks
$ virtualenv venv
$ source venv/bin/activate
(venv)$ pip install --upgrade pip
(venv)$ pip install -r requirements.txt
(venv)$ python run.py
 * Running on (Press CTRL+C to quit)

Go to


Part One: Writing The Views


Adding a guest to the database:

guest = Guest(name, message)

Getting a list of guests from the database:

guests = Guest.query.all()

Part Two: Writing The Templates


Using a form inside a template:

<form method="POST">
    {{ form.csrf_token }}
    {# Put form fields here #}
    <p><input type="submit" value="Submit"></p>

Jinja2 for loop:

{% for item in list %}
    {# do thing with item #}
{% endfor %}

Further Reading

The Flask Microframework
Flask is a web framework that is simple enough for beginners to use but configurable enough to allow more advanced users to have full control over their application. It has a very active community and fantastic documentation.
Intro to HTTP and REST
HTTP is the protocol that web clients and web servers use to communicate with each other, and REST is a set of web design guidelines that is takes advantage of HTTP's features and allows different applications to easily communicate with each other.