简体   繁体   中英

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

I am trying to pass lyrics to the html file through flask but it is not formatting properly.

This is how I want it formatted, and I am passing it in the text variable in the same format - The value of the text variable

This is how it is showing in the html website - The actual result

How do I get it formatted properly on the website as it is already passed in a formatted way.

This is the python code -

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() 

And This is the HTML code -

<!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>

I got the answer, just had to use the white-space: pre-line to spilt the lines

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

The Final Result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM