Updating text file

Hi, I have a text file that stores a variable for me but I can’t update the text file in streamlit.(this makes sense because it gets code from GitHub and you can’t update that) I was wondering if there was a way to do this

Hi @ShadowUTube, welcome to the forum!

You’re correct that storing the data in a text file is not ideal, at least when hosting on Streamlit Cloud, as that text file won’t exist permanently unless it’s saved in GitHub.

In general, it depends how long you need that variable (and any changes to it) to exist:

  • If it just needs to exist for one app run, then you don’t need to do anything special, just use a normal python variable
  • If it needs to exist for as long as a given user is using the app, you can use st.session_state Session State - Streamlit Docs
  • If the variable needs to exist permanently (including any changes to it), you’ll probably want to use a database of some sort to store the data. Streamlit doesn’t have a built-in database yet (though we’re looking into adding a build-in database in the near future to make problems like this easier), but there are a number of guides for connecting to external databases Connect to data sources - Streamlit Docs

Ok, thanks for all the help

@blackary If you connect to a database, can you save variables to the database (and not just call it)? For example, I have an OpenAI agent that builds a company profile, but I want my user to be able to save that profile and use it next session.
Then, on the next session, I want my user to load that variable, edit it, and save it again. Is that possible with streamlit? (I’ve read the streamlit docs)

Yes, you can save information to a database from a Streamlit app. Streamlit is just a means to create the user interface for your Python code. So any database or data source out there that lets you read and write through a Python API can be used with Streamlit to do the same.

In your case, if you create a database to store your user profiles, you can read and write to those profiles through that databases Python API.