Hi,
I am very new to Streamlit. So for a project, I have been trying to make st.text_input editable. So far, I have tried something like this:
for i, question in enumerate(st.session_state.question_list):
st.session_state.idx = i
st.sidebar.subheader(f"Question {i+1}")
st.session_state.edited_question = st.sidebar.text_input(
f"Edit the question {i+1}: ",
value = question,
key="widget3",
on_change=on_press
)
But the problem is, maybe because of Streamlit’s refreshing the whole page behaviour, the module is not getting edited and the entire loop is getting executed at once.
Is there any workaround to this problem?
I am basically looking for like below:
Let’s say for example, someone already asked a question:
What is your name.
Now, they want to edit the question and add a question mark after it (for example).
Then they will press the “Edit” button, and the question will be editable. After their edit, people can press the Done/Complete button to continue on to further functions.
I am really stuck in here. I would be very grateful if anyone pointed me in the correct direction. Thanks in advance.
Hi, thanks for the code snippet but I am trying to edit text field. So, for this solution I have to convert the list into dataframe and then edit that datafrmae, isn’t it?
Yes, it won’t be empty because the list will be initiated after user inputs. And then I can transfer it to Pandas df.
Thanks a lot for the suggestions.
if st.sidebar.button("Edit") and pdf_docs:
data = pd.DataFrame(st.session_state.question_list, columns=["Questions"], dtype=object)
edited_df = st.sidebar.data_editor(
data = data,
column_config = {
"questions": st.column_config.TextColumn()
},
num_rows="dynamic"
)
Now, the problem is that after the edit, I want to see the updated dataframe. But when I press the “Edit” button, the entire block gets executed. Rather, I want to update rows, save them, and then work on the updated dataframe. Is it actually possible in Streamlit? Because, with the st.text_input() function, I faced the same problem.
Do I need an “on_change” callback to locate where the change has happened and update accordingly?
Hi, I tried this, and it did not work either. The problem is actually providing editing facilities dynamically. That specific functionality cannot be handled with a loop, as the editing option requires a st.text_input() type function that runs all the way until the end of the loop and does not wait for the value. I could not figure out that solution. I tried with form, the same thing happens. I guess dynamically row by row eidt is still upcoming or maybe I am thinking totally wrong.
My code so far (with form):
if st.sidebar.button("Edit") and pdf_docs:
st.session_state.data_frame = pd.DataFrame(st.session_state.question_list, columns=["Questions"], dtype=str)
with st.sidebar.form("Edit Data"):
for idx, row in st.session_state.data_frame.iterrows():
st.write(row["Questions"])
if edit_input:= st.text_input("Question", key=f"edit_question{idx}"):s
# print("Edited Input: ", edit_input)
st.session_state.edited_questions[idx] = edit_input
So, I have to leave this part, I guess, and do something else to mitigate this. Thank you a lot by the way for all these suggestions.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.