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