I have encountered a very similar problem as reported by @Raphael_Fabre. @Hiro’s code works on my machine, but my code does not, even when modified explicitly according to Hiro’s protocol. Even when I check for changes between text_input input and output explicitly, there are no differences. It appears that text_input is not outputting updated values. Does this mean that this function has been corrupted somehow?
I share my code below. It is for a list editor. It is based on @Subrat_Mohapatra’s data editor example. I had decided that data_editor could not provide the functionality that I was looking for and that I should write my own list editor widget. I figured that would be easy-peasy. Little did I suspect!
I have not added the move up and move down functionality yet. That should be no problem. But when I try to add or edit a record I am unable to get updated input. It is the darnedest thing. What could be simpler than line data input widget. I have used them many times before with no problem. I have no idea what’s going on!
# started from https://discuss.streamlit.io/t/how-to-add-delete-a-new-row-in-data-editor-in-streamlit/70608
import streamlit as st
import pandas as pd
import sys
import os
from datetime import datetime as dt
def findSel(zItem, zList):
for i in range(0,len(zList)):
if zItem==zList[i]:
return(i)
return(None)
if 'pIndex' not in st.session_state:
st.session_state.pIndex=0
st.session_state.add=False
st.session_state.i=0
# Create a variable to hold the dataframe. Initialize it with the given list.
if 'zList' not in st.session_state:
list0 = [{'name': 'Love', 'year':1991},
{'name': 'Smells Like Teen Spirit', 'year': 1991},
{'name': 'Lithium', 'year': 1992},
{'name': 'All Apologies', 'year': 1993},
{'name': 'Stay Away', 'year': 1993}]
st.session_state.df = pd.DataFrame(list0)
st.session_state.zList=list(st.session_state.df['name'])
st.header('List Editor')
with st.container(border=True):
col1, col2 = st.columns([2, 1], gap='medium')
with col1:
# added num_rows='dynamic'
st.session_state.resp = st.radio(label="---", options=st.session_state.zList, index=st.session_state.pIndex, label_visibility='collapsed')
st.session_state.i+=1
print("now, i, resp", dt.now(), st.session_state.i, st.session_state.resp)
st.session_state.lenList=len(st.session_state.zList)
if st.session_state.resp:
st.session_state.pIndex=findSel(st.session_state.resp,st.session_state.zList)
print("resp, pIndex", st.session_state.resp, st.session_state.pIndex)
st.session_state.add=False
with col2:
Edit = st.button("Edit", use_container_width=True)
st.session_state.Add = st.button("Add", use_container_width=True)
Delete = st.button("Delete", use_container_width=True)
MoveUp = st.button("Move Up", use_container_width=True)
MoveDown = st.button("Move Down", use_container_width=True)
save = st.button('Save to data_temp.csv')
#if st.session_state.Add or st.session_state.add:
if True:
st.session_state.add=True
cz1, cz2 = st.columns(2)
with cz1:
if st.button("Insert Above", use_container_width=True):
newItem=st.text_input("Enter list value", "____", key=101)
if newItem:
print("newItem above", newItem)
st.session_state.zsList=st.session_state.zList.insert(st.session_state.pIndex, newItem)
print("zList", st.session_state.zList)
#st.session_state.add=False
with cz2:
if st.button("Insert Below", use_container_width=True):
st.session_state.newItem=st.text_input("Enter list value", "____", key = 102)
if st.session_state.newItem:
print("newItem", st.session_state.newItem)
st.session_state.zsList=st.session_state.zList.insert(st.session_state.pIndex+1, st.session_state.newItem)
print("zList below", st.session_state.zList)
#st.session_state.add=False
if Edit:
if st.session_state.pIndex:
st.session_state.targ=st.session_state.zList[st.session_state.pIndex]
changedItem = st.text_input("Enter list value", st.session_state.zList[st.session_state.pIndex],key='EDIT')
if changedItem != st.session_state.targ:
print("changedItem", changedItem)
st.session_state.zList[st.session_state.pIndex]=changedItem
print("zList", st.session_state.zList)
if Delete:
del st.session_state.zList[st.session_state.pIndex]
if st.session_state.pIndex==st.session_state.lenList-1:
st.session_state.pIndex-=1
st.session_state.lenList-=1
st.rerun()
if save:
with open('listTest.csv', 'wb') as myfile:
import csv
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(st.session_state.zList)
if 'df' not in st.session_state:
st.session_state.zList = list(pd.read_csv('listTest.csv'))