Skip to content

Commit

Permalink
Add add_page_title method
Browse files Browse the repository at this point in the history
  • Loading branch information
blackary committed Nov 15, 2022
1 parent 954ca99 commit e07c5b2
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 13 deletions.
4 changes: 4 additions & 0 deletions example_app/example_four.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import streamlit as st

from st_pages import add_page_title

add_page_title()

st.write("This is just a sample page!")
4 changes: 4 additions & 0 deletions example_app/example_one.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import streamlit as st

from st_pages import add_page_title

add_page_title()

st.write("This is just a sample page!")
4 changes: 4 additions & 0 deletions example_app/example_three.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import streamlit as st

from st_pages import add_page_title

add_page_title()

st.write("This is just a sample page!")
4 changes: 4 additions & 0 deletions example_app/example_two.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import streamlit as st

from st_pages import add_page_title

add_page_title()

st.write("This is just a sample page!")
24 changes: 12 additions & 12 deletions example_app/streamlit_app.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import streamlit as st

"# A Better way to Set Up Multi-Page Streamlit Apps"
# "# A Better way to Set Up Multi-Page Streamlit Apps"

with st.echo("above"):
from st_pages import Page, show_pages
with st.echo("below"):
from st_pages import Page, add_page_title, show_pages

add_page_title() # Optional method to add title and icon to current page

show_pages(
[
Page("example_app/streamlit_app.py", "Home", "🏠"),
Page(
"example_app/example_one.py", "Example One", ":books:"
), # Can use :<icon-name>: or the actual icon
Page(
"example_app/example_four.py", "Example Four", "📖"
), # The pages appear in the order you pass them
# Can use :<icon-name>: or the actual icon
Page("example_app/example_one.py", "Example One", ":books:"),
# The pages appear in the order you pass them
Page("example_app/example_four.py", "Example Four", "📖"),
Page("example_app/example_two.py", "Example Two", "✏️"),
Page(
"example_app/example_three.py"
), # Will use the default icon and name based on the page name
# Will use the default icon and name based on the filename if you don't
# pass them
Page("example_app/example_three.py"),
]
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "st-pages"
version = "0.1.0"
version = "0.1.1"
description = "An experimental version of Streamlit Multi-Page Apps"
authors = ["Zachary Blackwood <zachary@streamlit.io>"]
readme = "README.md"
Expand Down
27 changes: 27 additions & 0 deletions src/st_pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import requests
import streamlit as st
from streamlit.commands.page_config import get_random_emoji
from streamlit.errors import StreamlitAPIException

try:
from streamlit.web.server import Server
except ImportError:
from streamlit.server.server import Server # type: ignore

from streamlit.runtime.scriptrunner import get_script_run_ctx
from streamlit.source_util import _on_pages_changed, get_pages

try:
Expand All @@ -28,6 +30,31 @@ def page_icon_and_name(script_path: Path) -> Tuple[str, str]:
DEFAULT_PAGE: str = Server.main_script_path # type: ignore


def add_page_title(add_icon: bool = True):
"""
Adds the icon and title to the page
"""
pages = get_pages(DEFAULT_PAGE)
ctx = get_script_run_ctx()
if ctx is not None:
try:
current_page = pages[ctx.page_script_hash]
except KeyError:
return

page_title = current_page["page_name"]
page_icon = current_page["icon"]
try:
st.set_page_config(page_title=page_title, page_icon=page_icon)
except StreamlitAPIException:
pass

if add_icon:
st.title(f"{translate_icon(page_icon)} {page_title}")
else:
st.title(page_title)


@st.experimental_singleton
def get_icons() -> Dict[str, str]:
url = "https://raw.githubusercontent.com/omnidan/node-emoji/master/lib/emoji.json"
Expand Down

0 comments on commit e07c5b2

Please sign in to comment.