sb_dashboard / pages /page_2.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
from streamlit_extras.stylable_container import stylable_container
import pickle
import ast
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': '#ff7eb6', 'White':'#be95ff', 'Native American':'#0f62fe', 'Indian':'#82cfff', 'Japanese':'lightyellow', 'Korean':'gray','Chinese':'yellow', 'Hispanic':'#dface6', 'Pacific Islander':'#3ddbd9', 'Unkown/Other':'#c1c7cd','Filipino':'Green', 'Middle Eastern':'#000000','Vietnamese':'coral','Laotian':'cornsilk','Cambodian':'darkcyan','Other Asian':'darkgoldenrod', 'Asian':'#82cfff',}
file = open("login_state.pkl",'rb')
st.session_state['logged_in'] = pickle.load(file)
file.close()
#----------------------------NavBar-------------------------#
hide_menu_style = """
<style>
#MainMenu {visibility: hidden;}
header {visibility: hidden;}
</style>
"""
st.markdown(hide_menu_style, unsafe_allow_html=True)
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("""
<head>
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<nav class="navbar fixed-top navbar-expand-lg navbar-dark" style="background-color: #3498DB;">
<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" style="width:100%;">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link disabled" href="https://sanbernardinorja.streamlit.app/page_0" target="_self">Arrest Summary<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://sanbernardinorja.streamlit.app/page_1" target="_self">Charge By Race</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)
# with open("list_of_charges.pkl", "rb") as fp: # Unpickling
# charges = pickle.load(fp)
# Page 3
Page3 = stylable_container(key="Page3", css_styles=""" {box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 15px;} """)
Page3.header("Download Data", anchor = 'section-3', help = 'Download data relevant to your case. Use the filters to Determine what data do you need.')
selected_point = []
#loc = "DA_referrals_2022(1).csv"
Charge = Page3.selectbox('Select Type of Charge', ('Booking Charge', 'Filed Charge', 'CDCR'))
file_path = "Referral_page2.csv"
url = 'https://rja-sanbernardino.s3.us-east-1.amazonaws.com/dashboard/Referral_page2.csv?response-content-disposition=inline&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEM%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJGMEQCIFmSNoakzC4hQlNV2NcIpccGqt1lJiw7dumO58Kv5HHaAiBCHRkYbyL09%2BOEof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&X-Amz-Credential=ASIA47CRXVNAOF2MQFR4%2F20240611%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=a5063cb83d0ace24fa7d476c3edabc364ddbdf8c83c8995a762b032f33171a3c'
check_and_download_file(file_path, url)
file_path = "Court_page2.csv"
url = 'https://rja-sanbernardino.s3.us-east-1.amazonaws.com/dashboard/Court_page2.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=20240611T230325Z&X-Amz-SignedHeaders=host&X-Amz-Expires=43200&X-Amz-Credential=ASIA47CRXVNAOF2MQFR4%2F20240611%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=3e6ee21cfbc08692d3d55303bbdd19dd905c9f52e3bc25f82f018e95f94fddf2'
check_and_download_file(file_path, url)
file_path = "Sentence_page2.csv"
url = 'https://rja-sanbernardino.s3.us-east-1.amazonaws.com/dashboard/Sentence_page2.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=20240611T230350Z&X-Amz-SignedHeaders=host&X-Amz-Expires=43200&X-Amz-Credential=ASIA47CRXVNAOF2MQFR4%2F20240611%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=9bbc311f46095b2447d6c624b8e7151e9c921747b892225ec1fa6e38192c705b'
check_and_download_file(file_path, url)
loc = "Referral_page2.csv" if Charge == 'Booking Charge' else "Court_page2.csv" if Charge == 'Filed Charge' else "Sentence_page2.csv"
df = pd.read_csv(loc)
df = df.drop(columns='Unnamed: 0')
uid = 'UID' if Charge == 'Booking Charge' else 'Case Number'
filename = "list_of_charges.pkl" if Charge == 'Booking Charge' else "list_of_charges_detailed.pkl"
with open(filename, "rb") as fp: # Unpickling
charges = pickle.load(fp)
#split_first = False if loc == "Referral_page2.csv" else True
cols = Page3.columns(4)
Filtera = cols[0].multiselect('Select Charges', tuple(charges), help='Select Charges relevant to your case and client')
Filterb = cols[2].multiselect('Select Ethnicity', tuple(color.keys()), help='Select relevant Ethnicity')
Filterc = cols[1].selectbox('Select Function - Charges', ("AND", "OR"), help="AND - All Clients charged with all chosen charges \n\n OR - All Clients charged with atleast one of the chosen charges")
cols_list = df.columns.tolist()
cols_list.remove('Charges')
cols_list.remove('Race')
cols_list.remove(uid)
Filterd = cols[3].multiselect('Select Additional Columns to View', tuple(cols_list), help="1. Incident Number - Unique Incident ID assigned to SF Cases \n\n 2. Gender - Gender of the perpetrator \n\n 3. Booked.Case.Type / Filed.Case.Type - Felony / Misdemeanor \n\n 4. Age.at.Arrest - Age at which the person was arrested \n\n 5. Status.CTNum / Status.CTNum.Agg - Case Status After arrest or filing i.e. new charges filed or discharged \n\n 6. Description - Arrest / Charges Description \n\n 7. Year - Year of Arrest for Booking charge / Year of Charging for Filed Charge or Sentenced For \n\n 8. Case.Dispo - Case Disposition Number \n\n 9. Dispo.Description - Final court decision")
if len(Filtera) > 0:
filst = "|".join(Filtera)
df = df[df['Charges'].str.contains(filst, regex=True, na=False)]
if len(Filterb) > 0:
filst = "|".join(Filterb)
df = df[df['Races'].str.contains(filst, regex=True, na=False)]
if Filterc == 'AND' and len(Filtera) > 0:
cnos = [df[df['Charges'].str.contains(i)][uid].tolist() for i in Filtera]
cm_cnos = list(set.intersection(*map(set, cnos)))
df = df[df[uid].isin(cm_cnos)]
df = df.drop_duplicates()
df[uid] = df[uid].map(str)
for i in df.columns[1:]:
df[i] = df[i].str.strip('[]').str.split(',')
disp_cols = [uid, 'Charges', 'Race'] + Filterd
Page3.dataframe(df[disp_cols],width=1300)