Displaying newlines in streamlit table

Summary

My streamlit table consists of text data which has newlines in them, but streamlit does not add the newlines when displaying the table How can I fix this? thanks.

Steps to reproduce

Code snippet:

import streamlit as st
import pandas as pd

data = {‘Name’: [‘John Doe’, ‘Jane Smith’, ‘Bob Johnson’],
‘Description’: [‘line 1 \n line 2’,
‘line 3 \n line 4’,
‘line 5 \n line 6’,
]}

df = pd.DataFrame(data)
st.table(df)

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

I would like to see line breaks between lines in the description column.

Explain what you expect to happen when you run the code above.

Actual behavior:

Explain the undesired behavior or error you see when you run the code above.
If you’re seeing an error message, share the full contents of the error message here.

Debug info

  • Streamlit version: 1.22.0

  • Python version: 3.80.0

  • PyEnv

  • Browser version: 112.0.5615.137

Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.

Links

  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

If needed, add any other context about the problem here.

1 Like

For a static table, replacing the newline characters by HTML line breaks can work.

import streamlit as st
import pandas as pd

data = {"Name": ["John Doe", "Jane Smith", "Bob Johnson"],
"Description": ["line 1 \n line 2",
"line 3 \n line 4",
"line 5 \n line 6",
]}

df = pd.DataFrame(data)

# Replace the \n with an HTML linebreak
df = df.applymap(lambda x: x.replace('\n', '<br>'))

# Show as a static table
st.markdown(df.to_html(escape=False), unsafe_allow_html=True)
2 Likes

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