Programmatically Send User to a Web Page with No Click

Hello,

Is there way to send a user to a web page programmatically without the user clicking anything?

My use case is that a user enters some data in a form and clicks a “Submit” button. The Streamlit application does some processing (e.g. creating the user record in a database, etc.) and redirects the user to another web page. The destination URL will be determined by the data entered into the form. Ideally, I could optionally open a new tab or browser window for the destination URL. The destination URL is not necessarily another Streamlit app.

While I could create a button or markdown link with a hyperlink (per some other articles) and have the user click, this is an extra step that I would like to avoid.

Seems like a common use case and I may be missing something obvious in the docs or a common workaround. Any help would be appreciated.

Thanks,

Ryan

Answering my own question. I like Streamlit, so assumed the solution was in Streamit instead of looking elsewhere.

The solution is to use webbrowser as in:

import webbrowser
webbrowser.open_new_tab(target_url)

Documentation is available here: webbrowser — Convenient web-browser controller — Python 3.10.2 documentation

Sorry for the foolishness.

5 Likes

Nice solution - I have it working on localhost, but it doesn’t seem to play on Streamlit cloud for me. Did you experience the same?

This does not work because there is a fundamental misunderstanding about how Streamlit works.
Streamlit consists of a frontend and backend part.
Any interaction with streamlit must be done via the browser or browser APIs.
Anything trying to bypass this, may work on your local computer, but will fail as soon as streamlit is hosted, i.e. streamlit no longer runs on the same computer as the browser.
What happens when webbrowser is used? A browser is started on the computer on which the python application is running. But there is no browser on streamlit cloud and even if there was, it would be the wrong computer.

1 Like

Thank you for that - very helpful in preventing a lot of time going down the wrong path!

Thanks for pointing out that webbrowser will only work locally, given how Streamlit works.

Do you know if a JavaScript injection (for example, window.open(URL, '_blank')) would work properly? Thanks in advance.

For anyone still wondering if this is possible, yes it is. You can do some really funny things with simple HTML.

def nav_to(url):
    nav_script = """
        <meta http-equiv="refresh" content="0; url='%s'">
    """ % (url)
    st.write(nav_script, unsafe_allow_html=True)

This will immediately redirect the user after rendering.

10 Likes

Thank you!

Created an st.button and used the function to link to a new page. HTML buttons looked so bad!
Helped me :))

I’m trying to do the same thing, but open in a new tab. Not having much luck with the new tab part. :frowning:

Unfortunately, I don’t know hardly any HTML or javascript, so if anyone knows of something I can add to the html given above or javascript to do this, I would be eternally grateful! :slight_smile:

JS I’ve tried (found via google):
st_javascript(f'window.open({url}, "_blank", "location=yes,height=570,width=520,scrollbars=yes,status=yes");')
But that seems to do nothing at all. :frowning:

ideas?

:man_facepalming:
st_javascript(f'window.open("{url}", "_blank");') works!
It helps to remember all the quotes. :wink:

I hope this helps someone else! (edited - note that the above produces an error, but I found the issue - apparently it needs to return something; I’m returning the parent url below… but ignoring the return value.

def nav_to(url):
    js = f'window.open("{url}", "_blank").then(r => window.parent.location.href);'
    st_javascript(js)

1 Like