My Adopt a Pet solution

Hello this is my code solution for the project, hope this use it or is helpfull for other people :grinning:

from flask import Flask 
from helper import pets

app = Flask(__name__)

@app.route('/')
def index():
  return f'''
  <h1>Adopt a Pet!</h1>
  <p>Browse through the links below to 
  find your new furry friend:</p>
  <ul>
    <li><a href='/animals/dogs'>Dogs</a></li>
    <li><a href='/animals/cats'>Cats</a></li>
    <li><a href='/animals/rabbits'>Rabbits</a></li>
  </ul>
  '''

@app.route('/animals/<pet_type>')
def animals(pet_type):
  pet_list_html = ""
  for idx, item in enumerate(pets[pet_type]):
    pet_list_html += f'''<li><a href = '/animals/{pet_type}/{idx}'>{item['name']}</a></li>'''
  html = f'''
    <h1>List of {pet_type}</h1>
    <ul>
      {pet_list_html}
    </ul>
  '''
  return html

@app.route('/animals/<pet_type>/<int:pet_id>')
def pet(pet_type, pet_id):
  pet = pets[pet_type][pet_id]
  html = f'''
    <h1>{pet["name"]}</h1>
    <img src="{pet["url"]}" />
    <p>{pet["description"]}</p>
    <ul>
      <li>{pet["breed"]}</li>
      <li>{pet["age"]}</li>
    </ul>
    <ul>'''
  return html
1 Like

Yes, Thank you. I understand now. :grinning:

1 Like

Your solution is great! And, mine is great, too! :rofl:

from helper import pets

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
  return """
  <h1>Adopt a Pet!</h1>
  <p>Browse through the links below to find your new furry friend:</p>
  <ul>
  <li><a href='/animals/dogs'>Dogs</a></li>
  <li><a href='/animals/cats'>Cats</a></li>
  <li><a href='/animals/rabbits'>Rabbits</a></li>
  </ul>
  """

@app.route('/animals/<pet_type>')
def amimals(pet_type):
  pet_type_title = pet_type.title()
  html = f'<h1>List of {pet_type_title}</h1>'

  pet_type_lower = pet_type.lower()

  for idx, item in enumerate(pets[pet_type_lower]):
    href = f'/animals/{pet_type_lower}/{idx}'
    name = item['name']
    html += f'<ul><li><a href={href}>{name}</a></li></ul>'
  return html

@app.route('/animals/<pet_type>/<int:pet_id>')
def pet(pet_type, pet_id):
  pet = pets[pet_type][pet_id]
  name = pet['name']
  url = pet['url']
  description = pet['description']
  breed = pet['breed']
  age = pet['age']
  return f"""
  <h1>{name}</h1>
  <img src='{url}'>
  <p>{description}</p>
  <ul>
  <li>Age: {age}</li>
  <li>Breed: {breed}</li>
  </ul>
  """

Just added a link back to homepage for practice

from flask import Flask from helper import pets app = Flask(__name__) @app.route('/') def index(): return ''' <h1>Adopt a Pet!</h1> <p>Browse through the links below to find your new furry friend:</p> <ul> <li><a href="/animals/dogs">Dogs</a></li> <li><a href="/animals/cats">Cats</a></li> <li><a href="/animals/rabbits">Rabbits</a></li> </ul> ''' @app.route('/animals/<pet_type>') def animals(pet_type): html = f"<h1>List of {pet_type}</h1>" html += "<ul>" pet_type_lower = pet_type.lower() for idx, pet in enumerate(pets[pet_type_lower]): href = f"/animals/{pet_type_lower}/{idx}" p_name = pet["name"] strng = f"<li><a href={href}>{p_name}</a></li>" html += strng html += "</ul>" return html @app.route('/animals/<pet_type>/<int:pet_id>') def pet(pet_type, pet_id): type_of_pet = pets[pet_type.lower()] #type_of_anmal pet = type_of_pet[pet_id] # sub dictonary pet_name = pet["name"] pet_des = pet["description"] pet_img_url = pet["url"] pet_age = pet["age"] pet_breed = pet["breed"] href_home = '/' return f""" <h1>{pet_name}</h1> <img src="{pet_img_url}"> <p>{pet_des}</p> <ul> <li>Age: {pet_age}</li> <li>Breed: {pet_breed}</li> </ul> <footer><a href={href_home}>Return to home page</a></footer> """