How can I set a different layout per page?

Hi!

I know that the st.set_page_config function can only be called once in a Streamlit, but is there any workaround to be able to have a page with the layout wide and the another with layout centered?

Thus, in the following I pretended that the introduction page to be wide, but the conclusion to be centered:

import streamlit as st

st.set_page_config(
	page_title='Streamlit Page',
	layout='wide', #layout='centered'
	initial_sidebar_state='expanded',
	)


# Sidebar navigation
selected_page = st.sidebar.selectbox(
	'Select page',
	options=[
		'Introduction',
		'Conclusion',
#		'About',
		])


if selected_page == 'Introduction':
	st.text("Welcome to this Page")
	
elif selected_page == 'Conclusion':
	st.text("This is the conclusion page")

Thank you very much in advance for your help.

Try this trick, see comments in the code.

import streamlit as st
from streamlit import session_state as ss


# Declare and initialize the layout session variable.
if 'layout' not in ss:
	ss.layout = 'wide'
	

# The layout parameter will receive the value of layout session variable.
st.set_page_config(
	page_title='Streamlit Page',
	layout=ss.layout,
	initial_sidebar_state='expanded',
)


def layout_change_cb():
	"""Called when there is a change in selectbox.
	
	The value of selectbox is stored in ss.selpagek.
	Depending on the value, we will change the layout session variable
	value to either wide or centered.
	"""
	if ss.selpagek == 'Introduction':
		ss.layout = 'wide'
	else:
		ss.layout = 'centered'


# Sidebar navigation
st.sidebar.selectbox(
	'Select page',
	options=[
		'Introduction',
		'Conclusion'
	],
	key='selpagek',
	on_change=layout_change_cb  # callback
)

if ss.selpagek == 'Introduction':
	st.text("Welcome to this Page")	
elif ss.selpagek == 'Conclusion':
	st.text("This is the conclusion page")

It worked!
Thank you so much for your help :wink:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.