deepanshudey commited on
Commit
91ede3b
1 Parent(s): 3fb2465

Add application file

Browse files
Files changed (2) hide show
  1. app.py +92 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This page allows the user to view a list of members
2
+ # By default, the output will be a streamlit rendered pandas dataframe
3
+ # But there is also an option to render the output in raw HTML so that
4
+ # the member's profile URL can be visited by clicking on the
5
+ # "Profile" hyperlink that is only available in the raw HTML version.
6
+
7
+ import psycopg2
8
+ import os
9
+ import pandas as pd
10
+ import streamlit as st
11
+
12
+ # Obtain username/password saved as environment variables
13
+ user = 'project'
14
+ pwd = 'project'
15
+
16
+ # @st.cache prevents the streamlit app from executing redundant processes
17
+ # repeatedly that are expensive such as database connections or reading input
18
+ @st.cache(allow_output_mutation=True)
19
+ def get_query_results():
20
+ """ A function that returns a table of members.
21
+ Until streamlit provides a way to pageinate results,
22
+ maximum of 1000 rows will be returned.
23
+ Output can be either a streamlit rendered dataframe
24
+ or raw HTML version. Raw HTML version will provide
25
+ a hyperlink that will take you directly to the person's
26
+ company profile page. This can be used to double-check
27
+ that the profile URL has been correctly generated.
28
+ """
29
+
30
+ # Connect to the PostgreSQL database server
31
+ with psycopg2.connect(host='127.0.0.1',
32
+ port='5433',
33
+ database='project',
34
+ user=user,
35
+ password=pwd) as conn:
36
+
37
+ sql = """
38
+ SELECT
39
+ *
40
+ FROM
41
+ public.basic_member_info
42
+ ORDER BY
43
+ last_name
44
+ LIMIT 1000
45
+ """
46
+
47
+ # Execute query and return results as a pandas dataframe
48
+ df = pd.read_sql(sql, conn, index_col=None)
49
+
50
+ # Define a function to create a "Profile" hyperlink
51
+ def createProfileHref(url: str):
52
+ """ Function to create a new column that converts URL as HTML hyperlink """
53
+
54
+ value = '<a href="' + url + '"' + "/>Profile</a>"
55
+
56
+ return value
57
+
58
+ # Apply the function we created above and create our new hyperlink column
59
+ df['profile_href'] = df['web_url'].apply(createProfileHref)
60
+
61
+ # Change order of dataframe columns
62
+ df = df[['profile_href', 'first_name', 'last_name', 'web_id',
63
+ 'web_url', 'is_ahm']]
64
+
65
+ return df
66
+ ##
67
+ def write():
68
+ """ Writes content to the app """
69
+
70
+ st.title("Get Members Data from PostgreSQL")
71
+
72
+ # Check to see if checkbox was checked or not (boolean) and will be used to
73
+ # determine if the output should be a streamlit dataframe or raw HTML.
74
+ html = st.checkbox(
75
+ 'OPTIONAL: Render output as raw html to access the \"Profile\" hyperlink. ' +
76
+ 'Otherwise, just click on Execute botton.',
77
+ False)
78
+
79
+ # Define what happens when user clicks on the "Execute" button
80
+ if st.button("Execute"):
81
+ '''
82
+ ### Query results:
83
+ '''
84
+ if html:
85
+ # Render or display the raw HTML version of the dataframe
86
+ st.write(get_query_results().to_html(escape=False, index=False), unsafe_allow_html=True)
87
+ else:
88
+ # Render or display the streamlit dataframe
89
+ st.dataframe(get_query_results())
90
+
91
+ if __name__ == "__main__":
92
+ write()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ psycopg2
2
+ streamlit
3
+ pandas
4
+ requests
5
+ huggingface_hub
6
+ torch==1.9.0
7
+ scikit-learn==0.24.2
8
+ seaborn==0.11.2
9
+ numpy==1.21.2