Giter Site home page Giter Site logo

joshg's Introduction

from dash import Dash, html, dcc
from dash.dependencies import Input, Output
import requests
import datetime
import plotly.express as px


API_ENDPOINT = "http://api.openweathermap.org/data/2.5/forecast"
API_KEY = "41a1a3edd5af27415a85b88296f1bf5d"  # Replace with your own API key


zip_code = "50311"

params = {
    'zip': f"{zip_code},us", 
    'appid': API_KEY,
    'units': 'imperial'  
}

response = requests.get(API_ENDPOINT, params=params)
DATA = response.json()

if "list" not in DATA:
    print("Couldn't fetch the weather details.")


relevant_forecasts = DATA["list"]

# def format_forecast_data(forecasts):
#     formatted_data = []
#     for forecast in forecasts:
#         formatted_data.append({
#             'time': datetime.datetime.utcfromtimestamp(forecast["dt"]),
#             'temp': forecast['main']['temp'],
#             'humidity': forecast['main']['humidity'],
#             'feels_like': forecast['main']['feels_like']
#         })
#     return formatted_data

def format_forecast_data(relevant_forecasts):
    forecast_times = [datetime.datetime.utcfromtimestamp(forecast["dt"]) for forecast in relevant_forecasts]
    #You need to figure out what this should be, and make sure it works with the rest of your code, especially below.
    forecast_data = [x["main"] for x in relevant_forecasts]
    formatted_data = []

    for i in range(len(forecast_data)):
        formatted_data.append(forecast_data[i])
        formatted_data[i]["time"] = forecast_times[i]
    return formatted_data

#Very important to know how this data is structured.
forecast_data = format_forecast_data(relevant_forecasts)


# def format_forecast_data(relevant_forecasts):
#     forecast_times = [datetime.datetime.utcfromtimestamp(forecast["dt"]) for forecast in relevant_forecasts]
    
#     forecast_data = [x["main"] for x in relevant_forecasts]
#     formatted_data = []

#     for i in range(len(forecast_data)):
#         formatted_data.append(forecast_data[i])
#         formatted_data[i]["time"] = forecast_times[i]
#     return formatted_data


# forecast_data = format_forecast_data(relevant_forecasts)


# fig = px.line(forecast_data, x="time", y="temp", title='Forecast')
# fig2 = px.line(forecast_data, x="time", y="humidity", title='Humidity')
# fig3 = px.line(forecast_data, x="time", y="feels_like", title='feels_like')

app = Dash(__name__)

app.layout = html.Div(children = [
    dcc.Markdown( 
        id = "title",
        children = "## Weather Forecast for " + zip_code
    ),

    dcc.Dropdown( 
        id = "measure_select_dropdown",
        # options = ["temp", "humidity", "feels like"],
        options=[
            {'label': 'Temperature', 'value': 'temp'},
            {'label': 'Humidity', 'value': 'humidity'},
            {'label': 'Feels Like', 'value': 'feels_like'}
        ],
        # value = 'choose', 
        # multi = True
        value='temp',
        multi=False
    ),

    dcc.Graph(id = 'graph')
    #dcc.Graph( #displays the graph on the page
    #    id = "weather_line_graph",
    #    figure = fig
    #),

    #dcc.Graph( #displays humidity graph
    #    id = "humidity_line_graph",
    #    figure = fig2
    #),

    #dcc.Graph( #displays real feel graph
    #    id = "realfeel_line_graph",
    #    figure = fig3
    #)
])



@app.callback(
    Output("graph","figure"),
    Input("measure_select_dropdown","value"),
)
def update_weather_graph(chosen_graph): #takes arguments?
    if chosen_graph == "temp":
        graph = px.line(forecast_data, x="time", y="temp", title='Forecast')
    elif chosen_graph == "humidity":
        graph = px.line(forecast_data, x="time", y="humidity", title='Humidity')
    elif chosen_graph == "feels_like":
        graph = px.line(forecast_data, x="time", y="feels_like", title='feels_like')
    return graph

if __name__ == '__main__': # starts the server
    app.run_server(debug=True)

joshg's People

Contributors

cuvtpi1l avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.