# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="UWQY4dOFuwEC" colab_type="text"
# # COVID-19 Data (API).
# ### COVID-19 Country Data.
# [site](https://covid19api.com/) | [API info](https://documenter.getpostman.com/view/10808728/SzS8rjbc?version=latest)
#

# + id="oZm6OLzp5-Hk" colab_type="code" colab={}
# necessary imports for notebook.
import pandas as pd
import requests
import json
import plotly.graph_objects as go

# + id="PGUmewfUtjxw" colab_type="code" outputId="182ff55d-a7b2-44b4-dc45-b225a9631749" colab={"base_uri": "https://localhost:8080/", "height": 222}
# read in the API.
''' COVID-19 : Country/State/Province Daily Individual Case'''
response = requests.get("https://api.covid19api.com/country/us/status/confirmed/live")
print(response.status_code)
covid_ci = response.json()
covid_countryindividual = pd.json_normalize(covid_ci)
print(covid_countryindividual.shape)
covid_countryindividual.head()

# + id="jdlwlnWopB_d" colab_type="code" outputId="a9d0292c-24e5-4c53-9cf6-f0837ab44308" colab={"base_uri": "https://localhost:8080/", "height": 205}
# new data frame.
df = covid_countryindividual.copy()
print(df.shape)
df.head()

# + id="4Io8FrdNuMLN" colab_type="code" colab={}
# clean up the data.
df.drop(df[df['Lat'] == '0'].index, inplace = True)
df['Date'] = df['Date'].str.replace(r'T00:00:00Z', '').astype(object)
df = df.astype({'Lat': float})
df = df.astype({'Lon': float})

# + id="vAIMRVtCaSHs" colab_type="code" outputId="d66e65c1-2fcf-4831-8fa2-653c482f1330" colab={"base_uri": "https://localhost:8080/", "height": 205}
# final data frame.
print(df.shape)
df.head()

# + id="ymEByISLlN0N" colab_type="code" colab={}
# set the mapbox style and access token.
''' style url '''
MAPBOX_STYLE = 'mapbox://styles/cvanchieri/ck922oal90bxw1ilzl8opoz7d'
''' access token '''
MAPBOX_TOKEN = '<KEY>'

# + id="P4beLQQsldlb" colab_type="code" colab={}
# create the times and frames_data.
times = df.groupby(['Date'])['Date'].count().index.tolist()
frames_data = [df.loc[df['Date'] == t] for t in times]

# + id="is709T6Dlg1P" colab_type="code" colab={}
# create the frames density plot.
frames = [go.Frame(data=[go.Densitymapbox(lat=f['Lat'], lon=f['Lon'], z=f['Cases'], radius=10)], name=str(f.iloc[0]['Date'])) for f in frames_data]

# + id="FRkUoVfbljCd" colab_type="code" colab={}
# create the mapbox attributes, play and pause button.
buttons=[
         dict(label="Play",method="animate",args=[None, {'fromcurrent':True, "transition": {"duration": 1, "easing": "quadratic-in-out"}}]),
         dict(label="Pause",method="animate",args=[[None], {"frame": {"duration": 0, "redraw": False},"mode": "immediate", "transition": {"duration": 0}}])
]
sliders_dict = {
    'active':0,
    'currentvalue': dict(font=dict(size=15), prefix='Date: ', visible=True),
    "transition": {"duration": 300, "easing": "cubic-in-out"},
    'x': 0,
    'steps': []
}
for i,t in enumerate(times):
    slider_step = {"args": [
                        [t],
                        {"frame": {"duration": 300, "redraw": False},
                         "transition": {"duration": 1, "ease": "quadratic-in-out"}}
                    ],
            "label": t,
            "method": "animate",
            "value": t
    }
    sliders_dict['steps'].append(slider_step)

# + id="AxQV29bilk4U" colab_type="code" colab={}
# merge the mapbox and the data together and display.
fig = go.Figure(data = [go.Densitymapbox(lat=df['Lat'], lon=df['Lon'], z=df['Cases'], radius=5, colorscale='hot', zmax=5, zmin=0)],
               layout=go.Layout(updatemenus=[dict(type="buttons", buttons=buttons,showactive=True)] ), 
               frames=frames
)
fig.update_layout(mapbox_style=MAPBOX_STYLE, 
                  mapbox_accesstoken=MAPBOX_TOKEN,
                  mapbox_center_lon=-100,
                  mapbox_center_lat=38,
                  mapbox_zoom=3.5)
fig.update_layout(sliders=[sliders_dict],
                 title='USA Covid-19 Confirmed Cases Daily Count')
fig.update_layout(height=1000)
fig.show()

# + [markdown] id="5bG62WVzu81K" colab_type="text"
# ![Covid19MapBox](https://github.com/Lambda-School-Labs/earth-dashboard-ds/blob/feature/updating-notebooks---CV/Notebooks/Images/Covid19ConfirmedMapBox.png?raw=true)
#
#

StarcoderdataJupyterScriptsDedupFiltered