Spaces:
Sleeping
Sleeping
File size: 2,479 Bytes
006bd0b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
function fetchWeather() {
const city = document.getElementById('city-input').value;
const days = document.getElementById('forecast-days').value;
fetch(`/weather?city=${encodeURIComponent(city)}&days=${days}`)
.then(response => response.json())
.then(data => {
if (data.error) {
document.getElementById('weather-summary').innerHTML = `<p>${data.error}</p>`;
return;
}
// Update summary
const summary = `
<h3>Current Weather in ${city}</h3>
<p>Temperature: ${data.current.temp}°C</p>
<p>Condition: ${data.current.weather}</p>
<p>Humidity: ${data.current.humidity}%</p>
<p>Wind Speed: ${data.current.wind_speed} km/h</p>
`;
document.getElementById('weather-summary').innerHTML = summary;
// Temperature Graph
Plotly.newPlot('temperature-graph', [{
x: data.forecast.map(d => d.date),
y: data.forecast.map(d => d.temp),
type: 'scatter',
mode: 'lines+markers',
name: 'Temperature'
}], {
title: 'Temperature Forecast',
xaxis: { title: 'Date' },
yaxis: { title: 'Temperature (°C)' },
template: 'plotly_dark'
});
// Precipitation Graph
Plotly.newPlot('precipitation-graph', [{
x: data.forecast.map(d => d.date),
y: data.forecast.map(d => d.precipitation),
type: 'bar',
name: 'Precipitation'
}], {
title: 'Precipitation Forecast',
xaxis: { title: 'Date' },
yaxis: { title: 'Precipitation (mm)' },
template: 'plotly_dark'
});
// Wind Speed Graph
Plotly.newPlot('wind-graph', [{
x: data.forecast.map(d => d.date),
y: data.forecast.map(d => d.wind_speed),
type: 'scatter',
mode: 'lines+markers',
name: 'Wind Speed'
}], {
title: 'Wind Speed Forecast',
xaxis: { title: 'Date' },
yaxis: { title: 'Wind Speed (km/h)' },
template: 'plotly_dark'
});
})
.catch(error => console.error('Error:', error));
} |