Hello Everyone!
I’m going about converting an app from Flask to Streamlit. I’ve been stuck on this portion on the project so far for a while now. I created this more simple version of what I’m trying to accomplish (this was completely created for this post, so there may be some things I overlooked). The idea is that someone would go through a spreadsheet and tag some data. The main issue is that if you type in a rating or a comment, and then go to the next entry, if that entry is blank, the old values for the previous index will stick around. If you were to go up and down through the table to where its forced to override the values in the input columns, then it will look like it ‘fixed itself’. Is there a way to force Streamlit to update input values, without there having to be a change first? Not sure is there is a term for this. I’ve tried using SessionState for the comment and rating values but that also doesn’t seem to work.
I’m using the latest version of Streamlit and SessionState.
I think this issue could be related to this: Documentation needed around manipulating st.text_input elements · Issue #623 · streamlit/streamlit · GitHub
Thanks!
import streamlit as st
import SessionState
import numpy as np
import pandas as pd
ss = SessionState.get(index=0)
@st.cache(allow_output_mutation=True)
def load_data():
df = pd.DataFrame.from_records([
('pizza', 'yes', 'very good'),
('ice cream', 'maybe', 'had a bad experience'),
('corn','',''),
('cake','',''),
('candy','',''),
('hot dog','',''),
('fries','',''),
('sandwich','',''),
('chili','',''),
], columns=['Food', 'Would You Eat?', 'Comments'])
return df
@st.cache(suppress_st_warning=True, allow_output_mutation=True)
def define_layout():
data = st.empty()
index = st.empty()
up = st.empty()
down = st.empty()
food = st.empty()
rating = st.empty()
comments = st.empty()
update = st.empty()
layout = {
'data':data,
'index':index,
'up':up,
'down':down,
'food':food,
'rating':rating,
'comments':comments,
'update':update
}
return layout
def main():
df = load_data()
layout = define_layout()
rating_map = {
'':0,
'yes':1,
'no':2,
'maybe':3
}
if layout['up'].button('Up'):
ss.index = ss.index + 1
if layout['down'].button('Down'):
ss.index = ss.index - 1
food_val = layout['food'].write(f"Food: {df.at[ss.index, 'Food']}")
index_val = layout['index'].text_input('Data Index', value=ss.index)
rating_val = layout['rating'].selectbox('Would You Eat?', ['', 'yes', 'no', 'maybe'], index=rating_map[df.at[ss.index, 'Would You Eat?']])
comment_val = layout['comments'].text_input('Comments', value=df.at[ss.index, 'Comments'])
if layout['update'].button("Update"):
df.at[ss.index, 'Would You Eat?'] = rating_val
df.at[ss.index, 'Comments'] = comment_val
layout['data'].write(df)
if __name__ == "__main__":
main()