|
| 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 | +# PyGWalker |
| 9 | + |
| 10 | +st.set_page_config( |
| 11 | + page_title="Real-Time Data Science Dashboard", |
| 12 | + page_icon="✅", |
| 13 | + layout="wide", |
| 14 | +) |
| 15 | + |
| 16 | +# read csv from a github repo |
| 17 | +dataset_url = "https://raw.githubusercontent.com/Lexie88rus/bank-marketing-analysis/master/bank.csv" |
| 18 | + |
| 19 | +# read csv from a URL |
| 20 | +@st.cache_data |
| 21 | +def get_data() -> pd.DataFrame: |
| 22 | + return pd.read_csv(dataset_url) |
| 23 | + |
| 24 | +df = get_data() |
| 25 | + |
| 26 | +# dashboard title |
| 27 | + |
| 28 | +with st.sidebar: |
| 29 | + st.write("Demo Streamlit APP v0.0.1") |
| 30 | + |
| 31 | +st.markdown("### Deploy Apps faster with Python!") |
| 32 | +st.title("Real-Time Demo with Streamlit") |
| 33 | +camera_button_disabled = False |
| 34 | + |
| 35 | +camera_button = st.button("Take picture", disabled=camera_button_disabled ) |
| 36 | + |
| 37 | +if camera_button: |
| 38 | + camera = st.camera_input(label="Make a picture", disabled=camera_button_disabled) |
| 39 | + |
| 40 | +if camera_button: |
| 41 | + camera_button_disabled = True |
| 42 | + st.write(camera_button) |
| 43 | + if camera: |
| 44 | + st.image(camera) |
| 45 | +else: |
| 46 | + st.write(camera_button) |
| 47 | + |
| 48 | + |
| 49 | +st.divider() |
| 50 | + |
| 51 | +# top-level filters |
| 52 | +job_filter = st.selectbox("Select the Job", pd.unique(df["job"])) |
| 53 | + |
| 54 | +# creating a single-element container |
| 55 | +placeholder = st.empty() |
| 56 | + |
| 57 | +# dataframe filter |
| 58 | +df = df[df["job"] == job_filter] |
| 59 | + |
| 60 | +# near real-time / live feed simulation |
| 61 | +for seconds in range(200): |
| 62 | + |
| 63 | + df["age_new"] = df["age"] * np.random.choice(range(1, 5)) |
| 64 | + df["balance_new"] = df["balance"] * np.random.choice(range(1, 5)) |
| 65 | + |
| 66 | + # creating KPIs |
| 67 | + avg_age = np.mean(df["age_new"]) |
| 68 | + |
| 69 | + count_married = int( |
| 70 | + df[(df["marital"] == "married")]["marital"].count() |
| 71 | + + np.random.choice(range(1, 30)) |
| 72 | + ) |
| 73 | + |
| 74 | + balance = np.mean(df["balance_new"]) |
| 75 | + |
| 76 | + with placeholder.container(): |
| 77 | + |
| 78 | + # create three columns |
| 79 | + kpi1, kpi2, kpi3 = st.columns(3) |
| 80 | + |
| 81 | + # fill in those three columns with respective metrics or KPIs |
| 82 | + kpi1.metric( |
| 83 | + label="Age ⏳", |
| 84 | + value=round(avg_age), |
| 85 | + delta=round(avg_age) - 10, |
| 86 | + ) |
| 87 | + |
| 88 | + kpi2.metric( |
| 89 | + label="Married Count 💍", |
| 90 | + value=int(count_married), |
| 91 | + delta=-10 + count_married, |
| 92 | + ) |
| 93 | + |
| 94 | + kpi3.metric( |
| 95 | + label="A/C Balance $", |
| 96 | + value=f"$ {round(balance,2)} ", |
| 97 | + delta=-round(balance / count_married) * 100, |
| 98 | + ) |
| 99 | + |
| 100 | + # create two columns for charts |
| 101 | + fig_col1, fig_col2 = st.columns(2) |
| 102 | + with fig_col1: |
| 103 | + st.markdown("### First Chart") |
| 104 | + fig = px.density_heatmap( |
| 105 | + data_frame=df, y="age_new", x="marital" |
| 106 | + ) |
| 107 | + st.write(fig) |
| 108 | + |
| 109 | + with fig_col2: |
| 110 | + st.markdown("### Second Chart") |
| 111 | + fig2 = px.histogram(data_frame=df, x="age_new") |
| 112 | + st.write(fig2) |
| 113 | + |
| 114 | + st.markdown("### Detailed Data View") |
| 115 | + st.dataframe(df) |
| 116 | + time.sleep(1) |
0 commit comments