Multipage app: link button is always opening a link in new window. How to change it?

Hi Streamlit community,

Problem:
My multipage app with link buttons are always opening links in the new window (or new tab).
How to make it opening links in the same page or be able to configure the behavior?
I tested in Safari and Firefox with switched off function of ‘opening new pages in new window’, so it is not browser issue.

Some additional info:
Streamlit version 1.30

I am using multipage app with link buttons. The project setup is:

  1. Main page with link buttons to additional paged
  2. Link buttons are placed in the body of the main page
  3. ‘Pages’ folder with additional pages

See example of the link button code:

import streamlit as st

st.set_page_config(
        page_title= 'Hello',
        layout = "wide",
        initial_sidebar_state="collapsed")


st.link_button(label = 'Link A',
    url = '/link_a',
    use_container_width = True)
st.link_button(label = 'Link B',
        url = '/link_b',
        use_container_width = True)

    
1 Like

Hi @Andrew_P,

Thanks for posting!

Pages opening in a new tab when clicked is the expected behavior when using st.link_button.

We have a new element (streamlit v1.30) that solves this issue for you without workarounds; st.swtich_page. Here’s the docs for st.swtich_page.

import streamlit as st

if st.button("Home"):
    st.switch_page("your_app.py")
if st.button("Page 1"):
    st.switch_page("pages/page_1.py")
if st.button("Page 2"):
    st.switch_page("pages/page_2.py")
1 Like

Hi tonykip,

Awesome, it solved my problem!
Thank you for swift response!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.

Glad it worked! Happy Streamlit-ing!:balloon:

Just to add, with Streamlit 1.31.0 that just dropped at the end of last week, Streamlit also has st.page_link if you want a slightly different look.