Spaces:
Sleeping
Sleeping
| # -*- 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)) | |
| # 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=['temperature_2m','windspeed_10m']) | |
| st.plotly_chart(wind) | |
| # To execute: streamlit run "C:\Users\mritchey\.spyder-py3\Python Scripts\streamlit projects\api web app\app.py" | |