atmiLLM commited on
Commit
bc7afbc
·
verified ·
1 Parent(s): 93658e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
app.py CHANGED
@@ -4,27 +4,28 @@ import psycopg2
4
  import pandas as pd
5
 
6
 
7
- import streamlit as st
8
- import psycopg2
9
-
10
- # Initialize connection.
11
- # Uses st.experimental_singleton to only run once.
12
- @st.experimental_singleton
13
- def init_connection():
14
- return psycopg2.connect(**st.secrets["postgres"])
15
-
16
- conn = init_connection()
 
 
 
 
 
 
17
 
18
- # Perform query.
19
- # Uses st.experimental_memo to only rerun when the query changes or after 10 min.
20
- @st.experimental_memo(ttl=600)
21
- def run_query(query):
22
- with conn.cursor() as cur:
23
- cur.execute(query)
24
- return cur.fetchall()
25
 
26
- rows = run_query("SELECT * from CARS;")
 
27
 
28
- # Print results.
29
- for row in rows:
30
- st.write(f"{row[0]} has a :{row[1]}:")
 
4
  import pandas as pd
5
 
6
 
7
+ # Function to get data from the TimescaleDB
8
+ def get_data():
9
+ # Connect to your PostgreSQL database
10
+ conn = psycopg2.connect(
11
+ dbname="postgres",
12
+ user="postgres",
13
+ password="database",
14
+ host="localhost",
15
+ port="5432"
16
+ )
17
+ cur = conn.cursor()
18
+ cur.execute("SELECT BRAND, MODEL ,LAUNCH_YR FROM CARS LIMIT 100;")
19
+ data = cur.fetchall()
20
+ cur.close()
21
+ conn.close()
22
+ return data
23
 
24
+ # Streamlit application
25
+ st.title('Cars-Data')
 
 
 
 
 
26
 
27
+ data = get_data()
28
+ df = pd.DataFrame(data, columns=['BRAND', 'MODEL','LAUNCH_YR'])
29
 
30
+ st.write("## Latest 10 Records from cars")
31
+ st.dataframe(df)