Append on Button Click

I am building a dashboard that imports flight data. The user also has the option to add flights from the dashboard as long as they provide the four metrics shown below. When the user adds a flight I want it to append or concatenate to the imported flight data and update automatically in the data frame and other graphics when the button is clicked.

This is working fine for one flight but as soon as I click the button to add a second flight, the first one disappears and only the second shows up.

Any help to fix this loop so that I can add multiple flights to the data frame without the earlier ones disappearing? GPTs have been stumped.

if flight_schedule_data is not None:
    st.session_state.flight_schedule_df = pd.read_csv(flight_schedule_data)
    

    # Add input fields for origin, destination, arrival, and departure times
    with inputContainer:
        st.subheader("Add Flight")
        origin_airport = st.text_input("Origin Airport (3-letter code)", max_chars=3).upper()
        dest_airport = st.text_input("Destination Airport (3-letter code)", max_chars=3).upper()
        arrival_time = st.text_input("Arrival Time (MM/DD/YYYY HH:MM)", placeholder="MM/DD/YYYY HH:MM")
        departure_time = st.text_input("Departure Time (MM/DD/YYYY HH:MM)", placeholder="MM/DD/YYYY HH:MM")
        add_flight_button = st.button("Add Flight")

    # Check if the user has provided all the required information
    if add_flight_button and origin_airport and dest_airport and arrival_time and departure_time:
        try:
            # Create a new row with the user-provided data
            new_flight = pd.DataFrame({
                "Origin Airport": [origin_airport],
                "Destination Airport": [dest_airport],
                "Main Airport": [st.session_state.flight_schedule_df.loc[0, 'Main Airport']],
                "Arrival Time": [arrival_time],
                "Departure Time": [departure_time]
            })

            # Concatenate the new row with the existing flight schedule dataframe
            st.session_state.updated_flight_schedule_df = pd.concat([st.session_state.flight_schedule_df, new_flight], ignore_index=True)

            # Show a success message
            st.success("Flight added successfully!")
        except Exception as e:
            st.error(f"Error adding flight: {e}")
        
    with col2:
        st.header("Flight Schedule")
        if 'updated_flight_schedule_df' in st.session_state:
            flight_schedule_df = st.session_state.updated_flight_schedule_df
            #del st.session_state.updated_flight_schedule_df
        else:
            flight_schedule_df = st.session_state.flight_schedule_df

        st.dataframe(flight_schedule_df, hide_index=True)

Hi @gatech2024

You’d need to include a check to see if flight_schedule_df is in Streamlit’s session state, which keeps data through reruns. This prevents loss of newly added data, like flights, when the app is used.

Here’s a simple example:

import streamlit as st
import pandas as pd
from datetime import datetime

# Dummy flight schedule data
dummy_data = {
    "Origin Airport": ["JFK", "LAX"],
    "Destination Airport": ["LAX", "JFK"],
    "Main Airport": ["N/A", "N/A"],  # Assuming this is a placeholder for demonstration
    "Arrival Time": ["01/01/2024 12:00", "01/02/2024 15:00"],
    "Departure Time": ["01/01/2024 09:00", "01/02/2024 12:00"],
}
dummy_df = pd.DataFrame(dummy_data)

# Initialize the session state for flight schedule data if it doesn't exist
if "flight_schedule_df" not in st.session_state:
    st.session_state.flight_schedule_df = dummy_df

# User input for adding a new flight
st.subheader("Add Flight")
origin_airport = st.text_input("Origin Airport (3-letter code)", max_chars=3).upper()
dest_airport = st.text_input("Destination Airport (3-letter code)", max_chars=3).upper()
arrival_time = st.text_input(
    "Arrival Time (MM/DD/YYYY HH:MM)", placeholder="MM/DD/YYYY HH:MM"
)
departure_time = st.text_input(
    "Departure Time (MM/DD/YYYY HH:MM)", placeholder="MM/DD/YYYY HH:MM"
)
add_flight_button = st.button("Add Flight")

if (
    add_flight_button
    and origin_airport
    and dest_airport
    and arrival_time
    and departure_time
):
    try:
        # Create a new row with the user-provided data
        new_flight = pd.DataFrame(
            {
                "Origin Airport": [origin_airport],
                "Destination Airport": [dest_airport],
                "Main Airport": [
                    "N/A"
                ],  # Assuming this is a constant or fetched from somewhere else
                "Arrival Time": [arrival_time],
                "Departure Time": [departure_time],
            }
        )

        # Append the new row to the existing flight schedule dataframe
        st.session_state.flight_schedule_df = pd.concat(
            [st.session_state.flight_schedule_df, new_flight], ignore_index=True
        )

        # Show a success message
        st.success("Flight added successfully!")
    except Exception as e:
        st.error(f"Error adding flight: {e}")

# Display the updated flight schedule
st.header("Flight Schedule")
st.dataframe(st.session_state.flight_schedule_df, hide_index=True)
2 Likes

Here’s a quick demo where you can see the new fields being appended to the existing table.

streamlit_app_testStreamlit-ezgif.com-video-to-gif-converter

Let me know if that would work?

Best,
Charly

1 Like

It worked! Thank you @Charly_Wargnier

1 Like

Glad it did! :hugs:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.