How to use multiple buttons and keep output using SessionState

I would like to display multiple plotly plots / folium maps on one single page and keep the output when pressing a button or when playing around with other widgets like a multi select box. I have heard about SessionState but I haven’t figured out yet how it works when using buttons.

When clicking on Map 2 the first map disappears because of a rerun of the whole script. Any idea how to tackle this challenge? I haven’t found a concrete button example.

The closest example that explains my issue is here:

import streamlit as st
from streamlit import session_state
import folium
from streamlit_folium import folium_static

def create_basic_map():
    basic_map = folium.Map(location=[43.6532, -79.3832], tiles='openstreetmap', zoom_start=5)
    folium.Marker([43.6532, -79.3832]).add_to(basic_map)
    return folium_static(basic_map, width=500, height=300)


if 'key' not in session_state:
    session_state.key = 0

if st.button('Map 1') or session_state.key == 1:
    # session_state.key = True
    create_basic_map()
    st.write("Hello world 1")

if st.button("Map 2") or session_state.key == 2:
    # session_state.key = True
    create_basic_map()
    st.write("Hello world 2")

if st.button("Map 3") or session_state.key == 3:
    create_basic_map()
    st.write("Hello world 3")

When I click on Map 3:

1 Like

Hi @Danielse, and welcome to the Streamlit community! :balloon:

I’ve not had a chance to check your script, yet it seems you may need to rework your nested conditional mechanism with several AND coditions in order to this to work.

Let us know how you go! :raised_hands:

Best,
Charly

Very curious if anyone has gotten something like this to work