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