Saturday, June 1, 2019

Girls Who Code Weather Station - by Lou Person - STEM - Python - AWS

In this lesson we are going to build a weather station.  The project will consist of a simple web form that takes as input the name of the city.  It will then pass it to a script which will take the name of the city and lookup the weather for that city.  We will use an API (Application Programming Interface) from OpenWeatherMap.org in our script to get the Weather data.  An API is a secure way to access a programming, securely, that exists somewhere else and was written by someone else.  An API is a way to pass input and get output from a program written by another person or team.

First, create an html form and save it in the html folder through FTP.  Name it formweatheryourname.html (don't forget to change yourname to what your name really is).  Also, don't forget to change the name of the script to formweatheryourname.py in the same way.

Here is the form:
<form action="/cgi-bin/formweatherlou.py" method="post">
    City: <input type="text" name="city">
    </form>

Next, change to the cgi-bin folder.  Create the formweatheryourname.py script (change the name to your name).  Here is the code:

#!/bin/python
#import modules to use API
import requests, json
# Import modules for CGI handling
import cgi, cgitb
# Required header that tells the browser how to render the text.
print("Content-Type: text/plain\n\n")  # here text -- not html

# Python program to find current
# weather details of any city
# using openweathermap api

# import required modules
import requests, json
form = cgi.FieldStorage()
city_name = form.getvalue('city')

# Enter your API key here
api_key = "868114e4f2aef8df7211bc655077f6ee"

# base_url variable to store url
base_url = "http://api.openweathermap.org/data/2.5/weather?"

# Give city name
#city_name = input("Enter city name : ")
#city_name = "Delhi"

# complete_url variable to store
# complete url address
complete_url = base_url + "appid=" + api_key + "&q=" + city_name

# get method of requests module
# return response object
response = requests.get(complete_url)

# json method of response object
# convert json format data into
# python format data
x = response.json()

# Now x contains list of nested dictionaries
# Check the value of "cod" key is equal to
# "404", means city is found otherwise,
# city is not found
if x["cod"] != "404":

    # store the value of "main"
    # key in variable y
    y = x["main"]

    # store the value corresponding
    # to the "temp" key of y
    current_temperature = y["temp"]

    # store the value corresponding
    # to the "pressure" key of y
    current_pressure = y["pressure"]

    # store the value corresponding
    # to the "humidity" key of y
    current_humidiy = y["humidity"]

    # store the value of "weather"
    # key in variable z
    z = x["weather"]

    # store the value corresponding
    # to the "description" key at
    # the 0th index of z
    weather_description = z[0]["description"]

    # print following values
    print(" Temperature (in kelvin unit) = " +
                    str(current_temperature) +
          "\n atmospheric pressure (in hPa unit) = " +
                    str(current_pressure) +
          "\n humidity (in percentage) = " +
                    str(current_humidiy) +
          "\n description = " +
                    str(weather_description))

else:
    print(" City Not Found ")



No comments:

Post a Comment