I have been working on an application using Streamlit and I am having an issue with the formatting of my DataFrame. I have some columns that contain long strings of text, and they are not fully displayed within the cells of the DataFrame. Specifically, I have a column “C” which contains Lorem Ipsum text that is very long.
I would like to modify the formatting of this DataFrame so that the entire contents of the “C” column are displayed with automatic line breaks based on the width of the column. This would make it much easier for users to read the full text within the cell without having to scroll or manually adjust the cell size.
I have created a DataFrame using Pandas with three columns “A”, “B”, and “C”, where “C” contains the Lorem Ipsum text. Here’s the code:
lorem_ipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ultrices ante nec justo varius, vitae gravida mi suscipit. Nullam vehicula facilisis semper. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed euismod nisl quis tellus lobortis, vitae blandit orci congue. Phasellus ullamcorper lacinia dolor, non suscipit dui dictum vel. Donec ac magna eget augue cursus finibus. Sed lacinia, justo eu tincidunt vestibulum, felis quam auctor nibh, nec ullamcorper augue elit vel elit."
df = pd.DataFrame({
"A": [1, 2, 3],
"B": ["a", "b", "c"],
"C": [lorem_ipsum, lorem_ipsum, lorem_ipsum]
})
st.dataframe(df,use_container_width=True)
But it is not yet possible to wrap the rendered cell or change the height of the row. However, you can use st.table (for smaller dataframes) which automatically applies cell wrapping:
You can manually replace the True/False values with HTML for disabled checkboxes to achieve the result. If you use the to_html method with keyword escape=False you get this:
import streamlit as st
import pandas as pd
lorem_ipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ultrices ante nec justo varius, vitae gravida mi suscipit. Nullam vehicula facilisis semper. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed euismod nisl quis tellus lobortis, vitae blandit orci congue. Phasellus ullamcorper lacinia dolor, non suscipit dui dictum vel. Donec ac magna eget augue cursus finibus. Sed lacinia, justo eu tincidunt vestibulum, felis quam auctor nibh, nec ullamcorper augue elit vel elit."
df = pd.DataFrame({
"A": [1, 2, 3],
"B": ["a", "b", "c"],
"C": [lorem_ipsum, lorem_ipsum, lorem_ipsum],
"D": [True,False,True]
})
true_html = '<input type="checkbox" checked disabled="true">'
false_html = '<input type="checkbox" disabled="true">'
df['D'] = df['D'].apply(lambda b: true_html if b else false_html)
st.markdown(df.to_html(escape=False), 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.