Nirmal16 commited on
Commit
82504bd
·
verified ·
1 Parent(s): e386b57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -44
app.py CHANGED
@@ -1,56 +1,71 @@
1
  import streamlit as st
2
- import pandas as pd
3
  import altair as alt
 
4
 
5
- # Load dataset
6
- DATA_URL = "https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/building_inventory.csv"
7
 
 
8
  @st.cache
9
  def load_data():
10
- return pd.read_csv(DATA_URL)
11
-
12
- df = load_data()
13
-
14
- # Streamlit app title and description
15
- st.title("Building Inventory Visualization")
16
- st.markdown("""
17
- This app visualizes the building inventory dataset.
18
- Use the controls in the sidebar to explore the data.
19
- """)
20
-
21
- # Sidebar for user input
22
- st.sidebar.header("Filter Options")
23
- state_filter = st.sidebar.multiselect(
24
- "Select State(s):",
25
- options=df["State"].dropna().unique(),
26
- default=df["State"].dropna().unique()
27
  )
 
 
 
 
 
28
 
29
- agency_filter = st.sidebar.multiselect(
30
- "Select Agency(ies):",
31
- options=df["Agency Name"].dropna().unique(),
32
- default=df["Agency Name"].dropna().unique()
 
 
 
 
 
 
 
 
 
 
33
  )
34
 
35
- # Apply filters to the data
36
- filtered_data = df[df["State"].isin(state_filter) & df["Agency Name"].isin(agency_filter)]
37
-
38
- # Display filtered data table
39
- st.subheader("Filtered Data Table")
40
- st.write(filtered_data)
41
-
42
- # Bar chart of buildings by agency
43
- st.subheader("Buildings by Agency")
44
- agency_building_counts = filtered_data["Agency Name"].value_counts().reset_index()
45
- agency_building_counts.columns = ["Agency Name", "Building Count"]
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
- st.altair_chart(chart, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
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)