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