Converting stdout stream to html (add <br> on linebreaks)
<p>I'm trying to take some console output text and render it through django/js in a modal on my site. When printing the console output the line breaks work fine, but when rendered on the site it shows them all as one line. I tried replacing all the \n with <code>&lt;br&gt;</code> but it didn't seem to have any effect.  The <code>&lt;br&gt;</code> are shown in line as plain text. Any thoughts on a better way to do this/why this isn't working in the first place?</p>

<pre><code>import sys
from io import StringIO

# Save the old stdout
old_stdout = sys.stdout

# Save the stdout to variable
sys.stdout = mystdout = StringIO()

... # Do some processing that generates console text

# Reset the to the old stdout
sys.stdout = old_stdout

# Get the stdout
processing_std_out = mystdout.getvalue()

# Replace all the linebreaks with &lt;br&gt;
# This is the important part
processing_std_out = processing_std_out.replace("\n","&lt;br&gt;")

# return HttpResponse(json.dumps({'console_output':processing_std_out}), content_type="application/json")
</code></pre>

<p>The js is this:</p>

<pre><code>input_modal.find('.modal-body').text('Analysis complete'+response.console_output)
</code></pre>

<p>Simple mistake, I should have been passing the HTML, not the text. Also, adding pre tags to the text is much simpler than replacing all \n</p>

<pre><code>input_modal.find('.modal-body').html('Analysis complete'+response.console_output)
</code></pre>

javascript python html django