{ "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", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CityCountryLongitudeLatitudeWeather DescriptionTemperature (K)Pressure (hPa)Humidity (%)Wind Speed (m/s)Wind Direction (degrees)Cloudiness (%)Sunrise (UTC)Sunset (UTC)
0KolkataIN88.369722.5697overcast clouds301.811004804.061811002025-08-10 23:41:582025-08-11 12:41:41
\n", "
" ], "text/plain": [ " City Country Longitude Latitude Weather Description Temperature (K) \\\n", "0 Kolkata IN 88.3697 22.5697 overcast clouds 301.81 \n", "\n", " Pressure (hPa) Humidity (%) Wind Speed (m/s) Wind Direction (degrees) \\\n", "0 1004 80 4.06 181 \n", "\n", " Cloudiness (%) Sunrise (UTC) Sunset (UTC) \n", "0 100 2025-08-10 23:41:58 2025-08-11 12:41:41 " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = get_weather_data(input_city)\n", "data" ] }, { "cell_type": "code", "execution_count": null, "id": "87459352", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "5d22db19", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "9404e33b", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.7" } }, "nbformat": 4, "nbformat_minor": 5 }