PageRenderTime 27ms CodeModel.GetById 24ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/posts/2015-06-26-learn-to-use-apis.html

https://gitlab.com/stephenjbarr/stephen-planetbarr-com
HTML | 93 lines | 77 code | 16 blank | 0 comment | 0 complexity | 14de50759ae6d724d8b1d7be7c443c8e MD5 | raw file
 1
 2<div id="outline-container-sec-1" class="outline-2">
 3<h2 id="sec-1"><span class="section-number-2">1</span> APIs</h2>
 4<div class="outline-text-2" id="text-1">
 5<p>
 6If you are an academic, it is easy to put yourself live in a world all
 7data comes as nice clean <code>.csv</code> files, generated from curated,
 8expensive databases.
 9</p>
10
11<p>
12My strong piece of advice is <b>learn to use APIs!</b> That is where the
13fun data live, and this is how you can connect data and ideas from
14everywhere and do new things with them.
15</p>
16</div>
17</div>
18
19<div id="outline-container-sec-2" class="outline-2">
20<h2 id="sec-2"><span class="section-number-2">2</span> Example using Python</h2>
21<div class="outline-text-2" id="text-2">
22<p>
23As a quick example, I present an some extremely simple code to call
24Duck Duck Go's API.
25</p>
26
27<div class="org-src-container">
28
29<pre class="src src-sh">import requests
30import json
31from IPython.display import Image
32
33payload =  {<span style="color: #8b2252;">"q"</span> : <span style="color: #8b2252;">"BBQ"</span>,
34           <span style="color: #8b2252;">"format"</span> : <span style="color: #8b2252;">"json"</span>,
35           <span style="color: #8b2252;">"pretty"</span> : 1}
36
37r = requests.get(<span style="color: #8b2252;">'http://api.duckduckgo.com/'</span>, params = payload)
38r.url
39rj = r.json()
40<span style="color: #0000ff;">Image</span>(<span style="color: #a0522d;">url</span>=rj[<span style="color: #8b2252;">'Image'</span>])
41</pre>
42</div>
43
44
45<p>
46You can also view the code in <a href="http://nbviewer.ipython.org/gist/stephenjbarr/234b609ca63575c55196">this iPython notebook</a>.
47</p>
48
49<p>
50The code has the following structure:
51</p>
52
53<ol class="org-ol">
54<li>Lines 1-3 load modules useful for making the request, and displaying the image.
55</li>
56<li><code>payload</code> is a JSON structure encapsulating the what we want to ask.
57</li>
58<li>The <code>requests.get</code> command reaches out to Duck Duck Go's API endpoint, hands it the payload, and gets the response.
59</li>
60<li><code>r.url</code> shows how the payload was encoded.
61</li>
62<li><code>r.json()</code> takes the answer and transformsit into a JSON structure.
63</li>
64<li>The <code>Image</code> command displays within an iPython notebook an image given a URL.
65</li>
66</ol>
67
68<p>
69I highly suggest going to the notebook and try modifying the <code>q</code> object of the payload.
70In fact, download the notebook, <a href="http://ipython.org/install.html">install iPython</a>, and play with it yourself.
71</p>
72</div>
73</div>
74
75<div id="outline-container-sec-3" class="outline-2">
76<h2 id="sec-3"><span class="section-number-2">3</span> Reflect</h2>
77<div class="outline-text-2" id="text-3">
78<p>
79In a few simple lines of code, we have transformed a text string into an image.
80In Haskell / ML notation, we have a function of type:
81</p>
82
83<div class="org-src-container">
84
85<pre class="src src-haskell"><span style="color: #0000ff;">get_image</span> <span style="color: #a0522d;">::</span> <span style="color: #228b22;">String</span> <span style="color: #a0522d;">-&gt;</span> <span style="color: #228b22;">Picture</span>
86</pre>
87</div>
88
89<p>
90This was done with almost no work on our part!
91</p>
92</div>
93</div>