finish day 3

This commit is contained in:
ducoterra
2020-11-22 19:31:52 -05:00
parent 2d90f4aa31
commit 715b83f929
9 changed files with 132 additions and 1 deletions
Regular → Executable
+2
View File
@@ -1,3 +1,5 @@
#!/usr/bin/env/python3
import subprocess
import os
import time
+130 -1
View File
@@ -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 <http://localhost:8000/people>
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 `<Response [200]>`. 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 <http://localhost:8000/people>
```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!
Binary file not shown.

After

Width:  |  Height:  |  Size: 915 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 625 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 258 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 770 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 591 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 738 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB