finish day 3
2
converter.py
Normal file → Executable file
@@ -1,3 +1,5 @@
|
||||
#!/usr/bin/env/python3
|
||||
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
|
||||
131
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 <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
|
||||
```
|
||||
|
||||

|
||||
|
||||
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:
|
||||
|
||||

|
||||
|
||||
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:
|
||||
|
||||

|
||||
|
||||
10. Type `python api.py` and press ++enter++
|
||||
|
||||

|
||||
|
||||
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++
|
||||
|
||||

|
||||
|
||||
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++
|
||||
|
||||

|
||||
|
||||
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:
|
||||
|
||||

|
||||
|
||||
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!
|
||||
BIN
docs/img/day3/api.gif
Normal file
|
After Width: | Height: | Size: 915 KiB |
BIN
docs/img/day3/index.gif
Normal file
|
After Width: | Height: | Size: 625 KiB |
BIN
docs/img/day3/install_requests.gif
Normal file
|
After Width: | Height: | Size: 258 KiB |
BIN
docs/img/day3/open_terminal.gif
Normal file
|
After Width: | Height: | Size: 770 KiB |
BIN
docs/img/day3/people.gif
Normal file
|
After Width: | Height: | Size: 591 KiB |
BIN
docs/img/day3/read_api.gif
Normal file
|
After Width: | Height: | Size: 738 KiB |
BIN
docs/img/day3/switch_term.gif
Normal file
|
After Width: | Height: | Size: 1.0 MiB |