Wrap text from Python

I have a Python notebook that returns paragraphs of text.  When I print the paragraphs, they come out as a single long line that I can only read on a horizontal scroll.  I want the text to appear on the report as wrapped paragraphs.

I have tried several different commands in both Python and CSS, but nothing I have tried has caused the paragraphs to wrap.

Here is the Python:

pd.set_option('display.width', 30)

for index, row in df.iterrows():
  print row['mail_id'],row['created_at']
  print row['notes']
  print row['tags']
  print row['text_cleaned']
  print '----'

Here is the CSS:

  @import url('https://fonts.googleapis.com/css?family=Montserrat:400,600');
  .python-results-text {
    font-family: 'Montserrat', sans-serif;
    font-size: 14px;
    font-weight: 400;
    word-wrap: normal;

Here is the output:


5replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Hi Tara -

    Typically the best practice for this sort of thing is to return the dataframe as a table rather than printing the text. If you include:

    pd.set_option('display.max_colwidth', -1)

    you'll avoid truncating the long string, and the table will ensure the text is wrapped. Here's an example report that does this.

    Would this work for your use case?

    Reply Like 1
  • Thank so much!  It doesn't work in my case because table form just doesn't make much sense for what I am trying to print. I have one field that is several paragraphs and the rest which are very short ...if I print it as a table then the paragraphs will get squeezed into a column, just not a good use of space for user readability. I do understand how to print a dataframe, but I'm looking for something that will work with the print statements shown above as my output.  Any ideas?  Thanks again!

    Reply Like
  • Tara -

    Got it. In that case you can accomplish this by formatting the text output in HTML. To do that, you'll first need to cast your Python output to HTML and assign it a class. Then you'll be able to reference the class via the HTML editor and format it as you like.

    Here's an example report that does this, and here's the Python and HTML that powers it.

    Hope this helps!

    Reply Like 1
  • sorry it took me a while to get back to you, but wanted to let you know that this worked like a charm!  :-)

    Reply Like 1
  • Great!

    Reply Like
Like Follow
  • Status Answered
  • 1 yr agoLast active
  • 5Replies
  • 967Views
  • 3 Following