How to display all images in a directory with flask [duplicate]

2024/7/27 16:10:18

I am trying to display all images from a particular directory in static (static/plots). Here is my python:

hists = os.listdir('static/plots')
hists = ['plots/' + file for file in hists]
return render_template('report.html', hists = hists)

and my html:

<!DOCTYPE=html>
<html>
<head><title>Store Report</title>
</head>
<body>{{first_event}} to {{second_event}}{% for hist in hists %}<img src="{{url_for('static', filename='{{hist}}')}}" alt="{{hist}}">{% endfor %}
</body>
</html>`

And when the template successfully renders, the templates are not fetched. Opening the image in a new tab yields: http://127.0.0.1:8080/static/%7B%7Bhist%7D%7D

I imagine the problem is with this line, but I can't figure out what's correct:

<img src="{{url_for('static', filename='{{hist}}')}}" alt="{{hist}}">

Answer

Try changing the line to this:

<img src="{{url_for('static', filename=hist)}}" alt="{{hist}}">

You had an extra set of {{ }} in there which was rendering as %7B and %7D.

https://en.xdnf.cn/q/73060.html

Related Q&A

Reindex sublevel of pandas dataframe multiindex

I have a time series dataframe and I would like to reindex it by Trials and Measurements.Simplified, I have this:value Trial 1 0 131 32 42 3 NaN4 123…

How to publish to an Azure Devops PyPI feed with Poetry?

I am trying to set up Azure Devops to publish to a PyPI feed with Poetry. I know about Twine authentication and storing credentials to an Azure Key Vault. But is there any more straightforward method?…

Python Regex Match Before Character AND Ignore White Space

Im trying to write a regex to match part of a string that comes before / but also ignores any leading or trailing white space within the match.So far Ive got ^[^\/]* which matches everything before the…

Python Twisted integration with Cmd module

I like Pythons Twisted and Cmd. I want to use them together.I got some things working, but so far I havent figured out how to make tab-completion work, because I dont see how to receive tab keypres ev…

Read .pptx file from s3

I try to open a .pptx from Amazon S3 and read it using the python-pptx library. This is the code: from pptx import Presentation import boto3 s3 = boto3.resource(s3)obj=s3.Object(bucket,key) body = obj.…

PIL image display error It looks like the image was moved or renamed

Here is a bit of my code:from PIL import Imageimage = Image.open(fall-foliage-1740841_640.jpg) image.show()The error is when the default photo viewer is started and shows the error "It looks like …

How to delete numpy nan from a list of strings in Python?

I have a list of strings x = [A, B, nan, D]and want to remove the nan.I tried:x = x[~numpy.isnan(x)]But that only works if it contains numbers. How do we solve this for strings in Python 3+?

Pasting data into a pandas dataframe

This is the same question as this question, which is marked as a duplicate of this one. The problem, and the reason Im still asking, is that the solution provided (using pandas.read_clipboard()) curren…

Add graph description under graph in pylab [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Is there a way of drawing a caption box in matplotlib Is it possible to add graph description under graph in pylab?Lets s…

Using Python textwrap.shorten for string but with bytes width

Id like to shorten a string using textwrap.shorten or a function like it. The string can potentially have non-ASCII characters. Whats special here is that the maximal width is for the bytes encoding of…