Spaces:
Runtime error
Runtime error
BZ269
commited on
Commit
•
4a3adfc
1
Parent(s):
5b3846d
Add files via upload
Browse files
firebase/firebase_to_st_plotly_v1.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
from datetime import timedelta, date
|
3 |
+
import firebase_admin
|
4 |
+
from firebase_admin import credentials, storage, firestore
|
5 |
+
import streamlit as st
|
6 |
+
import pandas as pd
|
7 |
+
import plotly.express as px
|
8 |
+
import json, os, dotenv
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
os.environ["FIREBASE_CREDENTIAL"] = dotenv.get_key(dotenv.find_dotenv(), "FIREBASE_CREDENTIAL")
|
13 |
+
cred = credentials.Certificate(json.loads(os.environ.get("FIREBASE_CREDENTIAL")))
|
14 |
+
|
15 |
+
# Initialize Firebase (if not already initialized)
|
16 |
+
if not firebase_admin._apps:
|
17 |
+
firebase_admin.initialize_app(cred, {'storageBucket': 'healthhack-store.appspot.com'})
|
18 |
+
|
19 |
+
#firebase_admin.initialize_app(cred,{'storageBucket': 'healthhack-store.appspot.com'}) # connecting to firebase
|
20 |
+
db = firestore.client()
|
21 |
+
|
22 |
+
docs = db.collection("clinical_scores").stream()
|
23 |
+
|
24 |
+
# for doc in docs:
|
25 |
+
# print(f"{doc.id} => {doc.to_dict()}")
|
26 |
+
|
27 |
+
# Create a list of dictionaries from the documents
|
28 |
+
data = []
|
29 |
+
for doc in docs:
|
30 |
+
doc_dict = doc.to_dict()
|
31 |
+
doc_dict['document_id'] = doc.id # In case you need the document ID later
|
32 |
+
data.append(doc_dict)
|
33 |
+
|
34 |
+
# Create a DataFrame
|
35 |
+
df = pd.DataFrame(data)
|
36 |
+
|
37 |
+
# Set 'name' as the index of the DataFrame
|
38 |
+
# df.set_index('name', inplace=True)
|
39 |
+
|
40 |
+
# Now, 'df' is the DataFrame with 'name' as the index
|
41 |
+
print(df)
|
42 |
+
|
43 |
+
# Load your DataFrame (assuming it's named df)
|
44 |
+
# df = pd.read_csv('your_file.csv')
|
45 |
+
|
46 |
+
# Convert date from string to datetime if it's not already in datetime format
|
47 |
+
df['date'] = pd.to_datetime(df['date'], errors='coerce')
|
48 |
+
|
49 |
+
# Streamlit page configuration
|
50 |
+
st.set_page_config(page_title="Interactive Data Dashboard", layout="wide")
|
51 |
+
|
52 |
+
# Sidebar - Selection
|
53 |
+
st.sidebar.header("Filter here:")
|
54 |
+
selected_name = st.sidebar.multiselect(
|
55 |
+
"Select the Name:",
|
56 |
+
options=df['name'].unique(),
|
57 |
+
default=df['name'].unique()
|
58 |
+
)
|
59 |
+
|
60 |
+
df_selection = df[df['name'].isin(selected_name)]
|
61 |
+
|
62 |
+
|
63 |
+
# Main panel
|
64 |
+
st.title(":bar_chart: Student Performance Dashboard")
|
65 |
+
st.markdown("##")
|
66 |
+
|
67 |
+
# Total attempts by name
|
68 |
+
total_attempts_by_name = df.groupby("name")['date'].count().reset_index()
|
69 |
+
total_attempts_by_name.columns = ['name', 'total_attempts']
|
70 |
+
fig_total_attempts = px.bar(
|
71 |
+
total_attempts_by_name,
|
72 |
+
x="name",
|
73 |
+
y="total_attempts",
|
74 |
+
title="<b>Total Attempts by Name</b>",
|
75 |
+
color_discrete_sequence=["#0083B8"] * len(total_attempts_by_name),
|
76 |
+
template="plotly_white",
|
77 |
+
)
|
78 |
+
st.plotly_chart(fig_total_attempts, use_container_width=True)
|
79 |
+
|
80 |
+
# Score distribution
|
81 |
+
df['score'] = pd.Categorical(df['score'], categories=['A', 'B', 'C', 'D', 'E'], ordered=True)
|
82 |
+
fig_score_distribution = px.histogram(
|
83 |
+
df,
|
84 |
+
x="score",
|
85 |
+
title="<b>Score Distribution</b>",
|
86 |
+
color_discrete_sequence=["#33CFA5"]
|
87 |
+
)
|
88 |
+
st.plotly_chart(fig_score_distribution, use_container_width=True)
|
89 |
+
|
90 |
+
# Students with <5 attempts
|
91 |
+
students_with_less_than_5_attempts = total_attempts_by_name[total_attempts_by_name['total_attempts'] < 5]
|
92 |
+
fig_less_than_5_attempts = px.bar(
|
93 |
+
students_with_less_than_5_attempts,
|
94 |
+
x="name",
|
95 |
+
y="total_attempts",
|
96 |
+
title="<b>Students with <5 Attempts</b>",
|
97 |
+
color_discrete_sequence=["#D62728"] * len(students_with_less_than_5_attempts),
|
98 |
+
template="plotly_white",
|
99 |
+
)
|
100 |
+
st.plotly_chart(fig_less_than_5_attempts, use_container_width=True)
|
101 |
+
|
102 |
+
# Selection of a student for detailed view (<5 attempts)
|
103 |
+
selected_student_less_than_5 = st.selectbox("Select a student with less than 5 attempts to view details:", students_with_less_than_5_attempts['name'])
|
104 |
+
if selected_student_less_than_5:
|
105 |
+
st.write(df[df['name'] == selected_student_less_than_5])
|
106 |
+
|
107 |
+
# Students with at least one score of 'C', 'D', 'E'
|
108 |
+
students_with_cde = df[df['score'].isin(['C', 'D', 'E'])].groupby("name")['date'].count().reset_index()
|
109 |
+
students_with_cde.columns = ['name', 'total_attempts']
|
110 |
+
fig_students_with_cde = px.bar(
|
111 |
+
students_with_cde,
|
112 |
+
x="name",
|
113 |
+
y="total_attempts",
|
114 |
+
title="<b>Students with at least one score of 'C', 'D', 'E'</b>",
|
115 |
+
color_discrete_sequence=["#FF7F0E"] * len(students_with_cde),
|
116 |
+
template="plotly_white",
|
117 |
+
)
|
118 |
+
st.plotly_chart(fig_students_with_cde, use_container_width=True)
|
119 |
+
|
120 |
+
# Selection of a student for detailed view (score of 'C', 'D', 'E')
|
121 |
+
selected_student_cde = st.selectbox("Select a student with at least one score of 'C', 'D', 'E' to view details:", students_with_cde['name'])
|
122 |
+
if selected_student_cde:
|
123 |
+
st.write(df[df['name'] == selected_student_cde])
|
firebase/write_to_firebase_v1.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
from datetime import timedelta, date
|
3 |
+
import firebase_admin
|
4 |
+
from firebase_admin import credentials, storage, firestore
|
5 |
+
import json, os, dotenv
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
os.environ["FIREBASE_CREDENTIAL"] = dotenv.get_key(dotenv.find_dotenv(), "FIREBASE_CREDENTIAL")
|
10 |
+
cred = credentials.Certificate(json.loads(os.environ.get("FIREBASE_CREDENTIAL")))
|
11 |
+
firebase_admin.initialize_app(cred,{'storageBucket': 'healthhack-store.appspot.com'}) # connecting to firebase
|
12 |
+
db = firestore.client()
|
13 |
+
|
14 |
+
class Clinical_scores:
|
15 |
+
def __init__(self, name, date, score, total_attempts):
|
16 |
+
self.name = name
|
17 |
+
self.date = date
|
18 |
+
self.score = score
|
19 |
+
self.total_attempts = total_attempts
|
20 |
+
|
21 |
+
def to_dict(self):
|
22 |
+
clinical_scores_dict = {
|
23 |
+
"name": self.name,
|
24 |
+
"date": self.date,
|
25 |
+
"score": self.score,
|
26 |
+
"total_attempts": self.total_attempts
|
27 |
+
}
|
28 |
+
return clinical_scores_dict
|
29 |
+
|
30 |
+
def __repr__(self):
|
31 |
+
return f"Clinical_scores(\
|
32 |
+
name={self.name}, \
|
33 |
+
date={self.date}, \
|
34 |
+
score={self.score}, \
|
35 |
+
total_attempts={self.total_attempts}\
|
36 |
+
)"
|
37 |
+
|
38 |
+
# Function to generate random dates
|
39 |
+
def random_date(start, end):
|
40 |
+
return start + timedelta(
|
41 |
+
seconds=random.randint(0, int((end - start).total_seconds())))
|
42 |
+
|
43 |
+
# Start and end dates for the random date generation
|
44 |
+
start_date = date(2023, 11, 1)
|
45 |
+
end_date = date(2023, 12, 31)
|
46 |
+
|
47 |
+
# Names and scores lists
|
48 |
+
names = ["Tan Wei Ming", "Lim Yu Yan", "Ong Kai Wen", "Ng Zi Xuan", "Koh Hui Wen", "Lee Jia Yi", "Tan Kai Xin", "Goh Wei Ning", "Chen Yi Ling", "Toh Zhen Yu", "Sim Li Ting", "Wong Shu Ting", "Ng Jun Kai", "Tan Jing Yi", "Chua Xue Li", "Ho Kai Lin", "Lim Jia Hui", "Teo Wei Lin", "Chen Xiu Ting", "Ang Li Hui"]
|
49 |
+
scores = ['A', 'B', 'C', 'D', 'E']
|
50 |
+
|
51 |
+
clinical_scores_ref = db.collection("clinical_scores")
|
52 |
+
|
53 |
+
# Generating and storing instances
|
54 |
+
for name in names:
|
55 |
+
total_attempts = random.randint(1, 10) # Randomly assigning total attempts between 1 to 10
|
56 |
+
for attempt in range(total_attempts):
|
57 |
+
date_of_attempt = random_date(start_date, end_date).isoformat()
|
58 |
+
score = random.choice(scores)
|
59 |
+
clinical_score = Clinical_scores(name, date_of_attempt, score, total_attempts)
|
60 |
+
|
61 |
+
# Storing to Firebase (assuming `clinical_scores_ref` is already defined)
|
62 |
+
clinical_scores_ref.document().set(clinical_score.to_dict())
|
63 |
+
|
64 |
+
# Sample Firebase retrieval code (assuming the document ID is known)
|
65 |
+
doc_ref = db.collection("clinical_scores").document("some_document_id")
|
66 |
+
|
67 |
+
doc = doc_ref.get()
|
68 |
+
if doc.exists:
|
69 |
+
print(f"Document data: {doc.to_dict()}")
|
70 |
+
else:
|
71 |
+
print("No such document!")
|