How to make sidebar as navigation bar?

Hi team,

I am exploring Streamlit to use for one of my projects, and I have a requirement of having sidebar as navigation bars, means these should be selectable and should show only the content falling under it.

Can you please guide me how to do that?

Thanks.

Is something like a multipage app what you want?

1 Like

Hi Goyo,

Thanks for the reply. Yes its like multipage app, but how can I access same data between multiple pages. For example first page is importing data (Import.py), second page is correlation plot of same data (EDA.py), so same data has to be stored in 2 py files right? How to access same data on both pages if I use multipage app?

Thanks.

I don’t have experience with multipage apps but I assume that st.session_state will be shared amongst all pages.

@Prashant Yes, what @Goyo suggests is correct. If you import data in one page, one of the best options is to put that data in st.session_state to use it on other pages.

Hi Zachary, Hi Goyo,

Thanks for all replies. I really appreciate it.

I tried using st.session_state as per the streamlit documentation but still Its not working for me.

I have 3 pages app2 page (which is home page for me ) and other 2 pages which are multipage app and multipage2 app.

I am importing data in app2 page, and when I set session in multpage app - then its showing below error

I have included below code in app2.py

if “shared” not in st.session_state:
st.session_state[“shared”] = True

and this below code in multipage app

import streamlit as st
st.write(st.session_state[“shared”])

Can you please help me on this?

Thanks,
Prashant

In Multipage_app2.py you have to assign a value to data before calling data.head(). That is how python usually works, you need to define things before you can use them.

ok, but I am importing data in data variable in app2 page already through file uploader,
So my need is to take same data and do some other analysis in other page, lets say multipage app page.

Am I missing something here?
Thanks.

You are missing the part where app2.py stores the data in st.session_state so that Multipage_app2.py can find the data in `st.session_state’.

@Prashant Something like this should work

# page 1
import pandas as pd
import streamlit as st

csv = st.file_uploader("Upload a file", type="csv")

if csv is not None:
    df = pd.read_csv(csv)
    st.session_state["data"] = df


# page 2
import streamlit as st

if "data" in st.session_state:
    df = st.session_state["data"]

    st.write(df)


# page 3
import streamlit as st

if "data" in st.session_state:
    df = st.session_state["data"]

    st.write(df)

Thanks a ton Zachary. Yes your code worked for me, however when I go back to home page data disappears. Can you let me know how to save data on home page even if I toggle between multi pages.

Thanks,
Prashant

@Prashant, it may be the case that the file uploader no longer shows the uploaded file, but it will still be saved in st.session_state. You can access that data on the main page the same way you do on the other pages.

import pandas as pd
import streamlit as st

csv = st.file_uploader("Upload a file", type="csv")

if csv is not None:
    df = pd.read_csv(csv)
    st.session_state["data"] = df

if "data" in st.session_state:
    df = st.session_state["data"]

    st.write(df)
1 Like

Thanks a lot Zachary, You are a champ !!
It worked for me. I am new to streamlit so glad to have helping hands like you :slight_smile:

2 Likes