streamlit-extras icon indicating copy to clipboard operation
streamlit-extras copied to clipboard

🐛 [BUG] - Hardcoded main script path in switch_page_button extra

Open ivaniliash opened this issue 1 year ago • 2 comments

Description

The main script path is hardcoded in switch_page_button/__init__.py Screenshot 2024-02-21 at 14 18 08 It will always lead to a RerunException if the main script is renamed. Page switching still works, however the expection raising can lead to problems in error handling. For example it will not work if the switch_page method is part of a try/except block.

I already have a proposal to fix this and I'd love to contribute, however I'm not sure how to create a pull request 😄

Reproduction steps

1. Rename script
2. wrap switch_page in try/except block
3. Press switch page button
4. Nothing will happen

Version of streamlit

1.30.0

Version of streamlit-extras

0.4.0

ivaniliash avatar Feb 21 '24 13:02 ivaniliash

Thanks @ivaniliash - would adding a main_module_filename: str = "streamlit_app.py" as a parameter to the switch_page function solve your issue?

arnaudmiribel avatar Feb 22 '24 11:02 arnaudmiribel

@arnaudmiribel I simply replaced line 22 with pages = get_pages(os.path.basename(__main__.__file__)). Should work if its always the main script we are looking for.

Edit: Probably os.path.basename is not needed though, but not sure

ivaniliash avatar Feb 22 '24 11:02 ivaniliash

Closing this as switch_page is now a native command in Streamlit!

arnaudmiribel avatar Jul 31 '24 12:07 arnaudmiribel