Flask and .svg picture not show
Question:
I started to work with python – Flask and I found some problems which i didn’t solve.
When I want show picture in .svg format so it doesn’t show in flask but when i open html file (index.html) in for example Firefox it showed!
Code in Flask template:
when i show source code (in flask server) it is . Link /static/img/logo-landing.svg is found but image doesn’t show. It show only alt (‘logo’)
I don’t know what is wrong. I was use google full last night.
Thanks Mike
Answers:
You have to serve your svg-images with Content-Type image/svg+xml
.
from flask import send_file
@app.route('/svg_returned_endpoint', methods=['POST'])
def svg_returned_endpoint():
# HTML version
# this gets the json object from the user
json_obj = {'input_1': request.values['input_1']}
input_1_item = json_obj["input_1"]
# normal endpoint version
# this gets the json object from the user
json_obj = request.get_json(force=True)
input_1_item = json_obj["input_1"]
####
## Your Actual function/model Code Goes Here
####
filename = 'YOUR_SAVED_IMAGE.svg'
return send_file(filename, mimetype='image/svg+xml')
I started to work with python – Flask and I found some problems which i didn’t solve.
When I want show picture in .svg format so it doesn’t show in flask but when i open html file (index.html) in for example Firefox it showed!
Code in Flask template:
when i show source code (in flask server) it is . Link /static/img/logo-landing.svg is found but image doesn’t show. It show only alt (‘logo’)
I don’t know what is wrong. I was use google full last night.
Thanks Mike
You have to serve your svg-images with Content-Type image/svg+xml
.
from flask import send_file
@app.route('/svg_returned_endpoint', methods=['POST'])
def svg_returned_endpoint():
# HTML version
# this gets the json object from the user
json_obj = {'input_1': request.values['input_1']}
input_1_item = json_obj["input_1"]
# normal endpoint version
# this gets the json object from the user
json_obj = request.get_json(force=True)
input_1_item = json_obj["input_1"]
####
## Your Actual function/model Code Goes Here
####
filename = 'YOUR_SAVED_IMAGE.svg'
return send_file(filename, mimetype='image/svg+xml')