Skip to content

Commit

Permalink
abhishek-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek-Khanna24 committed Dec 4, 2020
1 parent 9548b2f commit 1f763cb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
66 changes: 65 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
import pandas as pd
import streamlit as st
import psycopg2
from configparser import ConfigParser

##import plotly.express as px
##import SessionState
userInfo={}
head=st.title("")

def get_config(filename='database.ini', section='postgresql'):
parser = ConfigParser()
parser.read(filename)
return {k: v for k, v in parser.items(section)}

@st.cache
def query_db(sql: str):
# print(f'Running query_db(): {sql}')
try:
db_info = get_config()

# Connect to an existing database
conn = psycopg2.connect(**db_info)
except:
conn = createConnection()
# Open a cursor to perform database operations
cur = conn.cursor()

# Execute a command: this creates a new table
cur.execute(sql)

# Obtain data
data = cur.fetchall()

column_names = [desc[0] for desc in cur.description]

# Make the changes to the database persistent
conn.commit()

# Close communication with the database
# cur.close()
#conn.close()

df = pd.DataFrame(data=data, columns=column_names)

return df

@st.cache
def load_tickets(user):
##todo: read data for a given user.
Expand Down Expand Up @@ -36,7 +75,7 @@ def auth(id,password,domain):
cursor.execute("ROLLBACK")
con.commit()
st.write("Error Occured",error)
return -1;
return -1


@st.cache(allow_output_mutation=True)
Expand Down Expand Up @@ -71,3 +110,28 @@ def createConnection():

else:
st.write("Please check your Credentials!")


'## Read tables'

sql_all_table_names = "select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';"
all_table_names = query_db(sql_all_table_names)['relname'].tolist()
table_name = st.selectbox('Choose a table', all_table_names)
if table_name:
f'Display the table'

sql_table = f'select * from {table_name};'
df = query_db(sql_table)
st.dataframe(df)


'## Query management_system'

sql_customer_names = 'select name from management_system;'
customer_names = query_db(sql_customer_names)['name'].tolist()
customer_name = st.selectbox('Choose a customer', customer_names)
if customer_name:
sql_customer = f"select * from customers where name = '{customer_name}';"
customer_info = query_db(sql_customer).loc[0]
c_age, c_city, c_state = customer_info['age'], customer_info['city'], customer_info['state']
st.write(f"{customer_name} is {c_age}-year old, and lives in {customer_info['city']}, {customer_info['state']}.")
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pandas==1.1.4
streamlit==0.71.0
psycopg2==2.8.6
ConfigParser

0 comments on commit 1f763cb

Please sign in to comment.