Skip to content

Commit 8a63b44

Browse files
authored
Merge pull request #245 from teemu-ruokolainen/web_api_narrative
web api: add narrative
2 parents b4ad9e9 + 3fe5235 commit 8a63b44

File tree

2 files changed

+54
-69
lines changed

2 files changed

+54
-69
lines changed

content/web-apis.ipynb

Lines changed: 53 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"# Embed the requests homepage\n",
6969
"from IPython.display import IFrame\n",
7070
"requests_documentation_url = \"https://requests.readthedocs.io/en/latest/\"\n",
71-
"IFrame(requests_documentation_url, 1000,300)"
71+
"IFrame(requests_documentation_url, '100%', '30%')"
7272
]
7373
},
7474
{
@@ -80,43 +80,7 @@
8080
"\n",
8181
"An **API (Application Programming Interface)** is the definition of the way computer programs communicate with each other. We use Requests to connect to the API of a web server, tell it what we want, and it returns it to us. This is called the **request-response** cycle.\n",
8282
"\n",
83-
"We can find a list of some free APIs (available without authentication) at <https://apipheny.io/free-api/#apis-without-key> ."
84-
]
85-
},
86-
{
87-
"cell_type": "code",
88-
"execution_count": 2,
89-
"id": "9f523046-6426-4c5d-b312-297e82165481",
90-
"metadata": {},
91-
"outputs": [
92-
{
93-
"data": {
94-
"text/html": [
95-
"\n",
96-
" <iframe\n",
97-
" width=\"1000\"\n",
98-
" height=\"500\"\n",
99-
" src=\"https://apipheny.io/free-api/#apis-without-key\"\n",
100-
" frameborder=\"0\"\n",
101-
" allowfullscreen\n",
102-
" \n",
103-
" ></iframe>\n",
104-
" "
105-
],
106-
"text/plain": [
107-
"<IPython.lib.display.IFrame at 0x7f6bfc5dc7f0>"
108-
]
109-
},
110-
"execution_count": 2,
111-
"metadata": {},
112-
"output_type": "execute_result"
113-
}
114-
],
115-
"source": [
116-
"# Embed apipheny.io (has a list of free APIs)\n",
117-
"from IPython.display import IFrame\n",
118-
"free_apis_url = \"https://apipheny.io/free-api/#apis-without-key\"\n",
119-
"IFrame(free_apis_url, 1000,500)"
83+
"We can find a list of some free APIs (available without authentication) at <https://apipheny.io/free-api/#apis-without-key> . These APIs can be used for developing and testing our code."
12084
]
12185
},
12286
{
@@ -131,7 +95,9 @@
13195
"* **GET** is the type of request we make and\n",
13296
"* `/fact` is the **path**.\n",
13397
"\n",
134-
"You can even test this in your web browser: <https://catfact.ninja/fact>"
98+
"You can even test this in your web browser: <https://catfact.ninja/fact>\n",
99+
"\n",
100+
"Using the Requests library, we do this with {meth}`~requests.get`."
135101
]
136102
},
137103
{
@@ -912,41 +878,19 @@
912878
"source": [
913879
"## After exercises: Saving retrieved data to disk\n",
914880
"\n",
915-
"To save the retrieved JSON objects to disk, it is practical to use the JSONLINES file format. The JSONLINES format contains a single valid JSON object on each line.\n",
881+
"Usually, we want to save the retrieved data to disk for later use. For example, we might collect data for one year and later analyze it for a longitudal study.\n",
882+
"\n",
883+
"To save the retrieved JSON objects to disk, it is practical to use the JSONLINES file format. The JSONLINES format contains a single valid JSON object on each line. This is preferable to saving each object as its own file since we don't, in general, want to end up with excessive amounts of individual files (say, hundreds of thousands or millions).\n",
916884
"\n",
917885
"For example, let's retrieve three cat facts and save them to a JSONLINES file using the [jsonlines library](https://jsonlines.readthedocs.io/en/latest/)."
918886
]
919887
},
920888
{
921889
"cell_type": "code",
922890
"execution_count": 23,
923-
"id": "6eccc88a-e3dd-4756-8ef6-d93f98bd5dc3",
891+
"id": "80a8dcd1-ba04-45de-840e-aa014c19a75c",
924892
"metadata": {},
925-
"outputs": [
926-
{
927-
"name": "stdout",
928-
"output_type": "stream",
929-
"text": [
930-
"Make request 0\n",
931-
"Make request 1\n",
932-
"Make request 2\n"
933-
]
934-
},
935-
{
936-
"data": {
937-
"text/plain": [
938-
"[{'fact': 'A cat has 230 bones in its body. A human has 206. A cat has no collarbone, so it can fit through any opening the size of its head.',\n",
939-
" 'length': 130},\n",
940-
" {'fact': 'The most popular pedigreed cat is the Persian cat, followed by the Main Coon cat and the Siamese cat.',\n",
941-
" 'length': 101},\n",
942-
" {'fact': 'The average cat food meal is the equivalent to about five mice.',\n",
943-
" 'length': 63}]"
944-
]
945-
},
946-
"metadata": {},
947-
"output_type": "display_data"
948-
}
949-
],
893+
"outputs": [],
950894
"source": [
951895
"# Import\n",
952896
"import jsonlines\n",
@@ -979,8 +923,48 @@
979923
"with jsonlines.open('catfacts.jsonl', mode='w') as writer:\n",
980924
"\n",
981925
" # Write\n",
982-
" writer.write(response_jsons)\n",
983-
" \n",
926+
" writer.write(response_jsons)"
927+
]
928+
},
929+
{
930+
"cell_type": "markdown",
931+
"id": "efb4a0ff-6a70-471a-af61-39c0350d4166",
932+
"metadata": {},
933+
"source": [
934+
"We can then read the objects from the disk using the same library."
935+
]
936+
},
937+
{
938+
"cell_type": "code",
939+
"execution_count": 23,
940+
"id": "2d424c23-bdf3-47f7-a643-4312a07c955a",
941+
"metadata": {},
942+
"outputs": [
943+
{
944+
"name": "stdout",
945+
"output_type": "stream",
946+
"text": [
947+
"Make request 0\n",
948+
"Make request 1\n",
949+
"Make request 2\n"
950+
]
951+
},
952+
{
953+
"data": {
954+
"text/plain": [
955+
"[{'fact': 'A cat has 230 bones in its body. A human has 206. A cat has no collarbone, so it can fit through any opening the size of its head.',\n",
956+
" 'length': 130},\n",
957+
" {'fact': 'The most popular pedigreed cat is the Persian cat, followed by the Main Coon cat and the Siamese cat.',\n",
958+
" 'length': 101},\n",
959+
" {'fact': 'The average cat food meal is the equivalent to about five mice.',\n",
960+
" 'length': 63}]"
961+
]
962+
},
963+
"metadata": {},
964+
"output_type": "display_data"
965+
}
966+
],
967+
"source": [
984968
"# Open a jsonlines reader\n",
985969
"with jsonlines.open('catfacts.jsonl', mode='r') as reader:\n",
986970
" \n",
@@ -991,7 +975,7 @@
991975
"assert response_jsons == jsons_from_file\n",
992976
"\n",
993977
"# Display the JSON objects read from file\n",
994-
"display(jsons_from_file)\n"
978+
"display(jsons_from_file)"
995979
]
996980
},
997981
{

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ sphinxext-opengraph
1010

1111
# for web-apis execution
1212
jsonlines
13+
bs4

0 commit comments

Comments
 (0)