Spaces:
Sleeping
Sleeping
Commit ·
58fa882
0
Parent(s):
added streamlit analytics for poems, connections, and voting
Browse files- .gitignore +1 -0
- app.py +145 -0
- requirements.txt +3 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.venv
|
app.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import psycopg2
|
| 4 |
+
from psycopg2 import Error
|
| 5 |
+
|
| 6 |
+
# Database connection configuration
|
| 7 |
+
DB_PARAMS = {
|
| 8 |
+
"host": "aws-0-us-east-2.pooler.supabase.com",
|
| 9 |
+
"port": "5432",
|
| 10 |
+
"database": "postgres",
|
| 11 |
+
"user": "postgres.reeiesvuumiuorjgnmad",
|
| 12 |
+
"password": "S2ef4ypnmYIWYFK4"
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
def get_db_connection():
|
| 16 |
+
try:
|
| 17 |
+
connection = psycopg2.connect(**DB_PARAMS)
|
| 18 |
+
return connection
|
| 19 |
+
except Error as e:
|
| 20 |
+
st.error(f"Error connecting to database: {e}")
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
def fetch_data():
|
| 24 |
+
conn = get_db_connection()
|
| 25 |
+
if conn is None:
|
| 26 |
+
return None, None
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
# Create a cursor object to execute PostgreSQL commands
|
| 30 |
+
cur = conn.cursor()
|
| 31 |
+
|
| 32 |
+
# Query for connections data with poem linkage
|
| 33 |
+
connections_query = """
|
| 34 |
+
SELECT c.symbol1, c.symbol2, c.reasoning, c.up_votes, c.down_votes, c.version, c.poem_id
|
| 35 |
+
FROM connections c
|
| 36 |
+
"""
|
| 37 |
+
connections_df = pd.read_sql_query(connections_query, conn)
|
| 38 |
+
|
| 39 |
+
# Query for total number of poems
|
| 40 |
+
poems_query = """
|
| 41 |
+
SELECT COUNT(*) as poem_count
|
| 42 |
+
FROM poems
|
| 43 |
+
"""
|
| 44 |
+
poems_df = pd.read_sql_query(poems_query, conn)
|
| 45 |
+
|
| 46 |
+
cur.close()
|
| 47 |
+
conn.close()
|
| 48 |
+
return connections_df, poems_df
|
| 49 |
+
|
| 50 |
+
except Error as e:
|
| 51 |
+
st.error(f"Error fetching data: {e}")
|
| 52 |
+
return None, None
|
| 53 |
+
|
| 54 |
+
def calculate_analytics(connections_df, poems_df):
|
| 55 |
+
if connections_df is None or connections_df.empty or poems_df is None:
|
| 56 |
+
return None, None, None, None
|
| 57 |
+
|
| 58 |
+
# Total number of poems from poems table
|
| 59 |
+
total_poems = poems_df['poem_count'].iloc[0]
|
| 60 |
+
|
| 61 |
+
# Total number of connections
|
| 62 |
+
total_connections = len(connections_df)
|
| 63 |
+
|
| 64 |
+
# Calculate vote ratio
|
| 65 |
+
connections_df['vote_ratio'] = connections_df.apply(
|
| 66 |
+
lambda row: row['up_votes'] / (row['up_votes'] + row['down_votes'])
|
| 67 |
+
if (row['up_votes'] + row['down_votes']) > 0 else 0,
|
| 68 |
+
axis=1
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Group by poem_id for connections per poem
|
| 72 |
+
connections_per_poem = connections_df.groupby('poem_id').agg({
|
| 73 |
+
'symbol1': 'count',
|
| 74 |
+
'up_votes': 'sum',
|
| 75 |
+
'down_votes': 'sum',
|
| 76 |
+
'vote_ratio': 'mean'
|
| 77 |
+
}).rename(columns={'symbol1': 'connection_count'})
|
| 78 |
+
|
| 79 |
+
# Group by version
|
| 80 |
+
connections_by_version = connections_df.groupby('version').agg({
|
| 81 |
+
'up_votes': 'sum',
|
| 82 |
+
'down_votes': 'sum',
|
| 83 |
+
'vote_ratio': 'mean',
|
| 84 |
+
'symbol1': 'count'
|
| 85 |
+
}).rename(columns={'symbol1': 'connections_count'})
|
| 86 |
+
|
| 87 |
+
return total_poems, total_connections, connections_per_poem, connections_by_version
|
| 88 |
+
|
| 89 |
+
def main():
|
| 90 |
+
st.title("Connections Analytics Dashboard")
|
| 91 |
+
|
| 92 |
+
# Fetch and process data
|
| 93 |
+
connections_df, poems_df = fetch_data()
|
| 94 |
+
if connections_df is None or poems_df is None:
|
| 95 |
+
st.error("Failed to load data from database")
|
| 96 |
+
return
|
| 97 |
+
|
| 98 |
+
total_poems, total_connections, connections_per_poem, connections_by_version = calculate_analytics(connections_df, poems_df)
|
| 99 |
+
|
| 100 |
+
if total_poems is None:
|
| 101 |
+
st.error("No data available to analyze")
|
| 102 |
+
return
|
| 103 |
+
|
| 104 |
+
# Display basic stats
|
| 105 |
+
st.header("Overview")
|
| 106 |
+
col1, col2 = st.columns(2)
|
| 107 |
+
with col1:
|
| 108 |
+
st.metric("Total Number of Poems", total_poems)
|
| 109 |
+
with col2:
|
| 110 |
+
st.metric("Total Number of Connections", total_connections)
|
| 111 |
+
|
| 112 |
+
# Connections per poem table
|
| 113 |
+
st.header("Connections per Poem")
|
| 114 |
+
st.dataframe(
|
| 115 |
+
connections_per_poem.reset_index(),
|
| 116 |
+
column_config={
|
| 117 |
+
"poem_id": "Poem ID",
|
| 118 |
+
"connection_count": "Number of Connections",
|
| 119 |
+
"up_votes": "Up Votes",
|
| 120 |
+
"down_votes": "Down Votes",
|
| 121 |
+
"vote_ratio": st.column_config.NumberColumn(
|
| 122 |
+
"Vote Ratio",
|
| 123 |
+
format="%.2f"
|
| 124 |
+
)
|
| 125 |
+
}
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Connections by version table
|
| 129 |
+
st.header("Connections by Version")
|
| 130 |
+
st.dataframe(
|
| 131 |
+
connections_by_version.reset_index(),
|
| 132 |
+
column_config={
|
| 133 |
+
"version": "Version",
|
| 134 |
+
"up_votes": "Up Votes",
|
| 135 |
+
"down_votes": "Down Votes",
|
| 136 |
+
"vote_ratio": st.column_config.NumberColumn(
|
| 137 |
+
"Vote Ratio",
|
| 138 |
+
format="%.2f"
|
| 139 |
+
),
|
| 140 |
+
"connections_count": "Number of Connections"
|
| 141 |
+
}
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
psycopg2-binary
|