To maintain session_state after coming back from other pages

hi,
i have created an app where the app has app.py and pages like home and un . in un i upload video and do some basic skipping, after clicking home in button i navigate to home, then after returning to the un from home the video data i uploaded vanishes. Please Guide me how can i do it. i also added the session_state but its not mainting itPreformatted text

ill share the code:

un.py

import streamlit as st
from lyrics import lyric
from lyrics import eve_odd
from streamlit_extras.switch_page_button import switch_page
js = '''
<script>
    var body = window.parent.document.querySelector(".main");
    console.log(body);
    body.scrollTop = 0;
</script>
'''


with st.container():
    vid=st.file_uploader("upload a file", type="mp4")
    st.session_state.video=vid
    st.empty()

container_placeholder = st.container()

a=3


goToHome, left, middle, backtotop ,right=st.columns(5)
if vid:
    with left:
        click1=st.button("0:15")

    with middle:
        click2=st.button("0:30")

    with right:
        refresh=st.button("refresh")

    with backtotop:
        back=st.button("back_to_top")
    
    with goToHome:
        home=st.button("Home")

    if "counter" not in st.session_state:
        st.session_state.counter=0

    with container_placeholder:
        if vid is not None:
            if click1:
                st.session_state.counter = 15
                st.video(st.session_state.video, start_time=st.session_state.counter)
                st.write(lyric)
            elif click2:
                st.session_state.counter = 30
                st.video(st.session_state.video, start_time=st.session_state.counter)
                st.write(lyric)
            elif refresh:
                st.video(st.session_state.video, start_time=st.session_state.counter)
                st.write(lyric)
                st.markdown("<a href='http://localhost:8501/' target='_blank'>Click here to refresh</a>", unsafe_allow_html=True)
            elif back:
                st.video(st.session_state.video, start_time=st.session_state.counter)
                st.write(lyric)
                #eve_odd(a)
                st.components.v1.html(js)
            elif home:
                switch_page("scroll")
            else:
                st.video(st.session_state.video, start_time=0)
                st.write(lyric)
else:
    st.write("")
    

scroll.py

import streamlit as st
import time
from streamlit_extras.switch_page_button import switch_page

st.title("Home")

st.write("""

Machine LearningIt is one of the applications of AI where machines are not explicitly programmed to perform certain tasks; rather, they learn and
improve from experience automatically. Deep Learning is a subset of machine learning based on artificial neural networks for
predictive analysis. There are various machine learning algorithms, such as Unsupervised Learning, Supervised Learning, and
Reinforcement Learning. In Unsupervised Learning, the algorithm does not use classified information to act on it without any
guidance. In Supervised Learning, it deduces a function from the training data, which consists of a set of an input object and the
desired output. Reinforcement learning is used by machines to take suitable actions to increase the reward to find the best possibility
which should be taken in to account.
Natural Language Processing(NLP)
It is the interactions between computers and human language where the computers are programmed to process natural languages.
Machine Learning is a reliable technology for Natural Language Processing to obtain meaning from human languages. In NLP, the
audio of a human talk is captured by the machine. Then the audio to text conversation occurs, and then the text is processed where
the data is converted into audio. Then the machine uses the audio to respond to humans. Applications of Natural Language
Processing can be found in IVR (Interactive Voice Response) applications used in call centres, language translation applications
like Google Translate and word processors such as Microsoft Word to check the accuracy of grammar in text. However, the nature
of human languages makes the Natural Language Processing difficult because of the rules which are involved in the passing of
information using natural language, and they are not easy for the computers to understand. So NLP uses algorithms to recognize
and abstract the rules of the natural languages where the unstructured data from the human languages can be converted to a format
that is understood by the computer.
Automation & RoboticsThe purpose of Automation is to get the monotonous and repetitive tasks done by machines which also improve productivity and
in receiving cost-effective and more efficient results. Many organizations use machine learning, neural networks, and graphs in

automation. Such automation can prevent fraud issues while financial transactions online by using CAPTCHA technology. Robotic
process automation is programmed to perform high volume repetitive tasks which can adapt to the change in different circumstances.
Machine VisionMachines can capture visual information and then analyze it. Here cameras are used to capture the visual information, the analogue
to digital conversion is used to convert the image to digital data, and digital signal processing is employed to process the data. Then
the resulting data is fed to a computer. In machine vision, two vital aspects are sensitivity, which is the ability of the machine to
perceive impulses that are weak and resolution, the range to which the machine can distinguish the objects. The usage of machine
vision can be found in signature identification, pattern recognition, and medical image analysis, etc.
Knowledge-Based Systems(KBS):
A KBS can be defined as a computer system capable of giving advice in a particular domain, utilizing knowledge provided by a
human expert. A distinguishing feature of KBS lies in the separation behind the knowledge, which can be represented in a number
of ways such as rules, frames, or cases, and the inference engine or algorithm which uses the knowledge base to arrive at a
conclusion.
Neural Networks:
NNs are biologically inspired systems consisting of a massively connected network of computational “neurons,” organized in layers.
By adjusting the weights of the network, NNs can be “trained” to approximate virtually any nonlinear function to a required degree
of accuracy. NNs typically are provided with a set of input and output exemplars. A learning algorithm (such as back propagation)
would then be used to adjust the weights in the network so that the network would give the desired output, in a type of learning
commonly called supervised learning.


""")

js = '''
<script>
    var body = window.parent.document.querySelector(".main");
    console.log(body);
    body.scrollTop = 0;
</script>
'''

if st.button("Back to top"):
    temp = st.empty()
    with temp:
        st.components.v1.html(js)
        time.sleep(.5) # To make sure the script can execute before being deleted
    temp.empty()

app.py:

import streamlit as st

def main():
    st.title("Streamlit Multi-pages")
    st.subheader("Main Page")


if __name__ == '__main__':
    main()
2 Likes

Your code below is incorrect.

vid=st.file_uploader("upload a file", type="mp4")
st.session_state.video=vid

But your intention to save the object is right.

To make this code correct, we have to check if vid is not None.

vid = st.file_uploader("upload a file", type="mp4")
if vid is not None:
    st.session_state.video = vid

So now we stored the vid that is not None in session_state. When you navigate to other pages, you can access it through session state video.

However, there is no guarantee that this will work all the time. The uploaded file (stored in RAM) then stored in st.session_state.video can be lost. There can be optimization or design decision, etc. done in session state management that other variables are not kept. Huge file size, memory limit exceeded, etc.

The proper way to get hold of the uploaded file is to store it somewhere else. For example you can save the file in SnowFlake, Deta Space drive, google cloud, etc.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.