vgosavi2 commited on
Commit
54fa991
·
verified ·
1 Parent(s): 536eaaf

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +77 -29
src/streamlit_app.py CHANGED
@@ -1,40 +1,88 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
5
 
 
 
 
 
 
 
 
 
6
  """
7
- # Welcome to Streamlit!
8
 
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
 
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
 
 
 
 
15
 
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
 
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
 
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
 
 
 
32
 
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
 
 
35
  .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ import altair as alt
4
+
5
+ # 1. Page title
6
+ st.title("Phase 02: Interactive Crime Data Pie Charts")
7
 
8
+ # 2. Data info & load
9
+ st.header("Dataset Information")
10
+ st.markdown(
11
+ """
12
+ - **Source:** Chicago crime incidents dataset
13
+ - **Rows:** incidents (one per row)
14
+ - **Columns:** e.g. `crm_cd_desc` (crime type), `arrest` (boolean), `date`, `location_description`, etc.
15
+ - **Purpose:** Explore crime-type distribution and arrest outcomes via pie charts.
16
  """
17
+ )
18
 
19
+ @st.cache_data
20
+ def load_data():
21
+ return pd.read_csv("crime_data.csv")
22
 
23
+ df = load_data()
24
+
25
+ # 3. Data preview
26
+ st.header("Data Preview")
27
+ st.write(f"Total records: {df.shape[0]} | Total columns: {df.shape[1]}")
28
+ st.dataframe(df.head())
29
 
30
+ # 4. Pie Chart 1: Top 10 Crime Types
31
+ st.header("Pie Chart 1: Top 10 Crime Types Distribution")
32
 
33
+ # Prepare Top 10 crime types
34
+ top_crimes = (
35
+ df["crm_cd_desc"]
36
+ .value_counts()
37
+ .nlargest(10)
38
+ .reset_index()
39
+ .rename(columns={"index": "Crime Type", "crm_cd_desc": "Count"})
40
+ )
41
+ top_crimes["Percentage"] = top_crimes["Count"] / top_crimes["Count"].sum()
42
+
43
+ # Build Altair pie chart
44
+ chart1 = (
45
+ alt.Chart(top_crimes)
46
+ .mark_arc(innerRadius=50)
47
+ .encode(
48
+ theta=alt.Theta(field="Count", type="quantitative"),
49
+ color=alt.Color(field="Crime Type", type="nominal"),
50
+ tooltip=[
51
+ alt.Tooltip("Crime Type:N", title="Crime Type"),
52
+ alt.Tooltip("Count:Q", title="Count"),
53
+ alt.Tooltip("Percentage:Q", title="Percentage", format=".1%")
54
+ ],
55
+ )
56
+ .properties(width=400, height=400, title="Top 10 Crime Types")
57
+ )
58
+ st.altair_chart(chart1, use_container_width=True)
59
 
60
+ # 5. Pie Chart 2: Arrest vs. Non-Arrest Distribution
61
+ st.header("Pie Chart 2: Arrest vs. Non-Arrest Distribution")
62
 
63
+ # Prepare arrest data
64
+ arrest_counts = (
65
+ df["arrest"]
66
+ .map({True: "Arrest Made", False: "No Arrest"})
67
+ .value_counts()
68
+ .reset_index()
69
+ .rename(columns={"index": "Arrest Status", "arrest": "Count"})
70
+ )
71
+ arrest_counts["Percentage"] = arrest_counts["Count"] / arrest_counts["Count"].sum()
72
 
73
+ # Build second Altair pie chart
74
+ chart2 = (
75
+ alt.Chart(arrest_counts)
76
+ .mark_arc(innerRadius=30)
77
  .encode(
78
+ theta=alt.Theta(field="Count", type="quantitative"),
79
+ color=alt.Color(field="Arrest Status", type="nominal"),
80
+ tooltip=[
81
+ alt.Tooltip("Arrest Status:N", title="Status"),
82
+ alt.Tooltip("Count:Q", title="Count"),
83
+ alt.Tooltip("Percentage:Q", title="Percentage", format=".1%")
84
+ ],
85
+ )
86
+ .properties(width=400, height=400, title="Arrest Outcomes")
87
+ )
88
+ st.altair_chart(chart2, use_container_width=True)