To wrap text in cell outputs in a Jupyter notebook, you can use the pandas
library to display the text in a DataFrame with word wrapping enabled. Here's an example:
import pandas as pd
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
df = pd.DataFrame({'text': [text]})
pd.set_option('display.max_colwidth', None)
display(df)
import pandas as pd
pd.set_option('display.max_colwidth', None)
text = prompt
display(pd.DataFrame({'text': [text]}))
This code defines a string text
with a long sentence, and then creates a DataFrame df
with a single column containing the text. The pd.set_option('display.max_colwidth', None)
line sets the maximum column width to unlimited, which enables word wrapping. Finally, the display(df)
function is used to display the DataFrame in the notebook output.
When you run this code in a Jupyter notebook, you should see the text wrapped to fit the width of the output cell.
Note that you can adjust the maximum column width by passing a value to the pd.set_option('display.max_colwidth', value)
function, where value
is the maximum number of characters to display in each column.