Open duolabmeng6 opened 1 month ago
would local storage work for multi-node scalable implementation?
would local storage work for multi-node scalable implementation?
This is just an example for maintaining state.
fyi, I'm building off of this: https://github.com/smboy/streamlit-auth (Forked)
You can check it out here: https://hugozhu.streamlit.app/
This seems to retain the user (google user or otherwise) and continues to maintain the state as it uses extra_streamlit_components
and st.cache_data
to store the user/session/uuid which is pretty good.
However, I have challenges navigating to other pages with this code.
fyi, I'm building off of this: https://github.com/smboy/streamlit-auth (Forked) You can check it out here: https://hugozhu.streamlit.app/ This seems to retain the user (google user or otherwise) and continues to maintain the state as it uses
extra_streamlit_components
andst.cache_data
to store the user/session/uuid which is pretty good. However, I have challenges navigating to other pages with this code.
pages/Translation_Assistant.py
import streamlit as st
import asyncio
st.set_page_config(page_title="Translation Assistant", page_icon="🌐")
import st_local_storage
st_ls = st_local_storage.StLocalStorage()
login = st_ls.get('login')
if not login:
st.warning("Please login first.")
st.stop()
@st.cache_data
async def translate_text(text, source_lang, target_lang):
# Replace with actual API call
await asyncio.sleep(1) # Simulating API call
return f"Translated: {text}"
async def main():
st.title("🌐 Translation Assistant")
source_lang = st.selectbox("Source Language", ["English", "Chinese", "Spanish"])
target_lang = st.selectbox("Target Language", ["Chinese", "English", "Spanish"])
text = st.text_area("Enter text to translate:")
if st.button("Translate"):
with st.spinner("Translating..."):
translated_text = await translate_text(text, source_lang, target_lang)
st.success(translated_text)
if __name__ == "__main__":
asyncio.run(main())
login.py
import streamlit as st
import st_local_storage
st_ls = st_local_storage.StLocalStorage()
login = st_ls.get('login')
if login:
st.markdown(f"### 欢迎回来, **{login}**")
if st.button("退出登录"):
st_ls['login'] = None
else:
st.markdown("## 用户登录")
username = st.text_input("用户名", max_chars=30, placeholder="输入用户名")
password = st.text_input("密码", type="password", max_chars=30, placeholder="输入密码")
if st.button("登录"):
if username == 'admin' and password == 'admin':
st_ls.set('login', username)
st.success("登录成功!正在跳转...")
else:
st.error("账号或密码错误")
streamlit run login.py
I tested the multi-page state retention with no problems
streamlit 的cookie库挺坑的 这里我重新写了一份能用的 streamlit 登录后保持登录状态的事例
st_local_storage.py
login.py
运行