-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
38 lines (32 loc) · 1.38 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from flask import Flask, render_template, request
import requests
#imports a dictionary of data from dog_breeds.py and "prettifies", or styles, the dog names when they appear in the HTML page
from dog_breeds import prettify_dog_breed
app = Flask("app")
#function adds a dash in the URL between breed names with multiple words like miniature poodle
def check_breed(breed):
return "/".join(breed.split("-"))
@app.route("/", methods=["GET","POST"])
def dog_image_gallery():
errors = []
if request.method == "POST":
breed = request.form.get("breed")
number = request.form.get("number")
if not breed:
errors.append("Oops! Please choose a breed.")
if not number:
errors.append("Oops! Please choose a number.")
if breed and number:
response = requests.get("https://dog.ceo/api/breed/" + check_breed(breed) + "/images/random/" + number)
data = response.json()
dog_images = data["message"]
return render_template("dogs.html", images=dog_images, breed=prettify_dog_breed(breed), errors=[])
return render_template("dogs.html", images=[], breed="", errors=errors)
@app.route("/random", methods=["POST"])
def get_random():
response = requests.get("https://dog.ceo/api/breeds/image/random")
data = response.json()
dog_images = [data["message"]]
return render_template("dogs.html", images=dog_images)
app.debug = True
app.run(host='0.0.0.0', port=8080)