4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Steamlitでブラウザ更新後もセッションIDを保持する

Posted at

変更前st.session_stateだとブラウザ更新の度にセッションIDが変わってしまうためst.query_paramsを使用してセッションIDを永続化

  • 変更前
import streamlit as st
import uuid

if "session_id" not in st.session_state:
    st.session_state.session_id = str(uuid.uuid4())

st.markdown(st.session_state.session_id)
  • 変更後
import streamlit as st
import uuid

if "session_id" in st.query_params:
    session_id = st.query_params.session_id
else:
    session_id = str(uuid.uuid4())
    st.query_params.session_id = session_id

st.markdown(st.query_params.session_id)

参考:st.query_params

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?