I’m successfully running my streamlit app on my server. It’s a multi-page app that uses some community tricks, which apparently with v1.44.0 are discontinued without any mention.
I really need to be able to migrate to the latest release but don’t know how. Can somebody please provide a hint, I’d be very grateful for it.
here’s a part of my code that breaks now:
import streamlit as st
from streamlit.runtime.scriptrunner import get_script_run_ctx
# >> this seems gone!
from streamlit.source_util import get_pages
# >> consequently this breaks
def get_current_page_name():
ctx = get_script_run_ctx()
if ctx is None:
raise RuntimeError("Couldn't get script context")
pages = get_pages("")
return pages[ctx.page_script_hash]["page_name"]
#>>
elif get_current_page_name() != "Home":
# If anyone tries to access a secret page without being logged in, redirect them to the login page
st.switch_page("Home.py")
Thanks @edsaac for the quick reply. I’m afraid I’m still a bit lost. The current logic was created from a community member and I can’t say I’m really understanding what it does.
Right now I’m broken ;( I provided the entire code I need to work, any chance you can provide the proper imports and change to my code so it works with the latest release? I’d be extremly grateful.
I tried this but it’s still complaining
import streamlit as st
from streamlit.runtime.scriptrunner import get_script_run_ctx
from streamlit.runtime.pages_manager import PagesManager
def get_current_page_name():
ctx = get_script_run_ctx()
if ctx is None:
raise RuntimeError(“Couldn’t get script context”)
# Get the script path from the context
this gives me grief – asssuming the PagesManager call below is correct
script_path = ctx.main_script_path
# Create a PagesManager with the main script path
pages_manager = PagesManager(main_script_path=script_path)
# Get all pages
pages = pages_manager.get_pages()
# Get current page information using the script hash from context
page_hash = ctx.page_script_hash
if page_hash in pages:
return pages[page_hash]["page_name"]
return None
Then your existing code can remain similar:
if get_current_page_name() != “Home”:
# If anyone tries to access a secret page without being logged in, redirect them to the login page
st.switch_page(“Home.py”)
Yeah, I had to fiddle a bit but actually not much has changed.
I only needed the page name for a later comparison. This does that.
from streamlit.runtime.scriptrunner import get_script_run_ctx
def get_current_page_name():
ctx = get_script_run_ctx()
# we can local info like timezone froom ctx object.
if ctx is None:
raise RuntimeError("Couldn't get script context")
page_name = Path(ctx.main_script_path).stem
# logger.info(f"Current page name: {page_name}")
return page_name