How to use st.session_state to create DataFrame from user Entry

Hi Folks,

I have been thinking of creating a pandas DataFrame from user inputs into streamlit using st.session_state but I didn’t have any sucess … the only things I was abel to do is to store only 1 row and the row is updated everytime I insert a new data into app.

Appreciate your support.

Code:

   with st.form(key='add row', clear_on_submit=True):
col0, col1, col2, col3, col4, col5, col6, col7, col8, col9= st.columns(10)
with col0:
    
        idx= st.text_input('Idx')
        
with col1:
        location = st.text_input('Name')

with col2:
        x = st.text_input('X Coo' )
        
with col3:
        y = st.text_input('Y Coo' )
        
with col4:
        demand = st.text_input('Demand' )
        
with col5:
        from_ = st.text_input('From')
        
with col6:
        to = st.text_input('To' )
        
with col7:
        service = st.text_input('Service' )
        
with col8:
        vehicle = st.text_input('Vehicle' )
        
with col9:
        capacity = st.text_input('Capacity' )
    
    
run= st.form_submit_button('Submit')
df_new = pd.DataFrame({'Location': location, 
                              'X': x, 
                              'Y': y, 
                              'Demand': demand, 
                              'From': from_, 
                              'To': to,
                              'Service': service, 
                              'Vehicle': vehicle, 
                              'Capacity': capacity}, index=[idx])    
      

df= pd.DataFrame(columns=['Location', 'X', 'Y', 'Demand', 'From', 'To',
                          'Service', 'Vehicle', 'Capacity'])

 
if run:
     st.session_state.df = pd.concat([df, df_new], axis=0)
   

 AgGrid(st.session_state.df)

 st.write(df.shape[0])

Hi @Hazemhmz , have a look at this link and see if it helps you.

There may be other such link, please search as required.

Cheers

1 Like

Thank you Shawn_Pereira for the link.

I tried to follow the instructions in the link but still, each time it generates a new row, it doesn’t append to the dataframe.


import streamlit as st
import pandas as pd
from st_aggrid import AgGrid, GridUpdateMode
from st_aggrid.grid_options_builder import GridOptionsBuilder

#---------------- Page Config ----------------
st.set_page_config(page_title='New Way of Making Apps', layout='wide')

with st.form(key='add row', clear_on_submit=True):
    col0, col1, col2, col3, col4, col5, col6, col7, col8, col9= st.columns(10)
    with col0:
        
            idx= st.text_input('Idx')
            
    with col1:
            location = st.text_input('Name')
    
    with col2:
            x = st.text_input('X Coo' )
            
    with col3:
            y = st.text_input('Y Coo' )
            
    with col4:
            demand = st.text_input('Demand' )
            
    with col5:
            from_ = st.text_input('From')
            
    with col6:
            to = st.text_input('To' )
            
    with col7:
            service = st.text_input('Service' )
            
    with col8:
            vehicle = st.text_input('Vehicle' )
            
    with col9:
            capacity = st.text_input('Capacity' )
        
        
    run= st.form_submit_button('Submit')


df = pd.DataFrame(columns=['Location', 'X', 'Y', 'Demand', 'From', 'To',
                          'Service', 'Vehicle', 'Capacity'])


st.session_state.df = pd.DataFrame(columns=['Location', 'X', 'Y', 'Demand', 'From', 'To',
                          'Service', 'Vehicle', 'Capacity'])
 
if run:
    if int(idx) > st.session_state.df.shape[0]: 
        st.session_state.df = pd.concat([df, pd.DataFrame({'Location': location, 
                              'X': x, 
                              'Y': y, 
                              'Demand': demand, 
                              'From': from_, 
                              'To': to,
                              'Service': service, 
                              'Vehicle': vehicle, 
                              'Capacity': capacity}, index=[idx]) ], axis=0)


st.write(st.session_state.df)


st.write(st.session_state.df.shape[0])



Hi @Hazemhmz,

Please copy-paste this code into a new file and run it to check if this is what you are looking for.

import streamlit as st
import pandas as pd

if "mdf" not in st.session_state:
    st.session_state.mdf = pd.DataFrame(columns=['Location', 'X', 'Y', 'Demand', 'From', 'To', 'Service', 'Vehicle', 'Capacity'])

col0, col1, col2, col3, col4, col5, col6, col7, col8, col9= st.columns(10)
idx= col0.text_input('Idx')
location = col1.text_input('Name')
x = col2.text_input('X Coo' )
y = col3.text_input('Y Coo' )
demand = col4.text_input('Demand' )
from_ = col5.text_input('From')
to = col6.text_input('To' )
service = col7.text_input('Service' )
vehicle = col8.text_input('Vehicle' )
capacity = col9.text_input('Capacity' )

run = st.button('Submit')

df_new = pd.DataFrame({'Location': location, 
                            'X': x, 
                            'Y': y, 
                            'Demand': demand, 
                            'From': from_, 
                            'To': to,
                            'Service': service, 
                            'Vehicle': vehicle, 
                            'Capacity': capacity}, index=[idx])    
        
if run:
    st.session_state.mdf = pd.concat([st.session_state.mdf, df_new], axis=0)
    st.dataframe(st.session_state.mdf)

st.write(f"Total Rows: {st.session_state.mdf.shape[0]}")

Cheers

2 Likes

Wonderful Shawn, Thank you for the code.

Cheers to You & to Streamlit Community

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