arnaudmiribel / streamlit-extras

Discover, try, install and share Streamlit re-usable bits we call "extras"!
https://extras.streamlit.app
Apache License 2.0
669 stars 117 forks source link

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

Open ivaniliash opened 4 months ago

ivaniliash commented 4 months ago

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

arnaudmiribel commented 4 months ago

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

ivaniliash commented 4 months ago

@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