Values not getting stored into list / dataframe

Hi Everyone,
Hope you guys are doing fine, :heart: :black_heart:

I’m trying to create a new data frame from the existing one with one new column of values, when I try to set a value to the cell or append new values to a list, the old values are getting deleted,

I don’t know why is this happening, is it due to the session state or some other thing.
TIA

import streamlit as st
import pandas as pd

if 'cc' not in st.session_state:
    st.session_state.cc = 0


def increment_counter():
    print('product', st.session_state.cc)
    st.session_state.cc += 1


data_li = []
df = pd.read_csv("demo.csv", usecols=['productId','productName'])
df = pd.DataFrame(df, columns=['productId','productName', 'Comments'])

col1, col2 = st.columns([5, 5])
with col1:
    st.dataframe(df)

with col2:
    with st.form("my_form"):
        if st.session_state.cc < len(df):
            pid = df["productId"][st.session_state.cc]
            pname = df["productName"][st.session_state.cc]
            comm = st.text_input("Comments :", value='Good')
        else:
            st.error("reached end of the file")

        data_li.append([pid, pname, comm])
        st.dataframe(data_li)

        approved = st.form_submit_button("Next", on_click=increment_counter())

        if approved:
            data_li = data_li.append([pid, pname, comm])
            st.dataframe(data_li)

st.dataframe(df)

st.dataframe(data_li)

Please take a look at the approved button, as the st.session_state.cc you created change values above and below it.

        st.write(st.session_state.cc)
        approved = st.form_submit_button("Next", on_click=increment_counter())
        st.write(st.session_state.cc)

Every time you press the approved button you are appending to the empty “data_li”, so if you would like to continue appending cell1,cell2… then you should put your data_li as a session state.

if 'data_li' not in st.session_state:
    st.session_state.data_li=[]

and then append it accordingly.

1 Like

Thanks @cualr , I’ll try this

Thanks, @cualr :sparkling_heart: Working as expected.

1 Like