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)
6 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

4 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.

8 Likes

Thanks a lot.

Hi andfanilo,
Am making an applications with multiple pages but I want a link like this below
Localhost:8501/page2 or some thing similar.
The st.experimental_get_query_params() doesn’t lead to my objective.
I want some thing atleast like how django,flask do their routing or even direct route
Am looking for a link that can be clicked and take me to my other streamlit pages in the same applications
Thank you

Hello @Kasibanteg,

This feature is currently in design for an official solution and hopefully out in the coming months. Until then, you’ll have to rely on mixing unofficial hacks with state, query params and programmatically loading modules for this.

Have you checked the following topics?

Cheers,
Fanilo :balloon:

2 Likes

Yes, I checked all them out alreadyalready.
The only solution am going to use is to run main app and sub web pages on different ports, then I connect main app to the rest of the pages via markdown.
Thats what am doing right now and it seems to be perfect

1 Like


I am getting the following error upon using this snippet. I have already pip installed bokeh.

Is there a way for the user to open multiple separate web pages when clicking a button? I tried the ideas mentioned in this post, but not getting multiple different pages to open when running on a server. Locally it runs as expected. For now I’m creating hyperlinks, but it would save my users some time if they could automatically open the links instead of having to click each individual hyperlink and then navigate back to the main page etc…

Have we gotten this updated on streamlit, a button that help open a browser