猿问

使用长颈瓶用图像替换字符串中的文本

我编写了一段匹配的代码,如果该字符串具有模式文本中的任何内容。someExtension,就我而言,它将是字符串中的fileName.png,将其转换为img标签,并使用python和在HTML文件上显示烧瓶。让我们以示例字符串为例:“此程序的输出是什么?e.png”,代码匹配e.png,然后将其替换为

"<br><img src="{{url_for('static', filename='pics/e.png')}}" /><br>"

图像e.png放在静态文件夹内的文件夹pics中。如果即使通过向其添加Markup()将该字符串推入flask变量中,它也不会呈现图像,而是显示以下输出。

为什么会这样呢?有什么办法让它显示图像e.png?


我的代码是:


import re

from flask import Flask,  Markup, render_template

app = Flask(__name__)


def rep(x):

    text = re.findall(r'\w+[.]\w+', x)

    for i in text:

        b ="<img src=\"{{ url_for('static',filename='pics/"+i+"')}}\">"

        x=x.replace(i,b)

    return x


@app.route('/')

def home():

    a = "What is the output of this program? e.png"

    a = rep(a)

    return render_template('new.html',var=Markup(a))


if __name__ == '__main__':

    app.run(debug=True, host='localhost', port=8032)

HTML文件是


<!DOCTYPE html>

<html>


<head>

  <meta charset="UTF-8">

  <title>Title</title>

</head>


<body>

  {{var}}

</body>


</html>


白板的微信
浏览 128回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答