Skip to content

Instantly share code, notes, and snippets.

@jeremyjbowers
Last active March 15, 2016 22:27
Show Gist options
  • Select an option

  • Save jeremyjbowers/b0ef0438ed155092017e to your computer and use it in GitHub Desktop.

Select an option

Save jeremyjbowers/b0ef0438ed155092017e to your computer and use it in GitHub Desktop.
Class Notes for the week preceding March 17th

Introduction

Hope your spring break was magical. Here's the things you should do before class on the 17th.

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

Side note: What is a CSV?

  • A .csv file is a "comma separated values" file -- a really primitive version of an Excel spreadsheet.
  • In a .csv file, 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; .txt are unstructured text files, .tsv are "tab separated values" spreadsheets where the individual cells are separated by tabs and not commas.
  • Make sure you see a file data.csv in your home directory. Do:
ls -l

And 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 templates

Updating your app

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

Updating your templates

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment