File size: 1,901 Bytes
3a197d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""
Created on Thu Jul  6 13:09:57 2023

@author: mritchey
"""
import streamlit as st
import pandas as pd
import requests
import plotly.express as px

# Sidebars
address = st.sidebar.text_input("Address", "123 Main Street, Columbus, OH 43215")
start_date = st.sidebar.date_input("Start Date",  pd.Timestamp(2022, 9, 1))
end_date = st.sidebar.date_input("End Date",  pd.Timestamp(2022, 9, 30))
variable_select = st.sidebar.radio("Variable", ('temperature_2m','windspeed_10m'))

# 1. Geocode API
add_input = address.replace(', ', '%2C+').replace(' ', '+')
url_geocode = f'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address={add_input}&benchmark=2020&format=json'
response_geocode = requests.get(url_geocode).json()

lat_lon = response_geocode['result']['addressMatches'][0]['coordinates']
df_address = pd.DataFrame(lat_lon, index=[0])

lon, lat = df_address.values[0]


# 2. Elevation API
url_elevation = f'https://epqs.nationalmap.gov/v1/json?x={lon}&y={lat}&units=Feet&wkid=4326&includeDate=False'
response_elevation = requests.get(url_elevation).json()
df_address['Elevation'] = response_elevation['value']


# 3. Weather API
start_date = start_date.strftime("%Y-%m-%d")
end_date = end_date.strftime("%Y-%m-%d")

url_weather = f'https://archive-api.open-meteo.com/v1/archive?latitude={lat}&longitude={lon}&start_date={start_date}&end_date={end_date}&hourly=temperature_2m,windspeed_10m&timezone=America%2FNew_York&temperature_unit=fahrenheit&windspeed_unit=ms&precipitation_unit=inch'
results_weather = requests.get(url_weather).json()
df_weather = pd.DataFrame(results_weather['hourly'])


# Display Dataframe
st.dataframe(df_address)

# Display Plotly Chart
wind = px.line(df_weather, x="time", y=variable_select)
st.plotly_chart(wind)

# To execute: streamlit run "C:\Users\mritchey\.spyder-py3\Python Scripts\streamlit projects\api web app\app.py"