import json import os import streamlit as st from helper.data_field import get_analyst_response, data_field, get_marketplace_response import time st.set_page_config(layout="centered") def write_client_footprint(): web = get_analyst_response("Website and Tools Analyst") result_web = {item["category"]: item["current_footprint"] for item in web} seo = get_analyst_response("SEO Analyst") seo = {item["category"]: item["current_footprint"] for item in seo} socmed = get_analyst_response("Social Media Analyst") socmed = {item["category"]: item["current_footprint"] for item in socmed} markdown_table = "| Source/Channel | Current KPI |\n" markdown_table += "|---|---|\n" markdown_table += f"| Website Health Score | {result_web['website_overall_health_score']} |\n" markdown_table += f"| Organic Traffic to the Website | {seo.get('organic_traffic', 'N/A')} |\n" markdown_table += f"| Paid Traffic to the Website | {seo.get('paid_traffic', 'N/A')} |\n" markdown_table += f"| Referral Traffic to the Website | {seo.get('referral_traffic', 'N/A')} |\n" markdown_table += f"| Email Traffic to the Website | N/A |\n" markdown_table += f"| Direct Traffic to the Website | {seo.get('direct_traffic', 'N/A')} |\n" markdown_table += f"| Social Traffic to the Website | N/A |\n" markdown_table += f"| Display Traffic to the Website | N/A |\n" markdown_table += f"| Email Database | N/A |\n" markdown_table += f"| Facebook Followers | {socmed.get('facebook_followers', 'N/A')} |\n" markdown_table += f"| Twitter Followers | {socmed.get('twitter_followers', 'N/A')} |\n" markdown_table += f"| Instagram Followers | {socmed.get('instagram_followers', 'N/A')} |\n" markdown_table += f"| Linkedin Followers | {socmed.get('linkedin_followers', 'N/A')} |\n" markdown_table += f"| Google My Business | N/A |\n" markdown_table += f"| # of Keywords Ranking in Top 10 | {seo.get('keyword_ranking_in_top_10', 'N/A')} |\n" markdown_table += f"| # of Keywords Ranking in Top 100 | {seo.get('keyword_ranking_in_top_100', 'N/A')} |\n" return markdown_table def write_snapshot(data): if data: try: parsed_data = data if isinstance(parsed_data, list): # Create Markdown table header markdown_table = "| Channel | Status | Requirements | What's Needed to Deliver |\n" markdown_table += "|---|---|---|---|\n" # Loop through the list of dictionaries for item in parsed_data: # Use .get() for safety in case keys are missing channel = item.get('channel', 'N/A') status = item.get('status', 'N/A') requirements = item.get('requirements', 'N/A') deliver = item.get('deliver', 'N/A') # Add a row to the Markdown table string # Replace underscores with spaces and apply title case to category # Replace underscores with spaces first channel_temp = channel.replace('_', ' ') # Apply title case if there are multiple words, otherwise uppercase if ' ' in channel_temp: # Check for spaces directly channel_formatted = channel_temp.title() else: channel_formatted = channel_temp.upper() # Use upper() instead of upper_case() markdown_table += f"| {channel_formatted} | {status} | {requirements} | {deliver} |\n" # Display the complete Markdown table st.markdown(markdown_table) # Handle case if data is not a list (e.g., a single dictionary) elif isinstance(parsed_data, dict): st.write("Analysis Result (Summary):") # You might want to display dictionary data differently st.json(parsed_data) # Example: Display as JSON else: st.warning("data is not in the expected list format.") st.write(parsed_data) # Show the raw data except json.JSONDecodeError: st.error("Error: Could not parse the data as JSON.") st.text(data) # Show the raw string data except AttributeError: st.error("Error: Could not find expected keys ('channel', 'status', 'requirements', 'deliver') in the data.") st.write(parsed_data) # Show the data that caused the error except Exception as e: st.error(f"An unexpected error occurred while processing data: {e}") st.write(data) # Show the raw data else: st.warning("No data retrieved for analysis.") # --- End: Loop and display data --- def write_table(website_and_tools_data): if website_and_tools_data: try: parsed_data = website_and_tools_data if isinstance(parsed_data, list): # Create Markdown table header markdown_table = "| | Current Footprint | Best of Breed Solution |\n" markdown_table += "|---|---|---|\n" # Loop through the list of dictionaries for item in parsed_data: # Use .get() for safety in case keys are missing category = item.get('category', 'N/A') current_footprint = item.get('current_footprint', 'N/A') best_of_breed = item.get('best_of_breed_solution', 'N/A') # Add a row to the Markdown table string # Replace underscores with spaces and apply title case to category category_formatted = category.replace('_', ' ').title() current_footprint_formatted = current_footprint.replace('_', ' ') best_of_breed_formatted = best_of_breed.replace('_', ' ') markdown_table += f"| {category_formatted} | {current_footprint_formatted} | {best_of_breed_formatted} |\n" # Display the complete Markdown table st.markdown(markdown_table) # Handle case if data is not a list (e.g., a single dictionary) elif isinstance(parsed_data, dict): st.write("Analysis Result (Summary):") # You might want to display dictionary data differently st.json(parsed_data) # Example: Display as JSON else: st.warning("data is not in the expected list format.") st.write(parsed_data) # Show the raw data except json.JSONDecodeError: st.error("Error: Could not parse the data as JSON.") st.text(website_and_tools_data) # Show the raw string data except AttributeError: st.error("Error: Could not find expected keys ('category', 'current_footprint', 'best_of_breed_solution') in the data.") st.write(parsed_data) # Show the data that caused the error except Exception as e: st.error(f"An unexpected error occurred while processing data: {e}") st.write(website_and_tools_data) # Show the raw data else: st.warning("No data retrieved for analysis.") # --- End: Loop and display data --- def seo_on_page_table(df_data): if df_data: try: parsed_data = df_data if isinstance(parsed_data, list): # Create Markdown table header markdown_table = "| Element | Description | Remarks |\n" markdown_table += "|---|---|---|\n" # Loop through the list of dictionaries for item in parsed_data: # Use .get() for safety in case keys are missing element = item.get('elements', 'N/A') description = item.get('description', 'Static information') remarks = item.get('remarks', 'N/A') # Add a row to the Markdown table string # Replace underscores with spaces and apply title case to element element_formatted = element.replace('_', ' ').title() description_formatted = description.replace('_', ' ') remarks_formatted = remarks.replace('_', ' ') markdown_table += f"| {element_formatted} | {description_formatted} | {remarks_formatted} |\n" # Display the complete Markdown table st.markdown(markdown_table) # Handle case if data is not a list (e.g., a single dictionary) elif isinstance(parsed_data, dict): st.write("Analysis Result (Summary):") # You might want to display dictionary data differently st.json(parsed_data) # Example: Display as JSON else: st.warning("data is not in the expected list format.") st.write(parsed_data) # Show the raw data except json.JSONDecodeError: st.error("Error: Could not parse the data as JSON.") st.text(df_data) # Show the raw string data except AttributeError: st.error("Error: Could not find expected keys ('category', 'current_footprint', 'best_of_breed_solution') in the data.") st.write(parsed_data) # Show the data that caused the error except Exception as e: st.error(f"An unexpected error occurred while processing data: {e}") st.write(df_data) # Show the raw data else: st.warning("No data retrieved for analysis.") # --- End: Loop and display data --- def display_outputs(): client_name = data_field("Client Name") client_website = data_field("Client Website") overview = get_analyst_response("DF Overview Analyst") st.markdown("# Digital Marketing Audit") st.markdown(f"for: **{client_name} ({client_website})**") st.write("") st.write("") st.markdown("#### Table of Contents") st.markdown("""
""", unsafe_allow_html=True) st.markdown("---") st.write("") st.write("") st.markdown("### DIGITAL FOOTPRINT OVERVIEW") st.markdown(f"{overview}") st.markdown("---") st.markdown("### Executive Summary") st.markdown(get_analyst_response("Executive Summary")) st.markdown("---") st.markdown("### CLIENT FOOTPRINT") st.markdown(write_client_footprint()) st.markdown( """