3

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 that I thought it'll do:

@app.route('/map_test/') def test(): svg = render_template('file.svg') response = make_response(svg) response.content_type = 'image/svg+xml' return render_template('test.html', svg=response) 

and test.html:

{% block content %} <section> {{ svg }} </section> {% endblock %} 

....which just returned <Response 126181 bytes [200 OK]> instead of svg element...

so... what do I need to do in order to get this to work?

2
  • what are your imports? Commented Jun 14, 2018 at 6:48
  • from flask import make_response, render_template, request Commented Jun 14, 2018 at 6:53

2 Answers 2

4

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)) 
Sign up to request clarification or add additional context in comments.

4 Comments

Markup( )? did you import this function? is it a custom function? is it a built-in function? Please edit your answer . Thank you
I guess you need to call read. So svg = open('file.svg').read()
Doesn't work, demo in poketyrunts.repl.co/favicon.svg and replit.com/@poketyrunts/poketyrunts?v=1 ( I want it to be like a real SVG )
Works for my svg file having hyperlinks - with small modification by changing .read to .read()
-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

1 Comment

umm... I want my svg element to be dynamic, that's why i've been trying to load the full svg element in... this'll get me to load my svg file as pictures but won't allow me to dynamically change the elements.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.