Textwrap does not work in streamlit?

Summary

I’m working on a streamlit app, need to use altair package to make long text in dataframe interact with other visuals. Leaving the interaction aside, I found the textwrap package does not seem to work in streamlit. Without it, long texts are truncated and incomplete.

Steps to reproduce

import streamlit as st
import pandas as pd
import altair as alt
from textwrap import wrap
st.set_page_config(page_title=‘test’,
layout=‘wide’,
page_icon=‘:chart:’)

df=pd.read_csv(‘comments_testV3.csv’)
df[‘COMMENTSV2’] = df[‘COMMENTS’].astype(str)
df[‘COMMENTSV2’] = df[‘COMMENTSV2’].apply(wrap, args=[120])

chart_1=alt.Chart(df).mark_text().transform_window(
row_number=‘row_number()’
).transform_filter(
alt.datum.row_number < 30
).encode(

y=alt.Y(‘COMMENTSV2’, axis=alt.Axis(labelLimit=1000, labelFontSize=16, grid=False, labelAlign=‘left’
, maxExtent=1
, domain=False
, bandPosition=0
, title=‘’
, ticks=False)),
)

st.altair_chart(chart_1, use_container_width=True,theme=None)

After running it as streamlit app, it looks like this:

Notice the text have brackets and long text are truncated with 3 dots in the end. If I run the same code without streamlit, brackets are gone, full texts are wrapped without truncations. See screenshot below:

Everything else is exact the same. the only difference is the st.set_page_config, and st.altair_chart calls I have to make for streamlit to run. Has anyone experienced this before? Any suggestions are appreciated.

Thank you

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