Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
from sqlalchemy import create_engine, text | |
# Create MySQL engine | |
engine = create_engine('mysql+pymysql://root:Binu1997#$@localhost/research_db') | |
# Function to fetch unique company names from the database | |
def get_company_names(): | |
query = text("SELECT DISTINCT `Company name` FROM research_db.ads_table") | |
with engine.connect() as conn: | |
result = conn.execute(query) | |
companies = [row[0] for row in result] | |
return companies | |
# Function to fetch ads data for a specific company | |
def get_ads_data(company_name): | |
query = text(f"SELECT * FROM research_db.ads_table WHERE `Company name` = :company_name") | |
with engine.connect() as conn: | |
df = pd.read_sql(query, conn, params={"company_name": company_name}) | |
return df | |
# Load custom CSS | |
def load_css(): | |
with open("styles.css") as f: | |
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True) | |
# Load custom CSS | |
load_css() | |
# Main title | |
st.markdown('<p class="main-title">Facebook AD Search</p>', unsafe_allow_html=True) | |
# Sidebar for company selection | |
st.sidebar.markdown('<p class="company-select">Select Telco Company</p>', unsafe_allow_html=True) | |
company_names = get_company_names() | |
selected_company = st.sidebar.selectbox("", company_names) | |
# Display selected company ads data | |
if selected_company: | |
ads_data = get_ads_data(selected_company) | |
st.markdown(f'<p class="heading">Displaying ads for: {selected_company}</p>', unsafe_allow_html=True) | |
if not ads_data.empty: | |
# Multiselect widget for column selection | |
columns = ads_data.columns.tolist() | |
selected_columns = st.sidebar.multiselect('Select columns to display:', columns, default=columns) | |
if selected_columns: | |
filtered_data = ads_data[selected_columns] | |
st.markdown('<div class="dataframe-style">', unsafe_allow_html=True) | |
st.write(filtered_data) | |
st.markdown('</div>', unsafe_allow_html=True) | |
# Display summary or statistics | |
st.markdown('<p class="heading">Summary</p>', unsafe_allow_html=True) | |
st.markdown(f'<p class="summary-text">Total ads found: {len(ads_data)}</p>', unsafe_allow_html=True) | |
else: | |
st.markdown('<p class="summary-text">Please select at least one column to display.</p>', unsafe_allow_html=True) | |
else: | |
st.markdown('<p class="summary-text">No ads found for the selected company.</p>', unsafe_allow_html=True) | |
else: | |
st.markdown('<p class="summary-text">Please select a company from the sidebar to view ads.</p>', unsafe_allow_html=True) | |