File size: 3,886 Bytes
b5b28f1
1476642
1ee3994
c550680
 
 
8acf7d6
c550680
3a73daa
 
 
 
 
 
 
 
b5b28f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8acf7d6
fee0793
8acf7d6
 
 
b5b28f1
8acf7d6
c550680
8acf7d6
c550680
8acf7d6
 
 
 
 
 
 
 
 
c550680
 
8acf7d6
 
c550680
8acf7d6
c550680
8acf7d6
 
1476642
8acf7d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1476642
b5b28f1
 
 
 
3a73daa
b5b28f1
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import streamlit as st
import pandas as pd
from met_api import search_for_images, get_objectsWithImages, get_images, department_counts

@st.cache_data
def cached_search_for_images(query):
    return search_for_images(query, 2)

@st.cache_data
def cache_objectsWithImages():
    return get_objectsWithImages()

@st.cache_data
def cache_images(total, objectIDs, limit):
    return get_images(total, objectIDs, limit)

# Page setup
st.set_page_config(page_title="The METrics", layout="centered")

st.markdown("<h1 style='text-align: center;'>The METrics</h1>", unsafe_allow_html=True)
st.markdown("<h4 style='text-align: center;'>Data Analysis of the Met Museum's Art Collection</h4>",
            unsafe_allow_html=True)

st.write("")

# CSS for fixed-height image boxes
st.markdown(
    """
    <style>
    .img-box {
        height: 150px;
        width: 150px;
        overflow: hidden;
        border: 1px solid #ccc;
        border-radius: 6px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .img-box img {
        height: 100%;
        width: auto;
        object-fit: cover;
    }
    </style>
    """,
    unsafe_allow_html=True
)

tab1, tab2, tab3 = st.tabs(["Random", "Search and Browse", "Dept Analytics"])

with tab1:
    total, ids = cache_objectsWithImages()
    st.write(f"There are {total} images in the Met collection\nHere are 3 random ones for your enjoyment...")

    imgs = cache_images(total, ids, 3)

    col1, col2, col3 = st.columns(3)

    with col1:
        st.image(imgs[0][0])
        st.write(imgs[0][1])
    with col2:
        st.image(imgs[1][0])
        st.write(imgs[1][1])
    with col3:
        st.image(imgs[2][0])
        st.write(imgs[2][1])


with tab2:
    q = st.text_input("🔎  Search Met's Art Collection....",value="cats", key="search_query")

    r = cached_search_for_images(q)

    if r.empty:
        st.write("No results found.")
    else:
        summary = r[["primaryImageSmall","title","department","objectName"]]
        config = {
            "primaryImageSmall": st.column_config.ImageColumn(),
        }
        event = st.dataframe(summary, column_config=config, use_container_width=True, on_select="rerun", selection_mode="single-row")
        if event.selection.rows:
            selected_index = event.selection.rows[0] # Get the index of the first selected row
            selected_row_data = r.iloc[selected_index]

            st.subheader("Details of Selected Piece:")
            st.image(selected_row_data["primaryImage"], caption=selected_row_data["title"], width=500)
            st.write(selected_row_data)
        else:
            st.info("Select a row in the table to see its details.")

with tab3:
    # Department analytic
    st.write("")
    st.subheader("Departments for your search")

    query = st.text_input("Search term (for images only)", value="cats", key="dept_query")
    max_ids = st.slider("How many results to analyze", 20, 400, 150, 10, key="dept_max")

    if st.button("Run department analytic", key="dept_run"):
        with st.spinner("Fetching and tallying departments…"):
            rows = department_counts(q=query, max_ids=max_ids)

        if not rows:
            st.info("No results found (or the API call failed). Try another term.")
        else:
            df = pd.DataFrame(rows, columns=["Department", "Count"])
            # show a quick bar chart + table
            st.bar_chart(df.set_index("Department"))
            st.dataframe(df, use_container_width=True)


# Footer
st.markdown("""
<div style='position: fixed; bottom: 10px; left: 0; right: 0; text-align: center; font-size: 12px;'>
    <span style="font-size: 14px;">ℹ️ Met API and data related information is available at –
    <a href="https://metmuseum.github.io/" target="_blank">https://metmuseum.github.io/</a></span>
</div>
""", unsafe_allow_html=True)