New library: streamlit-server-state, a new way to share states across sessions on the server

oh, it’s a bug. Thank you for reporting.
I created a ticket and will track the topic there:

Is this module still being maintained?

Yes

1 Like

Dear Whitphx, thanks a lot for this library. Unfortunately it does not seem to work with Streamlit 1.18.1 - looks like the session management functions have been moved to runtime.session_manager.
I also get error on _get_session_info:
venv\lib\site-packages\streamlit_server_state\session_info.py", line 27, in get_this_session_info
session_info = current_server.runtime.session_manager.get_session_info(session_id)

I’m trying to get the app_chat.py running.
Please advice,
thanks for your help

The fix would be included in v0.15.2.
Please try this new version.

Can ‘server_state[“chat_messages”]’ be printed on ‘text_area’ one line at a time?

I think it’s not this library’s responsibility.
Please modify the code to do what you want.

Hello.
I tried to add more functions to this library that I needed.

First,
I wanted to show the name of the person participating in the chat.

Second,
typing text in the text box and typing Enter will initialize the text box.

I searched the method of initializing the text box and applied the following method.

def on_message_input():
    st.session_state["message_input"] = ""  # clear text_input

The code worked well, but if there are multiple users, there is a problem that if someone enters the text first, the other person’s screen reloads at the same time, and the text being written is initialized.

It works well when used without initialization, but the process of erasing all text to rewrite the text feels like a huge waste.

My code may look messy, but I’d appreciate it if you could look at it from a beginner’s point of view.

Any advice from you will be appreciated.

import streamlit as st
from datetime import datetime,timedelta
from streamlit_server_state import server_state, server_state_lock, no_rerun

def on_message_input():
    new_message_text = st.session_state["message_input"]
    server_state["user"] = [nickname]

    if not new_message_text:
        return

    new_message_packet = {
        "nickname": nickname,
        "text": new_message_text,
        "time": (datetime.utcnow()+timedelta(hours=9)).strftime('%H:%M:%S')
    }
    with server_state_lock["chat_messages"]:
            server_state["chat_messages"] = server_state["chat_messages"] + [
                f"{new_message_packet['nickname']} : {new_message_packet['text']} \n {new_message_packet['time']}"
            ]

    st.session_state["message_input"] = ""

e = st.empty()
nickname = e.text_input('Nickname')

if nickname:
    e.empty()
    st.write(f"### Hi, {nickname}🎈")

    with server_state_lock["chat_messages"]:    
        if "chat_messages" not in server_state:
            server_state["chat_messages"] = []
            
    if "user" not in server_state:
        server_state["user"] = []

    else:
        if nickname not in server_state["user"]:
            server_state["user"] = [nickname] + server_state["user"]

    if st.button('claer'): 
        server_state["chat_messages"] = []

    if st.button('session_clear'): 
        st.session_state.clear()
        server_state.clear()

    st.info('\n'.join(set(server_state["user"])))
    st.text_input("Message", key="message_input", on_change=on_message_input)
    st.text_area('Chat','\n'.join(server_state["chat_messages"][::-1]), height=150)

    st.write(server_state.chat_messages)
    st.write(st.session_state.message_input)
    st.write(server_state.user)

Thank you.

Hi @whitphx,

I wanted to ask about multipage Streamlit applications. When this functionality is used, on rerun the default behavior is to redirect to the main page of the application. Is there an existing way to redirect the user back to the page they were previously on? Thank you!

@Shaarson It looks like a bug reported at Multi page application · Issue #167 · whitphx/streamlit-server-state · GitHub, which has not been fixed.
Please be patient until the fix will be released.
Thanks.