DS4UX (Spring 2016)/Day 5 lecture: Difference between revisions

From CommunityData
No edit summary
Line 1: Line 1:
<div style="font-family:Rockwell,'Courier Bold',Courier,Georgia,'Times New Roman',Times,serif; min-width:10em;">
<div style="float:left; width:100%; margin-right:2%;">
{{Link/Graphic/Main/2
|highlight color= 27666b
|color=460c40
|link=
|image=
|text-align=left
|top font-size= 1.1em
|top color=FFF
|line color=FFF
|top text=This page is a work in progress.
|bottom font-size= 1em
|bottom color= FFF
|bottom text=
|line= none
}}</div></div>
<div style="clear:both;"></div>
[[File:Highfivekitten.jpeg|200px|thumb|In which you learn how to use Python and web APIs to meet the likes of her!]]
[[File:Highfivekitten.jpeg|200px|thumb|In which you learn how to use Python and web APIs to meet the likes of her!]]


Line 46: Line 24:


== Lecture 1: What is an API? ==
== Lecture 1: What is an API? ==
''RAY: the content below is just for your reference. You can delete it or replace it as you see fit! [[User:Jtmorgan|Jtmorgan]] ([[User talk:Jtmorgan|talk]]) 16:57, 21 April 2016 (EDT)''


* API: a structured way for programs to talk to each other (aka an interface for programs)
* API: a structured way for programs to talk to each other (aka an interface for programs)
* Web APIs: like a website your programs can visit (you:a website::your program:a web API)
* Web APIs: like a website your programs can visit (you:a website::your program:a web API)




Line 76: Line 53:
** kittens of different sizes
** kittens of different sizes
** kittens in greyscale or color
** kittens in greyscale or color
* Now we write a small program to grab an arbitrary square from placekitten by asking for the size on standard in: [http://mako.cc/teaching/2014/cdsw-autumn/placekitten_raw_input.py placekitten_raw_input.py]
* Now we write a small program to grab an arbitrary square from placekitten by asking for the size on standard in.






== Lecture 2: Using data from APIs in Python ==
== Lecture 2: Using data from APIs in Python ==
''RAY: the content below is just for your reference. You can delete it or replace it as you see fit! [[User:Jtmorgan|Jtmorgan]] ([[User talk:Jtmorgan|talk]]) 16:57, 21 April 2016 (EDT)''
 


=== How do we write Python programs that make web requests? ===
=== How do we write Python programs that make web requests? ===
Line 117: Line 94:
* Brief intro to Wikipedia
* Brief intro to Wikipedia
* Overview of what you can get from the Wikipedia API
* Overview of what you can get from the Wikipedia API
* Harry Potter examples
* Example queries in the sandbox
* Introduce coding challenges
* Introduce [[DS4UX_(Spring_2016)/Day_5_coding_challenge|week 5 coding challenges]]





Revision as of 04:48, 25 April 2016

In which you learn how to use Python and web APIs to meet the likes of her!

Introduction and context

You can manipulate data in Python now. Congratulations! Today we'll learn how to put your new skills to good use. You'll learn how to gather data from the internet, using APIs (Application Programming Interfaces) and JSON ("JavaScript Object Notation"). You can manipulate this data in Python to answer questions (just like you did with the BabyNames, Wordplay, and Seattle Traffic datasets), and then export your findings to a file for later use or additional analysis.


Outline

Lecture 1 (Ray)
  • What is an API?
  • How do we use one to fetch interesting datasets?
  • How do we write programs that use the internet?
API exercise 1 (Jonathan)
  • How can we use the placekitten API to fetch kitten pictures?
Lecture 2 (Ray)
  • Introduction to structured data (JSON)
  • JSON and Python
API exercise 2 (Jonathan)
  • How do we build API queries to gather data about Wikipedia articles and editors?

Lecture 1: What is an API?

  • API: a structured way for programs to talk to each other (aka an interface for programs)
  • Web APIs: like a website your programs can visit (you:a website::your program:a web API)


How do we use an API to fetch datasets?

Basic idea: your program sends a request, the API sends data back

  • Where do you direct your request? The site's API endpoint.
  • How do I write my request? Put together a URL; it will be different for different web APIs.
    • Check the documentation, look for code samples
  • How do you send a request?
    • Python has modules you can use, like requests (they make HTTP requests)
  • What do you get back?
    • Structured data (usually in the JSON format)
  • How do you understand (i.e. parse) the data?
    • There's a module for that! (introduce requests)


Exercise 1: How do we use APIs to get kitten pictures?

placekitten.com

  • API that takes specially crafted URLs and gives appropriately sized picture of kittens
  • Exploring placekitten in a browser:
    • visit the API documentation
    • kittens of different sizes
    • kittens in greyscale or color
  • Now we write a small program to grab an arbitrary square from placekitten by asking for the size on standard in.


Lecture 2: Using data from APIs in Python

How do we write Python programs that make web requests?

To use APIs to build a dataset we will need:

  • all our tools from last session: variables, etc
  • the ability to open urls on the web
  • the ability to create custom URLS
  • the ability to save to files
  • the ability to understand (i.e., parse) JSON data that APIs usually give us

Introduction to structured data (JSON, JavaScriptObjectNotation)

  • what is json: useful for more structured data
  • import json; json.loads()
  • like Python (except no single quotes)
  • simple lists, dictionaries
  • can reflect more complicated data structures
  • Example file at http://mako.cc/cdsw.json
  • You can parse data directly with .json() on a requests call

Using other APIs

  • every API is different, so read the documentation!
  • If the documentation isn't helpful, search online
  • for popular APIs, there are python modules that help you make requests and parse json

Possible issues:

  • rate limiting
  • authentication
  • text encoding issues


Exercise 2: How do we use APIs to get data from Wikipedia?

  • Brief intro to Wikipedia
  • Overview of what you can get from the Wikipedia API
  • Example queries in the sandbox
  • Introduce week 5 coding challenges