{ "cells": [ { "cell_type": "code", "execution_count": 17, "id": "1ab5f09d", "metadata": {}, "outputs": [], "source": [ "# import required modules\n", "import requests,json\n", "import os\n", "from dotenv import load_dotenv\n", "import pandas as pd\n", "load_dotenv()\n", "api_key =os.getenv(\"OPENWEATHER_API_KEY\") \n", "input_city = \"Kolkata\"\n", "def get_weather(input_city):\n", " \n", " base_url = \"http://api.openweathermap.org/data/2.5/weather\"\n", "\n", " url=f\"{base_url}?q={input_city}&appid={api_key}\"\n", " r = requests.get(url)\n", " data = r.json()\n", " return data\n", "\n", "# make a function to get the above data\n", "def get_weather_data(input_city):\n", " data = get_weather(input_city)\n", " weather_data = { \n", " 'City': data['name'],\n", " 'Country': data['sys']['country'],\n", " 'Longitude': data['coord']['lon'],\n", " 'Latitude': data['coord']['lat'],\n", " 'Weather Description': data['weather'][0]['description'],\n", " 'Temperature (K)': data['main']['temp'],\n", " 'Pressure (hPa)': data['main']['pressure'],\n", " 'Humidity (%)': data['main']['humidity'],\n", " 'Wind Speed (m/s)': data['wind']['speed'],\n", " 'Wind Direction (degrees)': data['wind']['deg'],\n", " 'Cloudiness (%)': data['clouds']['all'],\n", " 'Sunrise (UTC)': pd.to_datetime(data['sys']['sunrise'], unit='s'),\n", " 'Sunset (UTC)': pd.to_datetime(data['sys']['sunset'], unit='s')\n", " }\n", " return pd.DataFrame([weather_data])\n" ] }, { "cell_type": "code", "execution_count": 18, "id": "5480d192", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | City | \n", "Country | \n", "Longitude | \n", "Latitude | \n", "Weather Description | \n", "Temperature (K) | \n", "Pressure (hPa) | \n", "Humidity (%) | \n", "Wind Speed (m/s) | \n", "Wind Direction (degrees) | \n", "Cloudiness (%) | \n", "Sunrise (UTC) | \n", "Sunset (UTC) | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "Kolkata | \n", "IN | \n", "88.3697 | \n", "22.5697 | \n", "overcast clouds | \n", "301.81 | \n", "1004 | \n", "80 | \n", "4.06 | \n", "181 | \n", "100 | \n", "2025-08-10 23:41:58 | \n", "2025-08-11 12:41:41 | \n", "