sb_dashboard / pages /page_2.py
Venkatakrishnan
all
cdd0177
raw
history blame
6 kB
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
# 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'))
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)