Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st # web development
|
2 |
+
import numpy as np # np mean, np random
|
3 |
+
import pandas as pd # read csv, df manipulation
|
4 |
+
import time # to simulate a real time data, time loop
|
5 |
+
import plotly.express as px # interactive charts
|
6 |
+
|
7 |
+
|
8 |
+
# read csv from a github repo
|
9 |
+
df = pd.read_csv("https://raw.githubusercontent.com/Lexie88rus/bank-marketing-analysis/master/bank.csv")
|
10 |
+
|
11 |
+
|
12 |
+
st.set_page_config(
|
13 |
+
page_title = 'Real-Time Data Science Dashboard',
|
14 |
+
page_icon = '✅',
|
15 |
+
layout = 'wide'
|
16 |
+
)
|
17 |
+
|
18 |
+
# dashboard title
|
19 |
+
|
20 |
+
st.title("Real-Time / Live Data Science Dashboard")
|
21 |
+
|
22 |
+
# top-level filters
|
23 |
+
|
24 |
+
job_filter = st.selectbox("Select the Job", pd.unique(df['job']))
|
25 |
+
|
26 |
+
|
27 |
+
# creating a single-element container.
|
28 |
+
placeholder = st.empty()
|
29 |
+
|
30 |
+
# dataframe filter
|
31 |
+
|
32 |
+
df = df[df['job']==job_filter]
|
33 |
+
|
34 |
+
# near real-time / live feed simulation
|
35 |
+
|
36 |
+
for seconds in range(200):
|
37 |
+
#while True:
|
38 |
+
|
39 |
+
df['age_new'] = df['age'] * np.random.choice(range(1,5))
|
40 |
+
df['balance_new'] = df['balance'] * np.random.choice(range(1,5))
|
41 |
+
|
42 |
+
# creating KPIs
|
43 |
+
avg_age = np.mean(df['age_new'])
|
44 |
+
|
45 |
+
count_married = int(df[(df["marital"]=='married')]['marital'].count() + np.random.choice(range(1,30)))
|
46 |
+
|
47 |
+
balance = np.mean(df['balance_new'])
|
48 |
+
|
49 |
+
with placeholder.container():
|
50 |
+
# create three columns
|
51 |
+
kpi1, kpi2, kpi3 = st.columns(3)
|
52 |
+
|
53 |
+
# fill in those three columns with respective metrics or KPIs
|
54 |
+
kpi1.metric(label="Age ⏳", value=round(avg_age), delta= round(avg_age) - 10)
|
55 |
+
kpi2.metric(label="Married Count 💍", value= int(count_married), delta= - 10 + count_married)
|
56 |
+
kpi3.metric(label="A/C Balance $", value= f"$ {round(balance,2)} ", delta= - round(balance/count_married) * 100)
|
57 |
+
|
58 |
+
# create two columns for charts
|
59 |
+
|
60 |
+
fig_col1, fig_col2 = st.columns(2)
|
61 |
+
with fig_col1:
|
62 |
+
st.markdown("### First Chart")
|
63 |
+
fig = px.density_heatmap(data_frame=df, y = 'age_new', x = 'marital')
|
64 |
+
st.write(fig)
|
65 |
+
with fig_col2:
|
66 |
+
st.markdown("### Second Chart")
|
67 |
+
fig2 = px.histogram(data_frame = df, x = 'age_new')
|
68 |
+
st.write(fig2)
|
69 |
+
st.markdown("### Detailed Data View")
|
70 |
+
st.dataframe(df)
|
71 |
+
time.sleep(1)
|
72 |
+
#placeholder.empty()
|
73 |
+
|