I am trying to implement the component streamlit_authenticator from @mkhorasani in my multipage app. I don´t find the way of hiding the sidebar that automatically appears with the pages. That´s something that doesn´t seem to be secure from the external user, and I would like to hide it (not just certify that the user cannot go in if it´s not loged in). Any ideas on how to do so?
# home.py
import streamlit as st
from services.database_service import get_credentials, get_cookie_info, get_preauthorized_emails
from streamlit_authenticator import Authenticate
# Set page configuration
st.set_page_config(page_title="Lubaq App", layout="wide")
def main():
# Load authenticator configuration
credentials = get_credentials()
cookie_info = get_cookie_info()
preauthorized = get_preauthorized_emails()
# Initialize Streamlit authenticator
authenticator = Authenticate(
credentials,
cookie_info['name'],
cookie_info['key'],
cookie_info['expiry_days'],
preauthorized
)
# Attempt to authenticate the user
name, authentication_status, username = authenticator.login('Login', 'main')
if authentication_status:
# If user is authenticated, show the app content
# Here you can call your page display function or write your page content
# For example:
st.title(f"Welcome {name}!")
# You could have buttons or other logic to navigate between pages
elif authentication_status == False:
st.error("Username/password is incorrect")
elif authentication_status == None:
st.warning("Please enter your username and password")
if __name__ == "__main__":
main()
And then I have a folder called pages, with several pages inside as page_name.py each of them. Although I do the stop per each page, here the example:
# studies_list.py
import streamlit as st
import pandas as pd
from datetime import datetime
# Assume this function checks if the user is authenticated
def is_user_authenticated():
# This should check an actual condition in your app's session state or other state mechanism
return st.session_state.get("authentication_status", False)
# Check if user is authenticated at the very top before any page logic
if not is_user_authenticated():
st.error("You must be logged in to view this page.")
st.stop() # Prevents the execution of the rest of the script
# The rest of your studies list page logic here since the user is authenticated
def display_studies_list_page():
# Mock data in the form of a DataFrame (replace this with your actual data source)
data = {
# ... your data structure ...
}
studies = pd.DataFrame(data)
# UI elements and DataFrame display
st.title("Scheduled Studies List")
# ... rest of your UI elements and logic for displaying studies ...
# Main logic
if __name__ == "__main__":
display_studies_list_page()
The problem is that initially, the sidebar appears, and even if I check the status of the session_state and stop the page if the user is not logged in, I still see the sidebar with the page names. I would like to handle it in a safer way, that does not appear secure (from the outside). Here what I see:
You could stop the other pages from loading if the user is not authorized;
# import libraries here
if not st.session_state.authentication_status:
st.info('Please Login from the Home page and try again.')
st.stop()
# rest of the operations