Python/Flask/HTML Output is adding spaces</a></h1> </div> <div class="grid fw-wrap pb8 mb16 bb bc-black-075"> <div class="grid--cell ws-nowrap mr16 mb8" title="2016-01-12 19:07:53Z"> <span class="fc-light mr2">Asked</span> <time itemprop="dateCreated" datetime="2016-11-15T19:16:21.533" class="fromnow">Nov 15 '16 at 19:16</time> </div> <div class="grid--cell ws-nowrap mr16 mb8"> <span class="fc-light mr2">Active</span> <time class="fromnow" title="2016-11-15T19:26:25.087" datetime="2016-11-15T19:26:25.087">Nov 15 '16 at 19:26</a> </div> <div class="grid--cell ws-nowrap mb8" title="Viewed 1,497 times"> <span class="fc-light mr2">Viewed</span> 1,497 times </div> </div> <div id="mainbar" role="main" aria-label="questions and answers"> <div id="question" class="question" data-questionid="40617953" data-ownerid="4001787" data-score="0"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="40617953"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> <button class="js-bookmark-btn s-btn s-btn__unset c-pointer py4"> <svg aria-hidden="true" class="svg-icon iconBookmark" width="18" height="18" viewBox="0 0 18 18"><path d="M6 1a2 2 0 00-2 2v14l5-4 5 4V3a2 2 0 00-2-2H6zm3.9 3.83h2.9l-2.35 1.7.9 2.77L9 7.59l-2.35 1.7.9-2.76-2.35-1.7h2.9L9 2.06l.9 2.77z"></path></svg> <div class="js-bookmark-count mt4" data-value=""></div> </button> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>So i am using python flask to output the result of a script to plain text on a webpage. When I print the function from CLI the output looks like this:</p> <pre><code>1 2 3 4 5 </code></pre> <p>However when it is on the website it looks like this:</p> <pre><code>1 2 3 4 5 </code></pre> <p>The output HTML file looks like this:</p> <pre><code>&lt;plaintext&gt; {% for rows in report %} {{rows}} {% endfor %} </code></pre> <p>Im really not great at HTML, but is there a way to remove the new lines that are added?</p></div> <div class="mt24 mb12"> <div class="post-taglist grid gs4 gsy fd-column"> <div class="grid ps-relative"> <a href="../../questions/tagged/python" class="post-tag js-gps-track" title="show questions tagged 'python'" rel="tag">python</a> <a href="../../questions/tagged/html" class="post-tag js-gps-track" title="show questions tagged 'html'" rel="tag">html</a> <a href="../../questions/tagged/flask" class="post-tag js-gps-track" title="show questions tagged 'flask'" rel="tag">flask</a> </div> </div> </div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature owner grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Nov 15 '16 at 19:16">asked Nov 15 '16 at 19:16</time> <a href="../../users/4001787/aladine" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/4001787.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Aladine" /> </a> <div class="s-user-card--info"> <a href="../../users/4001787/aladine" class="s-user-card--link">Aladine</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">185</li> <li class="s-award-bling s-award-bling__silver" title="3 silver badges">3</li> <li class="s-award-bling s-award-bling__bronze" title="16 bronze badges">16</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <div id="answers"> <a name="tab-top"></a> <div id="answers-header"> <div class="answers-subheader grid ai-center mb8"> <div class="grid--cell fl1"> <h2 class="mb0" data-answercount="9">1 Answers<span style="display:none;" itemprop="answerCount">1</span></h2> </div> </div> </div> <a name="40618042"></a> <div id="answer-40618042" class="answer accepted-answer" data-answerid="40618042" data-ownerid="2644759" data-score="1" itemprop="acceptedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="40618042"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> <div class="js-accepted-answer-indicator grid--cell fc-green-500 py6 mtn8"><div class="ta-center"><svg aria-hidden="true" class="svg-icon iconCheckmarkLg" width="36" height="36" viewBox="0 0 36 36"><path d="m6 14 8 8L30 6v8L14 30l-8-8v-8z"></path></svg></div></div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><ol> <li><p><code>&lt;plaintext&gt;</code> is obsolete. Use <code>&lt;pre&gt;</code> instead. See: <a class="external-link" href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/plaintext" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/plaintext</a></p></li> <li><p>The <code>rows</code> variable you used may contains line breaks already. And you don't need to use for loop if you just want to output every line in <code>report</code>. Try this:</p> <pre><code>&lt;pre&gt;{{ ''.join(report) }}&lt;/pre&gt; </code></pre></li> <li><p>Considering the control structure. You can use Jinja2's <a class="external-link" href="http://jinja.pocoo.org/docs/dev/templates/#whitespace-control" rel="nofollow noreferrer">whitespace control</a> feature to avoid extra spaces (and line breaks):</p> <pre><code>&lt;pre&gt; {% for row in report -%} {{ row }} {%- endfor %} &lt;/pre&gt; </code></pre></li> </ol></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2016-11-15T19:26:25.087" class="relativetime">Nov 15 '16 at 19:26</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Philip Tzou</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Nov 15 '16 at 19:21">answered Nov 15 '16 at 19:21</time> <a href="../../users/2644759/philip-tzou" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/2644759.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Philip Tzou" /> </a> <div class="s-user-card--info"> <a href="../../users/2644759/philip-tzou" class="s-user-card--link">Philip Tzou</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">5,926</li> <li class="s-award-bling s-award-bling__gold" title="2 gold badges">2</li> <li class="s-award-bling s-award-bling__silver" title="18 silver badges">18</li> <li class="s-award-bling s-award-bling__bronze" title="27 bronze badges">27</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-40618042" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="40618042" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-68470286" class="comment js-comment " data-comment-id="68470286" data-comment-owner-id="4001787" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment68470286_40618042"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Thank you! This works perfectly, sorry for the duplicate, I did not find that one when I searched.</span> –&nbsp;<a href="../../users/4001787/aladine" title="185 reputation" class="comment-user owner">Aladine</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/40617953/python-flask-html-plaintext-output-is-adding-spaces#comment68470286_40618042"><span title="2016-11-15T19:29:32.293 License: CC BY-SA 3.0" class="relativetime-clean">Nov 15 '16 at 19:29</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> </div> </div> </div> </div> <script src="../../static/js/stack-icons.js"></script> <script src="../../static/js/fromnow.js"></script> </body> </html>