簡體   English   中英

通過 flask 渲染模板到 html 文件的文本格式不正確

[英]The Text passed through the flask render template to the html file is not formatting properly

我正在嘗試通過 flask 將歌詞傳遞給 html 文件,但格式不正確。

這就是我希望它的格式,並且我以相同的格式將它傳遞到文本變量中 -文本變量的值

這就是它在 html 網站上的顯示方式 -實際結果

我如何在網站上正確格式化它,因為它已經以格式化的方式傳遞。

這是 python 代碼 -

import flask
import subprocess
import sys
import generate_unconditional_samples as model

app = flask.Flask(__name__, template_folder='templates')

@app.route('/', methods=['GET', 'POST'])
def main():
if flask.request.method == 'GET':
    return(flask.render_template('main.html'))

if flask.request.method == 'POST':
    text =  model.sample_model("Lyric", None, 0, 1, None, 0.8, 40, 0.0)  
    print(text)
    return flask.render_template('main.html', result = text,)
    

if __name__ == '__main__':
   app.run() 

這是 HTML 代碼 -

<!doctype html>

<html>
<style>

form {
    margin: auto;
    width: 35%;
 }

.result {
    margin: auto;
    width: 100%;
    border: 1px solid #ccc;
 }

 </style>
 <head>
 <title>Lyric Generator</title>
 </head>
 <body>

 <form action="{{ url_for('main') }}" method="POST">
    <fieldset>
       <input type="submit">
    </fieldset>
 </form>

 <div class="result" align="center">
     {% if result %}
         <br> Generated Lyrics:
         <p style="font-size:20px">{{ result }}</p>
     {% endif %}
 </div>
 <body>
 </html>

我得到了答案,只需要使用 white-space: pre-line 來溢出線條

.result {
margin: center
width: 100%;
white-space: pre-line;
border: 1px solid #ccc;
}

最終結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM