streamlit how do i store the value of a variable in a cache?

Guess I needed this SessionState gist copied in a SessionState.py file.
I initialize a variable session_state = SessionState.get(name='', img=None).
In the option==1, assigned session_state.img = my_image and then used it to get to my_image in option==2.

import streamlit as st
import cv2
import SessionState

def Camera():
    frames1 = st.empty()
    button1 = st.button('DONE', key=0)
    while cap.isOpened():
        frames1.image(cap.read()[1], channels="BGR")
        if button1:
            my_image = cap.read()[1]
            cap.release()
            break
    return my_image


option = st.sidebar.radio('Choose the option', [0, 1])
session_state = SessionState.get(name='', img=None)
if option == 0:
    cap = cv2.VideoCapture(0)

    my_image = Camera()
    st.image(my_image, channels="BGR")
    session_state.img = my_image

if option == 1:
    my_image = session_state.img
    st.image(my_image, channels='BGR')

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top