Skip to content Skip to sidebar Skip to footer

How Can I Load Svg File Into My Python Flask Page?

So I want to put an svg file into my page(not as an image, as an xml element) - need it like this so I could dynamically change elements within the page. and I made a simple code

Solution 1:

this did the trick:

from flask import Markup

@app.route('/map_test/')
def test():
   svg = open('file.svg').read
   return render_template('test.html', svg=Markup(svg))

Solution 2:

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def hello_world():
   img = './static/download.svg'

   return render_template('index.html', img=img)


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

index.html

<!DOCTYPE html>
<html lang="en">
<body>
<img src="{{ img }}">
</body>
</html>

put your svg file in dir called static


Post a Comment for "How Can I Load Svg File Into My Python Flask Page?"