hw391 commited on
Commit
ed803f8
·
2 Parent(s): 1ee3994815b969

Merge pull request #5 from MrinalGoel643/feature/tabs

Browse files
Files changed (2) hide show
  1. main.py +55 -56
  2. met_api.py +6 -2
main.py CHANGED
@@ -4,7 +4,7 @@ from met_api import search_for_images, get_objectsWithImages, get_images, depart
4
 
5
  @st.cache_data
6
  def cached_search_for_images(query):
7
- return search_for_images(query, 2,departments=[1,3,4,5,6,7])
8
 
9
  @st.cache_data
10
  def cache_objectsWithImages():
@@ -47,71 +47,70 @@ st.markdown(
47
  unsafe_allow_html=True
48
  )
49
 
 
50
 
51
- # Columns with fixed-height images
52
- col1, col2, col3 = st.columns(3)
 
53
 
54
- q = st.text_input("🔎 Search Met's Art Collection....",value="UFO", key="search_query")
55
 
56
- r = cached_search_for_images(q)
57
 
58
- summary = r[["primaryImageSmall","title","department","objectName"]]
 
 
 
 
 
 
 
 
59
 
60
- config = {
61
- "primaryImageSmall": st.column_config.ImageColumn(),
62
- }
63
 
64
- event = st.dataframe(summary, column_config=config, use_container_width=True, on_select="rerun", selection_mode="single-row")
 
65
 
66
- if event.selection.rows:
67
- selected_index = event.selection.rows[0] # Get the index of the first selected row
68
- selected_row_data = r.iloc[selected_index]
69
 
70
- st.subheader("Details of Selected Row:")
71
- st.image(selected_row_data["primaryImage"], caption=selected_row_data["title"], width=500)
72
- st.write(selected_row_data)
73
- else:
74
- st.info("Select a row in the table to see its details.")
75
-
76
- with col1:
77
- st.markdown(
78
- "<div class='img-box'><img src='https://images.metmuseum.org/CRDImages/ep/web-large/DP-29324-001.jpg'></div>",
79
- unsafe_allow_html=True
80
- )
81
- with col2:
82
- st.markdown(
83
- "<div class='img-box'><img src='https://images.metmuseum.org/CRDImages/ad/web-large/DP124705.jpg'></div>",
84
- unsafe_allow_html=True
85
- )
86
- with col3:
87
- st.markdown(
88
- "<div class='img-box'><img src='https://images.metmuseum.org/CRDImages/gr/web-large/DP21847edited.jpg'></div>",
89
- unsafe_allow_html=True
90
- )
91
-
92
- st.write("")
93
- st.write("")
94
-
95
- # Department analytic
96
- st.write("")
97
- st.subheader("Departments for your search")
98
-
99
- query = st.text_input("Search term (for images only)", value="cats", key="dept_query")
100
- max_ids = st.slider("How many results to analyze", 20, 400, 150, 10, key="dept_max")
101
-
102
- if st.button("Run department analytic", key="dept_run"):
103
- with st.spinner("Fetching and tallying departments…"):
104
- rows = department_counts(q=query, max_ids=max_ids)
105
-
106
- if not rows:
107
- st.info("No results found (or the API call failed). Try another term.")
108
  else:
109
- df = pd.DataFrame(rows, columns=["Department", "Count"])
110
- # show a quick bar chart + table
111
- st.bar_chart(df.set_index("Department"))
112
- st.dataframe(df, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
- st.write("")
115
 
116
  # Footer
117
  st.markdown("""
 
4
 
5
  @st.cache_data
6
  def cached_search_for_images(query):
7
+ return search_for_images(query, 2)
8
 
9
  @st.cache_data
10
  def cache_objectsWithImages():
 
47
  unsafe_allow_html=True
48
  )
49
 
50
+ tab1, tab2, tab3 = st.tabs(["Random", "Search and Browse", "Dept Analytics"])
51
 
52
+ with tab1:
53
+ total, ids = cache_objectsWithImages()
54
+ st.write(f"There are {total} images in the Met collection\nHere are 3 random ones for your enjoyment...")
55
 
56
+ imgs = cache_images(total, ids, 3)
57
 
58
+ col1, col2, col3 = st.columns(3)
59
 
60
+ with col1:
61
+ st.image(imgs[0][0])
62
+ st.write(imgs[0][1])
63
+ with col2:
64
+ st.image(imgs[1][0])
65
+ st.write(imgs[1][1])
66
+ with col3:
67
+ st.image(imgs[2][0])
68
+ st.write(imgs[2][1])
69
 
 
 
 
70
 
71
+ with tab2:
72
+ q = st.text_input("🔎 Search Met's Art Collection....",value="cats", key="search_query")
73
 
74
+ r = cached_search_for_images(q)
 
 
75
 
76
+ if r.empty:
77
+ st.write("No results found.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  else:
79
+ summary = r[["primaryImageSmall","title","department","objectName"]]
80
+ config = {
81
+ "primaryImageSmall": st.column_config.ImageColumn(),
82
+ }
83
+ event = st.dataframe(summary, column_config=config, use_container_width=True, on_select="rerun", selection_mode="single-row")
84
+ if event.selection.rows:
85
+ selected_index = event.selection.rows[0] # Get the index of the first selected row
86
+ selected_row_data = r.iloc[selected_index]
87
+
88
+ st.subheader("Details of Selected Piece:")
89
+ st.image(selected_row_data["primaryImage"], caption=selected_row_data["title"], width=500)
90
+ st.write(selected_row_data)
91
+ else:
92
+ st.info("Select a row in the table to see its details.")
93
+
94
+ with tab3:
95
+ # Department analytic
96
+ st.write("")
97
+ st.subheader("Departments for your search")
98
+
99
+ query = st.text_input("Search term (for images only)", value="cats", key="dept_query")
100
+ max_ids = st.slider("How many results to analyze", 20, 400, 150, 10, key="dept_max")
101
+
102
+ if st.button("Run department analytic", key="dept_run"):
103
+ with st.spinner("Fetching and tallying departments…"):
104
+ rows = department_counts(q=query, max_ids=max_ids)
105
+
106
+ if not rows:
107
+ st.info("No results found (or the API call failed). Try another term.")
108
+ else:
109
+ df = pd.DataFrame(rows, columns=["Department", "Count"])
110
+ # show a quick bar chart + table
111
+ st.bar_chart(df.set_index("Department"))
112
+ st.dataframe(df, use_container_width=True)
113
 
 
114
 
115
  # Footer
116
  st.markdown("""
met_api.py CHANGED
@@ -103,7 +103,7 @@ def search_for_images(query,
103
  #session = requests.Session()
104
 
105
  print("Searching for", query)
106
- print("Departments:", dept_ids)
107
 
108
  for dept_id in dept_ids:
109
  # limit to only those with images and is highlighted
@@ -174,7 +174,11 @@ def main():
174
  dept = None
175
  else:
176
  dept = [int(dept_no)]
177
- results = search_for_images(input("Search the Met for: "),2, departments=dept)
 
 
 
 
178
  if results.empty:
179
  print("No results found.")
180
  continue
 
103
  #session = requests.Session()
104
 
105
  print("Searching for", query)
106
+ print("Across departments:", dept_ids)
107
 
108
  for dept_id in dept_ids:
109
  # limit to only those with images and is highlighted
 
174
  dept = None
175
  else:
176
  dept = [int(dept_no)]
177
+ query = input("Search the Met for: ").strip()
178
+ if query == '':
179
+ print("Please enter a query.")
180
+ continue
181
+ results = search_for_images(query,2, departments=dept)
182
  if results.empty:
183
  print("No results found.")
184
  continue