Thread from breakout 1
# pacc-may-31-2023
s
Thread from breakout 1
🙌 1
🦜 1
import httpx  # requests capability, but can work with async
from prefect import flow, task
base_url = "<https://api.open-meteo.com/v1/forecast/>"
@task
def fetch_weather(lat: float, lon: float):
weather = httpx.get(
base_url,
params=dict(latitude=lat, longitude=lon, hourly="temperature_2m"),
)
most_recent_temp = float(weather.json()["hourly"]["temperature_2m"][0])
return most_recent_temp
@task
def fetch_precipitation(lat:float, lon:float):
weather = httpx.get(
base_url,
params=dict(latitude=lat, longitude=lon, hourly="precipitation"),
)
most_recent_precip = weather.json()["hourly"]["precipitation"][0]
return most_recent_precip
@task
def save_weather(temp: float, precip: str):
with open("weather.csv", "w+") as w:
w.write(f"{str(temp)},{str(precip)}")
return "Successfully wrote weather data"
@flow
def pipeline(lat: float, lon: float):
temp = fetch_weather(lat, lon)
precip = fetch_precipitation(lat, lon)
result = save_weather(temp, precip)
return result
if __name__ == "__main__":
pipeline(38.9, -77.0)