sb_dashboard / pages /page_1.py
Venkatakrishnan
aws links
1eabbd4
import streamlit as st
import numpy as np
import plotly.figure_factory as ff
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
import os
import argparse
from st_aggrid import GridOptionsBuilder, AgGrid, GridUpdateMode, DataReturnMode
from PIL import Image
from streamlit_extras.stylable_container import stylable_container
from streamlit_extras.metric_cards import style_metric_cards
import pickle
def check_and_download_file(file_path, url):
if os.path.exists(file_path):
print(f"The file '{file_path}' already exists.")
else:
print(f"The file '{file_path}' does not exist. Downloading...")
try:
response = requests.get(url)
response.raise_for_status() # Check if the request was successful
with open(file_path, 'wb') as file:
file.write(response.content)
print(f"File downloaded successfully and saved as '{file_path}'.")
except requests.exceptions.RequestException as e:
print(f"An error occurred while downloading the file: {e}")
# Implement AND condition when downloading data
st.set_page_config(layout="wide")
color = {'Black or African American': '#2993A3', 'White':'#666766', 'Native American':'#f4b780', 'Hispanic':'#a0cd7c', 'Pacific Islander':'#a680ba', 'Unknown/Other':'#3a393a','Asian':'#f37e85'}
file = open("login_state.pkl",'rb')
st.session_state['logged_in'] = pickle.load(file)
file.close()
#print(st.session_state.get("logged_in"))
#----------------------------NavBar-------------------------#
hide_menu_style = """
<style>
#MainMenu {visibility: hidden;}
header {visibility: hidden;}
</style>
"""
st.markdown(hide_menu_style, unsafe_allow_html=True)
# print(st.session_state.get("logged_in"))
if st.session_state.get("logged_in") == False or st.session_state.get("logged_in") == None:
st.switch_page("app.py")
st.markdown('<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">', unsafe_allow_html=True)
st.markdown("""
<nav class="navbar fixed-top navbar-expand-lg navbar-dark" style="background-color: #3498DB;background-image:url('/Users/shaashwatagrawal/Documents/SF County/dashboard/Background.jpeg');">
<a class="navbar-brand" href="https://www.ipr.northwestern.edu/who-we-are/faculty-experts/redbird.html" target="_blank">RJA Dashboard</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link disabled" href="https://sanbernardinorja.streamlit.app/page_0" target="_self">Arrest Summary</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="https://sanbernardinorja.streamlit.app/page_1" target="_self">Charge By Race<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://sanbernardinorja.streamlit.app/page_2" target="_self">Download Data</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item mr-auto" style="padding-left:5px;padding-right:5px;outline-color:#f0f2f5;border: 2px solid white;border-radius:10px;">
<a class="nav-link" href="https://sanbernardinorja.streamlit.app/" target="_self" >Logout</a>
</li>
</ul>
</div>
</nav>
""", unsafe_allow_html=True)
#---------------------------- Page 2 ----------------------------#
cols = st.columns(2)
Page2 = stylable_container(key="Page2", css_styles=""" {box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 15px;}""")
cols = Page2.columns([4,3,3])
cols[1].header("Charges By Race", anchor = 'section-2', help = 'Understanding Disparity at different stages of a criminal proceeding (i.e. Arrest, Charging and Sentencing).')
# parser = argparse.ArgumentParser()
# parser.add_argument('--charge', type=str, default='187A')
# args = parser.parse_args()
with open("list_of_charges.pkl", "rb") as fp: # Unpickling
charges = pickle.load(fp)
col = Page2.columns([0.5, 9, 0.5])
PC = col[1].multiselect('Select Type of Charge', tuple(charges), default='459')#index=list(charges).index(args.charge))
file_path = "Population.csv"
url = 'https://rja-sanbernardino.s3.us-east-1.amazonaws.com/dashboard/Population.csv?response-content-disposition=inline&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEM%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzL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&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20240611T224039Z&X-Amz-SignedHeaders=host&X-Amz-Expires=43200&X-Amz-Credential=ASIA47CRXVNAOF2MQFR4%2F20240611%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=96c7384f7b881f1b201abc0689608231042ce46b076fdf75e2c3e009efc26cb7'
check_and_download_file(file_path, url)
# Reading and Processing Data
pop = pd.read_csv("Population.csv")
file_path = "Arrest_page1.csv"
url = 'https://rja-sanbernardino.s3.us-east-1.amazonaws.com/dashboard/Arrest_page1.csv?response-content-disposition=inline&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEM%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJGMEQCIFmSNoakzC4hQlNV2NcIpccGqt1lJiw7dumO58Kv5HHaAiBCHRkYbyL09%2BOEofcZF%2Bns9A09PO6x%2B0OxiYD2hpoX9SrkAghnEAAaDDg5MTM3NzEzNDQwMCIMZmel5gOdx72MuskSKsECTeZ5%2BcyfkQg%2B%2B3A7JvIv%2BC1gROoz%2FDXmPWbU%2FSAZeVnNm52uZ%2BqIMs%2BwWYZetz3c6yCs4jAoaTtG5m%2BQUFHX0y8bA131w1uOZUPrG8vFPXNHWSgPIc2G%2BZoXdzeipp2WUaTIGlCwyWXDI0XfP9qVjd6Xq4HLnggPA4oSEu1YwgK%2B47jO0XM%2BucrzhxuqSmi6wVGtzHp93KmPFT6jVAyM%2Bl6kb3apdWTa8YHjAzVRSLF7Zz%2Fp%2BMqMHJu4rqCAxFjNHzYu6iNqfLa17QRksNm6ceMouz8Hmv3npsckPC47fZLRmUn1RHdT0lNBOq%2BqiQzrSDxhGIpVUsH9S8rVkSMsGKupUo8Hj18GsuAsTeqtIICu9QrV%2F0yEnkpMbv4YBkbIP06fCLDbvEOFYkR6E8%2BCNduIk2IsaFdCuA%2FrBojQ9DSdMLCbo7MGOrQCUO2zBp8Ayj4ia9p0LjRwbGHDNtKhAQzxdILs%2BTn%2BTREt231CGQ119MkAhv4MeK685Da%2F8VOpav58HESVRdNqcYh%2B3AYuXsCwnC2WHYIpsgz5VssWUvwH%2BvPMwkzzIgXcdwNVBNS4m67c5pcya%2BQIVR3ShsBOv4BiTESmnjwUlxORB%2ByYvdfTz5gkVx3IA97wri%2FEKTH5prAsLR80ue2ayQDYnciX8awXYavJ7ypQa4nXgiyzOoy8ZJ5eA5yDeGiZG9rGkopMkjLVgrZeDfK7LH87Vetx3Jcxrwwwh7NVIvXQ0rnf5nJEweuW7EcgRSEeyB11pMUsSZC3f4NCRFt%2B6FcwqpJsY%2FxJBLgtroXp3XBHcZlH4pmx6hcHqFRAYav6ZSUdjfavsbSA5gaarEscJfaQrdk%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20240611T225817Z&X-Amz-SignedHeaders=host&X-Amz-Expires=43200&X-Amz-Credential=ASIA47CRXVNAOF2MQFR4%2F20240611%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=a702152a4b591ee74c315cbe38efc60b9487eaeeaad8a50fd98812fadc68c232'
check_and_download_file(file_path, url)
df = pd.read_csv("Arrest_page1.csv")
#df = df[df['Charges'].str.contains('|'.join(PC))]
file_path = "Court_page1.csv"
url = 'https://rja-sanbernardino.s3.us-east-1.amazonaws.com/dashboard/Court_page1.csv?response-content-disposition=inline&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEM%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJGMEQCIFmSNoakzC4hQlNV2NcIpccGqt1lJiw7dumO58Kv5HHaAiBCHRkYbyL09%2BOEofcZF%2Bns9A09PO6x%2B0OxiYD2hpoX9SrkAghnEAAaDDg5MTM3NzEzNDQwMCIMZmel5gOdx72MuskSKsECTeZ5%2BcyfkQg%2B%2B3A7JvIv%2BC1gROoz%2FDXmPWbU%2FSAZeVnNm52uZ%2BqIMs%2BwWYZetz3c6yCs4jAoaTtG5m%2BQUFHX0y8bA131w1uOZUPrG8vFPXNHWSgPIc2G%2BZoXdzeipp2WUaTIGlCwyWXDI0XfP9qVjd6Xq4HLnggPA4oSEu1YwgK%2B47jO0XM%2BucrzhxuqSmi6wVGtzHp93KmPFT6jVAyM%2Bl6kb3apdWTa8YHjAzVRSLF7Zz%2Fp%2BMqMHJu4rqCAxFjNHzYu6iNqfLa17QRksNm6ceMouz8Hmv3npsckPC47fZLRmUn1RHdT0lNBOq%2BqiQzrSDxhGIpVUsH9S8rVkSMsGKupUo8Hj18GsuAsTeqtIICu9QrV%2F0yEnkpMbv4YBkbIP06fCLDbvEOFYkR6E8%2BCNduIk2IsaFdCuA%2FrBojQ9DSdMLCbo7MGOrQCUO2zBp8Ayj4ia9p0LjRwbGHDNtKhAQzxdILs%2BTn%2BTREt231CGQ119MkAhv4MeK685Da%2F8VOpav58HESVRdNqcYh%2B3AYuXsCwnC2WHYIpsgz5VssWUvwH%2BvPMwkzzIgXcdwNVBNS4m67c5pcya%2BQIVR3ShsBOv4BiTESmnjwUlxORB%2ByYvdfTz5gkVx3IA97wri%2FEKTH5prAsLR80ue2ayQDYnciX8awXYavJ7ypQa4nXgiyzOoy8ZJ5eA5yDeGiZG9rGkopMkjLVgrZeDfK7LH87Vetx3Jcxrwwwh7NVIvXQ0rnf5nJEweuW7EcgRSEeyB11pMUsSZC3f4NCRFt%2B6FcwqpJsY%2FxJBLgtroXp3XBHcZlH4pmx6hcHqFRAYav6ZSUdjfavsbSA5gaarEscJfaQrdk%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20240611T225908Z&X-Amz-SignedHeaders=host&X-Amz-Expires=43200&X-Amz-Credential=ASIA47CRXVNAOF2MQFR4%2F20240611%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=edf0d1114a2fe7f5917b855a2e350d58c6de7d4373b99d69ec723f637be52512'
check_and_download_file(file_path, url)
dfr = pd.read_csv("Court_page1.csv")
#print(PC)
#dfr = dfr[dfr['Charges'].str.contains('|'.join(PC))]
#print(dfr)
file_path = "Sentence_page1.csv"
url = 'https://rja-sanbernardino.s3.us-east-1.amazonaws.com/dashboard/Sentence_page1.csv?response-content-disposition=inline&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEM%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJGMEQCIFmSNoakzC4hQlNV2NcIpccGqt1lJiw7dumO58Kv5HHaAiBCHRkYbyL09%2BOEofcZF%2Bns9A09PO6x%2B0OxiYD2hpoX9SrkAghnEAAaDDg5MTM3NzEzNDQwMCIMZmel5gOdx72MuskSKsECTeZ5%2BcyfkQg%2B%2B3A7JvIv%2BC1gROoz%2FDXmPWbU%2FSAZeVnNm52uZ%2BqIMs%2BwWYZetz3c6yCs4jAoaTtG5m%2BQUFHX0y8bA131w1uOZUPrG8vFPXNHWSgPIc2G%2BZoXdzeipp2WUaTIGlCwyWXDI0XfP9qVjd6Xq4HLnggPA4oSEu1YwgK%2B47jO0XM%2BucrzhxuqSmi6wVGtzHp93KmPFT6jVAyM%2Bl6kb3apdWTa8YHjAzVRSLF7Zz%2Fp%2BMqMHJu4rqCAxFjNHzYu6iNqfLa17QRksNm6ceMouz8Hmv3npsckPC47fZLRmUn1RHdT0lNBOq%2BqiQzrSDxhGIpVUsH9S8rVkSMsGKupUo8Hj18GsuAsTeqtIICu9QrV%2F0yEnkpMbv4YBkbIP06fCLDbvEOFYkR6E8%2BCNduIk2IsaFdCuA%2FrBojQ9DSdMLCbo7MGOrQCUO2zBp8Ayj4ia9p0LjRwbGHDNtKhAQzxdILs%2BTn%2BTREt231CGQ119MkAhv4MeK685Da%2F8VOpav58HESVRdNqcYh%2B3AYuXsCwnC2WHYIpsgz5VssWUvwH%2BvPMwkzzIgXcdwNVBNS4m67c5pcya%2BQIVR3ShsBOv4BiTESmnjwUlxORB%2ByYvdfTz5gkVx3IA97wri%2FEKTH5prAsLR80ue2ayQDYnciX8awXYavJ7ypQa4nXgiyzOoy8ZJ5eA5yDeGiZG9rGkopMkjLVgrZeDfK7LH87Vetx3Jcxrwwwh7NVIvXQ0rnf5nJEweuW7EcgRSEeyB11pMUsSZC3f4NCRFt%2B6FcwqpJsY%2FxJBLgtroXp3XBHcZlH4pmx6hcHqFRAYav6ZSUdjfavsbSA5gaarEscJfaQrdk%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20240611T230008Z&X-Amz-SignedHeaders=host&X-Amz-Expires=43200&X-Amz-Credential=ASIA47CRXVNAOF2MQFR4%2F20240611%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=6acd3bc1c06a9071237e68a86bf5efe504338d9c243a1879c0e7ee7745dc0ebc'
check_and_download_file(file_path, url)
dfd = pd.read_csv("Sentence_page1.csv")
#dfd = dfd[dfd['Charges'].str.contains('|'.join(PC))]
cols = Page2.columns([0.5,2.25,2.25,0.5,2.5,1.5,0.5])
#cols[1].subheader("Offence by Race")
st.markdown("""
<style>
.stSlider [data-baseweb=slider]{
width: 100%;
}
</style>
""",unsafe_allow_html=True)
timeline = cols[2].slider('Select Timeline for Cases', 1990, 2024, (2015,2023))
perCap = cols[1].selectbox("Display Graph Per Capita", ("No", "Yes"), index=1)
cols[4].subheader("Charge Percentage Ratio")
c1, c2, c3, c4, c5 = Page2.columns([4.5, 0.5, 0.5, 4, 0.5])
with c1:
df = pd.concat([dfd, dfr, df], ignore_index=True, axis=0)
df = df[df['Charges'].str.fullmatch('|'.join(PC), na=False)]
df = df[(df['year'] >= timeline[0]) & (df['year'] <= timeline[1])]
df = df[df['Race'] != 'Unknown/Other']
df = df[['Charge Type', 'Race', 'Charges', 'count', 'normalized_vals']].groupby(['Charge Type','Race']).agg({'count':'sum', 'normalized_vals':'sum'}).reset_index()
custom_dict = {'Booking Charge': 0, 'Filed Charge': 1, 'Conviction Charge': 2}
df = df.sort_values(by=['Charge Type'], key=lambda x: x.map(custom_dict), ascending=False)
xaxs = 'count' if perCap == "No" else "normalized_vals"
fig = px.bar(df, y='Charge Type', x=xaxs,color_discrete_map=color, color='Race', orientation='h')
fig.update_layout(width=700, height=600,)
fig.update_layout(xaxis_title="Number of Cases", yaxis_title="")
fig.update_layout(legend=dict(yanchor="bottom", y=1.0, xanchor="left", x=-0.17, orientation='h', entrywidth=150))
fig.update_layout(font=dict(family="Myriad Pro",size=14))
fig.update_yaxes(tickangle=270, automargin= True)
st.plotly_chart(fig, theme=None)
with c4:
sel_col = 'count' if perCap == "No" else "normalized_vals"
cc1, cc2 = st.columns(2)
with cc1:
PCRace = st.selectbox('Select Race to Compare with', tuple(set(list(df['Race'].unique()) + list(dfr['Race'].unique()))))
with cc2:
st.header(" / White")
r1 = df[(df['Race'] == PCRace) & (df['Charge Type'] == 'Booking Charge')][sel_col].sum()*(10000000)
r2 = df[(df['Race'] == 'White') & (df['Charge Type'] == 'Booking Charge')][sel_col].sum()*(10000000)
if r2 != 0:
val = '%0.2f'%(r1/r2)+'/1'
else:
val = '%0.2f'%(r1)+'/'+str(r2)
new_title = '<p style="font-family:Myriad Pro; color:Black; font-size: 20px;display: inline;vertical-align: top;">'+PCRace+' rate of arrests by the white rate of arrests: '+val+'</p>'
#st.markdown(new_title, unsafe_allow_html=True)
#st.markdown('####')
#st.markdown('####')
st.metric(label= PCRace+' rate of arrests by the white rate of arrests:' , value=val, delta=None)
style_metric_cards()
r1 = df[(df['Race'] == PCRace) & (df['Charge Type'] == 'Filed Charge')][sel_col].sum()*(10000000)
r2 = df[(df['Race'] == 'White') & (df['Charge Type'] == 'Filed Charge')][sel_col].sum()*(10000000)
if r2 != 0:
val = '%0.2f'%(r1/r2)+'/1'
else:
val = '%0.2f'%(r1)+'/'+str(r2)
new_title = '<p style="font-family:Myriad Pro; color:Black; font-size: 20px;display: inline;vertical-align: top;">'+PCRace+' rate of charging by the white rate of charging is: '+val+'</p>'
# st.markdown(new_title, unsafe_allow_html=True)
# st.markdown('####')
# st.markdown('####')
st.metric(label=PCRace+' rate of charging by the white rate of charging is: ', value=val, delta=None)
style_metric_cards()
r1 = (df[(df['Race'] == PCRace) & (df['Charge Type'] == 'Conviction Charge')][sel_col].sum()*(10000000))
r2 = (df[(df['Race'] == 'White') & (df['Charge Type'] == 'Conviction Charge')][sel_col].sum()*(10000000))
#st.text(PCRace3+" rate of being sentenced by the white rate of being sentenced")
if r2 != 0:
val = '%0.2f'%(r1/r2)+'/1'
else:
val = '%0.2f'%(r1)+'/'+str(r2)
new_title = '<p style="font-family:Myriad Pro; color:Black; font-size: 20px;display: inline;vertical-align: top;">'+PCRace+' rate of being sentenced by the white rate of being sentenced is: '+val+'</p>'
# st.markdown(new_title, unsafe_allow_html=True)
st.metric(label=PCRace+' rate of being sentenced by the white rate of being sentenced is: ', value=val, delta=None)
style_metric_cards()