Skip to content

Commit cf132c1

Browse files
committed
update example
1 parent 4e5741a commit cf132c1

6 files changed

+383
-15
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# python run_weather_reports_2.py
3+
from wvpy.jtools import JTask, run_pool
4+
5+
6+
if __name__ == '__main__':
7+
state_codes = [
8+
"CA",
9+
"TX",
10+
"KS",
11+
]
12+
tasks = [
13+
JTask(
14+
sheet_name="weather_example_3.py",
15+
sheet_vars={"state_code": state_code},
16+
output_suffix=f"_{state_code}",
17+
)
18+
for state_code in state_codes
19+
]
20+
run_pool(tasks, use_Jupyter=False)

examples/weather/weather_example_0.ipynb

+18-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@
3434
"from plotnine import *"
3535
]
3636
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": null,
40+
"metadata": {},
41+
"outputs": [],
42+
"source": [
43+
"# configure\n",
44+
"html_render = True\n",
45+
"\n",
46+
"def present(txt: str):\n",
47+
" display(Markdown(txt))"
48+
]
49+
},
3750
{
3851
"cell_type": "code",
3952
"execution_count": null,
@@ -67,9 +80,9 @@
6780
"outputs": [],
6881
"source": [
6982
"# format some result info\n",
70-
"display(Markdown(f\"\"\"\n",
83+
"present(f\"\"\"\n",
7184
"[weather.gov](https://www.weather.gov/documentation/services-web-api) alerts for {state_code} retrieved at {time_stamp_str}.\n",
72-
"\"\"\"))"
85+
"\"\"\")"
7386
]
7487
},
7588
{
@@ -94,7 +107,7 @@
94107
"else:\n",
95108
" display_df = pd.DataFrame({col: [None] for col in display_cols})\n",
96109
"\n",
97-
"display_df"
110+
"present(display_df.to_markdown())"
98111
]
99112
},
100113
{
@@ -104,7 +117,7 @@
104117
"outputs": [],
105118
"source": [
106119
"# plot\n",
107-
"if sum(pd.isnull(display_df[\"properties.severity\"]) == False) > 0:\n",
120+
"if html_render and (sum(pd.isnull(display_df[\"properties.severity\"]) == False) > 0):\n",
108121
" with warnings.catch_warnings():\n",
109122
" warnings.simplefilter(\"ignore\")\n",
110123
" print(\n",
@@ -113,7 +126,7 @@
113126
" mapping=aes(x=\"properties.severity\"),\n",
114127
" )\n",
115128
" + geom_histogram()\n",
116-
" + ggtitle(f\"Weather severity distributiojn for {state_code}, retrieved at {time_stamp_str}\")\n",
129+
" + ggtitle(f\"Weather severity distribution for {state_code}, retrieved at {time_stamp_str}\")\n",
117130
" )"
118131
]
119132
},

examples/weather/weather_example_1.ipynb

+18-5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@
8181
"from plotnine import *"
8282
]
8383
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"metadata": {},
88+
"outputs": [],
89+
"source": [
90+
"# configure\n",
91+
"html_render = True\n",
92+
"\n",
93+
"def present(txt: str):\n",
94+
" display(Markdown(txt))"
95+
]
96+
},
8497
{
8598
"cell_type": "code",
8699
"execution_count": null,
@@ -114,9 +127,9 @@
114127
"outputs": [],
115128
"source": [
116129
"# format some result info\n",
117-
"display(Markdown(f\"\"\"\n",
130+
"present(f\"\"\"\n",
118131
"[weather.gov](https://www.weather.gov/documentation/services-web-api) alerts for {state_code} retrieved at {time_stamp_str}.\n",
119-
"\"\"\"))"
132+
"\"\"\")"
120133
]
121134
},
122135
{
@@ -141,7 +154,7 @@
141154
"else:\n",
142155
" display_df = pd.DataFrame({col: [None] for col in display_cols})\n",
143156
"\n",
144-
"display_df"
157+
"present(display_df.to_markdown())"
145158
]
146159
},
147160
{
@@ -151,7 +164,7 @@
151164
"outputs": [],
152165
"source": [
153166
"# plot\n",
154-
"if sum(pd.isnull(display_df[\"properties.severity\"]) == False) > 0:\n",
167+
"if html_render and (sum(pd.isnull(display_df[\"properties.severity\"]) == False) > 0):\n",
155168
" with warnings.catch_warnings():\n",
156169
" warnings.simplefilter(\"ignore\")\n",
157170
" print(\n",
@@ -160,7 +173,7 @@
160173
" mapping=aes(x=\"properties.severity\"),\n",
161174
" )\n",
162175
" + geom_histogram()\n",
163-
" + ggtitle(f\"Weather severity distributiojn for {state_code}, retrieved at {time_stamp_str}\")\n",
176+
" + ggtitle(f\"Weather severity distribution for {state_code}, retrieved at {time_stamp_str}\")\n",
164177
" )"
165178
]
166179
},

examples/weather/weather_example_2.ipynb

+18-5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@
4747
"from plotnine import *"
4848
]
4949
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"# configure\n",
57+
"html_render = True\n",
58+
"\n",
59+
"def present(txt: str):\n",
60+
" display(Markdown(txt))"
61+
]
62+
},
5063
{
5164
"cell_type": "code",
5265
"execution_count": null,
@@ -80,9 +93,9 @@
8093
"outputs": [],
8194
"source": [
8295
"# format some result info\n",
83-
"display(Markdown(f\"\"\"\n",
96+
"present(f\"\"\"\n",
8497
"[weather.gov](https://www.weather.gov/documentation/services-web-api) alerts for {state_code} retrieved at {time_stamp_str}.\n",
85-
"\"\"\"))"
98+
"\"\"\")"
8699
]
87100
},
88101
{
@@ -107,7 +120,7 @@
107120
"else:\n",
108121
" display_df = pd.DataFrame({col: [None] for col in display_cols})\n",
109122
"\n",
110-
"display_df"
123+
"present(display_df.to_markdown())"
111124
]
112125
},
113126
{
@@ -117,7 +130,7 @@
117130
"outputs": [],
118131
"source": [
119132
"# plot\n",
120-
"if sum(pd.isnull(display_df[\"properties.severity\"]) == False) > 0:\n",
133+
"if html_render and (sum(pd.isnull(display_df[\"properties.severity\"]) == False) > 0):\n",
121134
" with warnings.catch_warnings():\n",
122135
" warnings.simplefilter(\"ignore\")\n",
123136
" print(\n",
@@ -126,7 +139,7 @@
126139
" mapping=aes(x=\"properties.severity\"),\n",
127140
" )\n",
128141
" + geom_histogram()\n",
129-
" + ggtitle(f\"Weather severity distributiojn for {state_code}, retrieved at {time_stamp_str}\")\n",
142+
" + ggtitle(f\"Weather severity distribution for {state_code}, retrieved at {time_stamp_str}\")\n",
130143
" )"
131144
]
132145
},
+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# Basic weather API example using [weather.gov](https://www.weather.gov/documentation/services-web-api)."
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from wvpy.jtools import declare_task_variables"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"# set some variables to default values we are willing to override\n",
28+
"# we do this with the with context manager so that our Jupyter or IDE thinks these variables are defined in our environment\n",
29+
"# this defines both the set of names we allow overriding of and default values so we can debug in and IDE\n",
30+
"with declare_task_variables(globals()):\n",
31+
" # set what state we are querying for\n",
32+
" state_code = 'CA'"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"# import our packages\n",
42+
"import warnings\n",
43+
"import datetime\n",
44+
"import requests\n",
45+
"import pandas as pd\n",
46+
"from IPython.display import display, Markdown\n",
47+
"from plotnine import *"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"# configure\n",
57+
"html_render = False\n",
58+
"\n",
59+
"def present(txt: str):\n",
60+
" print(txt)"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"# do the query\n",
70+
"response = requests.get(f\"https://api.weather.gov/alerts/active?area={state_code}\")\n",
71+
"# get JSON response\n",
72+
"json_data = response.json()\n",
73+
"# convert to data frame\n",
74+
"df = pd.json_normalize(json_data[\"features\"])"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"# get local query time\n",
84+
"now = datetime.datetime.now()\n",
85+
"tz = now.astimezone().tzinfo\n",
86+
"time_stamp_str = now.strftime('%Y-%m-%d %H:%M:%S ') + str(tz)"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"# format some result info\n",
96+
"present(f\"\"\"\n",
97+
"[weather.gov](https://www.weather.gov/documentation/services-web-api) alerts for {state_code} retrieved at {time_stamp_str}.\n",
98+
"\"\"\")"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": null,
104+
"metadata": {},
105+
"outputs": [],
106+
"source": [
107+
"# show an excerpt of the returned data frame\n",
108+
"display_cols = [\n",
109+
" \"properties.parameters.NWSheadline\",\n",
110+
" 'properties.areaDesc',\n",
111+
" 'properties.effective',\n",
112+
" 'properties.severity',\n",
113+
" 'properties.certainty',\n",
114+
" 'properties.event',\n",
115+
"]\n",
116+
"if df.shape[0] > 0:\n",
117+
" display_df = df.loc[\n",
118+
" pd.isnull(df[\"properties.parameters.NWSheadline\"]) == False,\n",
119+
" display_cols].reset_index(drop=True, inplace=False)\n",
120+
"else:\n",
121+
" display_df = pd.DataFrame({col: [None] for col in display_cols})\n",
122+
"\n",
123+
"present(display_df.to_markdown())"
124+
]
125+
},
126+
{
127+
"cell_type": "code",
128+
"execution_count": null,
129+
"metadata": {},
130+
"outputs": [],
131+
"source": [
132+
"# plot\n",
133+
"if html_render and (sum(pd.isnull(display_df[\"properties.severity\"]) == False) > 0):\n",
134+
" with warnings.catch_warnings():\n",
135+
" warnings.simplefilter(\"ignore\")\n",
136+
" print(\n",
137+
" ggplot(\n",
138+
" data=display_df,\n",
139+
" mapping=aes(x=\"properties.severity\"),\n",
140+
" )\n",
141+
" + geom_histogram()\n",
142+
" + ggtitle(f\"Weather severity distribution for {state_code}, retrieved at {time_stamp_str}\")\n",
143+
" )"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": null,
149+
"metadata": {},
150+
"outputs": [],
151+
"source": [
152+
"# mark provenance\n",
153+
"display_df['QUERY_STATE_CODE'] = state_code\n",
154+
"display_df['QUERY_TIME_STAMP'] = time_stamp_str"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": null,
160+
"metadata": {},
161+
"outputs": [],
162+
"source": [
163+
"# save to CSV file (could also write to database)\n",
164+
"display_df.to_csv(\n",
165+
" f\"{state_code}_weather.csv\",\n",
166+
" index=False,\n",
167+
")"
168+
]
169+
}
170+
],
171+
"metadata": {
172+
"kernelspec": {
173+
"display_name": "wvpy_dev_env",
174+
"language": "python",
175+
"name": "python3"
176+
},
177+
"language_info": {
178+
"codemirror_mode": {
179+
"name": "ipython",
180+
"version": 3
181+
},
182+
"file_extension": ".py",
183+
"mimetype": "text/x-python",
184+
"name": "python",
185+
"nbconvert_exporter": "python",
186+
"pygments_lexer": "ipython3",
187+
"version": "3.11.3"
188+
}
189+
},
190+
"nbformat": 4,
191+
"nbformat_minor": 2
192+
}

0 commit comments

Comments
 (0)