Search code examples
pythonbuttonhyperlinkstreamlit

How to create a button with Hyperlink in Streamlit?


I would like to create a button with a Hyperlink in Streamlit. I thought maybe using st.button with this syntax: [Click Here](https://stackoverflow.com). But unfortunately this has no success, it only displays the text and doesn't make it as a hyperlink. I found this workaround, but this doesn't work anymore. I don't want to use st.markdown with a hyperlink because that doesn't create a button. Here is a reproducible example:

"""
# Streamlit app
"""

import streamlit as st
import pandas as pd

# Button with hyperlink
st.button('[Click Here](https://stackoverflow.com)')

Output:

enter image description here

As you can see, it doesn't create a hyperlink. So I was wondering if anyone knows how to create a button with a hyperlink in Streamlit?


Solution

  • You can use markdown, a and button tags.

    import streamlit as st
    
    url = 'https://stackoverflow.com'
    
    st.markdown(f'''
    <a href={url}><button style="background-color:GreenYellow;">Stackoverflow</button></a>
    ''',
    unsafe_allow_html=True)
    

    enter image description here