12
12
13
13
14
14
def main ():
15
+ """
16
+ This function scrapes the data from the web and wrangles it into a pandas DataFrame.
17
+ It then creates an interactive plotly line graph of covid cases
18
+ in New York State.
19
+ """
15
20
print ('Updating covid deaths...' )
16
21
update_covid_deaths ()
17
22
print ('Updating covid cases...' )
18
23
update_confirmed_cases ()
19
24
20
25
21
26
def update_covid_deaths ():
27
+ """
28
+ Update the plot of global COVID-19 deaths over time.
29
+
30
+ :param df: A pandas DataFrame with columns 'Continent', 'Date', and 'Total Deaths per Million'.
31
+ """
22
32
covid = pd .read_csv ('https://covid.ourworldindata.org/data/owid-covid-data.csv' ,
23
33
usecols = ['iso_code' , 'date' , 'total_deaths' , 'population' ])
24
34
continents = pd .read_csv ('https://gist.githubusercontent.com/stevewithington/20a69c0b6d2ff'
@@ -41,19 +51,45 @@ def update_covid_deaths():
41
51
42
52
43
53
def update_confirmed_cases ():
54
+ """
55
+ Update the file covid_cases.js with a plot of total cases, gold price, bitcoin price and Dow Jones index.
56
+ """
44
57
def main ():
58
+ """
59
+ This function scrapes the data from the web and wrangles it into a pandas DataFrame.
60
+ It then creates an interactive plotly line graph of covid cases
61
+ in New York State.
62
+ """
45
63
df = wrangle_data (* scrape_data ())
46
64
f = get_figure (df )
47
65
update_file ('covid_cases.js' , f )
48
66
f .layout .paper_bgcolor = 'rgb(255, 255, 255)'
49
67
write_to_png_file ('covid_cases.png' , f , width = 960 , height = 315 )
50
68
51
69
def scrape_data ():
70
+ """
71
+ This function scrapes data from the following sources:
72
+ 1. Our World in Data (Total Cases)
73
+ 2. Yahoo Finance (Bitcoin, Gold, Dow Jones)
74
+ The
75
+ function returns a list of pandas Series objects containing the scraped data.
76
+ """
52
77
def scrape_covid ():
78
+ """
79
+ This function scrapes the total number of covid cases from a csv file on the internet.
80
+ """
53
81
url = 'https://covid.ourworldindata.org/data/owid-covid-data.csv'
54
82
df = pd .read_csv (url , usecols = ['location' , 'date' , 'total_cases' ])
55
83
return df [df .location == 'World' ].set_index ('date' ).total_cases
56
84
def scrape_yahoo (slug ):
85
+ """
86
+ Downloads historical stock price data from Yahoo Finance.
87
+
88
+ :param str slug: The ticker symbol of the desired security. Expected to be a valid argument
89
+ for the `yfinance` function `Ticker()`.
90
+ :returns pd.Series(float): A pandas Series with timestamps as indices and adjusted closing prices as values,
91
+ sorted by timestamp in ascending order.
92
+ """
57
93
url = f'https://query1.finance.yahoo.com/v7/finance/download/{ slug } ' + \
58
94
'?period1=1579651200&period2=9999999999&interval=1d&events=history'
59
95
df = pd .read_csv (url , usecols = ['Date' , 'Close' ])
@@ -63,6 +99,14 @@ def scrape_yahoo(slug):
63
99
return map (pd .Series .rename , out , ['Total Cases' , 'Bitcoin' , 'Gold' , 'Dow Jones' ])
64
100
65
101
def wrangle_data (covid , bitcoin , gold , dow ):
102
+ """
103
+ This function joins the Dow Jones, Gold and Bitcoin dataframes into a single one.
104
+ It then sorts them by date and interpolates missing values. It
105
+ discards rows before '2020-02-23'.
106
+ Finally it calculates percentages relative to day 1 of each series (Dow Jones, Gold, Bitcoin)
107
+ and adds a column
108
+ with covid cases. The result is returned as a new dataframe sorted by date in descending order.
109
+ """
66
110
df = pd .concat ([dow , gold , bitcoin ], axis = 1 ) # Joins columns on dates.
67
111
df = df .sort_index ().interpolate () # Sorts by date and interpolates NaN-s.
68
112
yesterday = str (datetime .date .today () - datetime .timedelta (1 ))
@@ -72,6 +116,11 @@ def wrangle_data(covid, bitcoin, gold, dow):
72
116
return df .sort_values (df .index [- 1 ], axis = 1 ) # Sorts columns by last day's value.
73
117
74
118
def get_figure (df ):
119
+ """
120
+ This function returns a plotly figure that shows the total cases of COVID-19 in the US and its economic
121
+ indicators. The data is taken from [The New
122
+ York Times](#) and retrieved using [NYT API](#).
123
+ """
75
124
figure = go .Figure ()
76
125
for col_name in reversed (df .columns ):
77
126
yaxis = 'y1' if col_name == 'Total Cases' else 'y2'
@@ -97,6 +146,27 @@ def get_figure(df):
97
146
#
98
147
99
148
def update_file (filename , figure ):
149
+ """
150
+ Updates the file at `filename` with the plotly figure `figure`.
151
+
152
+ :param filename: The path to a JSON file containing a Plotly figure.
153
+ :type filename:
154
+ str, required.
155
+ The extension of the file must be .json or .js (for legacy reasons).
156
+
157
+ Note that if you are using JupyterLab and want to open
158
+ your updated
159
+ HTML files in an external browser window then you should save your
160
+ notebook as an HTML file instead of as a Jupyter notebook.
161
+ For more
162
+ information see this guide on [using Jupyter with Google Colab](http://jupyter-
163
+ notebook.readthedocs.io/en/stable/examples/Notebook/Running%20Code.html#Running-code).
164
+
165
+ If you are not using JupyterLab then it is recommended
166
+ that you use .html for all types of notebooks so that they can be opened in any web browser, including Chrome, Firefox and Edge on Windows and macOS
167
+ without any extra configuration needed (see below for more details). This is because some browsers do not support JavaScript which is used by default
168
+ by Plotly's exporting functions to generate
169
+ """
100
170
lines = read_file (filename )
101
171
f_json = figure .to_json (pretty = True ).replace ('\n ' , '\n ' )
102
172
out = lines [:6 ] + [f' { f_json } \n ' , ' )\n ' , '};\n ' ]
0 commit comments