Editing Community Data Science Workshops (Core)/Day 2 Lecture

From CommunityData

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
{{Template:CDSW Header}}
[[Image:Place kitten-200x287.jpg|thumb|In which you learn how to use Python and web APIs to meet the likes of these guys!]]
 
[[Image:Place kitten-200x287.jpg|thumb|In which you learn how to use Python and web APIs to meet the likes of this guys!]]


Welcome to the Saturday lecture section of the Community Data Science Workshop Session 2! For about 140 minutes, we'll work through an introduction to web APIs using Python via both a lecture and hand-on exercises.
Welcome to the Saturday lecture section of the Community Data Science Workshop Session 2! For about 140 minutes, we'll work through an introduction to web APIs using Python via both a lecture and hand-on exercises.
'''Resources:'''
* Notebook that Tommy taught from in the morning lecutre ([https://raw.githubusercontent.com/CommunityDataScienceCollective/LectureNotes/master/Lecture_2_APIs.ipynb IPYNB file])
* Dharma's "following along" version of the notebook" ([https://communitydata.science/~mako/cdsw-wi2020-lecture2/Lecture_2_APIs_ded.html in HTML] and [https://communitydata.science/~mako/cdsw-wi2020-lecture2/Lecture_2_APIs_ded.ipynb IPYNB])
To download the IPYNB files, you need to right click and click "Save as" to download.


== Lecture Outline ==
== Lecture Outline ==
Line 18: Line 9:
* Today we'll learn how to find/create data sets
* Today we'll learn how to find/create data sets
* Next week we'll get into data science (asking and answering questions)
* Next week we'll get into data science (asking and answering questions)


;Outline:  
;Outline:  
Line 25: Line 17:
* How do we use one to fetch interesting datasets?
* How do we use one to fetch interesting datasets?
* How do we write programs that use the internet?  
* How do we write programs that use the internet?  
* How can we use the placekitten API to fetch kitten pictures?
* How can we use the placebear API to fetch bear pictures?
* Introduction to structured data (JSON)
* Introduction to structured data (JSON)
* How do we use APIs in general?
* How do we use APIs in general?


;What is a (web) API?
;What is a (web) API?
Line 33: Line 26:
* 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)


; How do we use an API to fetch datasets?
; How do we use an API to fetch datasets?
Line 47: Line 41:
* How do you understand (i.e. parse) the data?  
* How do you understand (i.e. parse) the data?  
** The requests module can do that using the <code>.json()</code> function!
** The requests module can do that using the <code>.json()</code> function!


; How do we write Python programs that make web requests?
; How do we write Python programs that make web requests?
Line 56: Line 51:
* the ability to save to files  
* the ability to save to files  
* the ability to understand (i.e., parse) JSON data that APIs usually give us
* the ability to understand (i.e., parse) JSON data that APIs usually give us


; Session 1 review
; Session 1 review
Line 67: Line 63:
** printing
** printing
** importing modules, so you can use code other people have written for you!
** importing modules, so you can use code other people have written for you!


; New programming concepts:
; New programming concepts:


* interpolate variables into a string using [https://docs.python.org/3/reference/lexical_analysis.html#f-strings f-strings]
* interpolate variables into a string using format
* using <code>input()</code>
 
 
* [http://docs.python-requests.org/en/latest/ requests]
* [http://docs.python-requests.org/en/latest/ requests]
* open files and write to them
* open files and write to them
* parsing a string (turning the string into a data structure we can manipulate) using the json module
* parsing a string (turning the string into a data structure we can manipulate) using the json module


; How do we use an API to fetch pictures of kittens?


[http://placekitten.com/ placekitten.com]
; How do we use an API to fetch pictures of bears?
* API that takes specially crafted URLs and gives appropriately sized picture of kittens
 
* Exploring placekitten in a browser:
[http://placebear.com/ placebear.com]
* API that takes specially crafted URLs and gives appropriately sized picture of bears
* Exploring placebear in a browser:
** visit the API documentation
** visit the API documentation
** kittens of different sizes
** bears of different sizes
** kittens in greyscale or color
** bears 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/2016/cdsw-spring/placekitten.py placekitten.py])
* Now we write a small program to grab an arbitrary square from placebear by asking for the size on standard in ([http://mako.cc/teaching/2015/cdsw-spring/placebear_input.py placebear_input.py])
 


; Introduction to structured data (JSON, JavaScriptObjectNotation)
; Introduction to structured data (JSON, JavaScriptObjectNotation)
Line 97: Line 93:
* Example file at http://mako.cc/cdsw.json
* Example file at http://mako.cc/cdsw.json
* download it and parse it (e.g., with a program like [http://mako.cc/teaching/2015/cdsw-spring/parse_cdswjson.py parse_cdswjson.py])
* download it and parse it (e.g., with a program like [http://mako.cc/teaching/2015/cdsw-spring/parse_cdswjson.py parse_cdswjson.py])


; Using other APIs
; Using other APIs
Line 116: Line 113:
* [http://mako.cc/teaching/2014/cdsw-autumn/lecture2-web_apis.odp Slides (ODP Libreoffice Slides Format)] — For editing and modification
* [http://mako.cc/teaching/2014/cdsw-autumn/lecture2-web_apis.odp Slides (ODP Libreoffice Slides Format)] — For editing and modification


== Videos from 2016 ==
* [https://communitydata.cc/~mako/cdsw-sp2016-lecture2-20160423.ogv Screencast/recording of the lecture from 2016] (1.1GB) — The file should be viewable in Firefox and many other browsers. If you have trouble playing it, you can download [https://www.videolan.org/vlc/index.html the VLC media player] which will be a able to play it on Windows, OSX, or GNU/Linux.
[[Category:Shared_Pages]]
[[Category:Shared_Pages]]
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see CommunityData:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:

Cancel Editing help (opens in new window)

Template used on this page: