cipher-asap / utils.py
ayushnoori's picture
Demo for ASAP grant renewal
f027c05
import streamlit as st
import pandas as pd
import project_config
import base64
@st.cache_data(show_spinner = 'Loading knowledge graph nodes...')
def load_kg():
# with st.spinner('Loading knowledge graph...'):
kg_nodes = pd.read_csv(project_config.DATA_DIR / 'kg_nodes.csv', dtype = {'node_index': int}, low_memory = False)
return kg_nodes
@st.cache_data(show_spinner = 'Loading knowledge graph edges...')
def load_kg_edges():
# with st.spinner('Loading knowledge graph...'):
kg_edges = pd.read_csv(project_config.DATA_DIR / 'kg_edges.csv', dtype = {'edge_index': int, 'x_index': int, 'y_index': int}, low_memory = False)
return kg_edges
def capitalize_after_slash(s):
# Split the string by slashes first
parts = s.split('/')
# Capitalize each part separately
capitalized_parts = [part.title() for part in parts]
# Rejoin the parts with slashes
capitalized_string = '/'.join(capitalized_parts).replace('_', ' ')
return capitalized_string
# From https://stackoverflow.com/questions/73251012/put-logo-and-title-above-on-top-of-page-navigation-in-sidebar-of-streamlit-multi
# See also https://arnaudmiribel.github.io/streamlit-extras/extras/app_logo/
@st.cache_data()
def get_base64_of_bin_file(png_file):
with open(png_file, "rb") as f:
data = f.read()
return base64.b64encode(data).decode()
def build_markup_for_logo(
png_file,
background_position="50% 10%",
margin_top="10%",
padding="20px",
image_width="80%",
image_height="",
):
binary_string = get_base64_of_bin_file(png_file)
return """
<style>
[data-testid="stSidebarNav"] {
background-image: url("data:image/png;base64,%s");
background-repeat: no-repeat;
background-position: %s;
margin-top: %s;
padding: %s;
background-size: %s %s;
}
</style>
""" % (
binary_string,
background_position,
margin_top,
padding,
image_width,
image_height,
)
def add_logo(png_file):
logo_markup = build_markup_for_logo(png_file)
st.markdown(
logo_markup,
unsafe_allow_html=True,
)