Hallo,
ich stecke gerade in meinem Projekt fest, das ich mithilfe von ChatGPT gemacht habe.
Funktion: Ein anderes Python Script legt alle 10 Minuten ein Bild von einem Vogelhaus im Verzeichnis: /home/admin/Pictures/Vogelhaus/ ab.
Flask erstellt eine HTML Website, die immer das neuste Bild zeigt, was erstellt wurde. Dazu habe ich ein Flask Script erstellt:
from flask import Flask, render_template
import os
app = Flask(__name__)
@app.route('/')
def index():
# Get the path to the directory containing the photos
photos_dir = '/home/admin/Pictures/Vogelhaus'
# Get a list of all the files in the directory
files = os.listdir(photos_dir)
# Filter out any files that are not images
image_files = [f for f in files if f.endswith('.jpg') or f.endswith('.png')]
# Sort the list of image files by modification time
image_files.sort(key=lambda f: os.path.getmtime(os.path.join(photos_dir, f)))
# Get the path to the newest image file
newest_image = os.path.join(photos_dir, image_files[-1])
# Render the template with the path to the newest image
return render_template('index.html', image_path=newest_image)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
Display More
und dazu habe ich die die Index.html erstellt:
<!DOCTYPE html>
<html>
<head>
<title>Neuestes Foto</title>
</head>
<body>
<h1>Neuestes Foto</h1>
<img src="/home/admin/Pictures/Vogelhaus/" alt="Neuestes Foto">
</body>
</html>
So haben ChatGPT und ich das erarbeitet. (Hauptsächlich ChatGPT). Der Server läuft auch, er greift auf die HTML zu, allerdings schreibt er nur "Neustes Foto" hin. MIt einer 0 x 0 Pixel Grafik unterlegt. Ich vermute, dass das alt="Neuestes Foto" das Problem ist. Welchen Parameter muss ich hier eingeben, dass er tatsächlich immer das neuste Bild nimmt? Und welcher Befehl vom Flask Script soll den Dateinamen eigentlich ausgeben?
Danke schonmal vorab für die Hilfe