Chinar commited on
Commit
f5e14cc
1 Parent(s): 968eecb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +297 -0
app.py ADDED
@@ -0,0 +1,297 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import streamlit_option_menu as som
3
+ import plotly.graph_objects as go
4
+ import pandas as pd
5
+ import csv
6
+ import json
7
+
8
+ st.set_page_config(page_title="MUP", page_icon="bar-chart", layout = "wide")
9
+
10
+ #hiding default elememnts
11
+ hide_meu = """<style> #MainMenu {visibility: hidden;}
12
+ footer {visibility: hidden;} </style>"""
13
+ st.markdown(hide_meu, unsafe_allow_html=True)
14
+
15
+ main_bar_selected = som.option_menu(None, ["Home", "University View", "Institutional Comaprison"], icons = ["house-fill", "building", "book-fill"], orientation = "horizontal")
16
+ st.write("####")
17
+
18
+ def display_home_page():
19
+ st.markdown('<h1 style="text-align: center; color: black; font: serif">MUP: Measuring University Performance</h1>', unsafe_allow_html=True)
20
+ st.write("##")
21
+
22
+ col1, col2 = st.columns([2, 1])
23
+
24
+ with col1:
25
+ st.markdown('<h2 style=" color: black;">Blazing fast university analytics at your fingertips</h2>', unsafe_allow_html=True)
26
+ st.markdown('<h3 style=" color: black;">Administrators looking for peer institution data?</h3>', unsafe_allow_html=True)
27
+ st.markdown('<h3 style=" color: black;">Students finding the right institution for your research career?</h3>', unsafe_allow_html=True)
28
+ st.markdown('<h3 style=" color: black;">Researchers looking for the right university for your careers?</h3>', unsafe_allow_html=True)
29
+ st.markdown('<h2 style=" color: black;">You have come to the right place!</h2>', unsafe_allow_html=True)
30
+
31
+ with col2:
32
+ st.image('data/chart_image.jpeg')
33
+
34
+ def display_uni_view_page():
35
+
36
+ #code to filter by type and select the institutions
37
+ col1, col2 = st.columns(2)
38
+ with col1:
39
+ type_select = st.radio("Filter by Institution Type", ["All", "Private", "Public"], horizontal=True)
40
+ with col2:
41
+ view_type = st.radio("View Type", ["Latest Stats", "Chart View"], horizontal=True)
42
+
43
+ institution_list = []
44
+ if type_select == "Private":
45
+ with open("data/private_institution_list.csv", "r") as f:
46
+ reader = csv.reader(f)
47
+ for row in reader:
48
+ institution_list.append(row[0])
49
+ elif type_select == "Public":
50
+ with open("data/public_institution_list.csv", "r") as f:
51
+ reader = csv.reader(f)
52
+ for row in reader:
53
+ institution_list.append(row[0])
54
+ else:
55
+ with open("data/institution_list.csv", "r") as f:
56
+ reader = csv.reader(f)
57
+ for row in reader:
58
+ institution_list.append(row[0])
59
+
60
+ institution_select = st.selectbox("Select colleges to view", options = institution_list)
61
+ st.write("##")
62
+
63
+ def load_data(input_file_path, institution_name):
64
+ data = pd.read_excel(input_file_path)
65
+ data = data[data["Institution"] == institution_name]
66
+ return data
67
+
68
+ aamc = load_data("data/aamc.xlsx", institution_select)
69
+ doctorates = load_data("data/doctorates.xlsx", institution_select)
70
+ endowment = load_data("data/endowment.xlsx", institution_select)
71
+ faculty_awards = load_data("data/faculty_awards.xlsx", institution_select)
72
+ federal_research = load_data("data/federal_research.xlsx", institution_select)
73
+ giving = load_data("data/giving.xlsx", institution_select)
74
+ headcount = load_data("data/headcount.xlsx", institution_select)
75
+ national_academy = load_data("data/national_academy.xlsx", institution_select)
76
+ non_federal_research = load_data("data/non_federal_research.xlsx", institution_select)
77
+ postdocs = load_data("data/postdocs.xlsx", institution_select)
78
+ rnd_federal = load_data("data/rnd_by_discipline_federal.xlsx", institution_select)
79
+ rnd_total = load_data("data/rnd_by_discipline_total.xlsx", institution_select)
80
+ total_research = load_data("data/total_research.xlsx", institution_select)
81
+
82
+ def latest_stats(institution_select):
83
+ display_dict = {}
84
+ display_dict['Medical Research Spending (in USD)']= str(int(aamc['2018']) / 1000000) + " Million"
85
+ display_dict["PhD's graduated"]= int(doctorates['2018'])
86
+ display_dict["Endowment (in USD)"]= str(int(endowment['2018']) / 1000000) + " Million"
87
+ display_dict["Number of annual Faculty Awards"]= int(faculty_awards['2018'])
88
+ display_dict["Federal Research Spending (in USD)"]= str(int(federal_research['2018']) / 1000000) + " Million"
89
+ display_dict["Annual Giving (in USD)"]= str(int(giving['2018']) / 1000000) + " Million"
90
+ display_dict["Student Headcount"]= int(headcount['2018'])
91
+ display_dict["National Academy Members"]= int(national_academy['2018'])
92
+ display_dict["Non-Federal Research Spending (in USD)"]= str(int(non_federal_research['2018']) / 1000000) + " Million"
93
+ display_dict["Postdoctoral Fellows"]= int(postdocs['2018'])
94
+ display_dict["Total Research Spending (in USD)"]= str(int(total_research['2018']) / 1000000) + " Million"
95
+ df = pd.DataFrame.from_dict(display_dict, orient='index')
96
+ df.rename(columns = {0:'Values'}, inplace = True)
97
+ st.markdown('<h3 style="text-align: center; color: black; font: serif">Table View</h3>', unsafe_allow_html=True)
98
+ st.table(df.astype(str))
99
+ col1, col2 = st.columns(2)
100
+ with col1:
101
+ st.download_button("Download this data as CSV", data = df.to_csv(), file_name = str(institution_select) + "_at_a_glance.csv")
102
+ with col2:
103
+ st.download_button("Download this data as JSON", data = json.dumps(display_dict), file_name = str(institution_select) + "_at_a_glance.json")
104
+
105
+ def chart_view(institution_select):
106
+ #figure details should have x-axis title and y-axis title, in that order
107
+ def plot_helper(df, figure_details):
108
+ data = df.copy()
109
+ series = data.T[3:][::-1]
110
+ series.reset_index(inplace=True)
111
+ series.columns = ["Year", "Value"]
112
+ figure = go.Figure()
113
+ figure.add_trace(go.Scatter(x=series["Year"], y=series["Value"], name=list(data['Institution'])[0]))
114
+ del data
115
+ figure.update_layout(height = 600, width = 900, legend_orientation = 'h', xaxis_title = figure_details[0],
116
+ yaxis_title = figure_details[1], font = dict(family = 'Serif'))
117
+ figure.update_xaxes(nticks = 5)
118
+ figure.update_yaxes(rangemode="tozero")
119
+ return figure
120
+
121
+ line_charts = []
122
+ line_charts.append(plot_helper(federal_research, ["Year", "Spending"])) #0
123
+ line_charts.append(plot_helper(total_research, ["Year", "Spending"])) #1
124
+ line_charts.append(plot_helper(aamc, ["Year", "Spending"])) #2
125
+ line_charts.append(plot_helper(endowment, ["Year", "Fund size"])) #3
126
+ line_charts.append(plot_helper(giving, ["Year", "Giving"])) #4
127
+ line_charts.append(plot_helper(doctorates, ["Year", "Number of PhD's"])) #5
128
+ line_charts.append(plot_helper(postdocs, ["Year", "Number of Fellows"])) #6
129
+ line_charts.append(plot_helper(headcount, ["Year", "Headcount"])) #7
130
+ line_charts.append(plot_helper(national_academy, ["Year", "Number of Members"])) #8
131
+ line_charts.append(plot_helper(faculty_awards, ["Year", "Number of Awards"]))#9
132
+
133
+ rnd_fed_subjects = ["Fed_Life_Sci", "Fed_Phy_Sci", "Fed_Envir_Sci", "Fed_Eng","Fed_Comp_Sci", "Fed_Math","Fed_Psych","Fed_Social_Sci","Fed_Other_Sci"]
134
+ rnd_total_subjects = ["Tot_Life_Sci", "Tot_Phy_Sci", "Tot_Envir_Sci", "Tot_Eng","Tot_Comp_Sci", "Tot_Math","Tot_Psych","Tot_Social_Sci","Tot_Other_Sci"]
135
+
136
+ rnd_fed_bar = rnd_federal[rnd_fed_subjects]
137
+ rnd_fed_bar.reset_index(inplace=True)
138
+ rnd_fed_bar = rnd_fed_bar.T[1:]
139
+ fed_xlist = list(rnd_fed_bar.index)
140
+ fed_ylist = list(rnd_fed_bar[0])
141
+ rnd_fed_figure = go.Figure()
142
+ rnd_fed_figure.add_trace(go.Bar(x=fed_xlist, y=fed_ylist))
143
+ rnd_fed_figure.update_layout(xaxis_title = "Discipline", yaxis_title = "R&D (in USD)", font = dict(family = 'Serif'))
144
+
145
+ rnd_total_bar = rnd_total[rnd_total_subjects]
146
+ rnd_total_bar.reset_index(inplace=True)
147
+ rnd_total_bar = rnd_total_bar.T[1:]
148
+ total_xlist = list(rnd_total_bar.index)
149
+ total_ylist = list(rnd_total_bar[0])
150
+ rnd_total_figure = go.Figure()
151
+ rnd_total_figure.add_trace(go.Bar(x=total_xlist, y=total_ylist))
152
+ rnd_total_figure.update_layout(xaxis_title = "Discipline", yaxis_title = "R&D (in USD)", font = dict(family = 'Serif'))
153
+
154
+ col1, col2 = st.columns(2)
155
+ with col1:
156
+ st.write("<h3 style='text-align: center; color: black;'>" + "Federal Research Spending (in USD)" + "</h3>", unsafe_allow_html=True)
157
+ st.plotly_chart(line_charts[0], use_container_width=True)
158
+ st.write("<h3 style='text-align: center; color: black;'>" + "Medical Research Spending (in USD)" + "</h3>", unsafe_allow_html=True)
159
+ st.plotly_chart(line_charts[2], use_container_width=True)
160
+ st.write("<h3 style='text-align: center; color: black;'>" + "Annual Giving (in USD)" + "</h3>", unsafe_allow_html=True)
161
+ st.plotly_chart(line_charts[4], use_container_width=True)
162
+ st.write("<h3 style='text-align: center; color: black;'>" + "Number of Postdoctoral Fellows" + "</h3>", unsafe_allow_html=True)
163
+ st.plotly_chart(line_charts[6], use_container_width=True)
164
+ st.write("<h3 style='text-align: center; color: black;'>" + "Number of National Academy Members" + "</h3>", unsafe_allow_html=True)
165
+ st.plotly_chart(line_charts[8], use_container_width=True)
166
+ st.write("<h3 style='text-align: center; color: black;'>" + "R&D Breakup (Federal Dollars)" + "</h3>", unsafe_allow_html=True)
167
+ st.plotly_chart(rnd_fed_figure, use_container_width=True)
168
+ with col2:
169
+ st.write("<h3 style='text-align: center; color: black;'>" + "Total Research Spending (in USD)" + "</h3>", unsafe_allow_html=True)
170
+ st.plotly_chart(line_charts[1], use_container_width=True)
171
+ st.write("<h3 style='text-align: center; color: black;'>" + "Endowment Size (in USD)" + "</h3>", unsafe_allow_html=True)
172
+ st.plotly_chart(line_charts[3], use_container_width=True)
173
+ st.write("<h3 style='text-align: center; color: black;'>" + "Number of PhD's graduated" + "</h3>", unsafe_allow_html=True)
174
+ st.plotly_chart(line_charts[5], use_container_width=True)
175
+ st.write("<h3 style='text-align: center; color: black;'>" + "Total Student Headcount (all levels)" + "</h3>", unsafe_allow_html=True)
176
+ st.plotly_chart(line_charts[7], use_container_width=True)
177
+ st.write("<h3 style='text-align: center; color: black;'>" + "Annual Faculty Awards achieved" + "</h3>", unsafe_allow_html=True)
178
+ st.plotly_chart(line_charts[9], use_container_width=True)
179
+ st.write("<h3 style='text-align: center; color: black;'>" + "R&D Breakup (All Dollars)" + "</h3>", unsafe_allow_html=True)
180
+ st.plotly_chart(rnd_total_figure, use_container_width=True)
181
+
182
+ if view_type == "Latest Stats":
183
+ latest_stats(institution_select)
184
+ elif view_type == "Chart View":
185
+ chart_view(institution_select)
186
+
187
+ def display_institution_comparison():
188
+ col1, col2 = st.columns(2)
189
+ with col1:
190
+ type_select = st.radio("Filter by Institution Type", ["All", "Private", "Public"], horizontal=True)
191
+ with col2:
192
+ view = st.selectbox("Choose a view", ["Researcher View", "Recruiter View"])
193
+ institution_list = []
194
+ if type_select == "Private":
195
+ with open("data/private_institution_list.csv", "r") as f:
196
+ reader = csv.reader(f)
197
+ for row in reader:
198
+ institution_list.append(row[0])
199
+ elif type_select == "Public":
200
+ with open("data/public_institution_list.csv", "r") as f:
201
+ reader = csv.reader(f)
202
+ for row in reader:
203
+ institution_list.append(row[0])
204
+ else:
205
+ with open("data/institution_list.csv", "r") as f:
206
+ reader = csv.reader(f)
207
+ for row in reader:
208
+ institution_list.append(row[0])
209
+ institution_mselect = st.multiselect("Select Institutions to Compare",
210
+ options = institution_list, help = "For best results, select 2-3 institutions")
211
+
212
+ def load_data(input_file_path, institution_list):
213
+ data = pd.read_excel(input_file_path)
214
+ data = data[data["Institution"].isin(institution_list)]
215
+ return data
216
+
217
+ def plot_helper(df, figure_details):
218
+ data = df.copy()
219
+ series = (data.drop(columns = ["UnitID", "Control"]).T)
220
+ series.columns = series.iloc[0]
221
+ series.reset_index(inplace=True)
222
+ series = series.iloc[1:, :][::-1]
223
+ series.rename(columns = {"index": "Year"}, inplace = True)
224
+ figure = go.Figure()
225
+ for i in institution_mselect:
226
+ #add a line chart for each institution
227
+ figure.add_trace(go.Scatter(x = series["Year"], y = series[i], name = i))
228
+ figure.update_layout(height = 600, width = 900, legend_orientation = 'h', xaxis_title = figure_details[0],
229
+ yaxis_title = figure_details[1], legend_title = "Institution Key", font = dict(family = 'Serif'))
230
+ figure.update_xaxes(nticks = 5)
231
+ figure.update_yaxes(rangemode = "tozero")
232
+ del data
233
+ return figure
234
+
235
+ doctorates = load_data("data/doctorates.xlsx", institution_mselect)
236
+ faculty_awards = load_data("data/faculty_awards.xlsx", institution_mselect)
237
+ federal_research = load_data("data/federal_research.xlsx", institution_mselect)
238
+ national_academy = load_data("data/national_academy.xlsx", institution_mselect)
239
+ postdocs = load_data("data/postdocs.xlsx", institution_mselect)
240
+ total_research = load_data("data/total_research.xlsx", institution_mselect)
241
+ giving = load_data("data/giving.xlsx", institution_mselect)
242
+ headcount = load_data("data/headcount.xlsx", institution_mselect)
243
+ rnd_fed = load_data("data/rnd_by_discipline_federal.xlsx", institution_mselect)
244
+ rnd_total = load_data("data/rnd_by_discipline_total.xlsx", institution_mselect)
245
+
246
+ def researcher_content_writer():
247
+ st.write("##")
248
+ st.write("<h3 style='text-align: center; color: black;'>" + "Researcher View" + "</h3>", unsafe_allow_html=True)
249
+ st.write("##")
250
+ col1, col2 = st.columns(2)
251
+ with col1:
252
+ st.write("<h3 style='text-align: center; color: black;'>" + "Number of PhD's graduated" + "</h3>", unsafe_allow_html=True)
253
+ st.plotly_chart(plot_helper(doctorates, ["Year", "Number of PhD's graduated"]), use_container_width=True)
254
+ st.write("<h3 style='text-align: center; color: black;'>" + "Federal Research Spending" + "</h3>", unsafe_allow_html=True)
255
+ st.plotly_chart(plot_helper(federal_research, ["Year", "Spending (in USD)"]), use_container_width=True)
256
+ st.write("<h3 style='text-align: center; color: black;'>" + "National Academy Members" + "</h3>", unsafe_allow_html=True)
257
+ st.plotly_chart(plot_helper(national_academy, ["Year", "Number of Academy Members"]), use_container_width=True)
258
+ with col2:
259
+ st.write("<h3 style='text-align: center; color: black;'>" + "Number of Postdoctoral Fellows" + "</h3>", unsafe_allow_html=True)
260
+ st.plotly_chart(plot_helper(postdocs, ["Year", "Number of Postdocs"]), use_container_width=True)
261
+ st.write("<h3 style='text-align: center; color: black;'>" + "Total Research Spending" + "</h3>", unsafe_allow_html=True)
262
+ st.plotly_chart(plot_helper(total_research, ["Year", "Spending (in USD)"]), use_container_width=True)
263
+ st.write("<h3 style='text-align: center; color: black;'>" + "Annual Faculty Awards achieved" + "</h3>", unsafe_allow_html=True)
264
+ st.plotly_chart(plot_helper(faculty_awards, ["Year", "Number of Awards"]), use_container_width=True)
265
+
266
+
267
+ def recruiter_content_writer():
268
+ st.write("##")
269
+ st.write("<h3 style='text-align: center; color: black;'>" + "Recruiter View" + "</h3>", unsafe_allow_html=True)
270
+ st.write("##")
271
+ col1, col2 = st.columns(2)
272
+ with col1:
273
+ st.write("<h3 style='text-align: center; color: black;'>" + "Total Research Spending" + "</h3>", unsafe_allow_html=True)
274
+ st.plotly_chart(plot_helper(total_research, ["Year", "Spending (in USD)"]), use_container_width=True)
275
+ st.write("<h3 style='text-align: center; color: black;'>" + "Number of PhD's graduated" + "</h3>", unsafe_allow_html=True)
276
+ st.plotly_chart(plot_helper(doctorates, ["Year", "Number of PhD's graduated"]), use_container_width=True)
277
+ st.write("<h3 style='text-align: center; color: black;'>" + "National Academy Members" + "</h3>", unsafe_allow_html=True)
278
+ st.plotly_chart(plot_helper(national_academy, ["Year", "Number of Academy Members"]), use_container_width=True)
279
+ with col2:
280
+ st.write("<h3 style='text-align: center; color: black;'>" + "Annual Giving" + "</h3>", unsafe_allow_html=True)
281
+ st.plotly_chart(plot_helper(giving, ["Year", "Annual Giving (in USD)"]), use_container_width=True)
282
+ st.write("<h3 style='text-align: center; color: black;'>" + "Total Student Headcount" + "</h3>", unsafe_allow_html=True)
283
+ st.plotly_chart(plot_helper(headcount, ["Year", "Headcount"]), use_container_width=True)
284
+ st.write("<h3 style='text-align: center; color: black;'>" + "Annual Faculty Awards achieved" + "</h3>", unsafe_allow_html=True)
285
+ st.plotly_chart(plot_helper(faculty_awards, ["Year", "Number of Awards"]), use_container_width=True)
286
+
287
+ if view == "Researcher View":
288
+ researcher_content_writer()
289
+ elif view == "Recruiter View":
290
+ recruiter_content_writer()
291
+
292
+ if main_bar_selected == "Home":
293
+ display_home_page()
294
+ elif main_bar_selected == "University View":
295
+ display_uni_view_page()
296
+ elif main_bar_selected == "Institutional Comaprison":
297
+ display_institution_comparison()