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

View File

@@ -3,65 +3,80 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wetterstationen in Deutschland</title>
<title>Wetterdaten nach Stationen</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 0;
padding: 0;
padding: 20px;
background-color: #f4f4f4;
}
h1 {
color: #333;
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
table {
margin: 0 auto;
border-collapse: collapse;
width: 90%;
table, th, td {
border: 1px solid black;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
padding: 10px;
text-align: center;
}
th {
background-color: #007BFF;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
tr:hover {
background-color: #ddd;
}
</style>
</head>
<body>
<h1>Wetterdaten von Deutschen Wetterstationen</h1>
<h1>Wetterdaten nach Stationen</h1>
<table>
<thead>
<tr>
<th>Stationsnummer</th>
<th>Stationsname</th>
<th>Temperatur (°C)</th>
<th>Station Nummer</th>
<th>Station Name</th>
<th>Temperatur (K)</th>
<th>Taupunkt (K)</th>
<th>Relative Luftfeuchtigkeit (%)</th>
<th>Windgeschwindigkeit (m/s)</th>
<th>Luftdruck (Pa)</th>
</tr>
</thead>
<tbody>
{% for station in weather_data %}
<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 id="weather-table-body">
<!-- Daten werden hier geladen -->
</tbody>
</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>
</html>