I’m trying to center a table on my streamlit app. I’ve come across numerous suggestions on the forum, but I’m surprised that this is not a feature in Streamlit. I have had some success using st.markdown with unsafe_allow_html=True, but it feels like a hack and often leads to unpredictable failure/success. Currently I can center align the table using markdown, but streamlit shows a small box on the top right of the table that says: div.
Are there any plans to add a native feature for centering components?
In the meantime, if you want a centered dataframe/table that doesn’t take up the full width of your app, use three columns and place the dataframe in the middle column with use_container_width=True. You can make the first and third column smaller so the dataframe takes up the desired width (e.g. st.columns([1,10,1]).
st.table doesn’t have the arg use_container_width and using the columns left aligns the table with whichever column you put it in. I have to use st.table since I’m using pandas stylers.
But it’s great news that this is scheduled for an upcoming release!
I just tested, and I’m getting st.table to be the full container width by default. Is that not happening for you? (I checked the latest release and nightly.)
Is using three columns and putting the content of your table in the middle one a solution appliable to your case?
I needed to center some elements and this solution worked for me
3 columns doesn’t work since my dataframe can be different sizes (width and height), and 100% width produces close to my desired outcome, but for very small tables it looks way to spread out.
I ended up getting the table centered with the following code. I really hope an easier solution is on the way. Again, I had to use and st.markdown(df.to_html(), unsafe_allow_html=True) due to applying custom stylers to the table (not shown in example).
import streamlit as st
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50],
'C': [100, 200, 300, 400, 500]
})
df_html = df.to_html()
top_window, bottom_window = st.container(), st.container()
# Apply ccs in bottom window to avoid adding a empty space in the app
# Target only the table inside the stMarkdownContainer within div containing "st-key-CUSTOM_TABLE_ID"
with bottom_window:
st.markdown(
"""
<style>
.st-key-CUSTOM_TABLE_ID [data-testid="stMarkdownContainer"] table {
margin-left: auto;
margin-right: auto;
}
</style>
""",
unsafe_allow_html=True
)
with top_window:
# the key within container will add 'st-key-{key}' orto the divs class
# This is what I see with inspect: <div class="stVerticalBlock st-key-CUSTOM_TABLE_ID st-emotion-cache-1n76uvr eu6p4el3" ...
st.header('Center Table')
with st.container(key='CUSTOM_TABLE_ID'):
st.markdown(df_html, unsafe_allow_html=True)
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.