-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.py
42 lines (34 loc) · 953 Bytes
/
dashboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import streamlit as st
import pandas as pd
import pymysql
from dotenv import load_dotenv
import os
from urllib.parse import urlparse
load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL")
API_KEY = os.getenv("API_KEY")
if not DATABASE_URL:
raise ValueError("DATABASE_URL environment variable is not set")
def get_db_config(url):
parsed = urlparse(url)
return {
'host': parsed.hostname,
'user': parsed.username,
'password': parsed.password,
'database': parsed.path[1:],
'port': parsed.port or 3306
}
def get_data():
db_config = get_db_config(DATABASE_URL)
conn = pymysql.connect(**db_config)
try:
df = pd.read_sql('SELECT * FROM data', conn)
return df
finally:
conn.close()
st.title("Real-Time Analytics Dashboard")
data = get_data()
if not data.empty:
st.line_chart(data.set_index('timestamp')['value'])
else:
st.warning("No data available")