Problems with on_select in st.dataframe

I want to use the rows selected in my st.dataframe to filter my data. I’m using the new feature in streamlit 1.35. I’m running my app locally .

Snippet of my code:

def click_df_resumo():
if ‘df_resumo’ not in st.session_state:
st.session_state.df_resumo = None

df_resumo = st.session_state.get(‘df_resumo’)
selection = df_resumo.get(‘selection’) if df_resumo else None
rows = selection.get(‘rows’) if selection else None

if rows:
lin = rows
dados_filter = dados.iloc[lin]
else:
dados_filter = dados

st.dataframe(df_tabela, hide_index=True, use_container_width=True, on_select=click_df_resumo, selection_mode=‘multi-row’, key=‘df_resumo’)

But when i run the script, i get the error:

StreamlitAPIException: Values for st.button, st.download_button, st.file_uploader, st.data_editor, st.chat_input, and st.form cannot be set using st.session_state.

But if i change the on_select to ‘ignore’ and then change back to my function, it works perfectly.

To me, this error don’t make any sense because i’m using st.dataframe and not st.data_editor

Hi @Henrique2, welcome to the forum!

Could you please edit your post to have a code block for your code, so that it’s readable and runnable by others?

Based on the error you’re getting, I would guess that it’s in a different part of your code, probably somewhere where you are using one of these: st.button, st.download_button, st.file_uploader, st.data_editor, st.chat_input, or st.form

Thanks for the answer ! I changed the script so it’s as simple as may be

from conexao import consultar_tickets, consultar_tickets_ex, campos_extras
import streamlit as st
import pandas as pd
import locale
from datetime import datetime
from menu import menu

st.set_page_config(layout='wide', page_title='Dashboard Projetos')

if 'filtros' not in st.session_state:
    st.session_state.filtros = None

locale.setlocale(locale.LC_ALL, '')

menu()

st.write(st.session_state.filtros)

@st.cache_data(ttl='30m')
def carregar_dados():
    aux = consultar_tickets_ex()
    ext = campos_extras()

    tickets = pd.merge(aux, ext, how='left', on='ticket')

    _aux = tickets[tickets['status'].isin(['Aguardando', 'Em atendimento', 'Novo', 'Parado'])]

    return _aux


til1, til2 = st.columns([0.9,0.08],gap='small')
with til1:
    st.title('Gestão Excelência')
with til2:
    st.write('teste')


st.divider()

dados_aux = carregar_dados()


dados_aux['Dias_sol'] = float('nan')
dados_aux['Dias_Abert'] = float('nan')

def diferenca_de_dias(row):
    if pd.notna(row['prevsolucao']):
        data_especifica = row['prevsolucao']
    else:
        data_especifica = row['dtcriacao']
    diferenca = data_especifica.date() - datetime.now().date()
    return diferenca.days

def diferenca_de_dias_abert(row):
    data_especifica = row['dtcriacao']
    diferenca = data_especifica.date() - datetime.now().date()
    return diferenca.days


dados_aux['Dias_sol'] = dados_aux.apply(diferenca_de_dias, axis=1)
dados_aux['Dias_Abert'] = dados_aux.apply(diferenca_de_dias_abert, axis=1)


colunas = ['serv1', 'serv2', 'urgencia', 'urgencia_aux', 'urgencia_id', 'idresponsavel', 'equiperespons',
           'IDUSUARIO', 'USUARIO', 'yb', 'esforco', 'impacto', 'ie', 'setor', 'dias']


st.dataframe(dados_aux, key='filtros', selection_mode='multi-row', on_select='rerun')

A still get the error:
Values for st.button, st.download_button, st.file_uploader, st.data_editor,
st.chat_input, and st.form cannot be set using st.session_state.

If I create another st.write object and rerun the page, the st.dataframe appears normally, without any errors and the rows selection works perfectly

Please make this code much simpler, I’d shoot for something like 5-10 lines, as simple as possible while still showing the error.

For what it’s worth, I tried to run your code and could not because of the imports of different functions I don’t have access to, but I also see numerous things that are most likely unrelated to the error, and can be remove (e.g. setting the locale, setting up the columns, setting the title, pandas transformations, etc.

I tried removing all the pieces I don’t have access to, like the conexao imports, and I didn’t see the error you mentioned, so I expect it’s coming from some other file in your code.

Please try to create a simple script which anyone else could run, that doesn’t depend on importing extra modules like conexao in your example, and still shows the error. This will also help you in your own debugging, so you can narrow down the problem to the lines that are actually causing the error.