Lesson 10: Frameworks

Homepage Content Slides Video

Warning

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

Overview

Frameworks

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

Ruby
Rails
Arguably the most popular web-framework out there. Similar to Django in it's features out of the box.
Sinatra
Analogous to Flask on the Python side, very simple and easy to start with, encourages building up the features you need.
Node.js
ExpressJS
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():
        database.remove_account(account_name)
        return 'Success', 200
    else
        return 'Failure', 401

Templating Engines (mad-libs!)

<!DOCTYPE HTML>
<html>

    <head>
        <title>Template Example</title>
    </head>

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

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

Templating Engines (mad-libs!)

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

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

HTTP

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

HTTP/1.1 200 OK
Content-Type: text/html
...
<!DOCTYPE HTML>
...

HTTP Methods

REST

TODO: Dynamic Website

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

Go to

http://dobc-shell.osuosl.org:<http-port>

Part One: Writing The Views

app/views/add_guest.py
app/views/view_guests.py

Adding a guest to the database:

guest = Guest(name, message)
db.session.add(guest)
db.session.commit()

Getting a list of guests from the database:

guests = Guest.query.all()

Part Two: Writing The Templates

app/templates/add_guest.html
app/templates/view_guests.html

Using a form inside a template:

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

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.