itheenigma commited on
Commit
cbb8926
·
1 Parent(s): 3bd8465

renaming mcmc_dashboard file to app

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from marginal_cpc_calculator import *
4
+
5
+ st.set_page_config(
6
+ page_title="mCPC dashboard",
7
+ page_icon="socks",
8
+ layout="wide",
9
+ initial_sidebar_state="auto",
10
+ )
11
+
12
+ # dataframe operations first time
13
+ def impute_sub_channel(campaign):
14
+ if any([x in campaign for x in ['| Prospecting','| CPA','| Traffic','| CRM','Lead acquisition']]):
15
+ return 'TRAFFIC_DRIVING'
16
+ elif any([x in campaign for x in ['| Conversion','| Advantage+','| ASC+','Sales |','-Conversions']]):
17
+ return 'CONVERSION'
18
+ elif any([x in campaign for x in ['| Brand Awareness','| Reach']]):
19
+ return 'REACH'
20
+ else:
21
+ return 'OTHER'
22
+ df = pd.read_csv('meta_daily_ads_level_data/meta_ad_day_level_raw_data.csv.gz',compression='gzip',parse_dates=['Date'])
23
+ df.rename(columns={'Ad Set Name':'ad_set_name', 'Campaign Name':'campaign', 'Country':'country',
24
+ 'Date':'date', 'Amount Spent (SEK)':'cost', 'Clicks (all)':'clicks','Impressions':'impressions',
25
+ 'Purchases Conversion Value (SEK)':'platform_revenue','Ad Set Name':'ad_name',},inplace=True)
26
+ df.set_index('date',inplace=True)
27
+ df['cpc']=df['cost']/df['clicks']
28
+ df['platform']='meta'
29
+ df['sub_channel']=df.campaign.apply(impute_sub_channel)
30
+
31
+ # dashboard operations
32
+ def add_logo(logo_path, width, height):
33
+ """Read and return a resized logo"""
34
+ logo = Image.open(logo_path)
35
+ modified_logo = logo.resize((width, height))
36
+ return modified_logo
37
+
38
+ def get_date(input_min_date_month,input_min_date_year,input_max_date_month,input_max_date_year):
39
+ month_converter = {'Jun':'06','Jul':'07','Aug':'08','Sep':'09',
40
+ 'Oct':'10','Nov':'11','Dec':'12','Jan':'01','Feb':'02',
41
+ 'Mar':'03','Apr':'04','May':'05'}
42
+ min_date = input_min_date_year+'-'+month_converter[input_min_date_month]+'-01'
43
+ max_date = input_max_date_year+'-'+month_converter[input_max_date_month]+'-01'
44
+ return min_date,max_date
45
+
46
+ my_logo = add_logo(logo_path="hs_logo.png", width=180, height=60)
47
+ st.sidebar.image(my_logo)
48
+
49
+ st.title("mCPC & ad spend insights ✨")
50
+
51
+ # INLCUDE SECTION FOR INPUTS AND BUTTON TO GENERATE INSIGHTS
52
+ with st.sidebar:
53
+ st.write("Enter selections for mCPC spend profile")
54
+ input_country = st.selectbox('Country:',
55
+ ('','DE', 'US', 'UK','NL','SE','AU','CH','FR','BE'))
56
+ input_platform = st.selectbox('Platform:',
57
+ ('','meta'))
58
+ input_sub_channel = st.selectbox('Tactic / Sub channel:',
59
+ ('','CONVERSION','TRAFFIC_DRIVING','REACH'))
60
+ col1, col2 = st.columns(2)
61
+ with col1:
62
+ input_min_date_month = st.selectbox('From Month:',
63
+ ('','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May'))
64
+ with col2:
65
+ input_min_date_year = st.selectbox('From Year:',
66
+ ('','2022','2023'))
67
+ col3, col4 = st.columns(2)
68
+ with col3:
69
+ input_max_date_month = st.selectbox('To Month:',
70
+ ('','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May'))
71
+ with col4:
72
+ input_max_date_year = st.selectbox('To Year:',
73
+ ('','2022','2023'))
74
+ start = st.button("Generate mCPC curves", type='primary')
75
+ text_error_value = ''
76
+ if start:
77
+ for var,varname in [(input_country,'Country'),
78
+ (input_platform,'Platform'),
79
+ (input_min_date_month,'From Date'),
80
+ (input_min_date_year,'From Date'),
81
+ (input_max_date_month,'To Date'),
82
+ (input_max_date_year,'To Date')]:
83
+ if var=='':
84
+ text_error_value = f'{varname} cant be empty'
85
+ st.error(text_error_value)
86
+ break
87
+
88
+ # CHECKS TO ENSURE INPUTS ARE CLEAN BEFORE CALLING FUNCTIONS
89
+ if start:
90
+ if text_error_value=='':
91
+ min_date, max_date = get_date(input_min_date_month,
92
+ input_min_date_year,
93
+ input_max_date_month,
94
+ input_max_date_year)
95
+ st.markdown("""---""")
96
+ mid, mad = get_comparison_dates(min_date,max_date,period='previous_period')
97
+ st.subheader(f"Calculating for dates between {mid} & {mad}")
98
+ fig,output_msg = calculate_max_spend(df,mid,mad,
99
+ revenue_or_ebitda='revenue',
100
+ country=input_country,
101
+ platform=input_platform,
102
+ sub_channel=input_sub_channel,
103
+ pprint=True)
104
+ st.write(fig)
105
+ for l in output_msg:
106
+ st.write(l)
107
+ st.markdown("""---""")
108
+ mid, mad = get_comparison_dates(min_date,max_date,period='LY')
109
+ st.subheader(f"Calculating for dates between {mid} & {mad}")
110
+ fig,output_msg = calculate_max_spend(df,mid,mad,
111
+ revenue_or_ebitda='revenue',
112
+ country=input_country,
113
+ platform=input_platform,
114
+ sub_channel=input_sub_channel,
115
+ pprint=True)
116
+ st.write(fig)
117
+ for l in output_msg:
118
+ st.write(l)
119
+
120
+ else:
121
+ st.write(f'Text error value is {text_error_value}')
122
+ # DISPLAY INSIGHTS FOR PREVIOUS PERIOD AND LAST YEAR
123
+ # TEST FUNCTIONALITY OF APP
124
+ # DEPLOY TO HUGGINGFACE
125
+ # INCORPORATE GOOGLE
126
+ # INCORPORATE TIKTOK
127
+
128
+ st.markdown("<br><hr><center>Made with ❤️ for Happy Socks by <a href='mailto:vivekbharadwaj.ca@gmail.com?subject=mCPC dashboard queries&body=Please specify the issue you are facing with the dashboard.'><strong>Vivek</strong></a> ✨</center><hr>", unsafe_allow_html=True)