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