Source code for diva.gui.service_streamlit.main
# Copyright 2024 Mews
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
"""This is the main python function for the generation of the streamlit interface service
"""
import numpy as np
import streamlit as st
from streamlit_option_menu import option_menu
from loguru import logger
import diva.tools as tools
from diva import parameters
from diva.gui.service_streamlit import sidebar, tab1, tab2
import diva.monitoring.api_service_reliability
[docs]
def main(updating=False):
"""
Build and run the DestinE Chatbot application interface.
This function sets up the application's main interface and manages its core components, including:
- Configuring the Streamlit page layout and initializing authentication.
- Setting up the sidebar with navigation options.
- Managing the main tabs and their associated content, such as banners, session handling, and tabbed navigation.
Parameters
----------
updating : bool, optional
If True, displays a message indicating that the DIVA feature is currently unavailable due to an update
and prevents the full application from loading. Default is False.
Returns
-------
None
"""
# --- options ---
st.set_page_config(page_title="DIVA - DestinE Chatbot", page_icon="đ", layout="wide")
tools.generate_session_id()
if "connected_user" not in st.session_state:
st.session_state.connected_user = {"user": "Disconnected",
"info_url": "",
"user_type": ""}
tools.add_header()
tools.add_footer()
st.sidebar.title(parameters.prog_name)
file_path = "../images/earth_1.gif"
st.sidebar.image(file_path, use_container_width=True)
if updating:
st.write(
"DIVA is currently unavailable due to a feature update. Please check back later."
)
return None
authentication_status, keycloak_openid = tools.verify_authentification_keycloak()
if authentication_status:
cols = st.columns([0.9, 0.1])
tools.generate_css_disconnect_button()
with cols[-1]:
if st.button("đ", key='disconnect'):
tools.log_out_keycloak(keycloak_openid)
st.session_state.pop("access_token", None)
st.session_state.connected_user = {"user": "Disconnected",
"info_url": "",
"user_type": ""}
st.rerun()
tools.init_state()
options = [parameters.tab1, parameters.tab2]
with st.sidebar:
option_tab = option_menu(
None,
options,
icons=["robot", "info-circle"],
menu_icon="cast",
default_index=int(st.session_state["index_tab"]),
orientation="vertical",
key="current_tab",
)
st.session_state["index_tab"] = np.where(
np.array(option_tab) == st.session_state["current_tab"]
)[0][0]
if option_tab == parameters.tab1:
tab1_options = sidebar.app_sidebar(tab_id=1)
tab1.main(tab1_options)
if option_tab == parameters.tab2:
tab2_options = sidebar.app_sidebar(tab_id=2)
tab2.main(tab2_options)
elif authentication_status is None or not authentication_status:
st.info('The application is waiting for you to log in. Please authenticate to proceed.', icon="âšī¸")