File size: 5,856 Bytes
cde80d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
import Streamlit_functions as sf
import response_curves_model_quality_base as rc1
st.set_page_config(
    layout="wide"
)   
from pptx import Presentation
from pptx.util import Inches
from io import BytesIO
import plotly.io as pio
import Streamlit_functions as sf
import response_curves_model_quality_base as rc1


def save_ppt_file():

    # Initialize PowerPoint presentation
    prs = Presentation()
    # Helper function to add Plotly figure to slide
    def add_plotly_chart_to_slide(slide, fig, left, top, width, height):
        img_stream = BytesIO()
        pio.write_image(fig, img_stream, format='png')
        slide.shapes.add_picture(img_stream, left, top, width, height)

    # Slide 1: Model Quality with Chart
    slide_1 = prs.slides.add_slide(prs.slide_layouts[5])
    title_1 = slide_1.shapes.title
    title_1.text = "Model Quality"
    i = 0 
    # # print (i)
    # Generate Plotly chart
    fig = sf.mmm_model_quality()

    # Add the Plotly chart to the slide
    add_plotly_chart_to_slide(slide_1, fig, Inches(1), Inches(2), width=Inches(9), height=Inches(4.5))
    i = i+1
    # # print (i)
    # Slide 2: Media Data Elasticity
    slide_2 = prs.slides.add_slide(prs.slide_layouts[5])
    title_2 = slide_2.shapes.title
    title_2.text = "Media Data Elasticity"
    i = i+1
    # # print (i)
    # Generate Elasticity chart
    media_df = sf.media_data()
    fig = sf.elasticity(media_df)
    fig.update_layout(
        margin=dict(l=150, r=50, t=50, b=50),  # Adjust margins
        # xaxis=dict(tickangle=-45)  # Rotate x-axis labels if needed
    )
    i = i+1
    # # print (i)
    # Add the Plotly chart to the slide
    add_plotly_chart_to_slide(slide_2, fig, Inches(1), Inches(2), width=Inches(8), height=Inches(4.5))
    i = i+1
    # # print (i)
    # Slide 3: Half-Life Analysis
    slide_3 = prs.slides.add_slide(prs.slide_layouts[5])
    title_3 = slide_3.shapes.title
    title_3.text = "Half-Life Analysis"
    i = i+1
    # # print (i)
    # Generate Half-Life chart
    fig = sf.half_life(media_df)
    fig.update_layout(
        margin=dict(l=150, r=100, t=50, b=50),  # Adjust margins
        # xaxis=dict(tickangle=-45)  # Rotate x-axis labels if needed
    )
    i = i+1
    # # print (i)
    # Add the Plotly chart to the slide
    add_plotly_chart_to_slide(slide_3, fig, Inches(1), Inches(2), width=Inches(8), height=Inches(4.5))
    i = i+1
    # # print (i)
    # Slide 4: Response Curves

    # Generate Response Curves chart
    channels = [
        'Broadcast TV',
        'Cable TV',
        'Connected & OTT TV',
        'Display Prospecting',
        'Display Retargeting',
        'Video',
        'Social Prospecting',
        'Social Retargeting',
        'Search Brand',
        'Search Non-brand',
        'Digital Partners',
        'Audio',
        'Email']
    i = 4
    for channel_name in channels:
        slide_4 = prs.slides.add_slide(prs.slide_layouts[5])
        title_4 = slide_4.shapes.title
        title_4.text = "Response Curves"
        i = i+1
        # # print (i)
        selected_option = channel_name 
        selected_option2 = 'View Line Plot'
        fig = rc1.response_curves(selected_option, selected_option2)
        # Add the Plotly chart to the slide
        add_plotly_chart_to_slide(slide_4, fig, Inches(1), Inches(2), width=Inches(6), height=Inches(4.5))
    # Save the PowerPoint presentation
    # prs.save('MMM_Model_Quality_Presentation.pptx')
    # # # print("PowerPoint slides created successfully.")

    # Save to a BytesIO object
    ppt_stream = BytesIO()
    prs.save(ppt_stream)
    ppt_stream.seek(0)
    
    return ppt_stream.getvalue()




st.header("Model Quality")
# st.write("MMM Model Quality")

st.plotly_chart(sf.mmm_model_quality(),use_container_width=True)
fig = sf.mmm_model_quality()
# # print("aaa")
# fig.write_image("chart.png")
# # print("bbb")

media_df = sf.media_data()
    # Create two columns for start date and end date input
col1, col2 , col3 = st.columns([1,0.2,1])
df1 = sf.model_metrics_table_func()
st.dataframe(df1,hide_index  = True,use_container_width=True) 

# st.plotly_chart(sf.elasticity_and_media(media_df))
with col1:
    st.plotly_chart(sf.elasticity(media_df))
    fig = sf.elasticity(media_df)
    # fig.write_image("chart.png",engine="orca")
with col2:
    st.write("")
with col3:
    st.plotly_chart(sf.half_life(media_df))
    fig = sf.elasticity(media_df)
    # fig.write_image("chart.png",engine="orca")
    

# Dropdown menu options
options = [
    'Broadcast TV',
    'Cable TV',
    'Connected & OTT TV',
    'Display Prospecting',
    'Display Retargeting',
    'Video',
    'Social Prospecting',
    'Social Retargeting',
    'Search Brand',
    'Search Non-brand',
    'Digital Partners',
    'Audio',
    'Email']
options1 = [
    'View Line Plot',
    'View Scattered Plot',
    "View Both"]
col1, col2 = st.columns(2)
     # Create a dropdown menu   
with col1:
    selected_option = st.selectbox('Select A Media Channel:', options)
    selected_option2 = st.selectbox('Select A Chart Type', options1)
         # Display the selected option
    
with col2:
    st.write("")
st.plotly_chart(rc1.response_curves(selected_option,selected_option2))  

if st.button("Prepare Analysis Download"):
    ppt_file =  save_ppt_file()
        # Add a download button
    st.download_button(
            label="Download Analysis",
            data=ppt_file,
            file_name="MMM_Model_Quality_Presentation.pptx",
            mime="application/vnd.openxmlformats-officedocument.presentationml.presentation"
        )