I am implementing a streamlit Chatbot where I have to save the chat log state as a file.
(Could not save a state as a variable since every time, user input a new chat, streamlit will re-run all codes from the beginning, so the variable always got reset )
My questions are:
how can I get a unique user-connection ID?
e.g. when user-A connect, he will get a chat-log file-A , and when user-B connect, she will get a chatlog file-B .
Is there a way to know if the connection is close for like 30 minutes ? (similar to Colab), so that I can delete the chatlog of that user
I am a relatively new user but I did something related recently…
For Q1 you could try this:
from streamlit.report_thread import get_report_ctx
def _get_session():
import streamlit.report_thread as ReportThread
from streamlit.server.server import Server
session_id = get_report_ctx().session_id
session_info = Server.get_current()._get_session_info(session_id)
if session_info is None:
raise RuntimeError("Couldn't get your Streamlit Session object.")
return session_info.session
Then you can get a unique session ID for each connection using the line user_session = _get_session()
That user_session parameter does not change even when things are reloaded (ie. the streamlit state changes). Then you can use that user_session parameter in the file name (unique for each user).
I am not sure about question 2, but perhaps you can append the datetime in the chat file and check every 5 minutes to see if it is 30 minutes old?
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.