yash-b18 commited on
Commit
3a73daa
·
1 Parent(s): 07745b1

Added some basic api callouts to retrieve images dynamically

Browse files
Files changed (2) hide show
  1. main.py +39 -19
  2. met_api.py +35 -0
main.py CHANGED
@@ -1,4 +1,16 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  # Page setup
4
  st.set_page_config(page_title="The METrics", layout="centered")
@@ -33,31 +45,39 @@ st.markdown(
33
  unsafe_allow_html=True
34
  )
35
 
36
- # Columns with fixed-height images
37
- col1, col2, col3 = st.columns(3)
 
 
 
 
 
38
 
39
- with col1:
40
- st.markdown(
41
- "<div class='img-box'><img src='https://images.metmuseum.org/CRDImages/ep/web-large/DP-29324-001.jpg'></div>",
42
- unsafe_allow_html=True
43
- )
44
- with col2:
45
- st.markdown(
46
- "<div class='img-box'><img src='https://images.metmuseum.org/CRDImages/ad/web-large/DP124705.jpg'></div>",
47
- unsafe_allow_html=True
48
- )
49
- with col3:
50
- st.markdown(
51
- "<div class='img-box'><img src='https://images.metmuseum.org/CRDImages/gr/web-large/DP21847edited.jpg'></div>",
52
- unsafe_allow_html=True
53
- )
 
 
 
54
 
55
  st.write("")
56
  st.write("")
57
  # Search bar
58
  st.markdown("""
59
  <div style='display: flex; justify-content: center;'>
60
- <input type="text" placeholder=" 🔎 Search Met's Art Collection...."
61
  style="padding: 10px; width: 250px; border-radius: 20px; border: 1px solid #ccc;">
62
  </div>
63
  """, unsafe_allow_html=True)
@@ -67,7 +87,7 @@ st.write("")
67
  # Footer
68
  st.markdown("""
69
  <div style='position: fixed; bottom: 10px; left: 0; right: 0; text-align: center; font-size: 12px;'>
70
- <span style="font-size: 14px;">ℹ️ Met API and data related information is available at –
71
  <a href="https://metmuseum.github.io/" target="_blank">https://metmuseum.github.io/</a></span>
72
  </div>
73
  """, unsafe_allow_html=True)
 
1
  import streamlit as st
2
+ from met_api import get_objectsWithImages, get_images
3
+
4
+ # Caching Section
5
+ @st.cache_data
6
+ def cache_objectsWithImages():
7
+ return get_objectsWithImages()
8
+
9
+ @st.cache_data
10
+ def cache_images(total, objectIDs, limit):
11
+ return get_images(total, objectIDs, limit)
12
+
13
+
14
 
15
  # Page setup
16
  st.set_page_config(page_title="The METrics", layout="centered")
 
45
  unsafe_allow_html=True
46
  )
47
 
48
+ total, ids = get_objectsWithImages()
49
+ images = get_images(total, ids, limit=3)
50
+
51
+
52
+ if images:
53
+ # Columns with fixed-height images
54
+ col1, col2, col3 = st.columns(3)
55
 
56
+ with col1:
57
+ st.markdown(
58
+ f"<div class='img-box'><img src='{images[0][0]}'></div>",
59
+ unsafe_allow_html=True
60
+ )
61
+ st.markdown(f"<div>{images[0][1]}</div>", unsafe_allow_html=True)
62
+ with col2:
63
+ st.markdown(
64
+ f"<div class='img-box'><img src='{images[1][0]}'></div>",
65
+ unsafe_allow_html=True
66
+ )
67
+ st.markdown(f"<div>{images[1][1]}</div>", unsafe_allow_html=True)
68
+ with col3:
69
+ st.markdown(
70
+ f"<div class='img-box'><img src='{images[2][0]}'></div>",
71
+ unsafe_allow_html=True
72
+ )
73
+ st.markdown(f"<div>{images[2][1]}</div>", unsafe_allow_html=True)
74
 
75
  st.write("")
76
  st.write("")
77
  # Search bar
78
  st.markdown("""
79
  <div style='display: flex; justify-content: center;'>
80
+ <input type="text" placeholder=" 🔎 Search Met's Art Collection...."
81
  style="padding: 10px; width: 250px; border-radius: 20px; border: 1px solid #ccc;">
82
  </div>
83
  """, unsafe_allow_html=True)
 
87
  # Footer
88
  st.markdown("""
89
  <div style='position: fixed; bottom: 10px; left: 0; right: 0; text-align: center; font-size: 12px;'>
90
+ <span style="font-size: 14px;">ℹ️ Met API and data related information is available at –
91
  <a href="https://metmuseum.github.io/" target="_blank">https://metmuseum.github.io/</a></span>
92
  </div>
93
  """, unsafe_allow_html=True)
met_api.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import random
3
+
4
+ URL = "https://collectionapi.metmuseum.org/public/collection/v1/"
5
+
6
+ '''
7
+ Need to add error handling
8
+ '''
9
+
10
+ # gets information on a single object
11
+ def get_object(objectID):
12
+ return requests.get(f"{URL}/objects/{objectID}").json()
13
+
14
+ # gets all the objects that have some sort of image
15
+ def get_objectsWithImages():
16
+ response = requests.get(f"{URL}search?hasImages=true&q=*").json()
17
+ total = response["total"]
18
+ objectIDs = response["objectIDs"]
19
+ return total, objectIDs
20
+
21
+ # gets the urls for 3 random objects with images
22
+ def get_images(totalObjects, objectIDs, limit):
23
+ images = []
24
+ # grabbing extra in case a primary image is blank (Works best on small limits)
25
+ rand_indexes = random.sample(range(totalObjects), limit + 20)
26
+
27
+ for i in rand_indexes:
28
+ obj = get_object(objectIDs[i])
29
+ if obj["primaryImage"]:
30
+ images.append((obj["primaryImage"], obj["title"]))
31
+ limit += 1
32
+ if limit == 3:
33
+ break
34
+
35
+ return images