From 42c6604db0b2b4a4dc0746891e6e0879b0a2d3e2 Mon Sep 17 00:00:00 2001 From: Natarajan Sairam Date: Tue, 3 Sep 2024 19:47:07 +0100 Subject: [PATCH 01/10] refactor for toml no null --- pyproject.toml | 2 +- src/st_pages/__init__.py | 108 +++------------------------------------ 2 files changed, 8 insertions(+), 102 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1238072..60a46f5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "st-pages" -version = "1.0.0" +version = "1.0.2" license = "MIT" description = "An experimental version of Streamlit Multi-Page Apps" authors = ["Zachary Blackwood "] diff --git a/src/st_pages/__init__.py b/src/st_pages/__init__.py index 795019b..b6e4efb 100644 --- a/src/st_pages/__init__.py +++ b/src/st_pages/__init__.py @@ -39,7 +39,7 @@ def translate_icon(icon: str | None) -> str | None: def _get_nav_from_toml( - path: str = ".streamlit/pages.toml", + path: str = ".streamlit/pages.toml", ) -> list[StreamlitPage] | dict[str, list[StreamlitPage]]: """ Given a path to a TOML file, return a list or dictionary that can be passed to @@ -57,8 +57,8 @@ def _get_nav_from_toml( p for p in pages if ( - p.name not in st.session_state[HIDE_PAGES_KEY] - and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] + p.name not in st.session_state[HIDE_PAGES_KEY] + and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] ) or p.is_section ] @@ -76,7 +76,7 @@ def _get_nav_from_toml( for page in pages: if page.is_section: - if page.icon is not None: + if hasattr(page, 'icon') and page.icon != "": current_section = f"{translate_icon(page.icon)} {page.name}" else: current_section = cast(str, page.name) @@ -89,7 +89,7 @@ def _get_nav_from_toml( st.Page( page.path, title=page.name, - icon=translate_icon(page.icon), + icon=translate_icon(page.icon if hasattr(page, 'icon') else None), url_path=page.url_path, ) ) @@ -104,103 +104,9 @@ def _get_nav_from_toml( st.Page( page.path, title=page.name, - icon=translate_icon(page.icon), + icon=translate_icon(page.icon if hasattr(page, 'icon') else None), url_path=page.url_path, ) ) - return pages_data - - -get_nav_from_toml = gather_metrics("st_pages.get_nav_from_toml", _get_nav_from_toml) - - -def _hide_pages(pages: list[str]): - if ( - HIDE_PAGES_KEY not in st.session_state - or st.session_state[HIDE_PAGES_KEY] != pages - ): - st.session_state[HIDE_PAGES_KEY] = pages - st.rerun() - - -hide_pages = gather_metrics("st_pages.hide_pages", _hide_pages) - - -def _add_page_title(page: StreamlitPage): - """ - Adds the icon and page name to the page as an st.title. - """ - page_title = page.title - page_icon = translate_icon(page.icon) - - if page_icon and "/" in page_icon: - page_icon = None - - st.title(f"{page_icon} {page_title}" if page_icon else page_title) - - -add_page_title = gather_metrics("st_pages.add_page_title", _add_page_title) - - -@dataclass -class Page: - path: str - name: str | None = None - icon: str | None = None - is_section: bool = False - url_path: str | None = None - - def __post_init__(self): - _icon, _name = page_icon_and_name(Path(self.path)) - if self.icon is None: - self.icon = _icon - if self.name is None: - self.name = _name - self.icon = translate_icon(self.icon) - - -class Section(Page): - def __init__(self, name: str, icon: str | None = None, url_path: str | None = None): - super().__init__( - path="", name=name, icon=icon, is_section=True, url_path=url_path - ) - - -def _get_pages_from_config(path: str = ".streamlit/pages.toml") -> list[Page] | None: - """ - Given a path to a TOML file, read the file and return a list of Page objects - """ - try: - raw_pages: list[dict[str, str | bool]] = toml.loads( - Path(path).read_text(encoding="utf-8") - )["pages"] - except (FileNotFoundError, toml.decoder.TomlDecodeError, KeyError): - st.error( - f""" - Could not find a valid {path} file. Please create one - with the following format: - - ```toml - [[pages]] - path = "example_app/streamlit_app.py" - name = "Home" - icon = ":house" - - [[pages]] - path = "example_app/example_one.py" - - ... - """ - ) - return None - - pages: list[Page] = [] - for page in raw_pages: - if page.get("is_section"): - page["path"] = "" - pages.append(Section(page["name"], page["icon"])) # type: ignore - else: - pages.append(Page(**page)) # type: ignore - - return pages + return pages_data \ No newline at end of file From 7dd6747114d35be4692ebab2caf4816ed38a0843 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Sep 2024 18:57:00 +0000 Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/st_pages/__init__.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/st_pages/__init__.py b/src/st_pages/__init__.py index b6e4efb..048e58c 100644 --- a/src/st_pages/__init__.py +++ b/src/st_pages/__init__.py @@ -39,7 +39,7 @@ def translate_icon(icon: str | None) -> str | None: def _get_nav_from_toml( - path: str = ".streamlit/pages.toml", + path: str = ".streamlit/pages.toml", ) -> list[StreamlitPage] | dict[str, list[StreamlitPage]]: """ Given a path to a TOML file, return a list or dictionary that can be passed to @@ -57,8 +57,8 @@ def _get_nav_from_toml( p for p in pages if ( - p.name not in st.session_state[HIDE_PAGES_KEY] - and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] + p.name not in st.session_state[HIDE_PAGES_KEY] + and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] ) or p.is_section ] @@ -76,7 +76,7 @@ def _get_nav_from_toml( for page in pages: if page.is_section: - if hasattr(page, 'icon') and page.icon != "": + if hasattr(page, "icon") and page.icon != "": current_section = f"{translate_icon(page.icon)} {page.name}" else: current_section = cast(str, page.name) @@ -89,7 +89,7 @@ def _get_nav_from_toml( st.Page( page.path, title=page.name, - icon=translate_icon(page.icon if hasattr(page, 'icon') else None), + icon=translate_icon(page.icon if hasattr(page, "icon") else None), url_path=page.url_path, ) ) @@ -104,9 +104,9 @@ def _get_nav_from_toml( st.Page( page.path, title=page.name, - icon=translate_icon(page.icon if hasattr(page, 'icon') else None), + icon=translate_icon(page.icon if hasattr(page, "icon") else None), url_path=page.url_path, ) ) - return pages_data \ No newline at end of file + return pages_data From cd2c3f8b0da95690dc3883ce0a311d8986fa162e Mon Sep 17 00:00:00 2001 From: Natarajan Sairam Date: Tue, 3 Sep 2024 20:30:36 +0100 Subject: [PATCH 03/10] add back mistakenly deleted methods --- src/st_pages/__init__.py | 106 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 100 insertions(+), 6 deletions(-) diff --git a/src/st_pages/__init__.py b/src/st_pages/__init__.py index 048e58c..cb81e88 100644 --- a/src/st_pages/__init__.py +++ b/src/st_pages/__init__.py @@ -39,7 +39,7 @@ def translate_icon(icon: str | None) -> str | None: def _get_nav_from_toml( - path: str = ".streamlit/pages.toml", + path: str = ".streamlit/pages.toml", ) -> list[StreamlitPage] | dict[str, list[StreamlitPage]]: """ Given a path to a TOML file, return a list or dictionary that can be passed to @@ -57,8 +57,8 @@ def _get_nav_from_toml( p for p in pages if ( - p.name not in st.session_state[HIDE_PAGES_KEY] - and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] + p.name not in st.session_state[HIDE_PAGES_KEY] + and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] ) or p.is_section ] @@ -76,7 +76,7 @@ def _get_nav_from_toml( for page in pages: if page.is_section: - if hasattr(page, "icon") and page.icon != "": + if hasattr(page, 'icon') and page.icon != "": current_section = f"{translate_icon(page.icon)} {page.name}" else: current_section = cast(str, page.name) @@ -89,7 +89,7 @@ def _get_nav_from_toml( st.Page( page.path, title=page.name, - icon=translate_icon(page.icon if hasattr(page, "icon") else None), + icon=translate_icon(page.icon if hasattr(page, 'icon') else None), url_path=page.url_path, ) ) @@ -104,9 +104,103 @@ def _get_nav_from_toml( st.Page( page.path, title=page.name, - icon=translate_icon(page.icon if hasattr(page, "icon") else None), + icon=translate_icon(page.icon if hasattr(page, 'icon') else None), url_path=page.url_path, ) ) return pages_data + + +get_nav_from_toml = gather_metrics("st_pages.get_nav_from_toml", _get_nav_from_toml) + + +def _hide_pages(pages: list[str]): + if ( + HIDE_PAGES_KEY not in st.session_state + or st.session_state[HIDE_PAGES_KEY] != pages + ): + st.session_state[HIDE_PAGES_KEY] = pages + st.rerun() + + +hide_pages = gather_metrics("st_pages.hide_pages", _hide_pages) + + +def _add_page_title(page: StreamlitPage): + """ + Adds the icon and page name to the page as an st.title. + """ + page_title = page.title + page_icon = translate_icon(page.icon if hasattr(page, 'icon') else None) + + if page_icon and "/" in page_icon: + page_icon = None + + st.title(f"{page_icon} {page_title}" if hasattr(page, 'icon') else page_title) + + +add_page_title = gather_metrics("st_pages.add_page_title", _add_page_title) + + +@dataclass +class Page: + path: str + name: str | None = None + icon: str | None = None + is_section: bool = False + url_path: str | None = None + + def __post_init__(self): + _icon, _name = page_icon_and_name(Path(self.path)) + if self.icon is None: + self.icon = _icon + if self.name is None: + self.name = _name + self.icon = translate_icon(self.icon) + + +class Section(Page): + def __init__(self, name: str, icon: str | None = None, url_path: str | None = None): + super().__init__( + path="", name=name, icon=icon, is_section=True, url_path=url_path + ) + + +def _get_pages_from_config(path: str = ".streamlit/pages.toml") -> list[Page] | None: + """ + Given a path to a TOML file, read the file and return a list of Page objects + """ + try: + raw_pages: list[dict[str, str | bool]] = toml.loads( + Path(path).read_text(encoding="utf-8") + )["pages"] + except (FileNotFoundError, toml.decoder.TomlDecodeError, KeyError): + st.error( + f""" + Could not find a valid {path} file. Please create one + with the following format: + + ```toml + [[pages]] + path = "example_app/streamlit_app.py" + name = "Home" + icon = ":house" + + [[pages]] + path = "example_app/example_one.py" + + ... + """ + ) + return None + + pages: list[Page] = [] + for page in raw_pages: + if page.get("is_section"): + page["path"] = "" + pages.append(Section(page["name"], page["icon"] if hasattr(page, 'icon') else None)) # type: ignore + else: + pages.append(Page(**page)) # type: ignore + + return pages \ No newline at end of file From a017b9659359fb011e8743cf49719f209764ad44 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Sep 2024 19:36:34 +0000 Subject: [PATCH 04/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/st_pages/__init__.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/st_pages/__init__.py b/src/st_pages/__init__.py index cb81e88..7de076f 100644 --- a/src/st_pages/__init__.py +++ b/src/st_pages/__init__.py @@ -39,7 +39,7 @@ def translate_icon(icon: str | None) -> str | None: def _get_nav_from_toml( - path: str = ".streamlit/pages.toml", + path: str = ".streamlit/pages.toml", ) -> list[StreamlitPage] | dict[str, list[StreamlitPage]]: """ Given a path to a TOML file, return a list or dictionary that can be passed to @@ -57,8 +57,8 @@ def _get_nav_from_toml( p for p in pages if ( - p.name not in st.session_state[HIDE_PAGES_KEY] - and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] + p.name not in st.session_state[HIDE_PAGES_KEY] + and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] ) or p.is_section ] @@ -76,7 +76,7 @@ def _get_nav_from_toml( for page in pages: if page.is_section: - if hasattr(page, 'icon') and page.icon != "": + if hasattr(page, "icon") and page.icon != "": current_section = f"{translate_icon(page.icon)} {page.name}" else: current_section = cast(str, page.name) @@ -89,7 +89,7 @@ def _get_nav_from_toml( st.Page( page.path, title=page.name, - icon=translate_icon(page.icon if hasattr(page, 'icon') else None), + icon=translate_icon(page.icon if hasattr(page, "icon") else None), url_path=page.url_path, ) ) @@ -104,7 +104,7 @@ def _get_nav_from_toml( st.Page( page.path, title=page.name, - icon=translate_icon(page.icon if hasattr(page, 'icon') else None), + icon=translate_icon(page.icon if hasattr(page, "icon") else None), url_path=page.url_path, ) ) @@ -117,8 +117,8 @@ def _get_nav_from_toml( def _hide_pages(pages: list[str]): if ( - HIDE_PAGES_KEY not in st.session_state - or st.session_state[HIDE_PAGES_KEY] != pages + HIDE_PAGES_KEY not in st.session_state + or st.session_state[HIDE_PAGES_KEY] != pages ): st.session_state[HIDE_PAGES_KEY] = pages st.rerun() @@ -132,12 +132,12 @@ def _add_page_title(page: StreamlitPage): Adds the icon and page name to the page as an st.title. """ page_title = page.title - page_icon = translate_icon(page.icon if hasattr(page, 'icon') else None) + page_icon = translate_icon(page.icon if hasattr(page, "icon") else None) if page_icon and "/" in page_icon: page_icon = None - st.title(f"{page_icon} {page_title}" if hasattr(page, 'icon') else page_title) + st.title(f"{page_icon} {page_title}" if hasattr(page, "icon") else page_title) add_page_title = gather_metrics("st_pages.add_page_title", _add_page_title) @@ -199,8 +199,10 @@ def _get_pages_from_config(path: str = ".streamlit/pages.toml") -> list[Page] | for page in raw_pages: if page.get("is_section"): page["path"] = "" - pages.append(Section(page["name"], page["icon"] if hasattr(page, 'icon') else None)) # type: ignore + pages.append( + Section(page["name"], page["icon"] if hasattr(page, "icon") else None) + ) # type: ignore else: pages.append(Page(**page)) # type: ignore - return pages \ No newline at end of file + return pages From 3b0f252a7e121d8b5f89c2fd77133cc4ff1c4041 Mon Sep 17 00:00:00 2001 From: Natarajan Sairam Date: Wed, 4 Sep 2024 07:21:09 +0100 Subject: [PATCH 05/10] refactor to set page.icon to None during initialize;handle empty string for page title and section name --- src/st_pages/__init__.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/st_pages/__init__.py b/src/st_pages/__init__.py index 7de076f..bb227f7 100644 --- a/src/st_pages/__init__.py +++ b/src/st_pages/__init__.py @@ -39,7 +39,7 @@ def translate_icon(icon: str | None) -> str | None: def _get_nav_from_toml( - path: str = ".streamlit/pages.toml", + path: str = ".streamlit/pages.toml", ) -> list[StreamlitPage] | dict[str, list[StreamlitPage]]: """ Given a path to a TOML file, return a list or dictionary that can be passed to @@ -57,8 +57,8 @@ def _get_nav_from_toml( p for p in pages if ( - p.name not in st.session_state[HIDE_PAGES_KEY] - and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] + p.name not in st.session_state[HIDE_PAGES_KEY] + and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] ) or p.is_section ] @@ -76,7 +76,7 @@ def _get_nav_from_toml( for page in pages: if page.is_section: - if hasattr(page, "icon") and page.icon != "": + if hasattr(page, 'icon') and page.icon != "": current_section = f"{translate_icon(page.icon)} {page.name}" else: current_section = cast(str, page.name) @@ -89,7 +89,7 @@ def _get_nav_from_toml( st.Page( page.path, title=page.name, - icon=translate_icon(page.icon if hasattr(page, "icon") else None), + icon=translate_icon(page.icon), url_path=page.url_path, ) ) @@ -104,7 +104,7 @@ def _get_nav_from_toml( st.Page( page.path, title=page.name, - icon=translate_icon(page.icon if hasattr(page, "icon") else None), + icon=translate_icon(page.icon), url_path=page.url_path, ) ) @@ -117,8 +117,8 @@ def _get_nav_from_toml( def _hide_pages(pages: list[str]): if ( - HIDE_PAGES_KEY not in st.session_state - or st.session_state[HIDE_PAGES_KEY] != pages + HIDE_PAGES_KEY not in st.session_state + or st.session_state[HIDE_PAGES_KEY] != pages ): st.session_state[HIDE_PAGES_KEY] = pages st.rerun() @@ -132,12 +132,14 @@ def _add_page_title(page: StreamlitPage): Adds the icon and page name to the page as an st.title. """ page_title = page.title - page_icon = translate_icon(page.icon if hasattr(page, "icon") else None) + page_icon = translate_icon(page.icon) if page_icon and "/" in page_icon: page_icon = None - - st.title(f"{page_icon} {page_title}" if hasattr(page, "icon") else page_title) + if page_icon is not None and page_icon != "": + st.title(f"{page_icon} {page_title}" ) + else: + st.title(page_title) add_page_title = gather_metrics("st_pages.add_page_title", _add_page_title) @@ -199,10 +201,8 @@ def _get_pages_from_config(path: str = ".streamlit/pages.toml") -> list[Page] | for page in raw_pages: if page.get("is_section"): page["path"] = "" - pages.append( - Section(page["name"], page["icon"] if hasattr(page, "icon") else None) - ) # type: ignore + pages.append(Section(page["name"], page["icon"] if hasattr(page, 'icon') else None)) # type: ignore else: pages.append(Page(**page)) # type: ignore - return pages + return pages \ No newline at end of file From 0a887b639689a06748502dfebe9a7a0604badb4c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Sep 2024 06:22:40 +0000 Subject: [PATCH 06/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/st_pages/__init__.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/st_pages/__init__.py b/src/st_pages/__init__.py index bb227f7..31c5080 100644 --- a/src/st_pages/__init__.py +++ b/src/st_pages/__init__.py @@ -39,7 +39,7 @@ def translate_icon(icon: str | None) -> str | None: def _get_nav_from_toml( - path: str = ".streamlit/pages.toml", + path: str = ".streamlit/pages.toml", ) -> list[StreamlitPage] | dict[str, list[StreamlitPage]]: """ Given a path to a TOML file, return a list or dictionary that can be passed to @@ -57,8 +57,8 @@ def _get_nav_from_toml( p for p in pages if ( - p.name not in st.session_state[HIDE_PAGES_KEY] - and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] + p.name not in st.session_state[HIDE_PAGES_KEY] + and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] ) or p.is_section ] @@ -76,7 +76,7 @@ def _get_nav_from_toml( for page in pages: if page.is_section: - if hasattr(page, 'icon') and page.icon != "": + if hasattr(page, "icon") and page.icon != "": current_section = f"{translate_icon(page.icon)} {page.name}" else: current_section = cast(str, page.name) @@ -117,8 +117,8 @@ def _get_nav_from_toml( def _hide_pages(pages: list[str]): if ( - HIDE_PAGES_KEY not in st.session_state - or st.session_state[HIDE_PAGES_KEY] != pages + HIDE_PAGES_KEY not in st.session_state + or st.session_state[HIDE_PAGES_KEY] != pages ): st.session_state[HIDE_PAGES_KEY] = pages st.rerun() @@ -137,7 +137,7 @@ def _add_page_title(page: StreamlitPage): if page_icon and "/" in page_icon: page_icon = None if page_icon is not None and page_icon != "": - st.title(f"{page_icon} {page_title}" ) + st.title(f"{page_icon} {page_title}") else: st.title(page_title) @@ -201,8 +201,10 @@ def _get_pages_from_config(path: str = ".streamlit/pages.toml") -> list[Page] | for page in raw_pages: if page.get("is_section"): page["path"] = "" - pages.append(Section(page["name"], page["icon"] if hasattr(page, 'icon') else None)) # type: ignore + pages.append( + Section(page["name"], page["icon"] if hasattr(page, "icon") else None) + ) # type: ignore else: pages.append(Page(**page)) # type: ignore - return pages \ No newline at end of file + return pages From e44b19442032440691dea88681edd2d4d60518bc Mon Sep 17 00:00:00 2001 From: Natarajan Sairam Date: Wed, 4 Sep 2024 22:45:43 +0100 Subject: [PATCH 07/10] retain fix to check icon attr for sections --- src/st_pages/__init__.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/st_pages/__init__.py b/src/st_pages/__init__.py index 31c5080..ab07a91 100644 --- a/src/st_pages/__init__.py +++ b/src/st_pages/__init__.py @@ -39,7 +39,7 @@ def translate_icon(icon: str | None) -> str | None: def _get_nav_from_toml( - path: str = ".streamlit/pages.toml", + path: str = ".streamlit/pages.toml", ) -> list[StreamlitPage] | dict[str, list[StreamlitPage]]: """ Given a path to a TOML file, return a list or dictionary that can be passed to @@ -57,8 +57,8 @@ def _get_nav_from_toml( p for p in pages if ( - p.name not in st.session_state[HIDE_PAGES_KEY] - and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] + p.name not in st.session_state[HIDE_PAGES_KEY] + and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] ) or p.is_section ] @@ -76,7 +76,7 @@ def _get_nav_from_toml( for page in pages: if page.is_section: - if hasattr(page, "icon") and page.icon != "": + if page.icon is not None: current_section = f"{translate_icon(page.icon)} {page.name}" else: current_section = cast(str, page.name) @@ -117,8 +117,8 @@ def _get_nav_from_toml( def _hide_pages(pages: list[str]): if ( - HIDE_PAGES_KEY not in st.session_state - or st.session_state[HIDE_PAGES_KEY] != pages + HIDE_PAGES_KEY not in st.session_state + or st.session_state[HIDE_PAGES_KEY] != pages ): st.session_state[HIDE_PAGES_KEY] = pages st.rerun() @@ -136,10 +136,8 @@ def _add_page_title(page: StreamlitPage): if page_icon and "/" in page_icon: page_icon = None - if page_icon is not None and page_icon != "": - st.title(f"{page_icon} {page_title}") - else: - st.title(page_title) + + st.title(f"{page_icon} {page_title}" ) add_page_title = gather_metrics("st_pages.add_page_title", _add_page_title) @@ -201,10 +199,8 @@ def _get_pages_from_config(path: str = ".streamlit/pages.toml") -> list[Page] | for page in raw_pages: if page.get("is_section"): page["path"] = "" - pages.append( - Section(page["name"], page["icon"] if hasattr(page, "icon") else None) - ) # type: ignore + pages.append(Section(page["name"], page["icon"] if hasattr(page, 'icon') else None)) # type: ignore else: pages.append(Page(**page)) # type: ignore - return pages + return pages \ No newline at end of file From 86bc27b7a3ddcaf0b73f6a898f0cf8e5256a9b2c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Sep 2024 21:48:22 +0000 Subject: [PATCH 08/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/st_pages/__init__.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/st_pages/__init__.py b/src/st_pages/__init__.py index ab07a91..36e65ef 100644 --- a/src/st_pages/__init__.py +++ b/src/st_pages/__init__.py @@ -39,7 +39,7 @@ def translate_icon(icon: str | None) -> str | None: def _get_nav_from_toml( - path: str = ".streamlit/pages.toml", + path: str = ".streamlit/pages.toml", ) -> list[StreamlitPage] | dict[str, list[StreamlitPage]]: """ Given a path to a TOML file, return a list or dictionary that can be passed to @@ -57,8 +57,8 @@ def _get_nav_from_toml( p for p in pages if ( - p.name not in st.session_state[HIDE_PAGES_KEY] - and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] + p.name not in st.session_state[HIDE_PAGES_KEY] + and str(p.name).replace("_", " ") not in st.session_state[HIDE_PAGES_KEY] ) or p.is_section ] @@ -117,8 +117,8 @@ def _get_nav_from_toml( def _hide_pages(pages: list[str]): if ( - HIDE_PAGES_KEY not in st.session_state - or st.session_state[HIDE_PAGES_KEY] != pages + HIDE_PAGES_KEY not in st.session_state + or st.session_state[HIDE_PAGES_KEY] != pages ): st.session_state[HIDE_PAGES_KEY] = pages st.rerun() @@ -137,7 +137,7 @@ def _add_page_title(page: StreamlitPage): if page_icon and "/" in page_icon: page_icon = None - st.title(f"{page_icon} {page_title}" ) + st.title(f"{page_icon} {page_title}") add_page_title = gather_metrics("st_pages.add_page_title", _add_page_title) @@ -199,8 +199,10 @@ def _get_pages_from_config(path: str = ".streamlit/pages.toml") -> list[Page] | for page in raw_pages: if page.get("is_section"): page["path"] = "" - pages.append(Section(page["name"], page["icon"] if hasattr(page, 'icon') else None)) # type: ignore + pages.append( + Section(page["name"], page["icon"] if hasattr(page, "icon") else None) + ) # type: ignore else: pages.append(Page(**page)) # type: ignore - return pages \ No newline at end of file + return pages From 89816a179fcd4d1897e971a80ca18dc7823052b5 Mon Sep 17 00:00:00 2001 From: Zachary Blackwood Date: Tue, 10 Sep 2024 09:35:40 -0400 Subject: [PATCH 09/10] Fix syntax, add no-icon section to test --- example_app/.streamlit/pages_sections.toml | 10 ++++++++++ src/st_pages/__init__.py | 4 +--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/example_app/.streamlit/pages_sections.toml b/example_app/.streamlit/pages_sections.toml index 1acba71..c42233d 100644 --- a/example_app/.streamlit/pages_sections.toml +++ b/example_app/.streamlit/pages_sections.toml @@ -31,3 +31,13 @@ path = "example_five.py" name = "Example Five" icon = "🧰" url_path = "a_very_long_page_title" + +[[pages]] +name = "Section with no icon" +is_section = true + +[[pages]] +path = "example_five.py" +name = "Another example five!" +icon = "😵‍💫" +url_path = "example_five_title2" diff --git a/src/st_pages/__init__.py b/src/st_pages/__init__.py index 36e65ef..e9db8e6 100644 --- a/src/st_pages/__init__.py +++ b/src/st_pages/__init__.py @@ -199,9 +199,7 @@ def _get_pages_from_config(path: str = ".streamlit/pages.toml") -> list[Page] | for page in raw_pages: if page.get("is_section"): page["path"] = "" - pages.append( - Section(page["name"], page["icon"] if hasattr(page, "icon") else None) - ) # type: ignore + pages.append(Section(page["name"], page.get("icon", None))) # type: ignore else: pages.append(Page(**page)) # type: ignore From 7766c819f55f0dae7e57746e9bd26cf2df763b17 Mon Sep 17 00:00:00 2001 From: Zachary Blackwood Date: Tue, 10 Sep 2024 09:37:06 -0400 Subject: [PATCH 10/10] Undo change --- src/st_pages/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/st_pages/__init__.py b/src/st_pages/__init__.py index e9db8e6..869c69f 100644 --- a/src/st_pages/__init__.py +++ b/src/st_pages/__init__.py @@ -137,7 +137,7 @@ def _add_page_title(page: StreamlitPage): if page_icon and "/" in page_icon: page_icon = None - st.title(f"{page_icon} {page_title}") + st.title(f"{page_icon} {page_title}" if page_icon else page_title) add_page_title = gather_metrics("st_pages.add_page_title", _add_page_title)