# external libraries import streamlit as st import pandas as pd import os import datetime from src.utils.helper_functions import send_mail import pipeline from config import Config config = vars(Config) def main(): # variables if "state" not in st.session_state: st.session_state["state"] = True st.session_state["predictions_df"] = pd.DataFrame() st.session_state["send_mail"] = False st.session_state["text_input"] = '' st.session_state["input_date"] = None st.set_page_config( layout="centered", # Can be "centered" or "wide". In the future also "dashboard", etc. initial_sidebar_state="auto", # Can be "auto", "expanded", "collapsed" page_title=config['MAIN_TITLE'], # String or None. Strings get appended with "• Streamlit". page_icon=config['ICON_PATH'], # String, anything supported by st.image, or None. ) col1, col2, col3 = st.columns(3) col1.write(' ') col2.image(config['ICON_PATH']) col3.write(' ') st.markdown(f"

{config['MAIN_TITLE']}

", unsafe_allow_html=True) st.markdown(f"

{config['SUB_TITLE']}

", unsafe_allow_html=True) if st.session_state["state"]: dates = get_forecasting_horizon(config['FORECAST_START_DATE'],config['FORECAST_END_DATE']) col1, col2= st.columns([100,1]) input_date = col1.slider( "Forecast Interval", dates[0].date(), dates[-1].date(), value=(dates[0].date(),dates[0].date() + datetime.timedelta(weeks=12)), step=datetime.timedelta(weeks=4)) col1, col2, col3 , col4, col5 = st.columns(5) with col3 : center_button = st.button(config['FORECAST_BUTTON_TEXT'], on_click=predict, args=(input_date,)) if not st.session_state["predictions_df"].empty: pid = st.selectbox(config['LINE_PLOT_SELECTBOX_TEXT'],st.session_state["predictions_df"]['product_id'].unique()) st.table(st.session_state["predictions_df"].loc[st.session_state["predictions_df"]['product_id'] == pid,['product_id','date','demand']][:10]) st.write(f"Monthly demand for the product {pid}") st.line_chart(st.session_state["predictions_df"].loc[st.session_state["predictions_df"]['product_id'] == pid,:], x="date", y="demand", use_container_width=True) category = st.selectbox(config['BAR_PLOT_SELECTBOX_TEXT'],['product_application', 'product_marketing_name', 'product_main_family', 'planning_method_latest']) st.write(f'Average demand by category "{category}"') st.bar_chart(st.session_state["predictions_df"].groupby(category)['demand'].mean()) do_mail_sending = st.checkbox('Send mail') if do_mail_sending: # Display a text area if the checkbox is clicked st.session_state["send_mail"] = do_mail_sending st.session_state["text_input"] = st.text_input('Mail address:') input_save = st.checkbox(config['SAVE_CHECKBOX_TEXT']) confirm_params = { 'input_save':input_save, 'send_mail':do_mail_sending, 'email_adress': st.session_state["text_input"], 'forecast_start_date':st.session_state["input_date"][0].strftime("%Y/%m/%d"), 'forecast_end_date':st.session_state["input_date"][1].strftime("%Y/%m/%d") } confirm = st.button(config['SAVE_BUTTON_TEXT'], on_click=save, args=(confirm_params,)) if confirm: st.success(config['SAVE_BUTTON_SUCCESS_TEXT']) def get_forecasting_horizon(start_date, end_date): horizon = pd.date_range(start_date, end_date, freq='MS') return horizon # saving results def save(params): dir = 'demand_predictions' if not os.path.exists(f'{dir}'): os.makedirs(f'{dir}') if params['input_save']: today = datetime.datetime.today().strftime("%d-%m-%Y") st.session_state["predictions_df"][['product_id','date','demand']].to_excel(f'{dir}/predictions_{today}.xlsx', index=False) if params['send_mail']: status = send_mail( name='Alper Temel', email=params['email_adress'], subject='Forecast Results', message='Hi Captain', dataframe=st.session_state["predictions_df"].loc[:, ['product_id','date','demand']], forecast_start_date=params['forecast_start_date'], forecast_end_date=params['forecast_end_date'], toMail=params['email_adress'] ) if not status: # not successfull return False # forecasting def predict(input_date): st.session_state["input_date"] = input_date forecast_start_date = input_date[0].strftime("%Y-%m-%d") forecast_end_date = input_date[1].strftime("%Y-%m-%d") st.session_state["predictions_df"] = pipeline.run(forecast_start_date, forecast_end_date) if __name__ == "__main__": main()