Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,71 @@
|
|
1 |
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
import altair as alt
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
|
|
|
8 |
@st.cache
|
9 |
def load_data():
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
st.
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
"Select State(s):",
|
25 |
-
options=df["State"].dropna().unique(),
|
26 |
-
default=df["State"].dropna().unique()
|
27 |
)
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
)
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
chart = alt.Chart(agency_building_counts).mark_bar().encode(
|
48 |
-
x=alt.X("Building Count", title="Number of Buildings"),
|
49 |
-
y=alt.Y("Agency Name", sort="-x", title="Agency Name"),
|
50 |
-
tooltip=["Agency Name", "Building Count"]
|
51 |
-
).properties(
|
52 |
-
width=800,
|
53 |
-
height=400
|
54 |
)
|
55 |
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import altair as alt
|
3 |
+
import pandas as pd
|
4 |
|
5 |
+
# Title
|
6 |
+
st.title("Building Inventory Interactive Visualization")
|
7 |
|
8 |
+
# Load Dataset
|
9 |
@st.cache
|
10 |
def load_data():
|
11 |
+
url = "https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/building_inventory.csv"
|
12 |
+
return pd.read_csv(url)
|
13 |
+
|
14 |
+
data = load_data()
|
15 |
+
|
16 |
+
# Data Preview
|
17 |
+
if st.sidebar.checkbox("Show Raw Data"):
|
18 |
+
st.subheader("Raw Dataset")
|
19 |
+
st.dataframe(data)
|
20 |
+
|
21 |
+
# Add color scale for 'Bldg Status'
|
22 |
+
building_status_scale = alt.Scale(
|
23 |
+
domain=["Active", "Inactive", "Unknown"],
|
24 |
+
range=["#1f77b4", "#ff7f0e", "#d62728"]
|
|
|
|
|
|
|
25 |
)
|
26 |
+
color = alt.Color("Bldg Status:N", scale=building_status_scale)
|
27 |
+
|
28 |
+
# Selections
|
29 |
+
brush = alt.selection_interval(encodings=["x"])
|
30 |
+
click = alt.selection_point(encodings=["color"])
|
31 |
|
32 |
+
# Top Panel: Scatter Plot (Year Constructed vs Square Footage)
|
33 |
+
scatter = (
|
34 |
+
alt.Chart(data)
|
35 |
+
.mark_point()
|
36 |
+
.encode(
|
37 |
+
alt.X("Year Constructed:Q", title="Year Constructed"),
|
38 |
+
alt.Y("Square Footage:Q", title="Square Footage"),
|
39 |
+
color=alt.condition(brush, color, alt.value("lightgray")),
|
40 |
+
size=alt.Size("Total Floors:Q", scale=alt.Scale(range=[50, 300]), title="Total Floors"),
|
41 |
+
tooltip=["Agency Name", "Location Name", "Square Footage", "Year Constructed"]
|
42 |
+
)
|
43 |
+
.properties(width=550, height=300, title="Building Size by Construction Year")
|
44 |
+
.add_params(brush)
|
45 |
+
.transform_filter(click)
|
46 |
)
|
47 |
|
48 |
+
# Bottom Panel: Bar Chart (Count of Building Status)
|
49 |
+
bar_chart = (
|
50 |
+
alt.Chart(data)
|
51 |
+
.mark_bar()
|
52 |
+
.encode(
|
53 |
+
x="count()",
|
54 |
+
y=alt.Y("Bldg Status:N", title="Building Status"),
|
55 |
+
color=alt.condition(click, color, alt.value("lightgray")),
|
56 |
+
)
|
57 |
+
.transform_filter(brush)
|
58 |
+
.properties(width=550, title="Building Status Distribution")
|
59 |
+
.add_params(click)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
)
|
61 |
|
62 |
+
# Combined Chart
|
63 |
+
chart = alt.vconcat(scatter, bar_chart, title="Building Inventory Insights")
|
64 |
+
|
65 |
+
# Tabs for Themes
|
66 |
+
tab1, tab2 = st.tabs(["Streamlit Theme", "Altair Native Theme"])
|
67 |
+
|
68 |
+
with tab1:
|
69 |
+
st.altair_chart(chart, theme="streamlit", use_container_width=True)
|
70 |
+
with tab2:
|
71 |
+
st.altair_chart(chart, theme=None, use_container_width=True)
|