Dear Streamlit-Community,
I’d like to know if it’s possible to modify the behavior of the ‘+’ button to add a line. When the user wants to add a new line, the line already contains data entered in a form.
I’ve tried to do this by creating a button and a callback function, but I quickly run into problems updating the data. They’re only taken into account every other time. Then maybe the code isn’t optimized enough.
Thank you in advance for your help.
def add_row():
"""
Adds a new row to the DataFrame stored in the Streamlit session state under the key "edited_df".
"""
df = st.session_state.dataframe_metadata
form_data = st.session_state.form_data
st.dataframe(df)
new_row = pd.DataFrame([form_data], columns=[*form_data.keys()])
new_row['IdentifierAnalysis'] = None
new_row['Object/Sample'] = None
new_row['LocalisationAnalysis'] = None
st.session_state.dataframe_metadata = pd.concat([df, new_row], ignore_index=True)
def on_change():
st.session_state.dataframe_metadata = st.session_state.edited_dataframe
def step_metadata_files():
"""Step 3 page - Manage metadata with datafiles files"""
st.session_state["submit_enabled"] = True
st.header("Metadata Editor")
if "dataframe_metadata" not in st.session_state:
st.session_state["dataframe_metadata"] = None
form_data = st.session_state.form_data
if st.session_state['dataframe_metadata'] is not None:
df = st.session_state['dataframe_metadata'].copy()
else:
df = pd.DataFrame([form_data], columns=[*form_data.keys()])
# New column
df['IdentifierAnalysis'] = None
df['Object/Sample'] = None
df['LocalisationAnalysis'] = None
# ordering
columns_order = ['IdentifierAnalysis', 'Object/Sample', 'LocalisationAnalysis', *form_data.keys()]
st.session_state.dataframe_metadata = df[columns_order]
st.session_state.edited_dataframe = st.data_editor(st.session_state.dataframe_metadata,
num_rows="dynamic",
hide_index=True,
key="edited_df",
on_change=on_change)
# Display dataframe
st.button("Add row", on_click=add_row)