forked from ghsc-psm/Dynamic-Optimization-Routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
184 lines (155 loc) · 8.43 KB
/
app.py
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
import streamlit as st
from PIL import Image
from pathlib import Path
st.set_page_config(layout="wide", initial_sidebar_state='auto')
import SessionState
import traceback
import pandas as pd
from datetime import datetime
import random
import string
import configparser
from app_upload import app_upload
from app_refine import app_refine
from app_review import app_review
from app_optimize import app_optimize
from app_login import check_credentials
from app_admin import app_update_reference_data
from app_drorders import app_drorders
from app_release_notes import app_release_notes
from scenario import create_download_dict, save_scenario
from utils import get_table_download_link_xlsx, get_email_agent, get_binary_file_downloader_html
from psm.email_utility import *
def app_main():
"""Allow user to navigate to each page of the application."""
country_config = configparser.ConfigParser()
country_config.read("./country_cfg.toml")
country_name = "COUNTRY" if country_config.get("country", "country_name").title() =="" else country_config.get("country", "country_name").title()
session_state = SessionState.get(ref_file = "",
warehouse = "",
user_file = "",
scenario_file = "",
scenario_data = None,
scenario_ver_no = 0,
username = "",
user_type = "",
country = country_name,
calc_master_file = "",
calc_order_file = "",
calc_solution = [],
calc_buffer = 0,
data_file='',
calc_type_sel = [],
dro_order_details_file = "",
dro_facility_mapping_file = "",
dro_order_evaluation_template_file = "",
dro_scenario_key = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6)),
dro_order_file ="",
ver = "v1.4.7.8 (2022/01/26)",
)
banner_path = './images/banner.jpg'
tool_title = "Dynamic Routing Tool"
if session_state.country != "":
if country_config.get("country", "tool_title").title() != "":
tool_title = country_config.get("country", "tool_title").title()
else:
tool_title = f'{session_state.country.capitalize()} Routing Tool'
try:
banner_path = [str(f) for f in Path("images").rglob(f'{session_state.country}*')][0]
except:
pass
st.sidebar.title(tool_title)
st.sidebar.markdown(f"###### {session_state.ver}")
if session_state.username != "":
logout = st.sidebar.button("Logout")
if logout:
session_state.ref_file = ""
session_state.warehouse = ""
session_state.user_file = ""
session_state.scenario_file = ""
session_state.scenario_data = None
session_state.scenario_ver_no = 0
session_state.username = ""
session_state.user_type = ""
session_state.country = ""
session_state.calc_order_file = ""
session_state.calc_solution = []
session_state.calc_buffer = 0
session_state.data_file=''
session_state.calc_type_sel = []
session_state.dro_order_file =""
st.experimental_rerun()
st.image(Image.open(banner_path), use_column_width=True)
st.markdown('<style>' + open('icons.css').read() + '</style>', unsafe_allow_html=True)
warning_scenario_message = "Please initialize scenario in **Upload data**"
warning_solve_message = "Please solve scenario to review results in **Solve scenario** "
# facility group problems
# add an index into the distance and time (for both reference and when reading and writing data)
# will have to change the way I'm subsetting data and stuff
# if session_state.username == "":
# check_credentials(session_state)
# else:
st.sidebar.markdown(f'### Hello, {"Admin" if session_state.user_type == "admin" else session_state.username}')
app_options = ["Order Evaluation", "Dispatch Optimization", "Release Notes"]
show_app_options = [f for f in app_options if country_config.get('drt_functionality', f).title() == 'True']
if session_state.user_type == "admin":
show_app_options = ["Tool Administration"] + app_options
app_mode = st.sidebar.selectbox("", show_app_options)
title_column, filename_column, space = st.columns([3.8, 1, 0.2])
title_column.title(app_mode)
if app_mode == "Dispatch Optimization":
if session_state.scenario_data:
routing_options = ["Upload data", "Refine data", "Solve scenario", "Review results"]
else:
routing_options = ["Upload data"]
routing_mode = st.sidebar.radio("", routing_options)
# Populate scenario info
if session_state.scenario_data is not None:
# st.sidebar.markdown(f"### Scenario {session_state.scenario_data['Scenario']}")
filename_column.markdown(f"### Scenario {session_state.scenario_data['Scenario']}")
download_scenario = filename_column.button("Download scenario")
if download_scenario:
filename_column.markdown(get_binary_file_downloader_html(session_state.scenario_file, f"Scenario {session_state.scenario_data['Scenario']}"), unsafe_allow_html=True)
# download_df_dict = create_download_dict(session_state.scenario_data)
# filename = session_state.scenario_data["Scenario"]
# filename_column.markdown(get_table_download_link_xlsx("scenario", f"Scenario {filename}", **dict(download_df_dict)), unsafe_allow_html=True)
# Due to email policy change, disable this functionality for now.
# email_scenario = filename_column.button("Email scenario")
# if email_scenario:
# agent = get_email_agent()
# subject = f'{session_state.country.capitalize()} Routing tool: Scenario file {session_state.scenario_data["Scenario"]}'
# body = "Please find the scenario file attached"
# save_scenario(session_state.scenario_file, session_state.scenario_data)
# agent.send(session_state.username, subject, body, "", msg_type="html", attach_file=session_state.scenario_file)
# send_date = datetime.now().strftime('%m/%d/%Y %H:%M')
# filename_column.markdown(f"###### Sent: {send_date}")
try:
if routing_mode == "Upload data":
app_upload(session_state)
elif routing_mode == "Refine data":
if session_state.scenario_data is None:
st.markdown(f"### {warning_scenario_message}")
else:
app_refine(session_state)
elif routing_mode == "Solve scenario":
if session_state.scenario_data is None:
st.markdown(f"### {warning_scenario_message}")
else:
app_optimize(session_state)
elif routing_mode == "Review results":
if session_state.scenario_data is None:
st.markdown(f"### {warning_scenario_message}")
elif pd.isna(session_state.scenario_data['Solved']):
st.markdown(f"### {warning_solve_message}")
else:
app_review(session_state)
except Exception as e:
st.error(f"Application Error: {e}\n{traceback.format_exc()}")
elif app_mode == "Tool Administration":
app_update_reference_data(session_state)
elif app_mode == "Order Evaluation":
app_drorders(session_state)
elif app_mode == "Release Notes":
app_release_notes(session_state)
if __name__ == "__main__":
app_main()