Get url from

Hey there, I am using st.experimental_get_query_params() and would like to create a shareable link to the outputs of my app. If I have a fixed url, I can do that by attaching the url params to the base url, but I can’t figure out how the hosted public comes up with the base url. It didn’t seem to be based off the commit hash, so I’m not sure what else it could be.

st.markdown("[share](/?param=value)") should do.

If you actually need to get the full url as a string, you use streamlit-javascript like this:

import streamlit as st
from streamlit_javascript import st_javascript

url = st_javascript("await fetch('').then(r => window.parent.location.href)")


Alternatively, this package GitHub - aghasemi/streamlit_js_eval: A custom Streamlit component to evaluate arbitrary Javascript expressions has a built-in method:

from streamlit_js_eval import get_page_location


I didn’t realize this til now after building an app with more latency, but using the above js seems to be causing the app to run twice if I have the code at the very end.

That’s not too surprising to me, since the js might not return anything right away, and so the return value will change, and just like if you clicked a button on the page it will cause the app to rerun. In general, the solution to this is to use st.experimental_memo to cache slow data-gathering code, so then a rerun of the whole app should happen fast. This won’t solve all latency issues, but often helps.

So do you mean add a function to get the base url and decorate it with @st.experimental_memo? That way it would only rerun once? I guess I could use @st.cache for that, so I don’t really understand the use of the st.experimental_memo here.

Maybe the way I was trying to this function to get the domain url isn’t right. At the very end of my app, I get all the variables that are important to my app and combine that with the domain url to get a shareable link. I was running that st_javascript you suggested at the end which ended up making the whole script rerun, which isn’t ideal. Maybe I just need to have that js run at the very beginning and cache it so it doesn’t affect subsequent changes to widgets.

Oh well I just tried the answer by Goyo without getting the domain url and it seems to handle it correctly. I didn’t realize the domain would automatically get prepended? I’m not sure how that works, but seems to solve my use case.

st.markdown(f"[Share this app's output](?{url_params_str})")
1 Like

So, I couldn’t get any of the javascript components working.
But this built in works:

        import streamlit as st
        import urlib.parse
        sessions = st.runtime.get_instance()._session_mgr.list_active_sessions()
        req = st.runtime.get_instance()._session_mgr.get_active_session_info(sessions[0]).request
        joinme = (req.protocol,, "", "", "", "")
        my_url = urllib.parse.urlunparse(joinme)

YMMV. I haven’t yet tried it on cloud, or any deployed env, but I don’t see why it shouldn’t work.

Looks nice! But accessing “_session_mgr” may not be future proof.
For e.g. if they change the variable name in future, we will end up with a fault.
Thanks for this though!