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 streamlit_authenticator as stauth from st_aggrid import GridOptionsBuilder, AgGrid, GridUpdateMode, DataReturnMode from streamlit_extras.stylable_container import stylable_container from streamlit_extras.metric_cards import style_metric_cards import warnings from streamlit_extras.switch_page_button import switch_page import pickle import time import plotly.io as pio import os import requests 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}") # Example usage pio.templates.default = 'plotly' warnings.filterwarnings('ignore') st.set_page_config(layout="wide") color = {'Unknown/Other':'#3a393a', 'Black or African American': '#2993A3', 'White':'#666766', 'Native American':'#f4b780', 'Hispanic':'#a0cd7c', 'Pacific Islander':'#a680ba','Asian':'#f37e85'} file = open("login_state.pkl",'rb') st.session_state['logged_in'] = pickle.load(file) file.close() #----------------------------NavBar-------------------------------# hide_menu_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('', unsafe_allow_html=True) st.markdown("""
""", unsafe_allow_html=True) #------------------------------- Metric Cards ------------------------# 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%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=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) pop = pd.read_csv("Population.csv") cols = st.columns(2) cols[0].metric(label="Felony Convicitons for the Population (2015-2023)", value=32979, delta='inc') v = int(pop.loc[0, pop.columns.str.contains("Black")].values[0]) / int(pop.loc[0, pop.columns.str.contains("White")].values[0]) cols[1].metric(label="Black : White Population ratio", value=str(round(v*100, 2))+"/100", delta = "As per 2020") style_metric_cards() #------------------------------- Page 0 ------------------------------# Page0 = st.container(border=True) Page0 = stylable_container(key="Page0", css_styles=[""" {box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 25px;}"""])#, """{background-color:white;opacity:0.8;}"""])#, """ {border: 2px solid rgba(49, 51, 63, 0.2); border-radius: 10px;}"""]) cols = Page0.columns([0.5,5.5,0.2,3.3,0.5]) cols[1].header("Most Common Charges Booked in the County", anchor='section-1', help="A Graph to understrand the most commnly charged crimes in the county by racial distribution. The graph gives a yearly estimates of the crimes. You can choose to see the data by Race/Race and see upto top 300.") cols = Page0.columns([0.5,3, 3,3,0.5]) Race = cols[1].selectbox('Select Race', tuple(color.keys()), index=None) Dec = cols[2].selectbox('Change Data', tuple(['Court Data', 'Curently Incarcerated']), index=0) st.markdown(""" """,unsafe_allow_html=True) timeline = cols[3].slider('Select Timeline for Cases', 1990, 2022, (2015,2021)) cols = Page0.columns([6.5,3.5]) 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%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=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) file_path = "Court_appA.csv" url = 'https://rja-sanbernardino.s3.us-east-1.amazonaws.com/dashboard/Court_appA.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=20240611T225407Z&X-Amz-SignedHeaders=host&X-Amz-Expires=43200&X-Amz-Credential=ASIA47CRXVNAOF2MQFR4%2F20240611%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=5bd3a6b5fd9339bdd1828c445f171d57c1a5c29b6b8505f13386dcc6565013b5' check_and_download_file(file_path, url) file_path = "Incarceration_appA.csv" url = 'https://rja-sanbernardino.s3.us-east-1.amazonaws.com/dashboard/Incarceration_appA.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=20240611T225455Z&X-Amz-SignedHeaders=host&X-Amz-Expires=43200&X-Amz-Credential=ASIA47CRXVNAOF2MQFR4%2F20240611%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=61363e9a187b82e2c6001d849bd9156bca839ba76ea4fdfecbff26c95190fa80' check_and_download_file(file_path, url) file_path = "Incarceration_appB.csv" url = 'https://rja-sanbernardino.s3.us-east-1.amazonaws.com/dashboard/Incarceration_appB.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=20240611T225559Z&X-Amz-SignedHeaders=host&X-Amz-Expires=43200&X-Amz-Credential=ASIA47CRXVNAOF2MQFR4%2F20240611%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=90a515117cf5813700d9fd9129bde62adf37de225a6cf4769914b5b6d0ff0b14' check_and_download_file(file_path, url) if Race != None: df1 = pd.read_csv("Court_appA.csv") if Dec == 'Court Data' else pd.read_csv("Incarceration_appA.csv") df1 = df1[df1['Race'] == Race] df1 = df1[(df1['year'] >= timeline[0]) & (df1['year'] <= timeline[1])] df1 = df1[['Charges', 'Race', 'count']].groupby('Charges').agg({'count':'sum'}).reset_index() df1['count'] = (df1['count'] / (timeline[1] - timeline[0])) if timeline[1] != timeline[0] else df1['count'] df1 = df1.sort_values('count') fig = px.bar(pd.DataFrame(df1[-40:]), y='Charges', x='count', orientation='h') fig.update_traces(marker_color=color[Race]) else: df1 = pd.read_csv("Court_appB.csv") if Dec == 'Court Data' else pd.read_csv("Incarceration_appB.csv") df1 = df1[(df1['year'] >= timeline[0]) & (df1['year'] <= timeline[1])] df1 = df1[['Charges', 'Race', 'count']].groupby(['Charges','Race']).agg({'count':'sum'}).reset_index() df1['count'] = (df1['count'] / (timeline[1] - timeline[0])) if timeline[1] != timeline[0] else df1['count'] df1 = df1.sort_values(['Charges', 'count']) fig = px.bar(df1, y='Charges', x='count', orientation='h', color_discrete_map=color, color='Race') fig.update_layout(barmode='stack', yaxis={'categoryorder':'total ascending'}) if Dec == 'Court Data': fig.update_layout(width=900, height=900, xaxis = {'side':'top'},title_y=0.99, xaxis_title="Number of Cases Per Year", yaxis_title="Charges") else: fig.update_layout(width=900, height=900, xaxis = {'side':'top'},title_y=0.99, xaxis_title="Total Cases", yaxis_title="Charges") fig.update_layout(legend=dict(yanchor="top", y=0.7, xanchor="left", x=0.6)) cols[0].plotly_chart(fig, theme=None) # df = pd.read_excel("California Penal Code .xlsx") # # dfx = pd.read_csv("Court_appB.csv") # # df1 = df1[['Charges', 'count']].groupby(['Charges']).agg({'count':'sum'}).reset_index() # # df1 = df1.sort_values(['Charges', 'count']) # cols[1].markdown('######') # cols[1].markdown('####') # cols[1].dataframe(df, height=760) # if st.session_state["authentication_status"]: # st.session_state.auth.logout() # else: # switch_page('app')