diff --git a/converter.py b/converter.py old mode 100644 new mode 100755 index e3a7d30..97f4020 --- a/converter.py +++ b/converter.py @@ -1,3 +1,5 @@ +#!/usr/bin/env/python3 + import subprocess import os import time diff --git a/docs/day3.md b/docs/day3.md index fc3e629..bc07ec8 100644 --- a/docs/day3.md +++ b/docs/day3.md @@ -217,7 +217,136 @@ You saying the names of fruit out loud is funny but not very practical. Instead print("Not a valid emotion") ``` +8. Exit your terminal with `exit()` + ### Creating an API -You've got enough at this point to make a substantial API. Usually we'd write a python script at this point but we're going to build a Django API first this time. +You've got enough at this point to make a substantial API. Usually we'd write a python script at this point but we're going to build a Django API first this time. We'll use the people data we have above. +1. Open mysite/views.py +2. At the top of views.py add the following: + + ```python hl_lines="3 3" + import random + from django.shortcuts import render + from django.http import JsonResponse + ``` + +3. At the very bottom (below your weather function) add another function like so: + + ```python + def api(request): + people = [ + {"first name" : "Jim", "last name": "Fowler", "age": 24}, + {"first name" : "Bob", "last name": "Jones", "age": 36}, + {"first name" : "Alice", "last name": "Appleseed", "age": 52} + ] + + return JsonResponse({"data": people}) + ``` + + We're returning a "JsonResponse" this time. Remember how json is the current preferred way to send data over the web? Django has a formatter built in for it. + +4. let's add a url that points to our api endpoint. Open mysite/urls.py. +5. Add the following: + + ```python hl_lines="4 4" + urlpatterns = [ + path('', views.index), + path('weather/', views.weather), + path('people/', views.api), + ] + ``` + +6. Start your server with `python manage.py runserver` +7. Navigate to +8. You should see people data! +9. Stop your server with ++ctrl+c++ + +### Reading an API with python + +You have an API! Now let's read it with python. This is one of the most common applications for python in a work environment. + +1. First we need a pip package called `requests`. `requests` can make web requests on our behalf. Install it by typing the following in your terminal: + + ```bash + pip install requests + ``` + + ![install_requests](img/day3/install_requests.gif) + +2. Let's make sure that install worked. Open the python prompt by typing `python` +3. In your interpretor type: + + ```python + import requests + requests.get("https://google.com") + ``` + + You should see a ``. That means everything worked! + +4. Type `exit()` to exit. + +5. Create a new file called `api.py` in your my_website folder: + + ![api](img/day3/api.gif) + +6. Add the following to `api.py` + + ```python + import requests + + response = requests.get("http://localhost:8000") + print(response.text) + ``` + +7. Save with ++ctrl+s++ +8. Run your server by typing `python manage.py runserver` +9. Open a new terminal window by click the plus icon: + + ![open_terminal](img/day3/open_terminal.gif) + +10. Type `python api.py` and press ++enter++ + + ![index](img/day3/index.gif) + + We got our index page! Now let's get our API call: + +11. Modify api.py by changing our web call to + + ```python hl_lines="3 3" + import requests + + response = requests.get("http://localhost:8000/people") + print(response.text) + ``` + +12. Type `python api.py` and press ++enter++ + + ![people](img/day3/people.gif) + +13. Now we want to save our api response as python data. Let's add the following to `api.py` + + ```python hl_lines="2 5-8" + import requests + import json + + response = requests.get("http://localhost:8000/people") + people = response.json()["data"] + for person in people: + if person["age"] > 30: + print(f"{person['first name']} {person['last name']} is over 30.") + ``` + +14. Type `python api.py` and press ++enter++ + + ![read_api](img/day3/read_api.gif) + + You've just done one of the most important things in Python programming: used the results of an api call to do something! + +15. Click the terminal dropdown and switch back to the terminal running your web server: + + ![switch_term](img/day3/switch_term.gif) + +16. Stop your server with ++ctrl+c++ +17. Type `python api.py` and press ++enter++. Notice how you get an error that says "failed to establish new connection"? If requests can't connect to the web server you'll see this error. Remember it! \ No newline at end of file diff --git a/docs/img/day3/api.gif b/docs/img/day3/api.gif new file mode 100644 index 0000000..5dc777d Binary files /dev/null and b/docs/img/day3/api.gif differ diff --git a/docs/img/day3/index.gif b/docs/img/day3/index.gif new file mode 100644 index 0000000..578220f Binary files /dev/null and b/docs/img/day3/index.gif differ diff --git a/docs/img/day3/install_requests.gif b/docs/img/day3/install_requests.gif new file mode 100644 index 0000000..0cc3eb0 Binary files /dev/null and b/docs/img/day3/install_requests.gif differ diff --git a/docs/img/day3/open_terminal.gif b/docs/img/day3/open_terminal.gif new file mode 100644 index 0000000..10614b6 Binary files /dev/null and b/docs/img/day3/open_terminal.gif differ diff --git a/docs/img/day3/people.gif b/docs/img/day3/people.gif new file mode 100644 index 0000000..84d7076 Binary files /dev/null and b/docs/img/day3/people.gif differ diff --git a/docs/img/day3/read_api.gif b/docs/img/day3/read_api.gif new file mode 100644 index 0000000..bcfc643 Binary files /dev/null and b/docs/img/day3/read_api.gif differ diff --git a/docs/img/day3/switch_term.gif b/docs/img/day3/switch_term.gif new file mode 100644 index 0000000..539c043 Binary files /dev/null and b/docs/img/day3/switch_term.gif differ