How to store user inputs into local variables

I have the following code, intend to store and display some things according to user inputs on a form.

The problem I’m facing is that I can’t save what the user chooses/types every time he submits the form, the code below only rewrites the previous submit, instead of update it.
Also, I’m not able to store the data after a rerun.

I know I’ll probably have to use st.session_state somewhere, but I don’t know how or where to implement it.

I’d be really thankful if someone could help me ;).

import pandas as pd
import streamlit as st

# data
items = ['item1', 'item2','item3']
processos = ['processo1','processo2','processo3']
artigos ['artigo_x','artigo_y','artigo_z']
cliente = []
quantidade = []
vol = []
ped = []

# appending data
@st.cache_resource
def action(c,q,v,p):
    cliente.append(c)
    quantidade.append(q)
    vol.append(v)
    ped.append(p)

# panel title
st.title('Schedule - V Laundry :spiral_calendar_pad:')

# order register
with st.sidebar.form(key='cad_form', clear_on_submit=True):
    st.write("Cadastro de pedidos")

    client = st.text_input('Cliente', key='cli')

    its = st.selectbox("Selecione o item",pd.Series(items),key='it')

    article = st.selectbox("Selecione o artigo", pd.Series(artigos),key='art')

    processes = st.multiselect("Selecione o(s) processo(s)", pd.Series(processos),key='proc')

    quantities = st.number_input("Quantidade (pç)",key='qt')

    volume = st.number_input("Volume da peça (Kg)", key='v')

    new_ped = {'Cliente':client,'Item':its,'Artigo':article,'Processos':processes, 'Quantidade(pç)':quantities,'Volume(Kg)':volume}

    if st.form_submit_button("Cadastrar :white_check_mark:"):
        action(client,quantities,volume,new_ped)

# check
st.write(ped)

Hey @Arthur_S_de_Lima,

It sounds like you’re heading in the right direction with st.session_state – have you tried adding an on_click function for the st.form_submit_button and saving the values you need to session state in that function?

I’d also recommend checking out our doc on session state here!

Tried the following approach, but now, st.write(ped) shows that ped is actually empty.
Values are not being stored (they were before, on the previous code).

import pandas as pd
import streamlit as st

# data
items = ['item1', 'item2','item3']
processos = ['processo1','processo2','processo3']
artigos ['artigo_x','artigo_y','artigo_z']
cliente = []
quantidade = []
vol = []
ped = []

# session verify
if('cli' and 'qt' and 'v' and ped) not in st.session_state:
    st.session_state.cli = ' '
    st.session_state.qt = 0
    st.session_state.v = 0
    ped = []

# appending data
@st.cache_resource
def action():
    cliente.append(st.session_state.cli)
    quantidade.append(st.session_state.qt)
    vol.append(st.session_state.v)
    ped.append(new_ped)

# panel title
st.title('Schedule - V Laundry :spiral_calendar_pad:')

# order register
with st.sidebar.form(key='cad_form', clear_on_submit=True):
    st.write("Cadastro de pedidos")

    client = st.text_input('Cliente', key='cli')

    its = st.selectbox("Selecione o item",pd.Series(items),key='it')

    article = st.selectbox("Selecione o artigo", pd.Series(artigos),key='art')

    processes = st.multiselect("Selecione o(s) processo(s)", pd.Series(processos),key='proc')

    quantities = st.number_input("Quantidade (pç)",key='qt')

    volume = st.number_input("Volume da peça (Kg)", key='v')

    new_ped = {'Cliente':client,'Item':its,'Artigo':article,'Processos':processes, 'Quantidade(pç)':quantities,'Volume(Kg)':volume}

    if st.form_submit_button("Cadastrar :white_check_mark:",on_click=action):
        st.write("Pedido cadastrado! :heavy_check_mark:")
# check
st.write(ped)

Just found the solution with the following code:

import pandas as pd
import streamlit as st

# data
items = ['item1', 'item2','item3']
processos = ['processo1','processo2','processo3']
artigos = ['artigo_x','artigo_y','artigo_z']

# appending data
if "pedidos" not in st.session_state:
    st.session_state['pedidos'] = []

def register_order(p):
    st.session_state['pedidos'].append(p)

# panel title
st.title('Schedule - V Laundry :spiral_calendar_pad:')

# order register
with st.sidebar.form(key='cad_form', clear_on_submit=True):
    st.write("Cadastro de pedidos")

    client = st.text_input('Cliente', key='cli')

    its = st.selectbox("Selecione o item",pd.Series(items),key='it')

    article = st.selectbox("Selecione o artigo", pd.Series(artigos),key='art')

    processes = st.multiselect("Selecione o(s) processo(s)", pd.Series(processos),key='proc')

    quantities = st.number_input("Quantidade (pç)",key='qt')

    volume = st.number_input("Volume da peça (Kg)", key='v')

    new_ped = {'Cliente':client,'Item':its,'Artigo':article,'Processos':processes, 'Quantidade(pç)':quantities,'Volume(Kg)':volume}

    if st.form_submit_button("Cadastrar :white_check_mark:"):
        register_order(new_ped)
        st.write("Pedido cadastrado! :heavy_check_mark:")

# check
if len(st.session_state["pedidos"]) > 0:
    st.write(pd.DataFrame(st.session_state["pedidos"]))
else:
    st.write("Não há pedidos cadastrados.")

I wasn’t understanding quite well how st.session_state works, but after some readings and video tutorials I’ve finally catch up the idea!

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