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.
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:
Documentation is available here: webbrowser — Convenient web-browser controller — Python 3.10.2 documentation
Sorry for the foolishness.
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.
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.
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.
nav_script = """
<meta http-equiv="refresh" content="0; url='%s'">
""" % (url)
This will immediately redirect the user after rendering.