CCockrum commited on
Commit
2c76545
·
verified ·
1 Parent(s): 85285ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -12
app.py CHANGED
@@ -284,15 +284,17 @@ def get_compatibility_message(pet):
284
  return messages
285
 
286
  # Function to display pet details page
287
- # The fix is in the display_pet_details function
288
- def display_pet_details(pet_id, context="search"):
 
 
289
  pet = get_pet_details(pet_id)
290
  if not pet:
291
  st.error("Unable to retrieve pet details. Please try again.")
292
  return
293
 
294
- # Back button
295
- if st.button("← Back to Search Results", key=f"back_{context}_{pet_id}"):
296
  st.session_state.selected_pet = None
297
  st.rerun() # Force immediate rerun
298
 
@@ -373,20 +375,88 @@ def display_pet_details(pet_id, context="search"):
373
  if pet['url']:
374
  st.markdown(f"[View on Petfinder]({pet['url']})")
375
 
376
- # Add to favorites
377
  is_favorite = pet['id'] in [p['id'] for p in st.session_state.favorites]
378
  if not is_favorite:
379
- if st.button("Add to Favorites", key=f"add_fav_{context}_{pet_id}"):
380
  st.session_state.favorites.append(pet)
381
  st.success(f"Added {pet['name']} to favorites!")
382
  st.rerun()
383
  else:
384
- if st.button("Remove from Favorites", key=f"rem_fav_{context}_{pet_id}"):
385
  st.session_state.favorites = [p for p in st.session_state.favorites if p['id'] != pet['id']]
386
  st.success(f"Removed {pet['name']} from favorites!")
387
  st.rerun()
388
 
389
- # Main app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390
  def main():
391
  # Title
392
  st.markdown("<h1 class='main-header'>🐾 PetMatch</h1>", unsafe_allow_html=True)
@@ -398,7 +468,7 @@ def main():
398
  with tab1:
399
  # If a pet is selected, show details
400
  if st.session_state.selected_pet:
401
- display_pet_details(st.session_state.selected_pet, context="search")
402
  else:
403
  # Search form
404
  with st.expander("Search Options", expanded=True):
@@ -493,7 +563,7 @@ def main():
493
 
494
  for pet in results[start_idx:end_idx]:
495
  st.markdown("---")
496
- display_pet_card(pet)
497
 
498
  with tab2:
499
  st.markdown("### Your Favorite Pets")
@@ -503,11 +573,11 @@ def main():
503
  else:
504
  # Check if a pet is selected from favorites
505
  if st.session_state.selected_pet:
506
- display_pet_details(st.session_state.selected_pet, context="search")
507
  else:
508
  for pet in st.session_state.favorites:
509
  st.markdown("---")
510
- display_pet_card(pet, is_favorite=True, context="favorites")
511
 
512
  with tab3:
513
  st.markdown("### About PetMatch")
 
284
  return messages
285
 
286
  # Function to display pet details page
287
+ # Changes to make keys unique across different tabs
288
+
289
+ # Function to display pet details page with unique tab identifier
290
+ def display_pet_details(pet_id, context="search", tab_id="tab1"):
291
  pet = get_pet_details(pet_id)
292
  if not pet:
293
  st.error("Unable to retrieve pet details. Please try again.")
294
  return
295
 
296
+ # Back button with unique key that includes tab identifier
297
+ if st.button("← Back to Search Results", key=f"back_{tab_id}_{context}_{pet_id}"):
298
  st.session_state.selected_pet = None
299
  st.rerun() # Force immediate rerun
300
 
 
375
  if pet['url']:
376
  st.markdown(f"[View on Petfinder]({pet['url']})")
377
 
378
+ # Add to favorites with unique key
379
  is_favorite = pet['id'] in [p['id'] for p in st.session_state.favorites]
380
  if not is_favorite:
381
+ if st.button("Add to Favorites", key=f"add_fav_{tab_id}_{context}_{pet_id}"):
382
  st.session_state.favorites.append(pet)
383
  st.success(f"Added {pet['name']} to favorites!")
384
  st.rerun()
385
  else:
386
+ if st.button("Remove from Favorites", key=f"rem_fav_{tab_id}_{context}_{pet_id}"):
387
  st.session_state.favorites = [p for p in st.session_state.favorites if p['id'] != pet['id']]
388
  st.success(f"Removed {pet['name']} from favorites!")
389
  st.rerun()
390
 
391
+ # Function to format pet card with unique tab identifier
392
+ def display_pet_card(pet, is_favorite=False, context="search", tab_id="tab1"):
393
+ col1, col2 = st.columns([1, 2])
394
+
395
+ with col1:
396
+ if pet['photos'] and len(pet['photos']) > 0:
397
+ st.image(pet['photos'][0]['medium'], use_container_width=True)
398
+ else:
399
+ st.image("https://via.placeholder.com/300x300?text=No+Image", use_container_width=True)
400
+
401
+ with col2:
402
+ st.markdown(f"<div class='pet-name'>{pet['name']}</div>", unsafe_allow_html=True)
403
+
404
+ # Tags
405
+ tags_html = ""
406
+ if pet['status'] == 'adoptable':
407
+ tags_html += "<span class='tag' style='background-color: #808080;'>Adoptable</span> "
408
+ else:
409
+ tags_html += f"<span class='tag' style='background-color: #ffcdd2;'>{pet['status'].title()}</span> "
410
+
411
+ if pet['age']:
412
+ tags_html += f"<span class='tag'>{pet['age']}</span> "
413
+ if pet['gender']:
414
+ tags_html += f"<span class='tag'>{pet['gender']}</span> "
415
+ if pet['size']:
416
+ tags_html += f"<span class='tag'>{pet['size']}</span> "
417
+
418
+ st.markdown(f"<div>{tags_html}</div>", unsafe_allow_html=True)
419
+
420
+ st.markdown("<div class='pet-details'>", unsafe_allow_html=True)
421
+ if pet['breeds']['primary']:
422
+ breed_text = pet['breeds']['primary']
423
+ if pet['breeds']['secondary']:
424
+ breed_text += f" & {pet['breeds']['secondary']}"
425
+ if pet['breeds']['mixed']:
426
+ breed_text += " (Mixed)"
427
+ st.markdown(f"<strong>Breed:</strong> {breed_text}", unsafe_allow_html=True)
428
+
429
+ if pet['colors']['primary'] or pet['colors']['secondary'] or pet['colors']['tertiary']:
430
+ colors = [c for c in [pet['colors']['primary'], pet['colors']['secondary'], pet['colors']['tertiary']] if c]
431
+ st.markdown(f"<strong>Colors:</strong> {', '.join(colors)}", unsafe_allow_html=True)
432
+
433
+ if 'location' in pet and pet['contact']['address']['city'] and pet['contact']['address']['state']:
434
+ st.markdown(f"<strong>Location:</strong> {pet['contact']['address']['city']}, {pet['contact']['address']['state']}", unsafe_allow_html=True)
435
+
436
+ st.markdown("</div>", unsafe_allow_html=True)
437
+
438
+ if pet['description']:
439
+ st.markdown(f"<div class='pet-description'>{pet['description'][:300]}{'...' if len(pet['description']) > 300 else ''}</div>", unsafe_allow_html=True)
440
+
441
+ col1, col2 = st.columns(2)
442
+ with col1:
443
+ if st.button("View Details", key=f"details_{tab_id}_{context}_{pet['id']}"):
444
+ st.session_state.selected_pet = pet['id']
445
+ st.rerun()
446
+ with col2:
447
+ if not is_favorite:
448
+ if st.button("Add to Favorites", key=f"fav_{tab_id}_{context}_{pet['id']}"):
449
+ if pet['id'] not in [p['id'] for p in st.session_state.favorites]:
450
+ st.session_state.favorites.append(pet)
451
+ st.success(f"Added {pet['name']} to favorites!")
452
+ st.rerun()
453
+ else:
454
+ if st.button("Remove from Favorites", key=f"unfav_{tab_id}_{context}_{pet['id']}"):
455
+ st.session_state.favorites = [p for p in st.session_state.favorites if p['id'] != pet['id']]
456
+ st.success(f"Removed {pet['name']} from favorites!")
457
+ st.rerun()
458
+
459
+ # Main app with updated function calls
460
  def main():
461
  # Title
462
  st.markdown("<h1 class='main-header'>🐾 PetMatch</h1>", unsafe_allow_html=True)
 
468
  with tab1:
469
  # If a pet is selected, show details
470
  if st.session_state.selected_pet:
471
+ display_pet_details(st.session_state.selected_pet, context="search", tab_id="tab1")
472
  else:
473
  # Search form
474
  with st.expander("Search Options", expanded=True):
 
563
 
564
  for pet in results[start_idx:end_idx]:
565
  st.markdown("---")
566
+ display_pet_card(pet, tab_id="tab1")
567
 
568
  with tab2:
569
  st.markdown("### Your Favorite Pets")
 
573
  else:
574
  # Check if a pet is selected from favorites
575
  if st.session_state.selected_pet:
576
+ display_pet_details(st.session_state.selected_pet, context="favorites", tab_id="tab2")
577
  else:
578
  for pet in st.session_state.favorites:
579
  st.markdown("---")
580
+ display_pet_card(pet, is_favorite=True, context="favorites", tab_id="tab2")
581
 
582
  with tab3:
583
  st.markdown("### About PetMatch")