Python SVG嵌入HTML时不显示。单独显示即可
我正在使用Flask制作一个web应用程序。我创建了以下函数来动态呈现不同颜色的SVG图标 所有测试都是在MacOSX上的最新Google Chrome中完成的Python SVG嵌入HTML时不显示。单独显示即可,python,html,svg,flask,Python,Html,Svg,Flask,我正在使用Flask制作一个web应用程序。我创建了以下函数来动态呈现不同颜色的SVG图标 所有测试都是在MacOSX上的最新Google Chrome中完成的 @app.route("/testicon") def testicon(): return render_template("icons/test.svg", primary="ff0000") (显然,主参数在实践中不是硬编码的。这只是一个演示) 此函数用于呈现以下SVG文件: <?xml version="1.0"
@app.route("/testicon")
def testicon():
return render_template("icons/test.svg", primary="ff0000")
(显然,主
参数在实践中不是硬编码的。这只是一个演示)
此函数用于呈现以下SVG文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
height="150"
width="150">
<path
id="path3800" class="icon-primary"
d="m 32.745902,139.71585 -0.385246,-80.002735 31.333333,-41.349727 z"
style="fill:#{{ primary }};stroke:none;" />
</svg>
test.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<img src="{{ url_for('gameplay.testicon') }}" height="300px" width="300px">
</body>
</html>
标题
访问localhost:5000/test
时,没有显示任何内容。我可以检查img
元素,发现它的src
属性确实是/testicon
,但SVG不显示。而是显示标准的“未找到图像”图标。然而,我可以右键点击它并选择“在新选项卡中打开图像”,图像再次显示良好。Google Chrome控制台不会显示错误,就像我在图像的src
中添加了一个错误链接一样
如何才能正确显示此图像?我在点击“提交”之前找到了答案,所以我想我会把答案放在这里 我需要设置响应的mime类型。我将我的testicon功能更改为:
@app.route("/testicon")
def testicon():
response = make_response(render_template("icons/test.svg", primary="ff0000"))
response.mimetype = "image/svg+xml"
return response
现在它工作得很好。我在这个问题上点击“提交”前几分钟就找到了答案,所以我想我应该把答案放在这里 我需要设置响应的mime类型。我将我的testicon功能更改为:
@app.route("/testicon")
def testicon():
response = make_response(render_template("icons/test.svg", primary="ff0000"))
response.mimetype = "image/svg+xml"
return response
现在它工作得很好