modified: app.py
modified: templates/index.html
This commit is contained in:
56
app.py
56
app.py
@@ -1,32 +1,50 @@
|
||||
import requests
|
||||
import os
|
||||
import bz2
|
||||
import json
|
||||
from flask import Flask, jsonify
|
||||
import requests
|
||||
from flask import Flask, render_template
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# URL zur komprimierten Wetterdatei
|
||||
url = "https://opendata.dwd.de/weather/weather_reports/synoptic/germany/json/Z__C_EDZW_latest_bda01%2Csynop_bufr_GER_999999_999999__MW_XXX.json.bz2"
|
||||
|
||||
# Funktion zum Herunterladen und Entpacken der bz2-Datei
|
||||
def download_and_extract(url):
|
||||
response = requests.get(url)
|
||||
def download_and_extract_data():
|
||||
url = 'https://opendata.dwd.de/weather/weather_reports/synoptic/germany/json/Z__C_EDZW_latest_bda01,synop_bufr_GER_999999_999999__MW_XXX.json.bz2'
|
||||
response = requests.get(url, stream=True)
|
||||
|
||||
if response.status_code == 200:
|
||||
compressed_content = response.content
|
||||
decompressed_content = bz2.decompress(compressed_content)
|
||||
return decompressed_content
|
||||
# Entpacke die bz2-Datei
|
||||
compressed_file = bz2.BZ2File(response.raw)
|
||||
data = json.loads(compressed_file.read().decode('utf-8'))
|
||||
return data
|
||||
else:
|
||||
print(f"Fehler beim Abrufen der Daten: {response.status_code}")
|
||||
return None
|
||||
|
||||
# Flask-Route, die die Wetterdaten bereitstellt
|
||||
@app.route('/weather')
|
||||
def weather():
|
||||
data = download_and_extract(url)
|
||||
if data:
|
||||
weather_data = json.loads(data)
|
||||
return jsonify(weather_data)
|
||||
# Route für die Homepage
|
||||
@app.route('/')
|
||||
def home():
|
||||
weather_data = download_and_extract_data()
|
||||
|
||||
if weather_data is not None:
|
||||
# Sortiere die Daten nach stationNumber
|
||||
stations = {}
|
||||
for message in weather_data['messages']:
|
||||
for station_data in message[1:]:
|
||||
station_number = station_data[1]['value']
|
||||
station_name = station_data[2][0]['value']
|
||||
if station_number not in stations:
|
||||
stations[station_number] = {
|
||||
'station_name': station_name,
|
||||
'data': station_data
|
||||
}
|
||||
|
||||
# Sortiere die Stationen nach stationNumber
|
||||
sorted_stations = dict(sorted(stations.items()))
|
||||
|
||||
# Übergib die Daten an das Template
|
||||
return render_template('index.html', stations=sorted_stations)
|
||||
else:
|
||||
return jsonify({"error": "Fehler beim Abrufen der Wetterdaten"}), 500
|
||||
return "Fehler beim Abrufen der Wetterdaten"
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
app.run(debug=True)
|
||||
|
||||
@@ -3,66 +3,43 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Aktuelle Wetterdaten</title>
|
||||
<title>Wetterdaten nach Station</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #f4f4f4;
|
||||
margin: 0;
|
||||
margin: 20px;
|
||||
padding: 20px;
|
||||
text-align: center;
|
||||
}
|
||||
h1 {
|
||||
color: #333;
|
||||
}
|
||||
.weather-container {
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
text-align: center;
|
||||
}
|
||||
.station {
|
||||
padding: 10px;
|
||||
border: 1px solid #ddd;
|
||||
margin-bottom: 10px;
|
||||
background-color: white;
|
||||
text-align: left;
|
||||
padding: 15px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.station h2 {
|
||||
margin: 0;
|
||||
}
|
||||
.weather-data {
|
||||
margin-left: 20px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Aktuelle Wetterdaten</h1>
|
||||
<div id="weather-container" class="weather-container">
|
||||
<p>Daten werden geladen...</p>
|
||||
<h1>Wetterdaten nach Station</h1>
|
||||
|
||||
{% for station_number, station_info in stations.items() %}
|
||||
<div class="station">
|
||||
<h2>Station {{ station_number }}: {{ station_info.station_name }}</h2>
|
||||
<div class="weather-data">
|
||||
<p><strong>Temperatur:</strong> {{ station_info.data[3][3]['value'] }} K</p>
|
||||
<p><strong>Windrichtung:</strong> {{ station_info.data[3][4]['value'] }}°</p>
|
||||
<p><strong>Windgeschwindigkeit:</strong> {{ station_info.data[3][5]['value'] }} m/s</p>
|
||||
<p><strong>Luftdruck auf Meereshöhe:</strong> {{ station_info.data[3][8]['value'] }} Pa</p>
|
||||
<p><strong>Sichtweite:</strong> {{ station_info.data[3][9]['value'] }} m</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Funktion zum Abrufen der Wetterdaten von der Flask-API
|
||||
async function fetchWeatherData() {
|
||||
const response = await fetch('/weather');
|
||||
const data = await response.json();
|
||||
displayWeatherData(data);
|
||||
}
|
||||
|
||||
// Funktion zum Anzeigen der Wetterdaten
|
||||
function displayWeatherData(data) {
|
||||
const container = document.getElementById('weather-container');
|
||||
container.innerHTML = ''; // Lösche den Lade-Text
|
||||
|
||||
data.forEach(station => {
|
||||
const stationDiv = document.createElement('div');
|
||||
stationDiv.className = 'station';
|
||||
stationDiv.innerHTML = `
|
||||
<h3>${station.station}</h3>
|
||||
<p>Temperatur: ${station.temperature.toFixed(2)}°C</p>
|
||||
<p>Luftdruck: ${station.pressure} hPa</p>
|
||||
<p>Windgeschwindigkeit: ${station.wind_speed} m/s</p>
|
||||
<p>Wolkenbedeckung: ${station.cloud_cover}%</p>
|
||||
`;
|
||||
container.appendChild(stationDiv);
|
||||
});
|
||||
}
|
||||
|
||||
// Rufe die Wetterdaten ab, wenn die Seite geladen ist
|
||||
window.onload = fetchWeatherData;
|
||||
</script>
|
||||
{% endfor %}
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user