Welcome to the Streamlit community, and thanks for your detailed question!
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: