No need main page refresh

dear sir my code executes successfully without errors; however, a problem has emerged where clicking on text transfers it to a function and renders it within an expander, yet this operation causes the page to refresh. i do not wish for this page refresh behavior to occur. Rather, I prefer the data to appear exclusively within the st.expander without initiating a page refresh. Since I have multiple elements distributed across the page and users are actively engaged with the primary interface, a page refresh would terminate all ongoing user activities, necessitating that they re-enter all data to resume their work. My goal is to prevent the page from refreshing. it open to either displaying the data or navigating it to a different page; however, my essential requirement is that the primary page must remain unrefreshed. Please provide a solution and the corresponding code demonstrating how to maintain my primary page data without refreshing, or alternatively, how to store the data in an st.session_state variable, such as st.session_state.textpara, which I can incorporate into my code as required.

import streamlit as st

import streamlit as st

from datetime import datetime

# Get the current date and time

current_time = datetime.now().strftime(“%Y-%m-%d %H:%M:%S”)

# Print it in Streamlit

st.write(f"The current date and time is: **{current_time}**")

if “textpara” not in st.session_state:

st.session_state.textpara = ''

# Check if the specific query parameter was clicked

query_params = st.query_params

if “action” in query_params and query_params[“action”] == “clicked”:

st.session_state.textpara = query_params\["text"\]

st.write("![🎉](https://fonts.gstatic.com/s/e/notoemoji/17.0/1f389/72.png) The text link was clicked!--------",query_params\["text"\])

\# Clear parameters to prevent infinite looping action on refresh

st.query_params.clear()

mfa = “Muhammad”

mfa1 = “1Muhammad”

mfa2 = “2Muhammad”

mfa3 = “3Muhammad”

mfa4 = “4Muhammad”

mfa5 = “5Muhammad”

# Render the clickable text link

st.write(f’{mfa}', unsafe_allow_html=True)

st.write(f’{mfa1}', unsafe_allow_html=True)

st.write(f’{mfa2}', unsafe_allow_html=True)

st.write(f’{mfa3}', unsafe_allow_html=True)

st.write(f’{mfa4}', unsafe_allow_html=True)

st.write(f’{mfa5}', unsafe_allow_html=True)

# Using the ‘with’ notation (recommended)

with st.expander(“See explanation”):

st.write(“Here are the detailed notes or extra info.”)

st.write(st.session_state.textpara)

Welcome to the Streamlit community! Thanks for your detailed question—this is a common challenge when building interactive apps. In Streamlit, every widget interaction (including clicking a link or button) triggers a full script rerun, which can feel like a “refresh.” However, you can persist user data and UI state using st.session_state, so users don’t lose their progress or inputs when the script reruns. Partial updates (like only updating an expander) without a rerun aren’t natively supported, but you can design your app so that all user data is preserved and only the relevant content changes.

To achieve your goal, store all user inputs and selections in st.session_state. When a user clicks a text link, update a session state variable (e.g., st.session_state.textpara) and display its value inside the expander. This way, even though the script reruns, all other data remains intact. Here’s a code example based on your requirements:

import streamlit as st
from datetime import datetime

# Display current time
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
st.write(f"The current date and time is: **{current_time}**")

# Initialize session state
if "textpara" not in st.session_state:
    st.session_state.textpara = ''

# Function to handle text click
def handle_click(text):
    st.session_state.textpara = text

# Render clickable text as buttons
for label in ["Muhammad", "1Muhammad", "2Muhammad", "3Muhammad", "4Muhammad", "5Muhammad"]:
    if st.button(label, key=label):
        handle_click(label)

# Expander to show selected text
with st.expander("See explanation"):
    st.write("Here are the detailed notes or extra info.")
    st.write(st.session_state.textpara)

This approach ensures that clicking a button updates only the relevant part of the UI, and all other user data remains safe in st.session_state. If you need to persist more complex data or inputs, store them in st.session_state as well. For more on session state, see the Streamlit Session State docs.

Sources: