How to get selected or clicked hyperlink text / need current clicked hyperlink text for next working

Dear Manager,

I have successfully implemented code to retrieve hyperlink data from Google. However, I am encountering a persistent issue: when I attempt to capture the text of a clicked hyperlink into a variable for subsequent processing, the system consistently returns the final hyperlink (labeled as “5Teshing”) regardless of which hyperlink I actually select. Whether I click on hyperlink 1, 2, 3, or 4, the variable always retrieves the text from hyperlink 5. Notably, when the data is exported to a download file, it correctly saves the currently selected hyperlink text. This discrepancy suggests the variable is not properly capturing the clicked hyperlink at the moment of interaction. I would greatly appreciate your review of my code and your assistance in resolving this issue so that the variable accurately returns the text of the hyperlink that was actually clicked.

Thank you in advance for your support.

Welcome to the community, and thanks for your detailed question! :blush: This is a common issue in Streamlit apps when trying to capture which hyperlink was clicked, especially if you’re generating links in a loop. Streamlit’s widgets (like buttons) only persist their state for a single rerun, and if you use a variable inside a loop, it often ends up referencing the last value due to Python’s late binding in closures.

To capture the clicked hyperlink correctly, you should use a unique key for each clickable element and store the result in st.session_state. Here’s a minimal reproducible example using buttons (since hyperlinks in st.markdown or st.write can’t trigger Python callbacks):

import streamlit as st

links = ["Link 1", "Link 2", "Link 3", "Link 4", "5Teshing"]

if "clicked_link" not in st.session_state:
    st.session_state.clicked_link = None

for idx, link in enumerate(links):
    if st.button(link, key=f"link_{idx}"):
        st.session_state.clicked_link = link

if st.session_state.clicked_link:
    st.write(f"You clicked: {st.session_state.clicked_link}")

This approach ensures the correct link text is captured when clicked. If you’re using actual HTML hyperlinks, note that Streamlit can’t natively capture clicks on them—only widgets like buttons can trigger Python code. For more advanced needs, consider a custom component or using query parameters as a workaround.

Sources:

dear sir Please review my code carefully. I notice that I am not utilizing st.button; instead, I am employing st.write. I would appreciate it if you could examine my code once more and provide the corrected version. Additionally, I need to understand how the code saves the selected text and the corresponding content in a download file. Could you please clarify how to retrieve the currently selected hyperlink text? Specifically, I need to know whether I should open the text file or extract the information from the link_text variable.

I have successfully implemented code to retrieve hyperlink data from Google. However, I am encountering a persistent issue: when I attempt to capture the text of a clicked hyperlink into a variable for subsequent processing, the system consistently returns the final hyperlink (labeled as “5Teshing”) regardless of which hyperlink I actually select. Whether I click on hyperlink 1, 2, 3, or 4, the variable always retrieves the text from hyperlink 5. Notably, when the data is exported to a download file, it correctly saves the currently selected hyperlink text. This discrepancy suggests the variable is not properly capturing the clicked hyperlink at the moment of interaction. I would greatly appreciate your review of my code and your assistance in resolving this issue so that the variable accurately returns the text of the hyperlink that was actually clicked.

Thank you in advance for your support.

import base64

import streamlit as st

if “textpara” not in st.session_state:

st.session_state.textpara = ''

def get_text_download_link(text_content, filename=“download.txt”, link_text=“Download File”):

"""

Generates an HTML anchor link string that embeds the text content 

as a base64 encoded data URI.

"""

\# 1. Encode text to bytes, then base64 bytes, then decode back to a string

b64_encoded = base64.b64encode(text_content.encode('utf-8')).decode('utf-8')

\# 2. Build the HTML string using the data URI scheme

href = f'<a href="data:text/plain;base64,{b64_encoded}" download="{filename}">{link_text}</a>'

st.session_state.textpara = link_text

return href

mfa = "Teshing "

mfa1 = “1Teshing”

mfa2 = “2Teshing”

mfa3 = “3Teshing”

mfa4 = “4Teshing”

mfa5 = “5Teshing”

# Example App Usage

text_data = “Hello World! This text will be saved into the downloaded file.”

# Generate the HTML link

download_link_html = get_text_download_link(mfa1, filename=“my_saved_text.txt”, link_text=mfa1)

st.markdown(download_link_html, unsafe_allow_html=True)

download_link_html = get_text_download_link(mfa2, filename=“my_saved_text.txt”, link_text=mfa2)

st.markdown(download_link_html, unsafe_allow_html=True)

download_link_html = get_text_download_link(mfa3, filename=“my_saved_text.txt”, link_text=mfa3)

st.markdown(download_link_html, unsafe_allow_html=True)

download_link_html = get_text_download_link(mfa4, filename=“my_saved_text.txt”, link_text=mfa4)

st.markdown(download_link_html, unsafe_allow_html=True)

download_link_html = get_text_download_link(mfa5, filename=“my_saved_text.txt”, link_text=mfa5)

# Render via Markdown with unsafe HTML allowed

st.markdown(download_link_html, unsafe_allow_html=True)

st.write(“=============================================================================================”)

# 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)