Home > Back-end >  Getting None instead of string
Getting None instead of string

Time:01-31

I have a Streamlit application that lets the user login (by checking with SQLite databse) and then do some action. My problem is that I'm trying to print a welcome message with the user's username but I'm getting 'None' instead.

This is my Login function:

def show_login_page():
with loginSection:
    if st.session_state['loggedIn'] == False:
        username = st.text_input (label="Username", value="", placeholder="Enter your username")
        password = st.text_input (label="Password", value="", placeholder="Enter password", type="password")
        hashed_password = make_hash(password)
        st.button("Login", on_click=LoggedIn_Clicked, args= (username, hashed_password)) 
        return username

Then after clicking the login button, user's information will be checked with the database

def LoggedIn_Clicked(username, password):
    # check with database
    if login_user(username, password):
        st.session_state['loggedIn'] = True
    else:
        st.session_state['loggedIn'] = False
        st.error("Invalid username or password")

After sucessfully logging in, the session_state will change and the user will login to the main page

with headerSection:
    st.title("Streamlit Application")
    #first run will have nothing in session_state
    if 'loggedIn' not in st.session_state:
        st.session_state['loggedIn'] = False
        show_login_page() 
    else:
        if st.session_state['loggedIn']:
            show_logout_button() 
            # getting the username from login page
            # Problem is here
            usr = show_login_page()
            show_main_page(usr)
        else:
            show_login_page()

This is the main page function:

def show_main_page(username):
    with mainSection:
        st.header(f"Hello {username}")
        # Do some action
        processingClicked = st.button("Start Processing", key="processing")
        if processingClicked:
            st.balloons() 

I've tried many ways was to get the username variable from the login_page to the main_page function to no avail. Help would be appreciated.

CodePudding user response:

The problem is that the function show_login_page() returns the value of username and the code tries to use that value as the username argument in the show_main_page function, but username is not being passed correctly, resulting in the None value being displayed instead.

To fix the issue, you can store the returned username from show_login_page in a variable and pass that variable to show_main_page.

You can pass the username entered by the user in the show_login_page() function to the show_main_page() function by storing the username in a session state variable and accessing it from the show_main_page() function. Here's an example:


def show_login_page():
    with loginSection:
        if st.session_state['loggedIn'] == False:
            username = st.text_input (label="Username", value="", placeholder="Enter your username")
            password = st.text_input (label="Password", value="", placeholder="Enter password", type="password")
            hashed_password = make_hash(password)
            if st.button("Login", key="login"):
                LoggedIn_Clicked(username, hashed_password) 
            return username

def LoggedIn_Clicked(username, password):
    # check with database
    if login_user(username, password):
        st.session_state['loggedIn'] = True
        st.session_state['username'] = username
    else:
        st.session_state['loggedIn'] = False
        st.error("Invalid username or password")

with headerSection:
    st.title("Streamlit Application")
    #first run will have nothing in session_state
    if 'loggedIn' not in st.session_state:
        st.session_state['loggedIn'] = False
        show_login_page() 
    else:
        if st.session_state['loggedIn']:
            show_logout_button() 
            username = st.session_state.get("username", "")
            show_main_page(username)
        else:
            show_login_page()

def show_main_page(username):
    with mainSection:
        st.header(f"Hello {username}")
        # Do some action
        processingClicked = st.button("Start Processing", key="processing")
        if processingClicked:
            st.balloons()


Note that the username input field is now being displayed only if the user is logged in. Also, the username value is saved in st.session_state so that it's available in subsequent runs.

In conclusion, the original code had an issue where the function show_login_page() was returning the value of username but not using it as the argument in the show_main_page function, resulting in a None value being displayed. The corrected code solves this issue by saving the username value in st.session_state and using it as an argument in show_main_page when the user is logged in.

  • Related