Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 773
Quinten Avatar asked Nov 23 '25 17:11

Quinten


2 Answers

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

like image 141
ferdy Avatar answered Nov 26 '25 07:11

ferdy


Here is another example styled a bit more like streamlit:

def redirect_button(url: str, text: str= None, color="#FD504D"):
    st.markdown(
    f"""
    <a href="{url}" target="_self">
        <div style="
            display: inline-block;
            padding: 0.5em 1em;
            color: #FFFFFF;
            background-color: {color};
            border-radius: 3px;
            text-decoration: none;">
            {text}
        </div>
    </a>
    """,
    unsafe_allow_html=True
    )
redirect_button("http://stackoverflow.com","this leads to SO")

enter image description here

like image 33
Jakob Guldberg Aaes Avatar answered Nov 26 '25 06:11

Jakob Guldberg Aaes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!