-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
38 lines (30 loc) · 1.18 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
from dog_breeds import prettify_dog_breed
app = Flask("app")
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)