mup / app.py
Chinar's picture
Update app.py
42a6eae
import streamlit as st
import streamlit_option_menu as som
import plotly.graph_objects as go
import pandas as pd
import csv
import json
st.set_page_config(page_title="MUP", page_icon="bar-chart", layout = "wide")
#hiding default elememnts
hide_meu = """<style> #MainMenu {visibility: hidden;}
footer {visibility: hidden;} </style>"""
st.markdown(hide_meu, unsafe_allow_html=True)
main_bar_selected = som.option_menu(None, ["Home", "University View", "Institutional Comparison"], icons = ["house-fill", "building", "book-fill"], orientation = "horizontal")
st.write("####")
def display_home_page():
st.markdown('<h1 style="text-align: center; color: black; font: serif">MUP: Measuring University Performance</h1>', unsafe_allow_html=True)
st.write("##")
col1, col2 = st.columns([2, 1])
with col1:
st.markdown('<h2 style=" color: black;">Blazing fast university analytics at your fingertips</h2>', unsafe_allow_html=True)
st.markdown('<h3 style=" color: black;">Administrators looking for peer institution data?</h3>', unsafe_allow_html=True)
st.markdown('<h3 style=" color: black;">Students finding the right institution for your research career?</h3>', unsafe_allow_html=True)
st.markdown('<h3 style=" color: black;">Researchers looking for the right university for your careers?</h3>', unsafe_allow_html=True)
st.markdown('<h2 style=" color: black;">You have come to the right place!</h2>', unsafe_allow_html=True)
with col2:
st.image('data/chart_image.jpeg')
def display_uni_view_page():
#code to filter by type and select the institutions
col1, col2 = st.columns(2)
with col1:
type_select = st.radio("Filter by Institution Type", ["All", "Private", "Public"], horizontal=True)
with col2:
view_type = st.radio("View Type", ["Latest Stats", "Chart View"], horizontal=True)
institution_list = []
if type_select == "Private":
with open("data/private_institution_list.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
institution_list.append(row[0])
elif type_select == "Public":
with open("data/public_institution_list.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
institution_list.append(row[0])
else:
with open("data/institution_list.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
institution_list.append(row[0])
institution_select = st.selectbox("Select colleges to view", options = institution_list)
st.write("##")
def load_data(input_file_path, institution_name):
data = pd.read_excel(input_file_path)
data = data[data["Institution"] == institution_name]
return data
aamc = load_data("data/aamc.xlsx", institution_select)
doctorates = load_data("data/doctorates.xlsx", institution_select)
endowment = load_data("data/endowment.xlsx", institution_select)
faculty_awards = load_data("data/faculty_awards.xlsx", institution_select)
federal_research = load_data("data/federal_research.xlsx", institution_select)
giving = load_data("data/giving.xlsx", institution_select)
headcount = load_data("data/headcount.xlsx", institution_select)
national_academy = load_data("data/national_academy.xlsx", institution_select)
non_federal_research = load_data("data/non_federal_research.xlsx", institution_select)
postdocs = load_data("data/postdocs.xlsx", institution_select)
rnd_federal = load_data("data/rnd_by_discipline_federal.xlsx", institution_select)
rnd_total = load_data("data/rnd_by_discipline_total.xlsx", institution_select)
total_research = load_data("data/total_research.xlsx", institution_select)
def latest_stats(institution_select):
display_dict = {}
display_dict['Medical Research Spending (in USD)']= str(int(aamc['2018']) / 1000000) + " Million"
display_dict["PhD's graduated"]= int(doctorates['2018'])
display_dict["Endowment (in USD)"]= str(int(endowment['2018']) / 1000000) + " Million"
display_dict["Number of annual Faculty Awards"]= int(faculty_awards['2018'])
display_dict["Federal Research Spending (in USD)"]= str(int(federal_research['2018']) / 1000000) + " Million"
display_dict["Annual Giving (in USD)"]= str(int(giving['2018']) / 1000000) + " Million"
display_dict["Student Headcount"]= int(headcount['2018'])
display_dict["National Academy Members"]= int(national_academy['2018'])
display_dict["Non-Federal Research Spending (in USD)"]= str(int(non_federal_research['2018']) / 1000000) + " Million"
display_dict["Postdoctoral Fellows"]= int(postdocs['2018'])
display_dict["Total Research Spending (in USD)"]= str(int(total_research['2018']) / 1000000) + " Million"
df = pd.DataFrame.from_dict(display_dict, orient='index')
df.rename(columns = {0:'Values'}, inplace = True)
st.markdown('<h3 style="text-align: center; color: black; font: serif">Table View</h3>', unsafe_allow_html=True)
st.table(df.astype(str))
col1, col2 = st.columns(2)
with col1:
st.download_button("Download this data as CSV", data = df.to_csv(), file_name = str(institution_select) + "_at_a_glance.csv")
with col2:
st.download_button("Download this data as JSON", data = json.dumps(display_dict), file_name = str(institution_select) + "_at_a_glance.json")
def chart_view(institution_select):
#figure details should have x-axis title and y-axis title, in that order
def plot_helper(df, figure_details):
data = df.copy()
series = data.T[3:][::-1]
series.reset_index(inplace=True)
series.columns = ["Year", "Value"]
figure = go.Figure()
figure.add_trace(go.Scatter(x=series["Year"], y=series["Value"], name=list(data['Institution'])[0]))
del data
figure.update_layout(height = 600, width = 900, legend_orientation = 'h', xaxis_title = figure_details[0],
yaxis_title = figure_details[1], font = dict(family = 'Serif'))
figure.update_xaxes(nticks = 5)
figure.update_yaxes(rangemode="tozero")
return figure
line_charts = []
line_charts.append(plot_helper(federal_research, ["Year", "Spending"])) #0
line_charts.append(plot_helper(total_research, ["Year", "Spending"])) #1
line_charts.append(plot_helper(aamc, ["Year", "Spending"])) #2
line_charts.append(plot_helper(endowment, ["Year", "Fund size"])) #3
line_charts.append(plot_helper(giving, ["Year", "Giving"])) #4
line_charts.append(plot_helper(doctorates, ["Year", "Number of PhD's"])) #5
line_charts.append(plot_helper(postdocs, ["Year", "Number of Fellows"])) #6
line_charts.append(plot_helper(headcount, ["Year", "Headcount"])) #7
line_charts.append(plot_helper(national_academy, ["Year", "Number of Members"])) #8
line_charts.append(plot_helper(faculty_awards, ["Year", "Number of Awards"]))#9
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"]
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"]
rnd_fed_bar = rnd_federal[rnd_fed_subjects]
rnd_fed_bar.reset_index(inplace=True)
rnd_fed_bar = rnd_fed_bar.T[1:]
fed_xlist = list(rnd_fed_bar.index)
fed_ylist = list(rnd_fed_bar[0])
rnd_fed_figure = go.Figure()
rnd_fed_figure.add_trace(go.Bar(x=fed_xlist, y=fed_ylist))
rnd_fed_figure.update_layout(xaxis_title = "Discipline", yaxis_title = "R&D (in USD)", font = dict(family = 'Serif'))
rnd_total_bar = rnd_total[rnd_total_subjects]
rnd_total_bar.reset_index(inplace=True)
rnd_total_bar = rnd_total_bar.T[1:]
total_xlist = list(rnd_total_bar.index)
total_ylist = list(rnd_total_bar[0])
rnd_total_figure = go.Figure()
rnd_total_figure.add_trace(go.Bar(x=total_xlist, y=total_ylist))
rnd_total_figure.update_layout(xaxis_title = "Discipline", yaxis_title = "R&D (in USD)", font = dict(family = 'Serif'))
col1, col2 = st.columns(2)
with col1:
st.write("<h3 style='text-align: center; color: black;'>" + "Federal Research Spending (in USD)" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(line_charts[0], use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Medical Research Spending (in USD)" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(line_charts[2], use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Annual Giving (in USD)" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(line_charts[4], use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Number of Postdoctoral Fellows" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(line_charts[6], use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Number of National Academy Members" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(line_charts[8], use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "R&D Breakup (Federal Dollars)" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(rnd_fed_figure, use_container_width=True)
with col2:
st.write("<h3 style='text-align: center; color: black;'>" + "Total Research Spending (in USD)" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(line_charts[1], use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Endowment Size (in USD)" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(line_charts[3], use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Number of PhD's graduated" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(line_charts[5], use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Total Student Headcount (all levels)" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(line_charts[7], use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Annual Faculty Awards achieved" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(line_charts[9], use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "R&D Breakup (All Dollars)" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(rnd_total_figure, use_container_width=True)
if view_type == "Latest Stats":
latest_stats(institution_select)
elif view_type == "Chart View":
chart_view(institution_select)
def display_institution_comparison():
col1, col2 = st.columns(2)
with col1:
type_select = st.radio("Filter by Institution Type", ["All", "Private", "Public"], horizontal=True)
with col2:
view = st.selectbox("Choose a view", ["Researcher View", "Recruiter View"])
institution_list = []
if type_select == "Private":
with open("data/private_institution_list.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
institution_list.append(row[0])
elif type_select == "Public":
with open("data/public_institution_list.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
institution_list.append(row[0])
else:
with open("data/institution_list.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
institution_list.append(row[0])
institution_mselect = st.multiselect("Select Institutions to Compare",
options = institution_list, help = "For best results, select 2-3 institutions")
def load_data(input_file_path, institution_list):
data = pd.read_excel(input_file_path)
data = data[data["Institution"].isin(institution_list)]
return data
def plot_helper(df, figure_details):
data = df.copy()
series = (data.drop(columns = ["UnitID", "Control"]).T)
series.columns = series.iloc[0]
series.reset_index(inplace=True)
series = series.iloc[1:, :][::-1]
series.rename(columns = {"index": "Year"}, inplace = True)
figure = go.Figure()
for i in institution_mselect:
#add a line chart for each institution
figure.add_trace(go.Scatter(x = series["Year"], y = series[i], name = i))
figure.update_layout(height = 600, width = 900, legend_orientation = 'h', xaxis_title = figure_details[0],
yaxis_title = figure_details[1], legend_title = "Institution Key", font = dict(family = 'Serif'))
figure.update_xaxes(nticks = 5)
figure.update_yaxes(rangemode = "tozero")
del data
return figure
doctorates = load_data("data/doctorates.xlsx", institution_mselect)
faculty_awards = load_data("data/faculty_awards.xlsx", institution_mselect)
federal_research = load_data("data/federal_research.xlsx", institution_mselect)
national_academy = load_data("data/national_academy.xlsx", institution_mselect)
postdocs = load_data("data/postdocs.xlsx", institution_mselect)
total_research = load_data("data/total_research.xlsx", institution_mselect)
giving = load_data("data/giving.xlsx", institution_mselect)
headcount = load_data("data/headcount.xlsx", institution_mselect)
rnd_fed = load_data("data/rnd_by_discipline_federal.xlsx", institution_mselect)
rnd_total = load_data("data/rnd_by_discipline_total.xlsx", institution_mselect)
def researcher_content_writer():
st.write("##")
st.write("<h3 style='text-align: center; color: black;'>" + "Researcher View" + "</h3>", unsafe_allow_html=True)
st.write("##")
col1, col2 = st.columns(2)
with col1:
st.write("<h3 style='text-align: center; color: black;'>" + "Number of PhD's graduated" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(plot_helper(doctorates, ["Year", "Number of PhD's graduated"]), use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Federal Research Spending" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(plot_helper(federal_research, ["Year", "Spending (in USD)"]), use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "National Academy Members" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(plot_helper(national_academy, ["Year", "Number of Academy Members"]), use_container_width=True)
with col2:
st.write("<h3 style='text-align: center; color: black;'>" + "Number of Postdoctoral Fellows" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(plot_helper(postdocs, ["Year", "Number of Postdocs"]), use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Total Research Spending" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(plot_helper(total_research, ["Year", "Spending (in USD)"]), use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Annual Faculty Awards achieved" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(plot_helper(faculty_awards, ["Year", "Number of Awards"]), use_container_width=True)
def recruiter_content_writer():
st.write("##")
st.write("<h3 style='text-align: center; color: black;'>" + "Recruiter View" + "</h3>", unsafe_allow_html=True)
st.write("##")
col1, col2 = st.columns(2)
with col1:
st.write("<h3 style='text-align: center; color: black;'>" + "Total Research Spending" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(plot_helper(total_research, ["Year", "Spending (in USD)"]), use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Number of PhD's graduated" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(plot_helper(doctorates, ["Year", "Number of PhD's graduated"]), use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "National Academy Members" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(plot_helper(national_academy, ["Year", "Number of Academy Members"]), use_container_width=True)
with col2:
st.write("<h3 style='text-align: center; color: black;'>" + "Annual Giving" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(plot_helper(giving, ["Year", "Annual Giving (in USD)"]), use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Total Student Headcount" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(plot_helper(headcount, ["Year", "Headcount"]), use_container_width=True)
st.write("<h3 style='text-align: center; color: black;'>" + "Annual Faculty Awards achieved" + "</h3>", unsafe_allow_html=True)
st.plotly_chart(plot_helper(faculty_awards, ["Year", "Number of Awards"]), use_container_width=True)
if view == "Researcher View":
researcher_content_writer()
elif view == "Recruiter View":
recruiter_content_writer()
if main_bar_selected == "Home":
display_home_page()
elif main_bar_selected == "University View":
display_uni_view_page()
elif main_bar_selected == "Institutional Comaprison":
display_institution_comparison()