St.text_area issue

I’m trying to read file, update, then save.

with open(r’C:\Test\test.txt’, ‘r’) as f:
input = f.read()

text1 = st.text_area(‘SQL Statement as below:’, input, 500)

if st.button(‘Save’):
with open(r’C:\Test\test.txt’, ‘w’) as f:
f.write(text1)

but it only works at first ‘Save’ button clicked.

when I tried to update the text1 and click ‘Save’ button at the second time, text1 still shows the previous value. the change is not saved.

Hi @calvin , what do you mean by tried to update the text1? How are you updating it? Are you expecting the lines

with open(r’C:\Test\test.txt’, ‘w’) as f:
f.write(text1)

to update the text? If so, wouldn’t it have the same text?

I’m trying to read file content, and put it in the text area so I can update it, then write the update content back to the file if ‘save’ button clicked.

please see the screen below:

1, the first time, the file content is empty, if I input ‘the first time’ and click save. and it writes update content into the file.

2, but if I change content again, like input ‘Second time’, then click ‘savf’, the text in text area will show ‘the first time’ which is previous value.

Hi @calvin , it looks like what is happening is that if you are not doing a command + enter (on mac at least, im not sure how that translates to windows since it looks like ure using a windows machine), then the text does not save. As a result, the script will rerun and the text will not be written to the file.

Just to summarize, this is expected. The solution would be to make sure you’re doing a command + enter and then clicking save.

I’m not sure about a workaround but you can maybe try asking the community for a creative solution!

Sorry for the late reply; I was in yosemite and didn’t have signal!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.