How to edit values in a dataframe

Say I am showing a dataframe on the app, is it possible to edit the value of a dataframe on the app and save those changes? Basically an editable dataframe column.

1 Like

Hi @adi.kadrekar, welcome to Streamlit!

This isn’t currently possible; edits to the dataframe need to be made on the Python side.

You could work around this with, e.g., several st.text_inputs that specify which dataframe entry to edit, and what its new value should be - but I imagine thats more onerous than what you’re looking to do.

Could you explain your use case in more detail? Are you interested in tweaking some values in a dataframe and having the app re-run with those new values, or are you looking to do something purely browser-side where the dataframe changes don’t need to get sent back to your Python script?

Hello Tim,

I am looking more from a database perspective. I am generating textual commentary for the tabular data(dataframe) I show on the screen. I want to be able to store this commentary say on a database and I want my users to be able to edit this commentary on the app. Looking more from a backend perspective. Like connecting the app to a database. Is there any provision for that?

Thanks

Ah, now I understand your “editable dataframe column” better; this would map to a database column that you want people to edit via the Streamlit app, right?

There isn’t anything that does exactly what you’re looking for. You could write an app that fetches from your database and generates an st.text_field for each row that you want to edit. If there are only a few rows to edit (like on the order of a dozen or so), this might be reasonable, but if it’s a larger number, Streamlit may not not easily fit to your use case.

However: we’re currently planning on a plug-in architecture for frontend interactive widgets, so this is the sort of thing we hope to enable users to do in the future!

If you want to follow progress on the plug-in idea, you can watch this Github issue: https://github.com/streamlit/streamlit/issues/327

Yes, that is right.

I think it’s going to be a large number of edits.
This st.text_field for each row which you mentioned. If a user edits a field, will that information be saved in the dataframe? If that’s the case I could just save the dataframe with the latest edits on my machine.

What do you mean by plug-in architecture here? I will add my requirement on the git page.

Can I deploy this application? I didn’t dig into it but I saw some posts on AWS deployment.

Thanks

When an st.text_field is edited, your Python script is re-run, and the text_field function returns the value that was just entered on the browser. Then you could write this new value to the correct field in the database.

However, I don’t want to lead you too far down an unproductive path; because your python script is re-run from top to bottom each time any text field (or any other streamlit UI) is manipulated on the browser, you’d need to jump through some hoops (probably via @st.cache) to keep things performant so that you’re not sending tons of pointless updates to your database when any text field changes.

The key thing to understand is that a Streamlit app is (mostly) stateless. This is by design, as it keeps many use cases much simpler, but it means that there’s no event loop, and no way of saying “call this function when this value changes on the browser.” Any time a value changes, your entire script is re-run from top to bottom.

1 Like

Re: plug-in architecture: currently, Streamlit ships with a number of interactive widgets that you can use in your app. There’s st.button, st.radio, st.slider, etc. These are appropriate for many apps, but they don’t cover every possible use case.

With a plug-in mechanism, users would be able to write their own widget classes in JavaScript or TypeScript, and then create instances of those widgets from their apps, rather than being limited to the widgets that Streamlit ships with.

I got it. Thanks for the detailed explanation.
I understand now exactly what can I expect from my application.

Hi Tim,

Regarding your question:

“Are you interested in tweaking some values in a dataframe and having the app re-run with those new values”

Is there something in your roadmap to allow this functionality? Here is my use case:
I have a user upload an Excel file with a list of search queries, the app would extract terms from the list and create a lexicon of terms stored as a DataFrame. Then the user would delete rows or add rows to this lexicon. So far so good. Then, the user would use this lexicon created on the app to run other analysis. The user may want to go back to the lexicon and update it. This is where things are braking for me because Streamlit is running the script again from top to bottom losing all the prior changes.
Thanks!

Hi @eribero -

Your question sort of sounds like maintaining session state. Maybe take a look at this post or our session-state tag to see if it helps with this problem.

In general, the answer is yes, we’re working on functionality to make these sorts of applications easier, but I don’t know if we’ve thought about the issue directly at the Excel or single-cell level.

Best,
Randy

Thanks for the note and links Randy!

1 Like