Python for Beginners: Build a Real-Time Weather App – Hands-On API Project | Navid Ansari | Skillshare

Playback Speed


1.0x


  • 0.5x
  • 0.75x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 1.75x
  • 2x

Python for Beginners: Build a Real-Time Weather App – Hands-On API Project

teacher avatar Navid Ansari

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      01 weather app introduction

      0:36

    • 2.

      02 open weather map api

      2:29

    • 3.

      03 Get weather data

      6:49

    • 4.

      04 Create function for getting the weather

      4:30

    • 5.

      05 Use debuging

      6:00

    • 6.

      06 show temperture

      3:23

    • 7.

      07 Display weather

      4:05

    • 8.

      08 Get humidity and description

      2:41

    • 9.

      09 let user choose which city to show weather for

      2:58

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

3

Students

--

Projects

About This Class

Want to learn Python by building something practical? In this hands-on class, you’ll code a real-time Weather App that fetches live data from an API – perfect for beginners ready to level up their skills!

This class is ideal for:
 Python newbies who want to work with real-world data
 Aspiring developers curious about APIs and web requests
 Anyone who wants to automate weather checks with Python

✨ What You’ll Learn:
 How APIs work (and how to use them in Python)
 Making HTTP requests with the requests library
 Parsing JSON data (the language of the web)
 Error handling to make your app robust
 Building a user-friendly CLI tool

By the end, you’ll have:

Meet Your Teacher

Teacher Profile Image

Navid Ansari

Teacher

Hello, I'm Navid.

See full profile

Level: Beginner

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. 01 weather app introduction: In this Python practice course, you will learn how to create a weather app, and with it, you learn how to work with APIs. What is an API, how to get a free API key, how to make HTTP requests. You learn how you can get the response of those API requests in a JSON format and how you can work with a JSON file. Next, you learn how you can handle whatever error that can happen, and at the end, you will have fully functional weather app. So let's begin. 2. 02 open weather map api: First thing for creating our weather app, we need to sign up in one of the sites that give us all of these information, right? The best site that I found is Open weather map. It will give us all the information about the weather, right? So click on it to open it and you can see I did open it over here, so you don't in here, make sure you sign up. Right, no, I did sign up, so you sign up in there as well. And the bad thing about this is that it will take a little bit to be activated. So you can see, I did sign up in here and it's from four days ago, so it is activated. Even if it says activate in here, your API request on the first hour or two hour, it won't work. So when you create your open weather account, you need to wait two or three hour, and after that, you can have some API request to that. So bear in mind, you need to wait after you sign up, right? After that, you can go and create a key. For example, if I say, for example, learn, and hit Generate and you can see the API key was created successfully. By default, there is one over there. You can use that, but you can see you can create new ones, but you need to wait at least three hour to have API request. After you wait for two to three hour after you're signing up, you should go to API and in here go down. So part of this API are not free and it's not for you. But if you go down, there is a current weather data that you can click on API Doc. And in here, I want to go here, building API request by city name. Just click in there. Now, you can see it is giving us a link that with that link, we can get the weather data from whatever city name that we want, right? For example, London. Now that you find this that you can use this link. Let's see how we can actually get the weather data of London, for example, and after that, we will give the user this choice to select whatever city that they want. Let's begin in next 3. 03 Get weather data: No, we have a link that with that freely, we can get the weather of a city, right? But we need a project. So for doing that, in here, I will right click, create a new folder, and I want to call it weather site, and I want to go to VS code, click in here, open folder, go to desktop, Weather app, and in here, Headselect folder. In here, I want to click on New File, Python file. And Control S to save. I want to call it weather dot PY, right? Turn to save. I will zoom a little bit. Let me close this one. Know it that done, we need to request the API from this link, right? So first thing first, we need to copy this link. Control C to copy it. And in here, I want to create a variable and I can call it URL is equal to F, a string, right? Control V to paste that in here. And now you can see it will give us an error because the city name and API key is not there, right? So let's just give it those, right? So for those, I want to create variables. So in here, I will say city name is equal, for example, no, let's just say London. And after that, the API key. API key. Is equal to a string, and we can get it from the Open weather map in here. If you click in here, you go to Mc API keys and know if you wait for two to three hour, you can copy this default one, for example, just double click on it, Control C to copy it and no go here and paste it over here. This is my API key, right? But you can't use this because it has some limitations. If you want to create and publish your app, you need to buy subscription on Open weather map API, right? So this is just a free one that you can have some requests, but not a lot. So don't try to use this. Now, in here, we need to specify the city name. Instead of saying it like this, we say city name, the variable that we create up here, right? And the API key, I want to change it to API key, right? Cool. Now with this URL, we can request the weather data, right? So for doing that, first of all, we need a library to install. And the library is called requests, right? So for doing that, go to terminal in here and create a new terminal, and in here, you want to say PIP install requests head Inter, and it will start downloading the requests library. With that done, let me make this smaller. We don't need this big. After that, for autocompletions, you can just close and after that, open it up again. And all the autocomplete shoot works by just closing and opening DVS code. Now with that, we can say Import. Requests, right? And that's it. Now with this requests. Let's just see how we can use it. Requests dot GT. And after that, we need to pass the URL to it, right? And what this will give us, let me go down. You can see it will give us a response. Let's just save it inside a response as well. Response. You can call this response whatever you want. Now with that done, I want to just use a printing here to show the response, right? Control is to save. Now in here, I want to say PY, at tab, head inter, now, it will take a little bit time to get back, but you can see the response is 200. When you get the response 200, it means the request did go through successfully. So we need to check that. In here, we will say if response dot Tatus code was equal to 200 what it means, can say print success, right? Success. And after that, you can say whether data is here, right? Cool. And for getting those weather data, we can just say response dot JSON. Okay? This will convert the response to a dictionary, so we can use it, right? That's it. Now, we can save it inside whether data that is equal to this, right? And we can show that. We can say print whether data. Cool. Now, let's just run it one more time. We make this bigger. And now you can see it. It gives us a dictionary that the first key of it is CRT and you can see the value of it is another dictionary. After that, the next key is weather and after that, as a value of this key, there is a list of some informations key value pair, ID is 500, main is rain, description, light rain, icon is ten D, right. After that, we have a base key that the value of it is stations. After that, we have a main key in here, that the value of it is another dictionary that has key value pair. For example, the temperature feels like minimum TEP, maximum TEP, pressure, humidity, sea level after that ground level, after that visibility, wind after that, right, that this wind has a value of another dictionary. So you can see it did give us a lot of information that we can extract them really easily. But before doing that, I want to do the s part of this I. Let's just say se can say print. I want to use FS string in here, and I want to say error. And after that, I want to show the response that status code, right? That's it. If we can't reach it, it will give us the error code to us, status code to us. Beautiful. Now, we have lots of information to unpack. But this was just the basics of what we can do. I want our code to be organized. That's why I want to create a function for this. Let's see how we can do that in next video. 4. 04 Create function for getting the weather: No, I want to convert this to a function. For doing that, let me go here. We will copy and paste all of this code. We say death, get weather, right? And for the input of this function, I want to pass city name. And after that, what you want to do in there, don't need this city name anymore. We need this link, right, Control C to cut it. And know in here, we want to paste it over here, right? No, the city name will come from the input of this function, but the API key is a global variable that we have it over here. No, next thing, we need to cut all of this, control to cut and paste it over here. If it's not indented correctly, you can hold Alt and Shift di from top to bottom, head top, and know it is indented, right? Now, with that done, we don't need to show it. Don't need to save it somewhere. We can just return it, right? That's it. Return the response JSON. But I want to add more error check into this code, so it will be bulletproof. For doing that, what I want to do in here, I want to use a try, right. After that, I want to indent all of this right like this head tab. Now, we have a try and we need it except, right? So I will say except after that, I want to get the exception from the request because we are trying a G request, right, to get data from API. So in here, we say request dot exceptions, dot request exception. And we can tell Python to give this to me as a variable that we can call it E, right? And with that, we can show it, show the error. So in here, we say with the FS ring, error and show the E in coli bracket, right? Co. No after this, we want to return something for this function, right? We can return none. So nothing is returned. And in this case, that the status code wasn't 200, again, we need to return something. So we can say return none, right? In both of these cases, we return none. Now with that, let's just see how we can use this function. So in here, we say get weather, and let's just pass London right? And now, we can print the result, print like this controls to save. If it can return the result, it will be okay. But if it can't, it will return none, right? So in this case, if you run it, it will return it successfully, and you can see the temperature. You can see the minimum and maximum temperature. You can see the pressure, humidity, if it's raining or not, all the information that you can see over here, right? But let's say I don't have any Internet. But we click in here, disable my Internet completely. Now, if I run, what will happen, it will say none. Let's just wait. And now you can see first, it will show us the error. You can see STTP connection pool, and you can see all the messages. And at the end, it will say no. This error that we have in here, right, is where we are printing this error in here, right? You can see with this getting the exceptions, we get this error because we don't have an Internet. But if we again connect to Internet, everything will be fine. Sometime other problem will happen. For example, if you don't wait your open weather map API to be activated completely, it will give you another error as well. So now, let's just test that out one more time. And now you can see again we are getting the correct data, success but their data is here and we can see the temperature, pressure, wind, and everything. Now, let's see how we can unpack all of these information that we get in here. 5. 05 Use debuging: No next thing that I want to talk about is about the response that we get from the requests, right? Where did we find this status code? How we know that there is a status code, right? After that, how we know that we can convert this to a JSON. How we know that, right? So in VS code, we have something that is called debugging and we can debug stuff. For example, in here, when we get our response, we can go at the left side of this number. And add a breakpoint. So what will happen if we add the breakpoint, the execution of the codes will stop there, and we can look at the response completely what is inside it, right? And with that done there, we can go to here. Let me zoom a little bit so you can see everything better. Can click on Run and Debug. It will tell you how you want to run it. You can see the suggested is Python debugger, so choose the Python debugger, and it will tell you which file or module you want to divide. We say the Python file. Divide the currently active Python file. Just choose that. Now you can see it will run over code until it reached this point. And on the left side, in local variables, you can the response is there and you can see give us 200, the status code. If we expand this, if you go down, you can see the status code there. Let me find it. And you can see the status code is 200. And we can have more. For example, you can see the text, right? So we can get this text and it is a JSON file, right? So let's see how we can get this text. There is lots more in here that you can see over here, right? That you can play with. But for you to know what the response is giving us, you can just add a breakpoint, run in devoid mode, and after that, you can see what it is giving us, right? So in here, let's just get out of it, right. Now instead of just saying Jason, we say text, right? And just with that, no, let's just run over application. So in here, let's just say PY, weather, and head inter. And now you can see it is giving us the exact text. That we had there. Again, it's like before, it's giving us the coordinate. It will give us the weather. It will give us the temperature feels like. But this time, this is a JSON format. When we say in here dot JSON, it will convert it to a dictionary. But when we say text, it will give us as a JSON, right? So why I did change this to a text because I wanted to explain everything, how to work with this kind of file. So select everything, hold control shift after that C to copy it. And I want to go here, create a new file, and I want to call it wdt JSON for weather, right? I just want to show you some stuff control. We to paste it over here. You can see it is exactly a JSON and we can save it. We can right click in here, format document. Now you can see it is giving us in a format that it's really readable, right? So this JSON is a dictionary that the first key is coordinate and the value of this key is another dictionary that has longitude and latitude, and you can see the values there. Again, after that, we have another key value pair that is showing the weather. The weather key has a value that is just a list. And inside that list, we have a dictionary that's showing the ID, the main, that is rain, the description, light rain and an icon. Next, we have a base key that the value of it is stations. After that, we have a main key that the value of it is another dictionary that is showing us the temperature feels like temp max, pressure, humidity, and all of that. After that, we have a visibility key that the value of it is 10,000. After that, we have a wind key that the value of it is a dictionary that has a key value pair of speed and its value. And degree and its value, right? After that, we have the rain that has a key value pair as well. We have a closed key that has its own value that you can see it is another dictionary that has key value pair inside it. After that, we have a key that is called DT and its value. And again, we have another key in here that it has a dictionary as a value inside with it that it has a key value pair as well. So, for example, we can get the country from here. We have the sunrise and we have sunset. After that, we have the time zone, ID, name, and the last one, right? And with this, we can get the name of the city as well. So you can see it is a JSON fight. Know that we understand what type of dictionary or JSON file it's given us. We can just save it, and let's just go here. No, we don't need this text anymore. We can again say JSON. This JSON function, what it will do, it will convert this JSON that we talk about it in here to a dictionary so we can work with it, right? No, we have the response completely in here. We can get the keys from here and we can get whatever data that we want in here and show. 6. 06 show temperture: Now let's see how we can get the temperature of the London. So for doing that, in here, or get weather is giving us a dictionary, right? So we can save it somewhere. I will call it weather data. And we save it there. And know with that, we want to get the temperature. Let's see where it is. We have a really big JSON file that is exactly like a dictionary in here, right? And if you go down, you can see the temperature is inside a dictionary that this dictionary is the value of another key, right? So you can see if we have a dictionary after that inside that dictionary. We have a main key. And with that main key, it has a value that is another dictionary. And with that dictionary, we want to get the temp. So in here, we say whether data. No, we want to get the main key value, right? We copy the main, and in here, we want to get that. Let's see what this will give us, right? We say print and open and close prances and no, let's just run. I want to open up the terminal, and in here, I want to say PY whether that PY head inter. Let's see what will happen. You can see this weather data main is giving us a dictionary that has some key value pair that it give us temperature feels like tempix and all of them, right? Beautiful. No, one way to do it, we want to get the temperature. One way to do it is to save this inside a variable, and we can call it main dict, right? Main dictionary. And with that, what we can do, we can print main dictionary, right, and know what you want to get from this main dictionary, we want to get the temperature. So let's just copy that and paste it over here like this. Now, if you run it should just give us the temperature. You can see the temperature, no, it's okay. We are getting the temperature. But we don't want to do it like this. It will get really messy, really fast. So that's why I don't want to do it like this at all. I don't want to save this inside the vibal. We know that this weather data main, it will give us this dictionary, right? So with that, we can get the temp from it, right? Just like that. We can print it out. Let's just say print. And open and close parents, control as to save. Now if you run, it should give us the same result, right? So you can see, with this way of getting things, it's really easy to get stuff. If you go here, you can see, first we did get the main. After that, we get the temperature, and you can see, first we did get the main and after that, we get the temperature. You see, how beautiful we can get that, right? Know that we know how we can get the temperature and whatever information that we want from this JSON file, let's create a function for showing it in a better way. So let's see how we can do that next. 7. 07 Display weather: No, we want to create a function that whatever city that we tell it to show, it will show it to us. So I don't want to do it like this. We have a function for getting the weather. That's good. We can collapse that, right? So we say the display weather, right? And again, for this one, we want to pass the city name as well. And after that, a colon. So in this function, first and first, we get the weather, right? So we say get weather, right from the city name. Just like that, and it will give us the weather data. So we say that in weather data in here. Now, with that done, we want to check if there is a weather data, right? If there is a weather data, in that case, we want to show because this get weather data, it can return none. So if it is none, this if weather data won't happen, right? So if there is a weather data, we want to show those data. And for showing that, first of all, I will say print. I want to use F string just like this. And after that, I want to use BishNF after that, some equal sign. I want to call it weather report. After that, some more equal sign, that's it. No, we want to show the city name. So print in here, I want to use F string again like this. I don't want to say city. After that to Colon. After that, we want to show the city name. One way is to copy this city name over here. Or we can go toward JSON, and in here at the end, you can see we can get the name of that. Both way will works, but I don't want to use city name in here. I want to use the city name that our open weather map API gives us because it's much cleaner to do it this way. So for doing that, we can get it from weather data, right. After that, a square bracket in here. Now, let's see what we want to get. This name is just a key inside this dictionary, right? So we can just say name, right? We can just copy it from here. Name copy it from here, don't write anything like this at all. Just use copy and pasting. Now with that done, we want to use this function. It's easy. We can say display weather and in here, pass the, for example, an DN Control S to save. No, let's just run it one more time and if we bring it up, you can see it says weather report city London. That's working fine. Now let's see how we can show the temperature. For showing the temperature, we say print after that AFStrnF after that double quotation, and no in here, I want to say temper chair, after that, a colon and after that, a curly bracket. And I want to get it from weather data, right? And no, let's see where is the temperature. For temperature, we did talk about it. First, we need to get the main and after that, get the temp, right? So we need to copy this main. First, we need to get the main. And after that, with another square bracket, we want to get the temp, right? Just like that. Beautiful. Let's just test it one more time. If you run and now you can see, it says, weather report, city London temperature is this. Beautiful. No, I want to give you a challenge, right? So go through this, show the humidity and after that, go up, show the description, the light rain. Show these two, and we are finished. Try to do it yourself. We will do it in next. 8. 08 Get humidity and description: No, we want to show the humidity, right? So in here, we say print. After that, we want to use a string, and in here, I want to say humidity after that colon, after that cl bracket, and we want to get it from weather data, right? And let's see where is the humidity. If we go down in here and you can see the humidity is in main. So first, we need to get the main, paste it over here, and after that, we want to get the humidity. So copy it from here and paste it over here. Beautiful. No, next thing, we want to get the description. You can see it it's inside the weather, but this time, there is a difference. First, the value of this key weather is a list, and after that, a dictionary, right? So we should bear in mind, we have a list. So in here, forgeting that, we say print, right? After that, AF string, just like that, we say the script. N or whatever you want to put in there. After that, a curly bracket, we want to get the weather data. And from this weather data, first and first, we need to get the weather weather key. So put it over here. Next, first, we want to get the index zero of that list. So we get this list first, and inside that list, there is a dictionary that we want to get the description, right? So first, we get this index zero of that list, and after that, we get the description, right? Beautiful. No, let's just say settled. If we run over here, let's see what will happen. It says, description broken clothes. So you see the description has changed in just 1 hour. So no, it's broken clothes. Before it was light rain and no, it's broken clothes, right? And at the end, we want to do the s of this weather data. So we say se, and in here, I want to print something, right? For example, couldn't fetch data for this city, right? So if they didn't put the city name correctly or whatever problem happened, the weather data will receive none, and we say we couldn't fetch data for this city. That's it. No, it's time to ask user to choose a city to show weather to, right? So you try to do that, and we will do it next 9. 09 let user choose which city to show weather for: No for asking the user to put a name as a city and we get weather data for them, right? So for that, first thing first, I want to use print, and in here, I want to show a welcome message. First, backslash N and after that, we say, welcome to Python weather app, right? And we can add some equal sign at the left side of it and right side of it. That's it. Now, after this, I want to use a Wiloop wil true loop and in there, I want to use input. And in there, I want to say first another back slason so it will be easier to read, enter a city name. After that, let's just give them a hint or quit to exit to exit of application, right? After that, colon or after that in a space. This input will give us a string, and I want to use a method on it that is called a strip. What it will do if you hover over it, it will return a copy of the string with leading and trailing whites removed. So if they have some spaces at the beginning of the text and at the end of the text, it will strip it down and it will delete those whites spaces. After that, we can save this inside the variable. For example, city variable. No, we want to check the city. If city dot lower case, and after that, we check if it is equal to quit, right, in that case, we can use a print and say good Bye. And after that, we can break out of this loop, and our program will be finished right after that. Next, I want to check if there is a city if they add something. It's not empty, right? In that case, we want to say display weather. Of city. That's it. Now with that done, let's just test that out. If in here, if we run, it will say, welcome to Python Weather app Inter a city name. For example, if you put London, let's see what will happen. You can see success, weather data is here, weather report, city London, temperature, this this, and description is broken clothed. So in here, let's just say Vegas, right, head inter, and now you can see the city gas, temperature is this, humidity is this and overcast clothes. Let's just add some more, for example, stumble, right, head inter. And you can see the city stumble, temperature is this, humidity is this, and description, clear sky. And now, we can just quit out of or application. Goodbye. Congratulations on finishing this project.