Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Direct link to pre-selected items #2

Closed
giswqs opened this issue Aug 10, 2021 · 4 comments
Closed

Direct link to pre-selected items #2

giswqs opened this issue Aug 10, 2021 · 4 comments

Comments

@giswqs
Copy link

giswqs commented Aug 10, 2021

Thank you for creating this nice multi-apps template. I wondered if it is possible to generate a direct link to the pre-selected item. The instructions from https://discuss.streamlit.io/t/direct-link-to-pre-selected-items/4817 works for a single app, but I can't get it to work for multi-apps.

For example, I created this multi-apps based on your template.

When users select an item from the navigation panel, the URL will reflect the change (e.g., page=Demo). However, when users directly navigate to the URL by clicking the link below, it will always redirect to the home page rather than the pre-selected item. Any advice? Thanks.
https://share.streamlit.io/giswqs/leafmap-apps/app.py?page=Demo

Relevant source code: https://github.com/giswqs/leafmap-apps/blob/master/multiapp.py#L45

@upraneelnihar
Copy link
Owner

Hi,
We can try using the session state of the radio button to choose the required page from URL.

I have tried by modifying the run function in multiapp.py with following code, it solves the problem of navigating through URL. However I observed that this introduces a new issue where in we have to click twice on a radio button if you are selecting it from the interface.

I was unable to fix the later issue, but you can try along these lines and see if you can find a better solution for this.

    def run(self):
        app_state = st.experimental_get_query_params()
        app_state = {k: v[0] if isinstance(v, list) else v for k, v in app_state.items()} # fetch the first item in each query string as we don't have multiple values for each query string key in this example
        
        #st.write('before', app_state)
        
        titles = [a['title']  for a in self.apps]
        functions = [a['function'] for a in self.apps]
        default_radio = titles.index(app_state["page"]) if "page" in app_state else 0

        title = st.sidebar.radio(
            'Go To',
            titles,
            index=default_radio,
            key='radio')

        app_state['page'] = st.session_state.radio
        #st.write('after', app_state)

        st.experimental_set_query_params(**app_state)
        functions[titles.index(title)]()

@giswqs
Copy link
Author

giswqs commented Aug 10, 2021

Thanks a lot for this. It seems working fine on my local computer. However, when deployed to streamlit.io, the direct link is being redirected to the home page.

https://share.streamlit.io/giswqs/leafmap-apps/app.py?page=Dual

Source code: https://github.com/giswqs/leafmap-apps/blob/master/multiapp.py

@giswqs
Copy link
Author

giswqs commented Aug 10, 2021

Interestingly, this direct link works https://share.streamlit.io/giswqs/leafmap-apps/app.py?page=Demo
Other direct links don't work.

@upraneelnihar
Copy link
Owner

Thanks for testing the code.
I will try to find a better way to solve this problem. I will get back to you once I have a solution for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants