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?

1 Like

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

One more use case here. I wrote a script that generates fake product orders for optim problem and would like to display them and show how solver works (selects most profitable orders).

The fucntinality is: press a button to generate the simulated orders, edit them if you want some different orders, hit buttom to solve planning problem.

This fits excel well (eg via xlwings), but as some remote feature - an editale dataframe (small excel-like sheet) would be such a useful addition to steamlit for cases like mine.

There is even a set of open-source spread sheets https://github.com/topics/spreadsheet that may go into a data table editor plugin.

1 Like

Hey, my use case is similar, generate some data, let the user edit and fire up some computation.
Do you have some reference code that you can share ? I would appreciate it.