Need selected/clicked hyperlink and save in variable for next working using st.write href

Dear Sir,

I am writing to request your assistance with a technical matter. My code executes successfully, and when I click any hyperlink, it downloads a text file containing the text of the selected hyperlink without refreshing the page. However, I am encountering a challenge when attempting to store the selected hyperlink text in a variable. The code consistently returns the last hyperlink text from the list, regardless of which hyperlink I select. For instance, whether I select the first, second, third, or fourth hyperlink, the system always retrieves the final item from the list.

I would greatly appreciate your guidance on how to obtain the selected hyperlink text. Additionally, I must emphasize that I am not utilizing a button in my implementation, so I kindly request that you refrain from providing st.button code. Instead, I would prefer a solution using st.write href code to retrieve the selected hyperlink text.

please ```send code using only st.write href not st.button

import base64
import streamlit as st
from datetime import datetime
mmfa1 =
st.title(“Muhammad is the Best in all over the universes!”)

Get the current date and time

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

if “textpara” not in st.session_state:
st.session_state.textpara = ‘’

def create_download_link(data_str, filename=“data.txt”):
st.title(data_str)
mmfa1.append(data_str)
st.session_state.textpara = data_str
b64 = base64.b64encode(data_str.encode()).decode()
href = f’Click here to download {data_str} (no refresh)’
st.session_state.textpara = data_str

st.markdown(href, unsafe_allow_html=True)

mfa = "Teshing "
mfa1 = “1Teshing”
mfa2 = “2Teshing”
mfa3 = “3Teshing”
mfa4 = “4Teshing”
mfa5 = “5Teshing”

create_download_link(mfa, “test.txt”)
create_download_link(mfa1, “test.txt”)
create_download_link(mfa2, “test.txt”)
create_download_link(mfa3, “test.txt”)
create_download_link(mfa4, “test.txt”)
create_download_link(mfa5, “test.txt”)

Show data in expander

with st.expander(“See explanation”):
st.write(“Here are the detailed notes or extra info.”)
st.write(st.session_state.textpara)
for item in mmfa1:
st.write(item)

Welcome to the Streamlit community, and thanks for your detailed question! :blush: Unfortunately, my understanding is that with Streamlit’s current architecture, you can’t capture which hyperlink (created via st.write or st.markdown with an href) was clicked, nor can you store the clicked link’s text in a variable, because hyperlinks rendered this way only trigger a browser download—they don’t send any information back to the Streamlit backend. This is a browser limitation, not a Streamlit one. So, using only st.write or st.markdown with href, there’s no way to detect which link was clicked or update session state accordingly. This is confirmed in multiple community discussions and is not addressed in the official API docs (see here, here).

If you need to know which link was clicked, you must use an interactive widget (like st.button or st.download_button), which can trigger a callback and update session state. Hyperlinks created with st.write or st.markdown are static and cannot communicate back to your Python code. If you want to display links and track clicks, consider using st.download_button for each item, which is the Streamlit-supported way to handle downloads and state (see docs). If you still want to use only hyperlinks, you would need to implement a custom JavaScript component, which is outside the scope of standard Streamlit.

Sources: