Creating two data frames from two different pages

Summary

when I’m clicking on ‘move to task data’ button which will take the user to another form and let them enter the data, I’m getting below error.
First issue is, I need to click on this button twice for it to work… after clicking it twice I’m getting below error… can someone please help me rectify this issue and let me know what am I doing wrong ?

Steps to reproduce

Code snippet:

import pandas as pd
import streamlit as st


session_state = st.session_state

# Define a function to display the resource data collection page
def resource_data_page():

    

    # Define column names for the empty dataframe
    columns = ["Person_name", "Location", "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF"]
    # Create an empty dataframe with the defined columns
    empty_df = pd.DataFrame(columns=columns)
    # Define a list of people to choose from
    people = ["John", "Mary", "Jane", "Mark", "Lucy"]
    # Create an empty session state variable
    session_state = st.session_state


    # Add a button to navigate to the task data collection page
    if st.button("Move to Task Data"):
        st.session_state.page = "Task Data"
        
    # Check if the session state variable is already defined
    if "df" not in session_state:
        # Assign the initial data to the session state variable
        session_state.df = empty_df
        session_state.row = pd.Series(index=columns)

    # Create a selectbox for each column in the current row
    for col in columns:
        # Get unique values from the corresponding column in the resource_data dataframe
        if col == "Person_name":
            values = people
        else:
            values = ["Y", "N"]
        # Create a selectbox for the current column and add the selected value to the current row
        index = values.index(session_state.row[col]) if session_state.row[col] in values else 0

        session_state.row[col] = st.selectbox(col, values, key=col, index=index)

    # Add a button to add a new empty row to the dataframe and clear the values of the selectboxes for the current row
    if st.button("Add Row"):
        session_state.df = session_state.df.append(session_state.row, ignore_index=True)
        session_state.row = pd.Series(index=columns)

        # Save the updated data to the CSV file
        session_state.df.to_csv("data.csv", index=False)

    # Display the resulting dataframe
    st.dataframe(session_state.df)

# Define a function to display the task data collection page
def task_data_page():

    st.write("This is the Task Data page.")

    columns = ["task_name", "completion_time", "resource_required", "n_cases"]

    tasks = ["AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF"]
    completion_time = [4,8]
    resource_required = [1,2,3,4,5,6]
    n_cases = [0,2]
    empty_df = pd.DataFrame(columns=columns)

    session_state = st.session_state

    # Check if the session state variable is already defined
    if "df" not in session_state:
        # Assign the initial data to the session state variable
        session_state.df = empty_df
        session_state.row = pd.Series(index=columns)

    # Create a selectbox for each column in the current row
    for col in columns:
        # Get unique values from the corresponding column in the resource_data dataframe
        if col == "task_name":
            values = tasks
        elif col== 'completion_time':
            values = completion_time
        elif col== 'resource_required':
            values = resource_required
        else:
            values = [0,2]
        # Create a selectbox for the current column and add the selected value to the current row
        index = values.index(session_state.row[col]) if session_state.row[col] in values else 0

        session_state.row[col] = st.selectbox(col, values, key=col, index=index)

    # Add a button to add a new empty row to the dataframe and clear the values of the selectboxes for the current row
    if st.button("Add Row"):
        session_state.df = session_state.df.append(session_state.row, ignore_index=True)
        session_state.row = pd.Series(index=columns)

    # Add a button to navigate to the task data collection page
    if st.button("Next"):
        st.session_state.page = "Task Data"
    
    # Display the resulting dataframe
    st.dataframe(session_state.df)

    st.button("Back to Resource Data", on_click=resource_data_page)


# Define a dictionary to map page names to their corresponding functions
pages = {
    "Resource Data": resource_data_page,
    "Task Data": task_data_page
}

# Check if the current page is already defined in the session state variable
if "page" not in session_state:
    # Assign the initial page to the session state variable
    session_state.page = "Resource Data"

# Display the current page
pages[session_state.page]()

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

If I click on ‘move to task_data’ button, it should allow me to move to task_data and then allow me to enter my details.

Actual behavior:

Explain the undesired behavior or error you see when you run the code above.
If you’re seeing an error message, share the full contents of the error message here.

Closing this thread as duplicate of:

This topic was automatically closed after 1 minute. New replies are no longer allowed.