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)