so i have created the streamlit app , using the st_pages and my structure is
other_pages(directory) - Contacts.py,Projects.py
so here the login logic is in the streamlit_app.py , and im running that as a main script,after the login its going to st_pages , where session state and all accessible across all the pages , the problem is on the refresh , so when i refresh ,the app suppose to come back to the login page , but it stays on the pages itself
this is my streamlit_app.py
from st_pages import add_indentation,show_pages_from_config
import streamlit as st
if 'logged_in' not in st.session_state and "username" not in st.session_state:
st.session_state['logged_in'] = False
st.session_state['username'] = ""
if not st.session_state['logged_in'] and not st.session_state['username']:
username = st.text_input("Username")
st.success("Logged in successfully!")
st.session_state['logged_in'] = True
st.session_state['username'] = username
st.write("Logged in as : ", st.session_state['username'])
if __name__ == "__main__":
Contacts and project just has st.write(st.session_state[‘username’])
The recommended way to handle dynamically showing pages is:
- Always run
show_pages_from_config, no matter what
hide_pages to dynamically hide which pages are shown in the sidebar based on user state
That being said, st_pages does not do anything to automatically switch you to a certain page. You could use 🖱️ Switch page function - streamlit-extras, and when streamlit 1.30 comes out (should be in the next week or so) you can use st.switch_page. You could then add a check to each page that sees if the user is logged in, and if not does
@blackary thanks for your time , appreciate it .
And also i need to know is there any way to make streamlit session state to be persisting on refresh , coz all the pages logics are based on the session state ,if i refresh it going back to login page.
And followed by that how do i stop flickering , so after login , when on changing from one page to page , all the pages starts back to order in the same line (even though section applied in the pages.toml), to avoid this im using
add_indentation in all the pages .Even though im getting the flickering.
No, there is no way to get session state to persist on refresh, unfortunately. You could try using some of the 3rd party components for using cookies for this (just search for cookies on the forum to find different options).
The flickering for indented pages is unavoidable unfortunately, because the indentation is added via CSS, which doesn’t get applied until after the page loads.
Got it @blackary , Thank You
Hey @blackary , so have used the
streamlit_cookies_manager make the session state persistant and all seems to be working fine , except this page not found error , but when i close that move on to the pages , everything working fine . but this “page not found error” coming at very beginning of the script , even after i close and reopen the url using cookie its directly going to home page as
http://localhost:8501/home so this error not coming . Im not using the login inside the pages ,so when app is opened there is no endpoint after to the
http://localhost:8501/ may be due to that ? im using st_pages
And one more strange thing is that , this is my project structure , so have distinguished for the clarity of the Sections(Reports) and pages(Freshservice) inside that section, to add or remove in future . but the thing is i’m trying to import the auth from the root to the main.py of the page , its showing import error and also inside that freshservice page cannot able to import one another , which is very strange , but for the same structure i can able to import all the things using the sys and adding to the path and not for the streamlit.
@blackary any update on this …?
The first thing “page not found error” may be a bug in st_pages, but I’m not entirely sure. You could search in Issues · blackary/st_pages · GitHub for others experiencing this, or add it as a new issue if not.
The second one sounds like normal python behavior, and in general you should avoid adding to sys.path directly to try to avoid issues like this. There are lots of articles about this if you simply google for “python circular import”. My two most common methods are
- Think through the project structure to think about what are the base functions/classes, and put those in a standalone module, and then make other things depend on those.
- If that isn’t possible, move imports inside of functions, rather than at the top of the python file.