Search code examples
pythonhtmlweb2py

Web2Py add newlines in a column's text while rendering HTML table


I am using the following code to dynamically generate the content of my HTML table using Web2Py helpers:

response_results = []

for row in db(db.table.column_name == selected_column_val).select(db.table.ALL):
    body_col = row.body_col
    row_data = ['Col 1', row.col2, body_col.replace(".", ".<br/><br/>"), row.col4]
    response_results.append(row_data)

html = DIV(TABLE(THEAD(TR(TH('Row #'), TH('Col 1'), TH('Col 2'), TH('Col 3'), TH('Col 4')), _id=0),
             *[TR(response) for response in response_results],
             _id='records_table', _class='table table-bordered'),
       _class='table-responsive')

Basically, in here I need to render the content of the body_col in such a way that it's more readable so I am trying to add some newlines after each period '.'. I have tried using these two but none of them make any difference: body_col.replace(".", ".<br/><br/>") and body_col.replace(".", ".\n\n").

Regarding the output displayed:

  • When I directly add the output with the <br/> tags in HTML it looks fine.

Input:

This is line one.
This is line two.
This is line three.
This is line four.
This is line five.

Output:

This is line one.
This is line two.
This is line three.
This is line four.
This is line five.

  • When I render the output HTML dynamically using body_col.replace(".", ".\n\n"), it all comes as a single line without any line breaks.

Input:

This is line one.
This is line two.
This is line three.
This is line four.
This is line five.

Output:

This is line one. This is line two. This is line three. This is line four. This is line five.

  • When I render the output HTML dynamically using body_col.replace(".", ".<br/><br/>"), it all comes as a single line without any line breaks but with literal <br/> tags displayed.

Input:

This is line one.
This is line two.
This is line three.
This is line four.
This is line five.

Output:

This is line one.<br/><br/> This is line two.<br/><br/> This is line three.<br/><br/> This is line four.<br/><br/> This is line five.

Even when I analyze the input/output text in Notepad++ to check the presence of CR LF characters. I notice that the CR LF are always present except only when being generated dynamically. In that case there are no CR LF characters present, even though they were there in the input data of body_col. Somehow, they all are getting stripped out while Web2Py HTML helpers generate the output table.

I referred to this link as well but couldn't quite get it to apply for my specific need.

Can somebody guide me how can I accomplish this task?

All your help would be greatly appreciated.


Solution

  • One problem with the code is that it includes raw HTML, which web2py will escape and display literally in the rendered output. To prevent this, you must wrap any raw HTML strings in the XML() helper:

    row_data = ['Col 1', row.col2, XML(body_col.replace(".", ".<br/><br/>")), row.col4]
    

    However, that may not be your only problem -- if it were, you would expect to see literal "<br/>" strings displayed in the page, but you are not seeing that.