mbecchis commited on
Commit
a92dbc8
·
verified ·
1 Parent(s): 2c2b2f1

Upload hf_spaces_refresh.py

Browse files
Files changed (1) hide show
  1. hf_spaces_refresh.py +95 -0
hf_spaces_refresh.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import gspread
3
+ from google.oauth2.service_account import Credentials
4
+ import pandas as pd
5
+ import plotly.express as px
6
+ import time
7
+
8
+ # Page config
9
+ st.set_page_config(page_title="Customer Dashboard", layout="wide")
10
+
11
+ # Title and Refresh button
12
+ col_title, col_refresh = st.columns([5, 1])
13
+ with col_title:
14
+ st.title("📊 Customer Dashboard")
15
+ with col_refresh:
16
+ st.write("") # Spacer for alignment
17
+ if st.button("🔄 Refresh"):
18
+ st.cache_data.clear()
19
+ st.rerun()
20
+
21
+ # Meta refresh tag to reload page every 5 minutes
22
+ st.markdown(
23
+ """
24
+ <meta http-equiv="refresh" content="300">
25
+ """,
26
+ unsafe_allow_html=True
27
+ )
28
+
29
+ # Google Sheets connection
30
+ @st.cache_data(ttl=300) # Cache for 5 minutes (same as refresh interval)
31
+ def load_data():
32
+ scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly']
33
+ creds = Credentials.from_service_account_file('FYI-service-account-credentials.json', scopes=scopes)
34
+ client = gspread.authorize(creds)
35
+
36
+ sheet_id = '1J4YQEf8WvdlS3BjIOya0jsrPHolBEVodSAmhYF9XtSk'
37
+ workbook = client.open_by_key(sheet_id)
38
+ ws = workbook.worksheet("Customer Dashboard")
39
+
40
+ # Get values from the sheet
41
+ vals1 = ws.get("A5:B6")
42
+ vals2 = ws.get("A20:B21")
43
+ vals3 = ws.get("A7:B8")
44
+ vals4 = ws.get("A22:B23")
45
+
46
+ # Create DataFrames
47
+ cogeco_ingest_df = pd.DataFrame(vals1, columns=['Label', 'Count'])
48
+ cogeco_regular_df = pd.DataFrame(vals3, columns=['Label', 'Count'])
49
+ canada_ingest_df = pd.DataFrame(vals2, columns=['Label', 'Count'])
50
+ canada_regular_df = pd.DataFrame(vals4, columns=['Label', 'Count'])
51
+
52
+ return cogeco_ingest_df, cogeco_regular_df, canada_ingest_df, canada_regular_df
53
+
54
+ # Display last refresh time
55
+ st.caption(f"Last loaded: {time.strftime('%Y-%m-%d %H:%M:%S')} | Page refreshes every 5 minutes")
56
+
57
+ # Load data
58
+ cogeco_ingest_df, cogeco_regular_df, canada_ingest_df, canada_regular_df = load_data()
59
+
60
+ # Create explicit color mapping
61
+ color_map = {
62
+ 'Cogeco Missing Ingest': '#ea4335',
63
+ 'Cogeco Ready in Ingest': '#4285f4',
64
+ 'Canada Missing in Ingest': '#ea4335',
65
+ 'Canada Ready in Ingest': '#4285f4',
66
+ 'Cogeco Missing Regular': '#ea4335',
67
+ 'Cogeco Regular': '#4285f4',
68
+ 'Canada Missing Regular': '#ea4335',
69
+ 'Canada Regular': '#4285f4'
70
+ }
71
+
72
+ # Create 2x2 grid for pie charts
73
+ col1, col2 = st.columns(2)
74
+
75
+ with col1:
76
+ # Cogeco Ingest
77
+ fig1 = px.pie(cogeco_ingest_df, values='Count', names='Label',
78
+ title='Cogeco Ingest Status', color='Label', color_discrete_map=color_map)
79
+ st.plotly_chart(fig1, use_container_width=True)
80
+
81
+ # Cogeco Regular
82
+ fig3 = px.pie(cogeco_regular_df, values='Count', names='Label',
83
+ title='Cogeco Regular Status', color='Label', color_discrete_map=color_map)
84
+ st.plotly_chart(fig3, use_container_width=True)
85
+
86
+ with col2:
87
+ # Canada Ingest
88
+ fig2 = px.pie(canada_ingest_df, values='Count', names='Label',
89
+ title='Canada Ingest Status', color='Label', color_discrete_map=color_map)
90
+ st.plotly_chart(fig2, use_container_width=True)
91
+
92
+ # Canada Regular
93
+ fig4 = px.pie(canada_regular_df, values='Count', names='Label',
94
+ title='Canada Regular Status', color='Label', color_discrete_map=color_map)
95
+ st.plotly_chart(fig4, use_container_width=True)