Spaces:
Runtime error
Runtime error
BInura Yasodya
commited on
Commit
·
7c51e14
1
Parent(s):
97fe81a
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from sqlalchemy import create_engine, text
|
4 |
+
|
5 |
+
# Create MySQL engine
|
6 |
+
engine = create_engine('mysql+pymysql://root:Binu1997#$@localhost/research_db')
|
7 |
+
|
8 |
+
# Function to fetch unique company names from the database
|
9 |
+
def get_company_names():
|
10 |
+
query = text("SELECT DISTINCT `Company name` FROM research_db.ads_table")
|
11 |
+
with engine.connect() as conn:
|
12 |
+
result = conn.execute(query)
|
13 |
+
companies = [row[0] for row in result]
|
14 |
+
return companies
|
15 |
+
|
16 |
+
# Function to fetch ads data for a specific company
|
17 |
+
def get_ads_data(company_name):
|
18 |
+
query = text(f"SELECT * FROM research_db.ads_table WHERE `Company name` = :company_name")
|
19 |
+
with engine.connect() as conn:
|
20 |
+
df = pd.read_sql(query, conn, params={"company_name": company_name})
|
21 |
+
return df
|
22 |
+
|
23 |
+
# Load custom CSS
|
24 |
+
def load_css():
|
25 |
+
with open("styles.css") as f:
|
26 |
+
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
27 |
+
|
28 |
+
# Load custom CSS
|
29 |
+
load_css()
|
30 |
+
|
31 |
+
# Main title
|
32 |
+
st.markdown('<p class="main-title">Facebook AD Search</p>', unsafe_allow_html=True)
|
33 |
+
|
34 |
+
# Sidebar for company selection
|
35 |
+
st.sidebar.markdown('<p class="company-select">Select Telco Company</p>', unsafe_allow_html=True)
|
36 |
+
company_names = get_company_names()
|
37 |
+
selected_company = st.sidebar.selectbox("", company_names)
|
38 |
+
|
39 |
+
# Display selected company ads data
|
40 |
+
if selected_company:
|
41 |
+
ads_data = get_ads_data(selected_company)
|
42 |
+
st.markdown(f'<p class="heading">Displaying ads for: {selected_company}</p>', unsafe_allow_html=True)
|
43 |
+
|
44 |
+
if not ads_data.empty:
|
45 |
+
# Multiselect widget for column selection
|
46 |
+
columns = ads_data.columns.tolist()
|
47 |
+
selected_columns = st.sidebar.multiselect('Select columns to display:', columns, default=columns)
|
48 |
+
|
49 |
+
if selected_columns:
|
50 |
+
filtered_data = ads_data[selected_columns]
|
51 |
+
st.markdown('<div class="dataframe-style">', unsafe_allow_html=True)
|
52 |
+
st.write(filtered_data)
|
53 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
54 |
+
|
55 |
+
# Display summary or statistics
|
56 |
+
st.markdown('<p class="heading">Summary</p>', unsafe_allow_html=True)
|
57 |
+
st.markdown(f'<p class="summary-text">Total ads found: {len(ads_data)}</p>', unsafe_allow_html=True)
|
58 |
+
else:
|
59 |
+
st.markdown('<p class="summary-text">Please select at least one column to display.</p>', unsafe_allow_html=True)
|
60 |
+
else:
|
61 |
+
st.markdown('<p class="summary-text">No ads found for the selected company.</p>', unsafe_allow_html=True)
|
62 |
+
else:
|
63 |
+
st.markdown('<p class="summary-text">Please select a company from the sidebar to view ads.</p>', unsafe_allow_html=True)
|