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
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.