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.

Frameworks

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

Types of frameworks include:

  • Web frameworks
  • Game frameworks
  • GUI frameworks

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:

  • Good frameworks usually have:

    • Good documentation
    • Active developers
    • A helpful community

Web Frameworks

The Flask logo

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)

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!
...
<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

  • Servers are stateless
  • Resources are self-contained
  • HTTP methods have predictable side-effects

TODO: Dynamic Website

Part One: Writing The Views

Part Two: Writing The Templates

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.