Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
75 views
in Technique[技术] by (71.8m points)

python - Is there a way to pass HTML code in flask as string?

Is there a way to pass HTML code as a string in python/flask rather than render template HTML file in a directory?

for example

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def home():
   return render_template('index.html') # Instead of giving file path I want to pass HTML code directly 
if __name__ == '__main__':
   app.run()
question from:https://stackoverflow.com/questions/65856863/is-there-a-way-to-pass-html-code-in-flask-as-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I guess you mean something like this:

from flask import Flask
app = Flask(__name__)


HTML_AS_TEXT = """<p>hello world</p>"""

@app.route('/')
def home():
   return HTML_AS_TEXT

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...