modified: app.py

modified:   templates/index.html
This commit is contained in:
SimolZimol
2024-09-10 14:56:00 +02:00
parent 590f107d1b
commit 70e0c36724
2 changed files with 69 additions and 57 deletions

47
app.py
View File

@@ -22,38 +22,35 @@ def load_weather_data():
weather_data = [] weather_data = []
for message in data["messages"]: for message in data["messages"]:
# Jede Nachricht kann mehrere Wetterstationen enthalten
if isinstance(message, list): if isinstance(message, list):
for subset in message[1:]: # Ignoriere den ersten Header-Teil for station_data in message[1:]:
station_info = {} station_info = {}
if isinstance(subset, list): # Überprüfe, ob station_data eine Liste ist
for item in subset: if isinstance(station_data, list):
# Sicherstellen, dass wir ein Dictionary haben for info in station_data:
if isinstance(item, dict): # Stelle sicher, dass info ein Dictionary ist
key = item.get("key") if isinstance(info, dict) and "key" in info:
value = item.get("value") if info["key"] == "stationNumber":
# Hier relevante Daten extrahieren und speichern station_info["stationNumber"] = info["value"]
if key == "stationNumber": elif info["key"] == "stationOrSiteName":
station_info["stationNumber"] = value station_info["stationName"] = info["value"]
elif key == "stationOrSiteName": elif info["key"] == "airTemperature":
station_info["stationName"] = value station_info["temperature"] = round(info["value"] - 273.15, 2) # Umwandlung von K in °C
elif key == "airTemperature": elif info["key"] == "windSpeed":
station_info["temperature"] = round(value - 273.15, 2) # Umrechnung K -> °C station_info["windSpeed"] = info["value"]
elif key == "relativeHumidity": elif info["key"] == "pressureReducedToMeanSeaLevel":
station_info["humidity"] = value station_info["pressure"] = info["value"]
elif key == "pressureReducedToMeanSeaLevel":
station_info["pressure"] = value # Füge nur Daten hinzu, wenn sie vollständig sind
elif key == "windSpeed": if "stationNumber" in station_info:
station_info["windSpeed"] = value
# Füge nur vollständige Einträge hinzu
if "stationNumber" in station_info and station_info:
weather_data.append(station_info) weather_data.append(station_info)
# Sortiere nach stationNumber # Sortiere nach stationNumber
weather_data.sort(key=lambda x: x["stationNumber"] if "stationNumber" in x else None) weather_data.sort(key=lambda x: x["stationNumber"])
return weather_data return weather_data
# Flask Route für die Hauptseite # Flask Route für die Hauptseite
@app.route('/') @app.route('/')
def index(): def index():

View File

@@ -3,65 +3,80 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wetterstationen in Deutschland</title> <title>Wetterdaten nach Stationen</title>
<style> <style>
body { body {
font-family: Arial, sans-serif; font-family: Arial, sans-serif;
text-align: center; text-align: center;
margin: 0; margin: 0;
padding: 0; padding: 20px;
background-color: #f4f4f4; background-color: #f4f4f4;
} }
h1 {
color: #333; table {
width: 100%;
border-collapse: collapse;
margin: 20px 0; margin: 20px 0;
} }
table {
margin: 0 auto; table, th, td {
border-collapse: collapse; border: 1px solid black;
width: 90%;
} }
th, td { th, td {
border: 1px solid #ddd; padding: 10px;
padding: 8px; text-align: center;
} }
th { th {
background-color: #007BFF; background-color: #007BFF;
color: white; color: white;
} }
tr:nth-child(even) {
background-color: #f2f2f2;
}
tr:hover {
background-color: #ddd;
}
</style> </style>
</head> </head>
<body> <body>
<h1>Wetterdaten von Deutschen Wetterstationen</h1> <h1>Wetterdaten nach Stationen</h1>
<table> <table>
<thead> <thead>
<tr> <tr>
<th>Stationsnummer</th> <th>Station Nummer</th>
<th>Stationsname</th> <th>Station Name</th>
<th>Temperatur (°C)</th> <th>Temperatur (K)</th>
<th>Taupunkt (K)</th>
<th>Relative Luftfeuchtigkeit (%)</th> <th>Relative Luftfeuchtigkeit (%)</th>
<th>Windgeschwindigkeit (m/s)</th> <th>Windgeschwindigkeit (m/s)</th>
<th>Luftdruck (Pa)</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody id="weather-table-body">
{% for station in weather_data %} <!-- Daten werden hier geladen -->
<tr>
<td>{{ station.stationNumber }}</td>
<td>{{ station.stationName }}</td>
<td>{{ station.temperature }}</td>
<td>{{ station.humidity }}</td>
<td>{{ station.windSpeed }}</td>
<td>{{ station.pressure }}</td>
</tr>
{% endfor %}
</tbody> </tbody>
</table> </table>
<script>
async function fetchWeatherData() {
const response = await fetch('/weather_data');
const data = await response.json();
const tableBody = document.getElementById('weather-table-body');
tableBody.innerHTML = ''; // Tabelle leeren
data.forEach(station => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${station.stationNumber || 'N/A'}</td>
<td>${station.stationName || 'N/A'}</td>
<td>${station.temperature ? station.temperature.toFixed(2) : 'N/A'}</td>
<td>${station.dewpoint ? station.dewpoint.toFixed(2) : 'N/A'}</td>
<td>${station.humidity ? station.humidity.toFixed(2) : 'N/A'}</td>
<td>${station.windSpeed ? station.windSpeed.toFixed(2) : 'N/A'}</td>
`;
tableBody.appendChild(row);
});
}
// Wetterdaten laden, sobald die Seite geladen ist
window.onload = fetchWeatherData;
</script>
</body> </body>
</html> </html>