How to link a button to a webpage?

I want to create a button and upon clicking, it should open desired webpage. Not sure how I can about it

2 Likes

May be use button to show up a hyperlink using markdown ?

Hey,

Would the following do the trick ?

import streamlit as st
import webbrowser

url = 'https://www.streamlit.io/'

if st.button('Open browser'):
    webbrowser.open_new_tab(url)
3 Likes

This only works locally but not when the Streamlit app is accessed over network (e.g. deployed to a server).

Here is a hack that works in all cases (but is a hack):

from bokeh.models.widgets import Div
import streamlit as st

if st.button('Go to Streamlit'):
    js = "window.open('https://www.streamlit.io/')"  # New tab or window
    js = "window.location.href = 'https://www.streamlit.io/'"  # Current tab
    html = '<img src onerror="{}">'.format(js)
    div = Div(text=html)
    st.bokeh_chart(div)
3 Likes

There need to be a better solution than this

2 Likes

This worked but it was slow, not user friendly but works.

Simply use this -

link = '[GitHub](http://github.com)'
st.markdown(link, unsafe_allow_html=True)

This will open the link when clicked on it.

3 Likes