-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
166 lines (144 loc) · 5.33 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
import json
import uuid
import streamlit as st
import pandas as pd
from st_aggrid import (
AgGrid,
GridUpdateMode,
ColumnsAutoSizeMode,
AgGridTheme,
DataReturnMode
)
from gridoptions import go, go_dump
from db_handler import write_to_db, download_data
from constants import VERSION
from guidelines import guidelines
if "RetrievalKeys" in st.session_state:
del st.session_state["RetrievalKeys"]
def filter_lst(lst):
""" Filters out whitespace or nan items """
return list(filter(lambda x: x != " " and x != "nan", lst))
st.set_page_config(layout="wide")
data_df = pd.DataFrame({
"analytical_statement":[" "],
"comp_prev_months": [" "],
"evidence": [" "]
})
data_dump_df = pd.DataFrame({
"anecdotal":[" "],
"too_old": [" "],
"redundant":[" "],
"outliers": [" "],
"not_relevant": [" "]
})
data_download_btn = st.sidebar.button("Generate Download link")
if data_download_btn:
st.sidebar.markdown(
download_data(),
unsafe_allow_html=True
)
# Guidelines
guidelines()
with st.form("summ_tagging", clear_on_submit=True):
project_options = [
"Bangladesh",
"Burkina Faso",
"Columbia",
"DRC",
"Nigeria",
"Syria",
"Ukraine"
]
project_options.sort() # sort in asc
selected_project = st.selectbox("Projects", project_options)
sectors_col, pillars_col, subpillars_col, other_tags = st.columns(4)
sectors = sectors_col.text_input("Sectors", help="Enter sectors separated by comma")
pillars = pillars_col.text_input("Pillars", help="Enter pillars separated by comma")
subpillars = subpillars_col.text_input("Subpillars", help="Enter subpillars separated by comma")
othertags = other_tags.text_input("Other Tags", help="Enter other tags separated by comma")
published_date = st.date_input("Published on")
response = AgGrid(
data_df,
gridOptions=go,
update_mode=GridUpdateMode.VALUE_CHANGED,
data_return_mode=DataReturnMode.AS_INPUT,
allow_unsafe_jscode=True,
height=400,
width="100%",
enable_enterprise_modules=True,
theme=AgGridTheme.STREAMLIT,
columns_auto_size_mode=ColumnsAutoSizeMode.FIT_ALL_COLUMNS_TO_VIEW,
wrapText=True,
key='first',
reload_data=True
)
st.subheader("Data Dump")
response_dump = AgGrid(
data_dump_df,
gridOptions=go_dump,
allow_unsafe_jscode=True,
height=400,
width="100%",
enable_enterprise_modules=True,
theme=AgGridTheme.STREAMLIT,
columns_auto_size_mode=ColumnsAutoSizeMode.FIT_ALL_COLUMNS_TO_VIEW,
wrapText=True,
key='second',
reload_data=True
)
other_info = st.text_input("Other Information", help="Provide extra details if any")
submit_btn = st.form_submit_button("Submit")
if submit_btn:
first_df = response["data"]
first_df['analytical_statement'] = first_df['analytical_statement'].replace('nan', pd.NA).ffill()
df_as_ev = first_df.groupby("analytical_statement")["evidence"].apply(list)
df_as_cpm = first_df.groupby("analytical_statement")["comp_prev_months"].apply(list)
as_ev_dict = json.loads(df_as_ev.to_json())
as_cpm_dict = json.loads(df_as_cpm.to_json())
for key, val in as_ev_dict.items():
as_ev_dict[key] = filter_lst(val)
for key, val in as_cpm_dict.items():
as_cpm_dict[key] = filter_lst(val)
summaries_lst = []
for key, val in as_ev_dict.items():
summaries_lst.append(
{
"analytical_statement": key,
"evidence": val,
"comp_with_prev_month": as_cpm_dict[key] if key in as_cpm_dict else []
}
)
# dump data
second_df = response_dump["data"]
anecdotal_lst = filter_lst(second_df["anecdotal"].tolist())
too_old_lst = filter_lst(second_df["too_old"].tolist())
redundant_lst = filter_lst(second_df["redundant"].tolist())
outliers_lst = filter_lst(second_df["outliers"].tolist())
not_relevant_lst = filter_lst(second_df["not_relevant"].tolist())
main_dict = {
"summ_key": str(uuid.uuid1()),
"project_id": selected_project,
"sectors": sectors,
"pillars": pillars,
"subpillars": subpillars,
"other_tags": othertags,
"published_on": str(published_date),
"summaries": json.dumps(summaries_lst),
"anecdotal": json.dumps(anecdotal_lst),
"too_old": json.dumps(too_old_lst),
"redundant": json.dumps(redundant_lst),
"outliers": json.dumps(outliers_lst),
"not_relevant": json.dumps(not_relevant_lst),
"other_info": other_info,
"version": VERSION
}
# Handle the database
db_response = write_to_db(main_dict)
if ("ResponseMetadata" in db_response and
"HTTPStatusCode" in db_response["ResponseMetadata"]):
if db_response["ResponseMetadata"]["HTTPStatusCode"] == 200:
st.success("Successfully updated the database.")
else:
st.error("Failed to update the database.")
else:
st.error("Failed to update the database.")