bonosa commited on
Commit
099c29b
·
1 Parent(s): d4bf6b9
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -3,15 +3,18 @@ import requests
3
  import json
4
  import os
5
 
6
- def get_weather(city, units):
7
  API_KEY = os.environ["OPENWEATHERMAP_API_KEY"]
 
 
 
8
 
9
  response = requests.get(
10
- "https://api.openweathermap.org/data/2.5/weather?q={}&appid={}&units={}".format(city, API_KEY, units)
11
  )
12
 
13
  if response.status_code != 200:
14
- return None, None, None, None, "Unable to get weather data. Please check the city name and spelling."
15
 
16
  weather_data = json.loads(response.content.decode("utf-8"))
17
 
@@ -22,8 +25,8 @@ def get_weather(city, units):
22
 
23
  return weather_description, temperature, pressure, humidity, None
24
 
25
- def app(city: str, units: str, show_pressure_humidity: bool):
26
- weather_description, temperature, pressure, humidity, error = get_weather(city, units)
27
 
28
  if error:
29
  return error
@@ -41,12 +44,19 @@ def app(city: str, units: str, show_pressure_humidity: bool):
41
 
42
  return weather_info
43
 
 
 
 
 
 
 
44
  iface = gr.Interface(fn=app,
45
- inputs=["text",
46
  gr.inputs.Radio(["metric", "Imperial"], label="Temperature Units"),
47
- gr.inputs.Checkbox(label="Pressure and Humidity")],
48
  outputs="text",
49
  title="Weather Forecast",
50
- description="Enter a city and get the current weather forecast.")
 
51
 
52
  iface.launch()
 
3
  import json
4
  import os
5
 
6
+ def get_weather(city, state, country, units):
7
  API_KEY = os.environ["OPENWEATHERMAP_API_KEY"]
8
+
9
+ # Combine the city, state, and country into a single string.
10
+ location = ",".join(filter(None, [city, state, country]))
11
 
12
  response = requests.get(
13
+ "https://api.openweathermap.org/data/2.5/weather?q={}&appid={}&units={}".format(location, API_KEY, units)
14
  )
15
 
16
  if response.status_code != 200:
17
+ return None, None, None, None, "Unable to get weather data. Please check the location name and spelling."
18
 
19
  weather_data = json.loads(response.content.decode("utf-8"))
20
 
 
25
 
26
  return weather_description, temperature, pressure, humidity, None
27
 
28
+ def app(city: str, state: str, country: str, units: str, show_pressure_humidity: bool):
29
+ weather_description, temperature, pressure, humidity, error = get_weather(city, state, country, units)
30
 
31
  if error:
32
  return error
 
44
 
45
  return weather_info
46
 
47
+ examples = [
48
+ ["New York", "", "", "metric", False],
49
+ ["San Francisco", "CA", "", "metric", False],
50
+ ["Paris", "Ile-de-France", "France", "metric", True]
51
+ ]
52
+
53
  iface = gr.Interface(fn=app,
54
+ inputs=["text", "text", "text",
55
  gr.inputs.Radio(["metric", "Imperial"], label="Temperature Units"),
56
+ gr.inputs.Checkbox(label="Show Pressure and Humidity")],
57
  outputs="text",
58
  title="Weather Forecast",
59
+ description="Enter a city (and optionally state/province and country) and get the current weather forecast.",
60
+ examples=examples)
61
 
62
  iface.launch()