-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
74 lines (55 loc) · 1.76 KB
/
app.py
File metadata and controls
74 lines (55 loc) · 1.76 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import streamlit as st
import snowflake.connector
import pandas as pd
import os
import sys
from dotenv import load_dotenv
from src.utils.exception import CustomException
from src.utils.logger import logging
load_dotenv()
def get_connection():
try:
logging.info("connecting to snowflake")
conn = snowflake.connector.connect(
user=os.getenv("SNOWFLAKE_USER"),
password=os.getenv("SNOWFLAKE_PASSWORD"),
account=os.getenv("SNOWFLAKE_ACCOUNT"),
warehouse=os.getenv("SNOWFLAKE_WAREHOUSE"),
database=os.getenv("SNOWFLAKE_DATABASE"),
schema=os.getenv("SNOWFLAKE_SCHEMA")
)
logging.info("connection successfully")
return conn
except Exception as e:
logging.error("Error while connecting to snowflake")
raise ValueError(e,sys)
def fetch_data(conn):
try:
logging.info("fetching data from snowflake")
query= """
select OPEN_TIME, CLOSE, VOLUME
FROM CRYPTO_PRICES
ORDER BY OPEN_TIME DESC
LIMIT 200
"""
df = pd.read_sql(query,conn)
logging.info(f"fetched{len(df)} rows")
return df
except Exception as e:
raise ValueError (e,sys)
# StreamLit UI
st.title("-- BTC Price Dashboard --")
try:
conn = get_connection()
df = fetch_data(conn)
if not df.empty:
df["OPEN_TIME"] = pd.to_datetime(df["OPEN_TIME"])
st.line_chart(df.set_index("OPEN_TIME")["CLOSE"])
st.bar_chart(df.set_index("OPEN_TIME")["VOLUME"])
logging.info("charts displayed successfully")
else:
st.warning("No Data Available")
conn.close()
logging.info("connection closed")
except Exception as e:
st.error("something went wrong")