-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
38 lines (30 loc) · 1.16 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import streamlit as st
def switch_page_button(button_text: str, page_name: str):
"""
Switch page programmatically in a multipage app
Args:
page_name (str): Target page name
"""
from streamlit.runtime.scriptrunner import RerunData, RerunException
from streamlit.source_util import get_pages
import streamlit as st
def standardize_name(name: str) -> str:
return name.lower().replace("_", " ")
button = st.button(label=button_text)
if button:
page_name = standardize_name(page_name)
pages = get_pages("streamlit_app.py") # OR whatever your main page is called
for page_hash, config in pages.items():
if standardize_name(config["page_name"]) == page_name:
raise RerunException(
RerunData(
page_script_hash=page_hash,
page_name=page_name,
)
)
page_names = [
standardize_name(config["page_name"]) for config in pages.values()
]
raise ValueError(
f"Could not find page {page_name}. Must be one of {page_names}"
)