Set page title dynamically

Hi,

I have a scenario in which I would like to change the page title during runtime. My application is this:

I’ve built a rudimentary database which is visualized via st.dataframe. The dataframe contains clickable items. Whenever an item is clicked, a new streamlit page with details about the item is opened. Several arguments are passed to this new details page, e.g. the ID (just an integer) of the clicked item.

One of the users pointed out that it would be really helpful to have the ID displayed in the page title. In case multiple tabs with different items are open, it makes it easier to switch between tabs when the ID of the item is displayed in the page name.

I am aware that I can use this command to configure the page name via:

st.set_page_config(layout="wide", page_title="Module Details")

I can also do this:

st.set_page_config(layout="wide", page_title=f"Module {id}")

Now the problem that I am encountering is this:

id is retrieved from either st.session_state or st.query_params.
In order to get the id, I need to run a streamlit command before I can run st.set_page_config. But then st.set_page_config complains that it needs to be the first streamlit command to be executed.

Can I set/change the page_name later in the script? Or is there another way of passing an argument (integer) to the streamlit page and have it included in the page title without the use of streamlit commands?

Btw executing st.session_state or st.query_params as the very first streamlit command (before st.set_page_config) works, but the user is presented with an error message for a split second before the page renders. If possible, I’d like to avoid this.

Your best bet is to use Session State to store the ID or title and set the page title from Session State. You can’t call st.set_page_config after commands that affect three app’s displayed view. Session State is a little exception: you can work with Session State before setting the page configuration.

Here’s an example with dynamic page titles and icons: