File permisson error on streamlit.sharing

I recently deployed this app on share.streamlit. th eapp allows the user to edit scenarios and saves every change to a json file. the user can also create new scenarios. this works well locally, but on share.streamlit.io, I get the error. PermissionError: [Errno 13] Permission denied: ‘scenarios.json’. This happens with this code:

        with open('scenarios.json', 'wt') as myfile:
            json.dump(self.scenarios, myfile)

where scenarios is a dictionary holding all scenarios (to start with jst the default one).

The full code can be found on github.
Am I not allowed to save changes on share.streamlit? Is there an alternative to persist user input for future sessions? Thanks in advance on any insight on my problem.

Try to use a different folder like:

/home/appuser
/tmp

But it looks like each user will overwrite the files of the previous one, right?

hi @franciscoed, thank you for your suggestion, but moving the file to a folder did not help, still getting the "PermissionError: [Errno 13] Permission denied: ‘./tmp/scenarios.json’ " error. Eventually, I will add the username to the settings dictionary, so all users have their own setting, but first I need the basics to work. Also, I wonder: how do I prevent each deployment to overwrite my most recent settings generated during a session?

I would like to know, if it is generally not possible to write files on streamlit.sharing. I made the simple example below which works locally but not when deployed to streamlit sharing (again, error: PermissionError: [Errno 13] Permission denied: ‘demofile.txt’). What would be the alternative if I need the users to generate new scenarios and keep the saved scenarios persistent?

import streamlit as st

def main():
    st.write('testing writing a file')
    f = open("demofile.txt", "w")
    f.write("Now the file has some content!")
    f.close()
    f = open("demofile.txt", "r")
    st.write(f.read())
if __name__ == '__main__':
    main()

Write to /tmp/ folder

i.e:

import streamlit as st

def main():
    st.write('testing writing a file')
    f = open("/tmp/demofile.txt", "w")
    f.write("Now the file has some content!")
    f.close()
    f = open("/tmp/demofile.txt", "r")
    st.write(f.read())
if __name__ == '__main__':
    main()

thanks for your answer @franciscoed. I tried this, but it does not seem work in the streamlit-sharing environment either. still getting the permission error, while the same code works locally.

I’ve checked your env, you’re using

"./tmp/demofile.txt"

instead of

"/tmp/demofile.txt"

Remove this dot on the beginning

Yes it worked! However now it does not work locally anymore, since I am working on windows and /tmp produces a file not found error. However the issue is now clear and I simply used os.path.join to build the filename which now works on linux and windows. I was searching the error in the wrong direction. Thanks again!