Flask doesnt render any image [duplicate]

2024/10/5 16:23:43

I have a flask application where I need to render an image created by Pygal (a visualization library).So I plan to give users access to this created image at an endpoint /viz

app = Flask(__name__)@app.route("/viz")
def viz():img_url = '/home/souvik/PycharmProjects/ServiceHandler/static/histo_visual.svg'return render_template('app.html', image_url=img_url)

Below is the app.html file

<html>
<head><title> Metric Visualization</title>
</head>
<body><div><p>Bar Chart</p><object type="image/svg+xml" data="{{image_url}}"></object></div>
</body>
</html>

Here is my project structure

enter image description here

As you can see the file histo_visual.svg does exists in the static folder.So when I run the program and try to access the /viz endpoint, I get the below error

127.0.0.1 - - [10/Apr/2018 15:08:59] "GET /viz HTTP/1.1" 200 -
127.0.0.1 - - [10/Apr/2018 15:08:59] "GET /home/souvik/PycharmProjects/ServiceHandler/static/ HTTP/1.1" 404 -

Below is the page displayed

enter image description here

Why does it give 404 not found error when the file exists?I have even gone to the directory and run the file through a browser and it shows me the expected visualization.Then what is going wrong?

Answer

When you're accessing the image through a web server, the image url must be relative to the web root. You're using the full file path of the image, but this is not accessible to the application in a web context.

Flask has a neat way of generating URLs for static files:

@app.route("/viz")
def viz():img_url = url_for('static', filename='histo_visual.svg')return render_template('app.html', image_url=img_url)

or alternatively, simply render this directly in the Jinja template:

<object type="image/svg+xml" data="{{ url_for('static', filename='histo_visual.svg') }}"></object>
https://en.xdnf.cn/q/119675.html

Related Q&A

Bug in python thread

I have some raspberry pi running some python code. Once and a while my devices will fail to check in. The rest of the python code continues to run perfectly but the code here quits. I am not sure wh…

how does a function changes the value of a variable outside its scope? Python

i was coding this code and noticed something weird, after my function has been called on the variable, the value of the variable gets changed although its outside of the functions scope, how exactly is…

Python extracting element using bs4, very basic thing I think I dont understand

So Im using Beautiful Soup to try to get an element off of a page using the tag and class. Here is my code: import requests from bs4 import BeautifulSoup# Send a GET request to the webpage url = "…

Why Isnt my Gmail Account Bruteforcer Working? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

Python: Split Start and End Date into All Days Between Start and End Date

Ive got data called Planned Leave which includes Start Date, End Date, User ID and Leave Type.I want to be able to create a new data-frame which shows all days between Start and End Date, per User ID.S…

Python and java AES/ECB/PKCS5 encryption

JAVA VERSION:public class EncryptUtil {public static String AESEncode(String encodeRules, String content) {try {KeyGenerator keygen = KeyGenerator.getInstance("AES");keygen.init(128, new Secu…

How to find the center point of this rectangle

I am trying to find the center point of the green rectangle which is behind the fish, but my approach is not working. Here is my code:#Finding contours (almost always finds those 2 retangles + some noi…

Simple Battleships game implementation in Python

Okay Im not sure how to develop another board with hidden spaces for the computers ships per-se, and have it test for hits. Again Im not even sure how Im going to test for hits on the board I have now.…

How to remove WindowsPath and parantheses from a string [duplicate]

This question already has an answer here:Reference - What does this regex mean?(1 answer)Closed 4 years ago.I need to remove WindowsPath( and some of the closing parentheses ) from a directory string.…

How to escape escape-characters

I have a string variable which is not printing properly, I guess because it contains escape characters. How to escape these escape-characters?>>> print "[p{Aa}\\P{InBasic_Latin}\r\t\n]&q…