"""Functions for the sidebar of the Streamlit app.""" import base64 import os from datetime import date import streamlit as st from src.config_parameters import config # Check if app is deployed def is_app_on_streamlit(): """Check whether the app is on streamlit or runs locally.""" return "HOSTNAME" in os.environ and os.environ["HOSTNAME"] == "streamlit" # General layout def toggle_menu_button(): """If app is on streamlit, hide menu button.""" if is_app_on_streamlit(): st.markdown( """ """, unsafe_allow_html=True, ) # Sidebar sidebar_title = "Flood Mapping Tool" @st.cache(allow_output_mutation=True) def get_base64_of_bin_file(png_file): """ Get base64 from image file. Inputs: png_file (str): image filename Returns: str: encoded ASCII 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=f"{config['MA_logo_background_position']}", image_width=f"{config['MA_logo_width']}", image_height="", sidebar_header_fontsize=config["sidebar_header_fontsize"], sidebar_header_fontweight=config["sidebar_header_fontweight"], ): """ Create full string for navigation bar, including logo and title. Inputs: png_file (str): image filename background_position (str): position logo image_width (str): width logo image_height (str): height logo Returns str: full string with logo and title for sidebar """ binary_string = get_base64_of_bin_file(png_file) return """ """ % ( binary_string, background_position, image_width, image_height, sidebar_title, sidebar_header_fontsize, sidebar_header_fontweight, ) def add_logo(png_file): """ Add logo to sidebar. Inputs: png_file (str): image filename Returns: None """ logo_markup = build_markup_for_logo(png_file) st.markdown( logo_markup, unsafe_allow_html=True, ) def add_about(): """ Add about and contacts to sidebar. Inputs: None Returns: None """ today = date.today().strftime("%B %d, %Y") # About textbox st.sidebar.markdown("## About") st.sidebar.markdown( """
Last update: %s
%s