我试图通过Flask将歌词传递到HTML文件中,但格式不正确。
这是我希望的格式,我以相同的格式传递到文本变量中 – 文本变量的值
这是在HTML网站上显示的格式 – 实际结果
如何在网站上正确格式化它,因为它已经被以格式化方式传递了。
这是Python代码 –
import flaskimport subprocessimport sysimport generate_unconditional_samples as modelapp = 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: centerwidth: 100%;white-space: pre-line;border: 1px solid #ccc;}