-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
43 lines (33 loc) · 1.24 KB
/
app.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
39
40
41
42
43
from flask import Flask, render_template, request
import requests
from dog_breeds import prettify_dog_breed
# Initialize the Flask application
app = Flask(__name__)
def check_breed(breed):
return "/".join(breed.split("-"))
@app.route("/", methods=["GET","POST"])
def dog_images_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
# Run the flask server
if __name__ == "__main__":
app.run()