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',engine="orca") 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",engine="orca") # 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" )