How to get download text file text in variable before download or get selected hyperlink text in any vay by using st.write href

Dear Sir,

I am seeking assistance with retrieving the text content of a selected hyperlink prior to file download and storing it within a variable. My current implementation contains multiple hyperlinks, and while the code successfully downloads files without errors (as demonstrated below), I require the ability to capture the selected hyperlink text for subsequent processing. I would appreciate guidance on obtaining the selected hyperlink text through any viable method—whether integrated with the download functionality or implemented separately. The critical requirement is that I need to extract only the selected hyperlink text using st.write href, without utilizing st.button, and without triggering a page refresh during the file download process. Employing the action method results in an undesired page refresh, which I must avoid. I would be grateful if you could review my code and provide the appropriate solution for capturing the selected hyperlink text without page refresh.

import base64
import streamlit as st
from datetime import datetime

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.session_state.textpara = data_str
  b64 = base64.b64encode(data_str.encode()).decode()
  href = f'<a href="data:file/txt;base64,{b64}" download="{filename}">Click here to download (no refresh)</a>'
  st.markdown(href, unsafe_allow_html=True)

create_download_link("111111111Hello Streamlit!", "test.txt")
create_download_link("222222222Hello Streamlit!", "test.txt")
create_download_link("333333333333Hello Streamlit!", "test.txt")
create_download_link("44444444444Hello Streamlit!", "test.txt")
create_download_link("55555555555Hello Streamlit!", "test.txt")

with st.expander("See explanation"):
    st.write(st.session_state.textpara)

thank you in advance for your help

Welcome to the Streamlit community, and thanks for your detailed question! :blush:

It could be possible that what you’re asking—capturing which hyperlink was clicked and storing its text in a variable, all without using st.button and without a page refresh—is not natively supported by Streamlit’s built-in features. Streamlit’s standard workflow doesn’t provide a way to trigger Python code from a plain HTML hyperlink click (created via st.markdown or st.write) without a page rerun, since those links are handled entirely on the frontend and don’t communicate back to the Python backend. This means you can’t directly update st.session_state or any Python variable when a user clicks a download link generated with st.markdown, as described in your code. This limitation is discussed in several community threads and is not explicitly addressed in the official API docs or knowledge base articles.

If you need to capture which link was clicked without a page refresh or using st.button, you would likely need to implement a custom Streamlit component with JavaScript to send the clicked link’s info back to Python, but this is not covered in the official documentation and would require advanced workarounds. For most use cases, Streamlit recommends using st.button or st.download_button, which do trigger a rerun and allow you to capture the clicked state, but you mentioned you want to avoid this.

Sources: