-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_utilities.py
98 lines (81 loc) · 2.9 KB
/
streamlit_utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: utf-8 -*-
"""
Created on Thu May 25 00:56:32 2023
@author: MDP
"""
import streamlit as st
import pandas as pd
import s3fs
DATA_DIR = './data/'
S3_DIR = 'jmrl-visualization/'
category_colors = {
'Bookmobile': (227, 119, 194, 128),
'Central': (214, 39, 40, 128),
'Charlottesville': (214, 39, 40, 128),
'Crozet': (255, 127, 14, 128),
'Gordon': (148, 103, 189, 128),
'Greene': (44, 160, 44, 128),
'Louisa': (188, 189, 34, 128),
'Nelson': (140, 86, 75, 128),
'Northside': (31, 119, 180, 128),
'Albemarle': (31, 119, 180, 128),
'Out of Area': (255, 0, 255, 128),
# 'Out of Area': (127, 127, 127, 128),
'Scottsville': (23, 190, 207, 128)
}
# %% load data once
@st.cache_data
def load_data_s3(data_file):
# Create an S3 file system
fs = s3fs.S3FileSystem(anon=False)
s3_path = f'{S3_DIR}{data_file}'
with fs.open(s3_path, 'rb') as f:
df_loaded = pd.read_csv(f)
return df_loaded
@st.cache_data
def load_data_pickle(data_file):
path = f'{DATA_DIR}{data_file}'
df_loaded = pd.read_pickle(path)
return df_loaded
def hex_to_rgb(hex):
return [int(hex[i:i+2], 16) for i in (0, 2, 4)]
def rgb_to_hex(red, green, blue, alpha):
"""Return color as #rrggbb for the given color values."""
return '#%02x%02x%02x' % (red, green, blue)
def check_password():
"""Returns `True` if the user had the correct password."""
def password_entered():
"""Checks whether a password entered by the user is correct."""
if st.session_state["password"] == st.secrets["password"]:
st.session_state["password_correct"] = True
del st.session_state["password"] # don't store password
else:
st.session_state["password_correct"] = False
if "password_correct" not in st.session_state:
# First run, show input for password.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
return False
elif not st.session_state["password_correct"]:
# Password not correct, show input + error.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
st.error("😕 Password incorrect")
return False
else:
# Password correct.
return True
def compute_median_patron(df):
return pd.Series({
'lat': df['lat'].median(),
'lon': df['lon'].median(),
'frequent_location': df['frequent_location'].mode().iloc[0] if not df['frequent_location'].mode().empty else 'Unknown',
'home_branch': df['home_branch'].mode().iloc[0],
'jurisdiction': df['jurisdiction'].mode().iloc[0],
'nearest_branch_dist': df['nearest_branch_dist'].median(),
'circ_phy_avg': df['circ_phy_avg'].mean(),
'circ_dig_avg': df['circ_dig_avg'].mean(),
'count': 1 # Set to 1 for the bar chart
})