Hope your spring break was magical. Here's the things you should do before class on the 17th.
- Downloading the data you need for your project.
- Updating your app.py you need to get the data into your templates.
- Adding some code to your templates so that you can browse through the data.
Go to PythonAnywhere and get a Bash shell. If you're using the Guantanamo detainees data set, in your shell, type:
curl -o data.csv "https://dl.dropboxusercontent.com/u/145539/detainees.csv"If you're using the Wisconsin hunting accidents data set, in your shell, type:
curl -o data.csv "https://dl.dropboxusercontent.com/u/145539/wisconsin_hunting_accidents.csv"This will take a moment to complete, and will put a file called data.csv in your home directory. You should also download the data files to your computer so that you can browse through them.
- A
.csvfile is a "comma separated values" file -- a really primitive version of an Excel spreadsheet. - In a
.csvfile, the first row are the headers and each row after are the data rows. - Each row consists of text "cells" separated by commas. Here's an example:
first_name,last_name,arrest_date,in_custody
"Jeremy","Bowers",2016-03-01,false
"Becky","Bowers",2016-03-02,true
- There are other file types that are similar;
.txtare unstructured text files,.tsvare "tab separated values" spreadsheets where the individual cells are separated by tabs and not commas. - Make sure you see a file
data.csvin your home directory. Do:
ls -lAnd make sure you see the file in the list, like I do:
total 0
-rw-r--r-- 1 jbowers staff 0 Mar 14 10:29 app.py
-rw-r--r-- 1 jbowers staff 0 Mar 14 10:29 data.csv
drwxr-xr-x 2 jbowers staff 68 Mar 14 10:29 templatesNow that you have your data file, we need to update your app.py to read the data. We can do this by telling Python to open your data.csv when a request hits your index route, /.
Here's what your index route should look like now:
@app.route('/')
def index():
import csv
with open('data.csv', 'r') as readfile:
rows = list(csv.DictReader(readfile))
return render_template('index.html', rows=rows)Your whole app.py file should look like this:
#!/usr/bin/env python
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
import csv
with open('data.csv', 'r') as readfile:
rows = list(csv.DictReader(readfile))
return render_template('index.html', rows=rows)
if __name__ == "__main__":
app.run()If your app.py doesn't look like mine, above, paste my code over the top of yours -- or edit yours to match mine if you're working ahead.
Okay, now that we have an updated app.py that can read the lines from your data.csv file, we need to edit our templates to match. In your templates folder, you should have a file called index.html.
Make yours look like mine:
<h1>Detainees / Hunting accident sufferers</h1>
<ul>
{% for row in rows %}
<li>{{ row }}</li>
{% endfor %}
</ul>Here's what we're doing: Your app.py will read the data.csv file where your rows of data live. If you have Guantanamo detainees, those rows will have attributes like name, country_of_origin and status. If you have hunting accidents, you'll see things like firearm, wound and cause.
Each row in the file represents one detainee or one hunting accident. These attributes help describe that incident or detainee. You can access these rows by looping over the variable rows in your template and assigning a variable named row to hold the current iteration of the loop.
If you want to print all of the names of the Guantanamo detainees, you'd do something like this:
<ul>
{% for row in rows %}
<li>{{ row.name }}</li>
{% endfor %}
</ul>If you wanted to print the type of firearm used in each of the Wisconsin hunting accidents, you'd do this:
<ul>
{% for row in rows %}
<li>{{ row.firearm }}</li>
{% endfor %}
</ul>Whatever the column is named, you can access it like row.COLUMN_NAME. So if you have a column date you'd use {{ row.date }}, or if you have a column died_in_custody, you'd use {{ row.died_in_custody }}.
Starting to make sense?
Take a look at your data's columns and see what you'd like to build. When you come to class on the 17th, you should have an idea in mind of what you