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.
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 are collections of classes, functions, and constants designed to make completing a task easier.
Types of frameworks include:
To take care of the boring stuff.
Use a framework if you are making a cookie cutter application.
If a framework exists for what you're doing, consider using it.
Things to keep in mind when looking for a framework:
Good frameworks usually have:
- Good documentation
- Active developers
- A helpful community
Frameworks for building websites or APIs
There are two types of websites: Static and Dynamic.
@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
<!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
GET http://web.site/page.html HTTP/1.1
HTTP/1.1 200 OK
Content-Type: text/html
...
<!DOCTYPE HTML>
...
$ 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>
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()
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 %}