Spaces:
Running
Running
Commit ·
c215c18
1
Parent(s): d2711ff
Added domain object for interacting with contact list
Browse files- .quarto/xref/85c87286 +1 -1
- 01_main.ipynb +502 -58
- __pycache__/main.cpython-312.pyc +0 -0
- _site/01_main.html +460 -149
- _site/search.json +1 -1
- contactdb.json +1 -62
- main.py +59 -8
- templates/index.html +16 -3
- templates/layout.html +3 -0
- templates/view.html +14 -0
.quarto/xref/85c87286
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
{"
|
|
|
|
| 1 |
+
{"entries":[],"headings":[]}
|
01_main.ipynb
CHANGED
|
@@ -9,18 +9,9 @@
|
|
| 9 |
},
|
| 10 |
{
|
| 11 |
"cell_type": "code",
|
| 12 |
-
"execution_count":
|
| 13 |
"metadata": {},
|
| 14 |
-
"outputs": [
|
| 15 |
-
{
|
| 16 |
-
"name": "stdout",
|
| 17 |
-
"output_type": "stream",
|
| 18 |
-
"text": [
|
| 19 |
-
"The autoreload extension is already loaded. To reload it, use:\n",
|
| 20 |
-
" %reload_ext autoreload\n"
|
| 21 |
-
]
|
| 22 |
-
}
|
| 23 |
-
],
|
| 24 |
"source": [
|
| 25 |
"%load_ext autoreload\n",
|
| 26 |
"%autoreload 2"
|
|
@@ -28,33 +19,22 @@
|
|
| 28 |
},
|
| 29 |
{
|
| 30 |
"cell_type": "code",
|
| 31 |
-
"execution_count":
|
| 32 |
"metadata": {},
|
| 33 |
"outputs": [],
|
| 34 |
"source": [
|
| 35 |
"#|export\n",
|
| 36 |
"import pandas as pd\n",
|
| 37 |
-
"from flask import Flask, redirect, request\n",
|
| 38 |
-
"
|
| 39 |
-
"import
|
| 40 |
]
|
| 41 |
},
|
| 42 |
{
|
| 43 |
"cell_type": "code",
|
| 44 |
-
"execution_count":
|
| 45 |
"metadata": {},
|
| 46 |
-
"outputs": [
|
| 47 |
-
{
|
| 48 |
-
"data": {
|
| 49 |
-
"text/plain": [
|
| 50 |
-
"'contactdb.json'"
|
| 51 |
-
]
|
| 52 |
-
},
|
| 53 |
-
"execution_count": 57,
|
| 54 |
-
"metadata": {},
|
| 55 |
-
"output_type": "execute_result"
|
| 56 |
-
}
|
| 57 |
-
],
|
| 58 |
"source": [
|
| 59 |
"people = [\n",
|
| 60 |
" {\n",
|
|
@@ -121,15 +101,16 @@
|
|
| 121 |
"\n",
|
| 122 |
"# Writing the JSON data to a file\n",
|
| 123 |
"file_path = 'contactdb.json'\n",
|
| 124 |
-
"
|
| 125 |
-
"
|
| 126 |
-
"\n",
|
| 127 |
-
"
|
|
|
|
| 128 |
]
|
| 129 |
},
|
| 130 |
{
|
| 131 |
"cell_type": "code",
|
| 132 |
-
"execution_count":
|
| 133 |
"metadata": {},
|
| 134 |
"outputs": [
|
| 135 |
{
|
|
@@ -138,46 +119,56 @@
|
|
| 138 |
"[{'firstname': 'John',\n",
|
| 139 |
" 'lastname': 'Doe',\n",
|
| 140 |
" 'phone': '555-1234',\n",
|
| 141 |
-
" 'email': 'john.doe@example.com'
|
|
|
|
| 142 |
" {'firstname': 'Jane',\n",
|
| 143 |
" 'lastname': 'Smith',\n",
|
| 144 |
" 'phone': '555-5678',\n",
|
| 145 |
-
" 'email': 'jane.smith@example.com'
|
|
|
|
| 146 |
" {'firstname': 'Alice',\n",
|
| 147 |
" 'lastname': 'Johnson',\n",
|
| 148 |
" 'phone': '555-8765',\n",
|
| 149 |
-
" 'email': 'alice.johnson@example.com'
|
|
|
|
| 150 |
" {'firstname': 'Bob',\n",
|
| 151 |
" 'lastname': 'Williams',\n",
|
| 152 |
" 'phone': '555-4321',\n",
|
| 153 |
-
" 'email': 'bob.williams@example.com'
|
|
|
|
| 154 |
" {'firstname': 'Charlie',\n",
|
| 155 |
" 'lastname': 'Brown',\n",
|
| 156 |
" 'phone': '555-9876',\n",
|
| 157 |
-
" 'email': 'charlie.brown@example.com'
|
|
|
|
| 158 |
" {'firstname': 'Diana',\n",
|
| 159 |
" 'lastname': 'Miller',\n",
|
| 160 |
" 'phone': '555-6543',\n",
|
| 161 |
-
" 'email': 'diana.miller@example.com'
|
|
|
|
| 162 |
" {'firstname': 'Edward',\n",
|
| 163 |
" 'lastname': 'Davis',\n",
|
| 164 |
" 'phone': '555-3456',\n",
|
| 165 |
-
" 'email': 'edward.davis@example.com'
|
|
|
|
| 166 |
" {'firstname': 'Fiona',\n",
|
| 167 |
" 'lastname': 'Garcia',\n",
|
| 168 |
" 'phone': '555-6789',\n",
|
| 169 |
-
" 'email': 'fiona.garcia@example.com'
|
|
|
|
| 170 |
" {'firstname': 'George',\n",
|
| 171 |
" 'lastname': 'Martinez',\n",
|
| 172 |
" 'phone': '555-7890',\n",
|
| 173 |
-
" 'email': 'george.martinez@example.com'
|
|
|
|
| 174 |
" {'firstname': 'Hannah',\n",
|
| 175 |
" 'lastname': 'Rodriguez',\n",
|
| 176 |
" 'phone': '555-8901',\n",
|
| 177 |
-
" 'email': 'hannah.rodriguez@example.com'
|
|
|
|
| 178 |
]
|
| 179 |
},
|
| 180 |
-
"execution_count":
|
| 181 |
"metadata": {},
|
| 182 |
"output_type": "execute_result"
|
| 183 |
}
|
|
@@ -189,7 +180,7 @@
|
|
| 189 |
},
|
| 190 |
{
|
| 191 |
"cell_type": "code",
|
| 192 |
-
"execution_count":
|
| 193 |
"metadata": {},
|
| 194 |
"outputs": [
|
| 195 |
{
|
|
@@ -217,6 +208,7 @@
|
|
| 217 |
" <th>lastname</th>\n",
|
| 218 |
" <th>phone</th>\n",
|
| 219 |
" <th>email</th>\n",
|
|
|
|
| 220 |
" </tr>\n",
|
| 221 |
" </thead>\n",
|
| 222 |
" <tbody>\n",
|
|
@@ -226,17 +218,18 @@
|
|
| 226 |
" <td>Doe</td>\n",
|
| 227 |
" <td>555-1234</td>\n",
|
| 228 |
" <td>john.doe@example.com</td>\n",
|
|
|
|
| 229 |
" </tr>\n",
|
| 230 |
" </tbody>\n",
|
| 231 |
"</table>\n",
|
| 232 |
"</div>"
|
| 233 |
],
|
| 234 |
"text/plain": [
|
| 235 |
-
" firstname lastname phone email\n",
|
| 236 |
-
"0 John Doe 555-1234 john.doe@example.com"
|
| 237 |
]
|
| 238 |
},
|
| 239 |
-
"execution_count":
|
| 240 |
"metadata": {},
|
| 241 |
"output_type": "execute_result"
|
| 242 |
}
|
|
@@ -253,14 +246,182 @@
|
|
| 253 |
},
|
| 254 |
{
|
| 255 |
"cell_type": "code",
|
| 256 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
"metadata": {},
|
| 258 |
"outputs": [],
|
| 259 |
"source": [
|
| 260 |
"#|export\n",
|
| 261 |
"class Contact(object):\n",
|
| 262 |
" def __init__(self) -> None:\n",
|
| 263 |
-
" self.
|
| 264 |
"\n",
|
| 265 |
" def search(self, q):\n",
|
| 266 |
" predicate_firstname = self.db['firstname'].str.lower().str.contains(q.lower())\n",
|
|
@@ -269,30 +430,187 @@
|
|
| 269 |
" predicate_email= self.db['email'].str.lower().str.contains(q.lower())\n",
|
| 270 |
" filter_df = self.db[predicate_firstname|predicate_lastname|predicate_phone|predicate_email]\n",
|
| 271 |
" return filter_df.to_dict('records')\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
"\n",
|
| 273 |
" def all(self):\n",
|
| 274 |
-
" return self.db.to_dict('records')"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
]
|
| 276 |
},
|
| 277 |
{
|
| 278 |
"cell_type": "code",
|
| 279 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
"metadata": {},
|
| 281 |
"outputs": [],
|
| 282 |
"source": [
|
| 283 |
"#|export\n",
|
| 284 |
-
"\n",
|
| 285 |
"app = Flask(__name__)"
|
| 286 |
]
|
| 287 |
},
|
| 288 |
{
|
| 289 |
"cell_type": "code",
|
| 290 |
-
"execution_count":
|
| 291 |
"metadata": {},
|
| 292 |
"outputs": [],
|
| 293 |
"source": [
|
| 294 |
"#|export\n",
|
| 295 |
-
"\n",
|
| 296 |
"@app.get(\"/\")\n",
|
| 297 |
"def index():\n",
|
| 298 |
" return redirect(\"/contacts\")"
|
|
@@ -300,12 +618,11 @@
|
|
| 300 |
},
|
| 301 |
{
|
| 302 |
"cell_type": "code",
|
| 303 |
-
"execution_count":
|
| 304 |
"metadata": {},
|
| 305 |
"outputs": [],
|
| 306 |
"source": [
|
| 307 |
"#|export\n",
|
| 308 |
-
"\n",
|
| 309 |
"@app.get(\"/contacts\")\n",
|
| 310 |
"def contacts():\n",
|
| 311 |
" search = request.args.get(\"q\")\n",
|
|
@@ -318,7 +635,134 @@
|
|
| 318 |
},
|
| 319 |
{
|
| 320 |
"cell_type": "code",
|
| 321 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
"metadata": {},
|
| 323 |
"outputs": [],
|
| 324 |
"source": [
|
|
|
|
| 9 |
},
|
| 10 |
{
|
| 11 |
"cell_type": "code",
|
| 12 |
+
"execution_count": 1,
|
| 13 |
"metadata": {},
|
| 14 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
"source": [
|
| 16 |
"%load_ext autoreload\n",
|
| 17 |
"%autoreload 2"
|
|
|
|
| 19 |
},
|
| 20 |
{
|
| 21 |
"cell_type": "code",
|
| 22 |
+
"execution_count": 2,
|
| 23 |
"metadata": {},
|
| 24 |
"outputs": [],
|
| 25 |
"source": [
|
| 26 |
"#|export\n",
|
| 27 |
"import pandas as pd\n",
|
| 28 |
+
"from flask import Flask, redirect, request, render_template\n",
|
| 29 |
+
"import json\n",
|
| 30 |
+
"from IPython.display import display_html"
|
| 31 |
]
|
| 32 |
},
|
| 33 |
{
|
| 34 |
"cell_type": "code",
|
| 35 |
+
"execution_count": 3,
|
| 36 |
"metadata": {},
|
| 37 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
"source": [
|
| 39 |
"people = [\n",
|
| 40 |
" {\n",
|
|
|
|
| 101 |
"\n",
|
| 102 |
"# Writing the JSON data to a file\n",
|
| 103 |
"file_path = 'contactdb.json'\n",
|
| 104 |
+
"# df = pd.read_json(file_path)\n",
|
| 105 |
+
"df = pd.DataFrame()\n",
|
| 106 |
+
"df = df.from_dict(people)\n",
|
| 107 |
+
"df['id'] = df.index\n",
|
| 108 |
+
"df.to_json(file_path, orient='records')"
|
| 109 |
]
|
| 110 |
},
|
| 111 |
{
|
| 112 |
"cell_type": "code",
|
| 113 |
+
"execution_count": 4,
|
| 114 |
"metadata": {},
|
| 115 |
"outputs": [
|
| 116 |
{
|
|
|
|
| 119 |
"[{'firstname': 'John',\n",
|
| 120 |
" 'lastname': 'Doe',\n",
|
| 121 |
" 'phone': '555-1234',\n",
|
| 122 |
+
" 'email': 'john.doe@example.com',\n",
|
| 123 |
+
" 'id': 0},\n",
|
| 124 |
" {'firstname': 'Jane',\n",
|
| 125 |
" 'lastname': 'Smith',\n",
|
| 126 |
" 'phone': '555-5678',\n",
|
| 127 |
+
" 'email': 'jane.smith@example.com',\n",
|
| 128 |
+
" 'id': 1},\n",
|
| 129 |
" {'firstname': 'Alice',\n",
|
| 130 |
" 'lastname': 'Johnson',\n",
|
| 131 |
" 'phone': '555-8765',\n",
|
| 132 |
+
" 'email': 'alice.johnson@example.com',\n",
|
| 133 |
+
" 'id': 2},\n",
|
| 134 |
" {'firstname': 'Bob',\n",
|
| 135 |
" 'lastname': 'Williams',\n",
|
| 136 |
" 'phone': '555-4321',\n",
|
| 137 |
+
" 'email': 'bob.williams@example.com',\n",
|
| 138 |
+
" 'id': 3},\n",
|
| 139 |
" {'firstname': 'Charlie',\n",
|
| 140 |
" 'lastname': 'Brown',\n",
|
| 141 |
" 'phone': '555-9876',\n",
|
| 142 |
+
" 'email': 'charlie.brown@example.com',\n",
|
| 143 |
+
" 'id': 4},\n",
|
| 144 |
" {'firstname': 'Diana',\n",
|
| 145 |
" 'lastname': 'Miller',\n",
|
| 146 |
" 'phone': '555-6543',\n",
|
| 147 |
+
" 'email': 'diana.miller@example.com',\n",
|
| 148 |
+
" 'id': 5},\n",
|
| 149 |
" {'firstname': 'Edward',\n",
|
| 150 |
" 'lastname': 'Davis',\n",
|
| 151 |
" 'phone': '555-3456',\n",
|
| 152 |
+
" 'email': 'edward.davis@example.com',\n",
|
| 153 |
+
" 'id': 6},\n",
|
| 154 |
" {'firstname': 'Fiona',\n",
|
| 155 |
" 'lastname': 'Garcia',\n",
|
| 156 |
" 'phone': '555-6789',\n",
|
| 157 |
+
" 'email': 'fiona.garcia@example.com',\n",
|
| 158 |
+
" 'id': 7},\n",
|
| 159 |
" {'firstname': 'George',\n",
|
| 160 |
" 'lastname': 'Martinez',\n",
|
| 161 |
" 'phone': '555-7890',\n",
|
| 162 |
+
" 'email': 'george.martinez@example.com',\n",
|
| 163 |
+
" 'id': 8},\n",
|
| 164 |
" {'firstname': 'Hannah',\n",
|
| 165 |
" 'lastname': 'Rodriguez',\n",
|
| 166 |
" 'phone': '555-8901',\n",
|
| 167 |
+
" 'email': 'hannah.rodriguez@example.com',\n",
|
| 168 |
+
" 'id': 9}]"
|
| 169 |
]
|
| 170 |
},
|
| 171 |
+
"execution_count": 4,
|
| 172 |
"metadata": {},
|
| 173 |
"output_type": "execute_result"
|
| 174 |
}
|
|
|
|
| 180 |
},
|
| 181 |
{
|
| 182 |
"cell_type": "code",
|
| 183 |
+
"execution_count": 5,
|
| 184 |
"metadata": {},
|
| 185 |
"outputs": [
|
| 186 |
{
|
|
|
|
| 208 |
" <th>lastname</th>\n",
|
| 209 |
" <th>phone</th>\n",
|
| 210 |
" <th>email</th>\n",
|
| 211 |
+
" <th>id</th>\n",
|
| 212 |
" </tr>\n",
|
| 213 |
" </thead>\n",
|
| 214 |
" <tbody>\n",
|
|
|
|
| 218 |
" <td>Doe</td>\n",
|
| 219 |
" <td>555-1234</td>\n",
|
| 220 |
" <td>john.doe@example.com</td>\n",
|
| 221 |
+
" <td>0</td>\n",
|
| 222 |
" </tr>\n",
|
| 223 |
" </tbody>\n",
|
| 224 |
"</table>\n",
|
| 225 |
"</div>"
|
| 226 |
],
|
| 227 |
"text/plain": [
|
| 228 |
+
" firstname lastname phone email id\n",
|
| 229 |
+
"0 John Doe 555-1234 john.doe@example.com 0"
|
| 230 |
]
|
| 231 |
},
|
| 232 |
+
"execution_count": 5,
|
| 233 |
"metadata": {},
|
| 234 |
"output_type": "execute_result"
|
| 235 |
}
|
|
|
|
| 246 |
},
|
| 247 |
{
|
| 248 |
"cell_type": "code",
|
| 249 |
+
"execution_count": 6,
|
| 250 |
+
"metadata": {},
|
| 251 |
+
"outputs": [],
|
| 252 |
+
"source": [
|
| 253 |
+
"db.columns\n",
|
| 254 |
+
"\n",
|
| 255 |
+
"a={'firstname': 'Rahul',\n",
|
| 256 |
+
" 'lastname': 'Saraf',\n",
|
| 257 |
+
" 'phone':'7347306799',\n",
|
| 258 |
+
" 'email': 'rahuketu86'}\n",
|
| 259 |
+
"a['id'] = len(db)\n",
|
| 260 |
+
"# db.loc[len(db)] = \n",
|
| 261 |
+
"a\n",
|
| 262 |
+
"db.loc[len(db), a.keys()] = a.values()"
|
| 263 |
+
]
|
| 264 |
+
},
|
| 265 |
+
{
|
| 266 |
+
"cell_type": "code",
|
| 267 |
+
"execution_count": 7,
|
| 268 |
+
"metadata": {},
|
| 269 |
+
"outputs": [
|
| 270 |
+
{
|
| 271 |
+
"data": {
|
| 272 |
+
"text/html": [
|
| 273 |
+
"<div>\n",
|
| 274 |
+
"<style scoped>\n",
|
| 275 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 276 |
+
" vertical-align: middle;\n",
|
| 277 |
+
" }\n",
|
| 278 |
+
"\n",
|
| 279 |
+
" .dataframe tbody tr th {\n",
|
| 280 |
+
" vertical-align: top;\n",
|
| 281 |
+
" }\n",
|
| 282 |
+
"\n",
|
| 283 |
+
" .dataframe thead th {\n",
|
| 284 |
+
" text-align: right;\n",
|
| 285 |
+
" }\n",
|
| 286 |
+
"</style>\n",
|
| 287 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 288 |
+
" <thead>\n",
|
| 289 |
+
" <tr style=\"text-align: right;\">\n",
|
| 290 |
+
" <th></th>\n",
|
| 291 |
+
" <th>firstname</th>\n",
|
| 292 |
+
" <th>lastname</th>\n",
|
| 293 |
+
" <th>phone</th>\n",
|
| 294 |
+
" <th>email</th>\n",
|
| 295 |
+
" <th>id</th>\n",
|
| 296 |
+
" </tr>\n",
|
| 297 |
+
" </thead>\n",
|
| 298 |
+
" <tbody>\n",
|
| 299 |
+
" <tr>\n",
|
| 300 |
+
" <th>0</th>\n",
|
| 301 |
+
" <td>John</td>\n",
|
| 302 |
+
" <td>Doe</td>\n",
|
| 303 |
+
" <td>555-1234</td>\n",
|
| 304 |
+
" <td>john.doe@example.com</td>\n",
|
| 305 |
+
" <td>0.0</td>\n",
|
| 306 |
+
" </tr>\n",
|
| 307 |
+
" <tr>\n",
|
| 308 |
+
" <th>1</th>\n",
|
| 309 |
+
" <td>Jane</td>\n",
|
| 310 |
+
" <td>Smith</td>\n",
|
| 311 |
+
" <td>555-5678</td>\n",
|
| 312 |
+
" <td>jane.smith@example.com</td>\n",
|
| 313 |
+
" <td>1.0</td>\n",
|
| 314 |
+
" </tr>\n",
|
| 315 |
+
" <tr>\n",
|
| 316 |
+
" <th>2</th>\n",
|
| 317 |
+
" <td>Alice</td>\n",
|
| 318 |
+
" <td>Johnson</td>\n",
|
| 319 |
+
" <td>555-8765</td>\n",
|
| 320 |
+
" <td>alice.johnson@example.com</td>\n",
|
| 321 |
+
" <td>2.0</td>\n",
|
| 322 |
+
" </tr>\n",
|
| 323 |
+
" <tr>\n",
|
| 324 |
+
" <th>3</th>\n",
|
| 325 |
+
" <td>Bob</td>\n",
|
| 326 |
+
" <td>Williams</td>\n",
|
| 327 |
+
" <td>555-4321</td>\n",
|
| 328 |
+
" <td>bob.williams@example.com</td>\n",
|
| 329 |
+
" <td>3.0</td>\n",
|
| 330 |
+
" </tr>\n",
|
| 331 |
+
" <tr>\n",
|
| 332 |
+
" <th>4</th>\n",
|
| 333 |
+
" <td>Charlie</td>\n",
|
| 334 |
+
" <td>Brown</td>\n",
|
| 335 |
+
" <td>555-9876</td>\n",
|
| 336 |
+
" <td>charlie.brown@example.com</td>\n",
|
| 337 |
+
" <td>4.0</td>\n",
|
| 338 |
+
" </tr>\n",
|
| 339 |
+
" <tr>\n",
|
| 340 |
+
" <th>5</th>\n",
|
| 341 |
+
" <td>Diana</td>\n",
|
| 342 |
+
" <td>Miller</td>\n",
|
| 343 |
+
" <td>555-6543</td>\n",
|
| 344 |
+
" <td>diana.miller@example.com</td>\n",
|
| 345 |
+
" <td>5.0</td>\n",
|
| 346 |
+
" </tr>\n",
|
| 347 |
+
" <tr>\n",
|
| 348 |
+
" <th>6</th>\n",
|
| 349 |
+
" <td>Edward</td>\n",
|
| 350 |
+
" <td>Davis</td>\n",
|
| 351 |
+
" <td>555-3456</td>\n",
|
| 352 |
+
" <td>edward.davis@example.com</td>\n",
|
| 353 |
+
" <td>6.0</td>\n",
|
| 354 |
+
" </tr>\n",
|
| 355 |
+
" <tr>\n",
|
| 356 |
+
" <th>7</th>\n",
|
| 357 |
+
" <td>Fiona</td>\n",
|
| 358 |
+
" <td>Garcia</td>\n",
|
| 359 |
+
" <td>555-6789</td>\n",
|
| 360 |
+
" <td>fiona.garcia@example.com</td>\n",
|
| 361 |
+
" <td>7.0</td>\n",
|
| 362 |
+
" </tr>\n",
|
| 363 |
+
" <tr>\n",
|
| 364 |
+
" <th>8</th>\n",
|
| 365 |
+
" <td>George</td>\n",
|
| 366 |
+
" <td>Martinez</td>\n",
|
| 367 |
+
" <td>555-7890</td>\n",
|
| 368 |
+
" <td>george.martinez@example.com</td>\n",
|
| 369 |
+
" <td>8.0</td>\n",
|
| 370 |
+
" </tr>\n",
|
| 371 |
+
" <tr>\n",
|
| 372 |
+
" <th>9</th>\n",
|
| 373 |
+
" <td>Hannah</td>\n",
|
| 374 |
+
" <td>Rodriguez</td>\n",
|
| 375 |
+
" <td>555-8901</td>\n",
|
| 376 |
+
" <td>hannah.rodriguez@example.com</td>\n",
|
| 377 |
+
" <td>9.0</td>\n",
|
| 378 |
+
" </tr>\n",
|
| 379 |
+
" <tr>\n",
|
| 380 |
+
" <th>10</th>\n",
|
| 381 |
+
" <td>Rahul</td>\n",
|
| 382 |
+
" <td>Saraf</td>\n",
|
| 383 |
+
" <td>7347306799</td>\n",
|
| 384 |
+
" <td>rahuketu86</td>\n",
|
| 385 |
+
" <td>10.0</td>\n",
|
| 386 |
+
" </tr>\n",
|
| 387 |
+
" </tbody>\n",
|
| 388 |
+
"</table>\n",
|
| 389 |
+
"</div>"
|
| 390 |
+
],
|
| 391 |
+
"text/plain": [
|
| 392 |
+
" firstname lastname phone email id\n",
|
| 393 |
+
"0 John Doe 555-1234 john.doe@example.com 0.0\n",
|
| 394 |
+
"1 Jane Smith 555-5678 jane.smith@example.com 1.0\n",
|
| 395 |
+
"2 Alice Johnson 555-8765 alice.johnson@example.com 2.0\n",
|
| 396 |
+
"3 Bob Williams 555-4321 bob.williams@example.com 3.0\n",
|
| 397 |
+
"4 Charlie Brown 555-9876 charlie.brown@example.com 4.0\n",
|
| 398 |
+
"5 Diana Miller 555-6543 diana.miller@example.com 5.0\n",
|
| 399 |
+
"6 Edward Davis 555-3456 edward.davis@example.com 6.0\n",
|
| 400 |
+
"7 Fiona Garcia 555-6789 fiona.garcia@example.com 7.0\n",
|
| 401 |
+
"8 George Martinez 555-7890 george.martinez@example.com 8.0\n",
|
| 402 |
+
"9 Hannah Rodriguez 555-8901 hannah.rodriguez@example.com 9.0\n",
|
| 403 |
+
"10 Rahul Saraf 7347306799 rahuketu86 10.0"
|
| 404 |
+
]
|
| 405 |
+
},
|
| 406 |
+
"execution_count": 7,
|
| 407 |
+
"metadata": {},
|
| 408 |
+
"output_type": "execute_result"
|
| 409 |
+
}
|
| 410 |
+
],
|
| 411 |
+
"source": [
|
| 412 |
+
"db"
|
| 413 |
+
]
|
| 414 |
+
},
|
| 415 |
+
{
|
| 416 |
+
"cell_type": "code",
|
| 417 |
+
"execution_count": 8,
|
| 418 |
"metadata": {},
|
| 419 |
"outputs": [],
|
| 420 |
"source": [
|
| 421 |
"#|export\n",
|
| 422 |
"class Contact(object):\n",
|
| 423 |
" def __init__(self) -> None:\n",
|
| 424 |
+
" self.refresh()\n",
|
| 425 |
"\n",
|
| 426 |
" def search(self, q):\n",
|
| 427 |
" predicate_firstname = self.db['firstname'].str.lower().str.contains(q.lower())\n",
|
|
|
|
| 430 |
" predicate_email= self.db['email'].str.lower().str.contains(q.lower())\n",
|
| 431 |
" filter_df = self.db[predicate_firstname|predicate_lastname|predicate_phone|predicate_email]\n",
|
| 432 |
" return filter_df.to_dict('records')\n",
|
| 433 |
+
" \n",
|
| 434 |
+
" def refresh(self):\n",
|
| 435 |
+
" self.file_path = 'contactdb.json'\n",
|
| 436 |
+
" self.db = pd.read_json(self.file_path)\n",
|
| 437 |
+
"\n",
|
| 438 |
"\n",
|
| 439 |
" def all(self):\n",
|
| 440 |
+
" return self.db.to_dict('records')\n",
|
| 441 |
+
" \n",
|
| 442 |
+
" def get(self, id):\n",
|
| 443 |
+
" contact = self.db[self.db['id']==id]\n",
|
| 444 |
+
" # print(contact)\n",
|
| 445 |
+
" return contact.to_dict('records')[0]\n",
|
| 446 |
+
" \n",
|
| 447 |
+
" def add(self, firstname, lastname, phone, email):\n",
|
| 448 |
+
" a={'firstname': firstname,\n",
|
| 449 |
+
" 'lastname': lastname,\n",
|
| 450 |
+
" 'phone':phone,\n",
|
| 451 |
+
" 'email': email}\n",
|
| 452 |
+
" a['id'] = len(self.db)\n",
|
| 453 |
+
" self.db.loc[len(self.db), a.keys()] = a.values()\n",
|
| 454 |
+
" self.db.to_json(file_path, orient='records')\n",
|
| 455 |
+
" self.refresh()\n",
|
| 456 |
+
"\n",
|
| 457 |
+
" def delete(self, id):\n",
|
| 458 |
+
" self.db.drop(index=id, inplace=True, errors='raise')\n",
|
| 459 |
+
" self.db.to_json(file_path, orient='records')\n",
|
| 460 |
+
" self.refresh()\n",
|
| 461 |
+
"\n",
|
| 462 |
+
" def edit(self, id, firstname, lastname, phone, email):\n",
|
| 463 |
+
" a={'firstname': firstname,\n",
|
| 464 |
+
" 'lastname': lastname,\n",
|
| 465 |
+
" 'phone':phone,\n",
|
| 466 |
+
" 'email': email,\n",
|
| 467 |
+
" 'id': id}\n",
|
| 468 |
+
" self.db.loc[id, a.keys()] = a.values()\n",
|
| 469 |
+
" self.db.to_json(file_path, orient='records')\n",
|
| 470 |
+
" self.refresh()\n"
|
| 471 |
+
]
|
| 472 |
+
},
|
| 473 |
+
{
|
| 474 |
+
"cell_type": "code",
|
| 475 |
+
"execution_count": 10,
|
| 476 |
+
"metadata": {},
|
| 477 |
+
"outputs": [
|
| 478 |
+
{
|
| 479 |
+
"data": {
|
| 480 |
+
"text/plain": [
|
| 481 |
+
"dict_values(['Alice', 'Johnson', '555-8765', 'alice.johnson@example.com', 2])"
|
| 482 |
+
]
|
| 483 |
+
},
|
| 484 |
+
"execution_count": 10,
|
| 485 |
+
"metadata": {},
|
| 486 |
+
"output_type": "execute_result"
|
| 487 |
+
}
|
| 488 |
+
],
|
| 489 |
+
"source": [
|
| 490 |
+
"c = Contact()\n",
|
| 491 |
+
"c.db.loc[2].to_dict().values()"
|
| 492 |
]
|
| 493 |
},
|
| 494 |
{
|
| 495 |
"cell_type": "code",
|
| 496 |
+
"execution_count": 11,
|
| 497 |
+
"metadata": {},
|
| 498 |
+
"outputs": [
|
| 499 |
+
{
|
| 500 |
+
"data": {
|
| 501 |
+
"text/plain": [
|
| 502 |
+
"{'firstname': 'Alice',\n",
|
| 503 |
+
" 'lastname': 'Johnson',\n",
|
| 504 |
+
" 'phone': '555-8767',\n",
|
| 505 |
+
" 'email': 'alice.johnson@example.com',\n",
|
| 506 |
+
" 'id': 2}"
|
| 507 |
+
]
|
| 508 |
+
},
|
| 509 |
+
"execution_count": 11,
|
| 510 |
+
"metadata": {},
|
| 511 |
+
"output_type": "execute_result"
|
| 512 |
+
}
|
| 513 |
+
],
|
| 514 |
+
"source": [
|
| 515 |
+
"new_vals = [2, 'Alice', 'Johnson', '555-8767', 'alice.johnson@example.com']\n",
|
| 516 |
+
"c.edit(*new_vals)\n",
|
| 517 |
+
"c.get(2)"
|
| 518 |
+
]
|
| 519 |
+
},
|
| 520 |
+
{
|
| 521 |
+
"cell_type": "code",
|
| 522 |
+
"execution_count": 13,
|
| 523 |
+
"metadata": {},
|
| 524 |
+
"outputs": [
|
| 525 |
+
{
|
| 526 |
+
"data": {
|
| 527 |
+
"text/plain": [
|
| 528 |
+
"[{'firstname': 'Alice',\n",
|
| 529 |
+
" 'lastname': 'Johnson',\n",
|
| 530 |
+
" 'phone': '555-8767',\n",
|
| 531 |
+
" 'email': 'alice.johnson@example.com',\n",
|
| 532 |
+
" 'id': 2},\n",
|
| 533 |
+
" {'firstname': 'Bob',\n",
|
| 534 |
+
" 'lastname': 'Williams',\n",
|
| 535 |
+
" 'phone': '555-4321',\n",
|
| 536 |
+
" 'email': 'bob.williams@example.com',\n",
|
| 537 |
+
" 'id': 3},\n",
|
| 538 |
+
" {'firstname': 'Charlie',\n",
|
| 539 |
+
" 'lastname': 'Brown',\n",
|
| 540 |
+
" 'phone': '555-9876',\n",
|
| 541 |
+
" 'email': 'charlie.brown@example.com',\n",
|
| 542 |
+
" 'id': 4},\n",
|
| 543 |
+
" {'firstname': 'Diana',\n",
|
| 544 |
+
" 'lastname': 'Miller',\n",
|
| 545 |
+
" 'phone': '555-6543',\n",
|
| 546 |
+
" 'email': 'diana.miller@example.com',\n",
|
| 547 |
+
" 'id': 5},\n",
|
| 548 |
+
" {'firstname': 'Edward',\n",
|
| 549 |
+
" 'lastname': 'Davis',\n",
|
| 550 |
+
" 'phone': '555-3456',\n",
|
| 551 |
+
" 'email': 'edward.davis@example.com',\n",
|
| 552 |
+
" 'id': 6},\n",
|
| 553 |
+
" {'firstname': 'Fiona',\n",
|
| 554 |
+
" 'lastname': 'Garcia',\n",
|
| 555 |
+
" 'phone': '555-6789',\n",
|
| 556 |
+
" 'email': 'fiona.garcia@example.com',\n",
|
| 557 |
+
" 'id': 7},\n",
|
| 558 |
+
" {'firstname': 'George',\n",
|
| 559 |
+
" 'lastname': 'Martinez',\n",
|
| 560 |
+
" 'phone': '555-7890',\n",
|
| 561 |
+
" 'email': 'george.martinez@example.com',\n",
|
| 562 |
+
" 'id': 8},\n",
|
| 563 |
+
" {'firstname': 'Hannah',\n",
|
| 564 |
+
" 'lastname': 'Rodriguez',\n",
|
| 565 |
+
" 'phone': '555-8901',\n",
|
| 566 |
+
" 'email': 'hannah.rodriguez@example.com',\n",
|
| 567 |
+
" 'id': 9},\n",
|
| 568 |
+
" {'firstname': 'John',\n",
|
| 569 |
+
" 'lastname': 'Doe',\n",
|
| 570 |
+
" 'phone': '555-1234',\n",
|
| 571 |
+
" 'email': 'john.doe@example.com',\n",
|
| 572 |
+
" 'id': 9},\n",
|
| 573 |
+
" {'firstname': 'Jane',\n",
|
| 574 |
+
" 'lastname': 'Smith',\n",
|
| 575 |
+
" 'phone': '555-5678',\n",
|
| 576 |
+
" 'email': 'jane.smith@example.com',\n",
|
| 577 |
+
" 'id': 9}]"
|
| 578 |
+
]
|
| 579 |
+
},
|
| 580 |
+
"execution_count": 13,
|
| 581 |
+
"metadata": {},
|
| 582 |
+
"output_type": "execute_result"
|
| 583 |
+
}
|
| 584 |
+
],
|
| 585 |
+
"source": [
|
| 586 |
+
"c = Contact()\n",
|
| 587 |
+
"a_dict = c.db.loc[0].to_dict()\n",
|
| 588 |
+
"a_dict.pop('id', None); a_dict\n",
|
| 589 |
+
"c.delete(0)\n",
|
| 590 |
+
"\n",
|
| 591 |
+
"c.add(*list(a_dict.values()))\n",
|
| 592 |
+
"c.all()\n",
|
| 593 |
+
"\n",
|
| 594 |
+
"\n"
|
| 595 |
+
]
|
| 596 |
+
},
|
| 597 |
+
{
|
| 598 |
+
"cell_type": "code",
|
| 599 |
+
"execution_count": 14,
|
| 600 |
"metadata": {},
|
| 601 |
"outputs": [],
|
| 602 |
"source": [
|
| 603 |
"#|export\n",
|
|
|
|
| 604 |
"app = Flask(__name__)"
|
| 605 |
]
|
| 606 |
},
|
| 607 |
{
|
| 608 |
"cell_type": "code",
|
| 609 |
+
"execution_count": 15,
|
| 610 |
"metadata": {},
|
| 611 |
"outputs": [],
|
| 612 |
"source": [
|
| 613 |
"#|export\n",
|
|
|
|
| 614 |
"@app.get(\"/\")\n",
|
| 615 |
"def index():\n",
|
| 616 |
" return redirect(\"/contacts\")"
|
|
|
|
| 618 |
},
|
| 619 |
{
|
| 620 |
"cell_type": "code",
|
| 621 |
+
"execution_count": 16,
|
| 622 |
"metadata": {},
|
| 623 |
"outputs": [],
|
| 624 |
"source": [
|
| 625 |
"#|export\n",
|
|
|
|
| 626 |
"@app.get(\"/contacts\")\n",
|
| 627 |
"def contacts():\n",
|
| 628 |
" search = request.args.get(\"q\")\n",
|
|
|
|
| 635 |
},
|
| 636 |
{
|
| 637 |
"cell_type": "code",
|
| 638 |
+
"execution_count": 17,
|
| 639 |
+
"metadata": {},
|
| 640 |
+
"outputs": [
|
| 641 |
+
{
|
| 642 |
+
"data": {
|
| 643 |
+
"text/plain": [
|
| 644 |
+
"{'firstname': 'Bob',\n",
|
| 645 |
+
" 'lastname': 'Williams',\n",
|
| 646 |
+
" 'phone': '555-4321',\n",
|
| 647 |
+
" 'email': 'bob.williams@example.com',\n",
|
| 648 |
+
" 'id': 3}"
|
| 649 |
+
]
|
| 650 |
+
},
|
| 651 |
+
"execution_count": 17,
|
| 652 |
+
"metadata": {},
|
| 653 |
+
"output_type": "execute_result"
|
| 654 |
+
}
|
| 655 |
+
],
|
| 656 |
+
"source": [
|
| 657 |
+
"Contact().db[df['id']==1].to_dict('records')[0]"
|
| 658 |
+
]
|
| 659 |
+
},
|
| 660 |
+
{
|
| 661 |
+
"cell_type": "code",
|
| 662 |
+
"execution_count": 18,
|
| 663 |
+
"metadata": {},
|
| 664 |
+
"outputs": [],
|
| 665 |
+
"source": [
|
| 666 |
+
"#|export\n",
|
| 667 |
+
"@app.get(\"/contacts/<int:id>\")\n",
|
| 668 |
+
"def view(id:int):\n",
|
| 669 |
+
" return render_template(\"view.html\", contact=Contact().get(id))"
|
| 670 |
+
]
|
| 671 |
+
},
|
| 672 |
+
{
|
| 673 |
+
"cell_type": "code",
|
| 674 |
+
"execution_count": 19,
|
| 675 |
+
"metadata": {},
|
| 676 |
+
"outputs": [
|
| 677 |
+
{
|
| 678 |
+
"name": "stdout",
|
| 679 |
+
"output_type": "stream",
|
| 680 |
+
"text": [
|
| 681 |
+
"<!doctype html>\n",
|
| 682 |
+
"<html>\n",
|
| 683 |
+
" <head>\n",
|
| 684 |
+
" <title>Contact.App</title>\n",
|
| 685 |
+
" <link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css\">\n",
|
| 686 |
+
" </head>\n",
|
| 687 |
+
" <body>\n",
|
| 688 |
+
" <main class=\"container\">\n",
|
| 689 |
+
" <h1> CONTACTS.APP</h1>\n",
|
| 690 |
+
" <h2> A demo contact application </h2>\n",
|
| 691 |
+
" <hr>\n",
|
| 692 |
+
" \n",
|
| 693 |
+
" <article>\n",
|
| 694 |
+
" <h1>Alice Johnson </h1>\n",
|
| 695 |
+
" <div role=\"group\">\n",
|
| 696 |
+
" <div>555-8767</div>\n",
|
| 697 |
+
" <div>alice.johnson@example.com</div>\n",
|
| 698 |
+
" </div>\n",
|
| 699 |
+
" <footer>\n",
|
| 700 |
+
" <a href=\"/contacts\" role=\"button\">Back</a>\n",
|
| 701 |
+
" </footer>\n",
|
| 702 |
+
" </article>\n",
|
| 703 |
+
"\n",
|
| 704 |
+
" </main>\n",
|
| 705 |
+
" </body>\n",
|
| 706 |
+
"</html>\n"
|
| 707 |
+
]
|
| 708 |
+
}
|
| 709 |
+
],
|
| 710 |
+
"source": [
|
| 711 |
+
"with app.app_context():\n",
|
| 712 |
+
" print(view(2))"
|
| 713 |
+
]
|
| 714 |
+
},
|
| 715 |
+
{
|
| 716 |
+
"cell_type": "code",
|
| 717 |
+
"execution_count": 20,
|
| 718 |
+
"metadata": {},
|
| 719 |
+
"outputs": [
|
| 720 |
+
{
|
| 721 |
+
"data": {
|
| 722 |
+
"text/plain": [
|
| 723 |
+
"{'firstname': 'Alice',\n",
|
| 724 |
+
" 'lastname': 'Johnson',\n",
|
| 725 |
+
" 'phone': '555-8767',\n",
|
| 726 |
+
" 'email': 'alice.johnson@example.com',\n",
|
| 727 |
+
" 'id': 2}"
|
| 728 |
+
]
|
| 729 |
+
},
|
| 730 |
+
"execution_count": 20,
|
| 731 |
+
"metadata": {},
|
| 732 |
+
"output_type": "execute_result"
|
| 733 |
+
}
|
| 734 |
+
],
|
| 735 |
+
"source": [
|
| 736 |
+
"Contact().get(2)"
|
| 737 |
+
]
|
| 738 |
+
},
|
| 739 |
+
{
|
| 740 |
+
"cell_type": "code",
|
| 741 |
+
"execution_count": 21,
|
| 742 |
+
"metadata": {},
|
| 743 |
+
"outputs": [],
|
| 744 |
+
"source": [
|
| 745 |
+
"#|export\n",
|
| 746 |
+
"@app.put(\"/contacts/<int:id>/edit\")\n",
|
| 747 |
+
"def edit(id):\n",
|
| 748 |
+
" return \"Edit View\""
|
| 749 |
+
]
|
| 750 |
+
},
|
| 751 |
+
{
|
| 752 |
+
"cell_type": "code",
|
| 753 |
+
"execution_count": 22,
|
| 754 |
+
"metadata": {},
|
| 755 |
+
"outputs": [],
|
| 756 |
+
"source": [
|
| 757 |
+
"#|export\n",
|
| 758 |
+
"@app.post(\"/contacts/new\")\n",
|
| 759 |
+
"def new():\n",
|
| 760 |
+
" return \"New View\""
|
| 761 |
+
]
|
| 762 |
+
},
|
| 763 |
+
{
|
| 764 |
+
"cell_type": "code",
|
| 765 |
+
"execution_count": 24,
|
| 766 |
"metadata": {},
|
| 767 |
"outputs": [],
|
| 768 |
"source": [
|
__pycache__/main.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ
|
|
|
_site/01_main.html
CHANGED
|
@@ -198,148 +198,149 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
|
| 198 |
|
| 199 |
|
| 200 |
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
|
| 201 |
-
<div id="cell-
|
| 202 |
-
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"
|
| 203 |
-
<
|
| 204 |
-
<
|
| 205 |
-
|
| 206 |
-
</div>
|
| 207 |
-
|
| 208 |
-
<div
|
| 209 |
-
<
|
| 210 |
-
<span id="
|
| 211 |
-
<span id="
|
| 212 |
-
<span id="
|
| 213 |
-
</
|
| 214 |
-
<
|
| 215 |
-
<
|
| 216 |
-
<span id="
|
| 217 |
-
<span id="
|
| 218 |
-
<span id="
|
| 219 |
-
<span id="
|
| 220 |
-
<span id="
|
| 221 |
-
<span id="
|
| 222 |
-
<span id="
|
| 223 |
-
<span id="
|
| 224 |
-
<span id="
|
| 225 |
-
<span id="
|
| 226 |
-
<span id="
|
| 227 |
-
<span id="
|
| 228 |
-
<span id="
|
| 229 |
-
<span id="
|
| 230 |
-
<span id="
|
| 231 |
-
<span id="
|
| 232 |
-
<span id="
|
| 233 |
-
<span id="
|
| 234 |
-
<span id="
|
| 235 |
-
<span id="
|
| 236 |
-
<span id="
|
| 237 |
-
<span id="
|
| 238 |
-
<span id="
|
| 239 |
-
<span id="
|
| 240 |
-
<span id="
|
| 241 |
-
<span id="
|
| 242 |
-
<span id="
|
| 243 |
-
<span id="
|
| 244 |
-
<span id="
|
| 245 |
-
<span id="
|
| 246 |
-
<span id="
|
| 247 |
-
<span id="
|
| 248 |
-
<span id="
|
| 249 |
-
<span id="
|
| 250 |
-
<span id="
|
| 251 |
-
<span id="
|
| 252 |
-
<span id="
|
| 253 |
-
<span id="
|
| 254 |
-
<span id="
|
| 255 |
-
<span id="
|
| 256 |
-
<span id="
|
| 257 |
-
<span id="
|
| 258 |
-
<span id="
|
| 259 |
-
<span id="
|
| 260 |
-
<span id="
|
| 261 |
-
<span id="
|
| 262 |
-
<span id="
|
| 263 |
-
<span id="
|
| 264 |
-
<span id="
|
| 265 |
-
<span id="
|
| 266 |
-
<span id="
|
| 267 |
-
<span id="
|
| 268 |
-
<span id="
|
| 269 |
-
<span id="
|
| 270 |
-
<span id="
|
| 271 |
-
<span id="
|
| 272 |
-
<span id="
|
| 273 |
-
<span id="
|
| 274 |
-
<span id="
|
| 275 |
-
<span id="
|
| 276 |
-
<span id="
|
| 277 |
-
<span id="
|
| 278 |
-
|
| 279 |
-
<
|
| 280 |
-
<span id="
|
| 281 |
-
<span id="
|
| 282 |
-
<
|
| 283 |
-
<span id="cb4-69"><a href="#cb4-69" aria-hidden="true" tabindex="-1"></a>file_path</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 284 |
-
<div class="cell-output cell-output-display" data-execution_count="57">
|
| 285 |
-
<pre><code>'contactdb.json'</code></pre>
|
| 286 |
-
</div>
|
| 287 |
-
</div>
|
| 288 |
-
<div id="cell-5" class="cell" data-execution_count="58">
|
| 289 |
-
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>db <span class="op">=</span> pd.read_json(<span class="st">'contactdb.json'</span>)</span>
|
| 290 |
-
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>db.to_dict(<span class="st">'records'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 291 |
-
<div class="cell-output cell-output-display" data-execution_count="58">
|
| 292 |
<pre><code>[{'firstname': 'John',
|
| 293 |
'lastname': 'Doe',
|
| 294 |
'phone': '555-1234',
|
| 295 |
-
'email': 'john.doe@example.com'
|
|
|
|
| 296 |
{'firstname': 'Jane',
|
| 297 |
'lastname': 'Smith',
|
| 298 |
'phone': '555-5678',
|
| 299 |
-
'email': 'jane.smith@example.com'
|
|
|
|
| 300 |
{'firstname': 'Alice',
|
| 301 |
'lastname': 'Johnson',
|
| 302 |
'phone': '555-8765',
|
| 303 |
-
'email': 'alice.johnson@example.com'
|
|
|
|
| 304 |
{'firstname': 'Bob',
|
| 305 |
'lastname': 'Williams',
|
| 306 |
'phone': '555-4321',
|
| 307 |
-
'email': 'bob.williams@example.com'
|
|
|
|
| 308 |
{'firstname': 'Charlie',
|
| 309 |
'lastname': 'Brown',
|
| 310 |
'phone': '555-9876',
|
| 311 |
-
'email': 'charlie.brown@example.com'
|
|
|
|
| 312 |
{'firstname': 'Diana',
|
| 313 |
'lastname': 'Miller',
|
| 314 |
'phone': '555-6543',
|
| 315 |
-
'email': 'diana.miller@example.com'
|
|
|
|
| 316 |
{'firstname': 'Edward',
|
| 317 |
'lastname': 'Davis',
|
| 318 |
'phone': '555-3456',
|
| 319 |
-
'email': 'edward.davis@example.com'
|
|
|
|
| 320 |
{'firstname': 'Fiona',
|
| 321 |
'lastname': 'Garcia',
|
| 322 |
'phone': '555-6789',
|
| 323 |
-
'email': 'fiona.garcia@example.com'
|
|
|
|
| 324 |
{'firstname': 'George',
|
| 325 |
'lastname': 'Martinez',
|
| 326 |
'phone': '555-7890',
|
| 327 |
-
'email': 'george.martinez@example.com'
|
|
|
|
| 328 |
{'firstname': 'Hannah',
|
| 329 |
'lastname': 'Rodriguez',
|
| 330 |
'phone': '555-8901',
|
| 331 |
-
'email': 'hannah.rodriguez@example.com'
|
| 332 |
-
</
|
| 333 |
-
</div>
|
| 334 |
-
|
| 335 |
-
<div
|
| 336 |
-
<span id="
|
| 337 |
-
<span id="
|
| 338 |
-
<span id="
|
| 339 |
-
<span id="
|
| 340 |
-
<span id="
|
| 341 |
-
<span id="
|
| 342 |
-
<
|
|
|
|
| 343 |
<div>
|
| 344 |
<div>
|
| 345 |
|
|
@@ -352,6 +353,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
|
| 352 |
<th data-quarto-table-cell-role="th">lastname</th>
|
| 353 |
<th data-quarto-table-cell-role="th">phone</th>
|
| 354 |
<th data-quarto-table-cell-role="th">email</th>
|
|
|
|
| 355 |
</tr>
|
| 356 |
</thead>
|
| 357 |
<tbody>
|
|
@@ -361,6 +363,133 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
|
| 361 |
<td>Doe</td>
|
| 362 |
<td>555-1234</td>
|
| 363 |
<td>john.doe@example.com</td>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
</tr>
|
| 365 |
</tbody>
|
| 366 |
</table>
|
|
@@ -369,43 +498,225 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
|
| 369 |
</div>
|
| 370 |
</div>
|
| 371 |
</div>
|
| 372 |
-
<div id="cell-
|
| 373 |
-
<div class="sourceCode cell-code" id="
|
| 374 |
-
<span id="
|
| 375 |
-
<span id="
|
| 376 |
-
<span id="
|
| 377 |
-
<span id="
|
| 378 |
-
<span id="
|
| 379 |
-
<span id="
|
| 380 |
-
<span id="
|
| 381 |
-
<span id="
|
| 382 |
-
<span id="
|
| 383 |
-
<span id="
|
| 384 |
-
<span id="
|
| 385 |
-
<span id="
|
| 386 |
-
<span id="
|
| 387 |
-
</
|
| 388 |
-
<
|
| 389 |
-
<
|
| 390 |
-
</
|
| 391 |
-
<
|
| 392 |
-
<
|
| 393 |
-
<span id="
|
| 394 |
-
<span id="
|
| 395 |
-
</
|
| 396 |
-
<
|
| 397 |
-
<
|
| 398 |
-
<span id="
|
| 399 |
-
<span id="
|
| 400 |
-
<span id="
|
| 401 |
-
<span id="
|
| 402 |
-
<span id="
|
| 403 |
-
<span id="
|
| 404 |
-
<span id="
|
| 405 |
-
</
|
| 406 |
-
<
|
| 407 |
-
<
|
| 408 |
-
<span id="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
</div>
|
| 410 |
|
| 411 |
|
|
|
|
| 198 |
|
| 199 |
|
| 200 |
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
|
| 201 |
+
<div id="cell-3" class="cell" data-execution_count="2">
|
| 202 |
+
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span>
|
| 203 |
+
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> flask <span class="im">import</span> Flask, redirect, request, render_template</span>
|
| 204 |
+
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> json</span>
|
| 205 |
+
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> IPython.display <span class="im">import</span> display_html</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 206 |
+
</div>
|
| 207 |
+
<div id="cell-4" class="cell" data-execution_count="3">
|
| 208 |
+
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>people <span class="op">=</span> [</span>
|
| 209 |
+
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> {</span>
|
| 210 |
+
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"firstname"</span>: <span class="st">"John"</span>,</span>
|
| 211 |
+
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"lastname"</span>: <span class="st">"Doe"</span>,</span>
|
| 212 |
+
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"phone"</span>: <span class="st">"555-1234"</span>,</span>
|
| 213 |
+
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"email"</span>: <span class="st">"john.doe@example.com"</span></span>
|
| 214 |
+
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a> },</span>
|
| 215 |
+
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a> {</span>
|
| 216 |
+
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"firstname"</span>: <span class="st">"Jane"</span>,</span>
|
| 217 |
+
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a> <span class="st">"lastname"</span>: <span class="st">"Smith"</span>,</span>
|
| 218 |
+
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a> <span class="st">"phone"</span>: <span class="st">"555-5678"</span>,</span>
|
| 219 |
+
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a> <span class="st">"email"</span>: <span class="st">"jane.smith@example.com"</span></span>
|
| 220 |
+
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a> },</span>
|
| 221 |
+
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a> {</span>
|
| 222 |
+
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a> <span class="st">"firstname"</span>: <span class="st">"Alice"</span>,</span>
|
| 223 |
+
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a> <span class="st">"lastname"</span>: <span class="st">"Johnson"</span>,</span>
|
| 224 |
+
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a> <span class="st">"phone"</span>: <span class="st">"555-8765"</span>,</span>
|
| 225 |
+
<span id="cb2-18"><a href="#cb2-18" aria-hidden="true" tabindex="-1"></a> <span class="st">"email"</span>: <span class="st">"alice.johnson@example.com"</span></span>
|
| 226 |
+
<span id="cb2-19"><a href="#cb2-19" aria-hidden="true" tabindex="-1"></a> },</span>
|
| 227 |
+
<span id="cb2-20"><a href="#cb2-20" aria-hidden="true" tabindex="-1"></a> {</span>
|
| 228 |
+
<span id="cb2-21"><a href="#cb2-21" aria-hidden="true" tabindex="-1"></a> <span class="st">"firstname"</span>: <span class="st">"Bob"</span>,</span>
|
| 229 |
+
<span id="cb2-22"><a href="#cb2-22" aria-hidden="true" tabindex="-1"></a> <span class="st">"lastname"</span>: <span class="st">"Williams"</span>,</span>
|
| 230 |
+
<span id="cb2-23"><a href="#cb2-23" aria-hidden="true" tabindex="-1"></a> <span class="st">"phone"</span>: <span class="st">"555-4321"</span>,</span>
|
| 231 |
+
<span id="cb2-24"><a href="#cb2-24" aria-hidden="true" tabindex="-1"></a> <span class="st">"email"</span>: <span class="st">"bob.williams@example.com"</span></span>
|
| 232 |
+
<span id="cb2-25"><a href="#cb2-25" aria-hidden="true" tabindex="-1"></a> },</span>
|
| 233 |
+
<span id="cb2-26"><a href="#cb2-26" aria-hidden="true" tabindex="-1"></a> {</span>
|
| 234 |
+
<span id="cb2-27"><a href="#cb2-27" aria-hidden="true" tabindex="-1"></a> <span class="st">"firstname"</span>: <span class="st">"Charlie"</span>,</span>
|
| 235 |
+
<span id="cb2-28"><a href="#cb2-28" aria-hidden="true" tabindex="-1"></a> <span class="st">"lastname"</span>: <span class="st">"Brown"</span>,</span>
|
| 236 |
+
<span id="cb2-29"><a href="#cb2-29" aria-hidden="true" tabindex="-1"></a> <span class="st">"phone"</span>: <span class="st">"555-9876"</span>,</span>
|
| 237 |
+
<span id="cb2-30"><a href="#cb2-30" aria-hidden="true" tabindex="-1"></a> <span class="st">"email"</span>: <span class="st">"charlie.brown@example.com"</span></span>
|
| 238 |
+
<span id="cb2-31"><a href="#cb2-31" aria-hidden="true" tabindex="-1"></a> },</span>
|
| 239 |
+
<span id="cb2-32"><a href="#cb2-32" aria-hidden="true" tabindex="-1"></a> {</span>
|
| 240 |
+
<span id="cb2-33"><a href="#cb2-33" aria-hidden="true" tabindex="-1"></a> <span class="st">"firstname"</span>: <span class="st">"Diana"</span>,</span>
|
| 241 |
+
<span id="cb2-34"><a href="#cb2-34" aria-hidden="true" tabindex="-1"></a> <span class="st">"lastname"</span>: <span class="st">"Miller"</span>,</span>
|
| 242 |
+
<span id="cb2-35"><a href="#cb2-35" aria-hidden="true" tabindex="-1"></a> <span class="st">"phone"</span>: <span class="st">"555-6543"</span>,</span>
|
| 243 |
+
<span id="cb2-36"><a href="#cb2-36" aria-hidden="true" tabindex="-1"></a> <span class="st">"email"</span>: <span class="st">"diana.miller@example.com"</span></span>
|
| 244 |
+
<span id="cb2-37"><a href="#cb2-37" aria-hidden="true" tabindex="-1"></a> },</span>
|
| 245 |
+
<span id="cb2-38"><a href="#cb2-38" aria-hidden="true" tabindex="-1"></a> {</span>
|
| 246 |
+
<span id="cb2-39"><a href="#cb2-39" aria-hidden="true" tabindex="-1"></a> <span class="st">"firstname"</span>: <span class="st">"Edward"</span>,</span>
|
| 247 |
+
<span id="cb2-40"><a href="#cb2-40" aria-hidden="true" tabindex="-1"></a> <span class="st">"lastname"</span>: <span class="st">"Davis"</span>,</span>
|
| 248 |
+
<span id="cb2-41"><a href="#cb2-41" aria-hidden="true" tabindex="-1"></a> <span class="st">"phone"</span>: <span class="st">"555-3456"</span>,</span>
|
| 249 |
+
<span id="cb2-42"><a href="#cb2-42" aria-hidden="true" tabindex="-1"></a> <span class="st">"email"</span>: <span class="st">"edward.davis@example.com"</span></span>
|
| 250 |
+
<span id="cb2-43"><a href="#cb2-43" aria-hidden="true" tabindex="-1"></a> },</span>
|
| 251 |
+
<span id="cb2-44"><a href="#cb2-44" aria-hidden="true" tabindex="-1"></a> {</span>
|
| 252 |
+
<span id="cb2-45"><a href="#cb2-45" aria-hidden="true" tabindex="-1"></a> <span class="st">"firstname"</span>: <span class="st">"Fiona"</span>,</span>
|
| 253 |
+
<span id="cb2-46"><a href="#cb2-46" aria-hidden="true" tabindex="-1"></a> <span class="st">"lastname"</span>: <span class="st">"Garcia"</span>,</span>
|
| 254 |
+
<span id="cb2-47"><a href="#cb2-47" aria-hidden="true" tabindex="-1"></a> <span class="st">"phone"</span>: <span class="st">"555-6789"</span>,</span>
|
| 255 |
+
<span id="cb2-48"><a href="#cb2-48" aria-hidden="true" tabindex="-1"></a> <span class="st">"email"</span>: <span class="st">"fiona.garcia@example.com"</span></span>
|
| 256 |
+
<span id="cb2-49"><a href="#cb2-49" aria-hidden="true" tabindex="-1"></a> },</span>
|
| 257 |
+
<span id="cb2-50"><a href="#cb2-50" aria-hidden="true" tabindex="-1"></a> {</span>
|
| 258 |
+
<span id="cb2-51"><a href="#cb2-51" aria-hidden="true" tabindex="-1"></a> <span class="st">"firstname"</span>: <span class="st">"George"</span>,</span>
|
| 259 |
+
<span id="cb2-52"><a href="#cb2-52" aria-hidden="true" tabindex="-1"></a> <span class="st">"lastname"</span>: <span class="st">"Martinez"</span>,</span>
|
| 260 |
+
<span id="cb2-53"><a href="#cb2-53" aria-hidden="true" tabindex="-1"></a> <span class="st">"phone"</span>: <span class="st">"555-7890"</span>,</span>
|
| 261 |
+
<span id="cb2-54"><a href="#cb2-54" aria-hidden="true" tabindex="-1"></a> <span class="st">"email"</span>: <span class="st">"george.martinez@example.com"</span></span>
|
| 262 |
+
<span id="cb2-55"><a href="#cb2-55" aria-hidden="true" tabindex="-1"></a> },</span>
|
| 263 |
+
<span id="cb2-56"><a href="#cb2-56" aria-hidden="true" tabindex="-1"></a> {</span>
|
| 264 |
+
<span id="cb2-57"><a href="#cb2-57" aria-hidden="true" tabindex="-1"></a> <span class="st">"firstname"</span>: <span class="st">"Hannah"</span>,</span>
|
| 265 |
+
<span id="cb2-58"><a href="#cb2-58" aria-hidden="true" tabindex="-1"></a> <span class="st">"lastname"</span>: <span class="st">"Rodriguez"</span>,</span>
|
| 266 |
+
<span id="cb2-59"><a href="#cb2-59" aria-hidden="true" tabindex="-1"></a> <span class="st">"phone"</span>: <span class="st">"555-8901"</span>,</span>
|
| 267 |
+
<span id="cb2-60"><a href="#cb2-60" aria-hidden="true" tabindex="-1"></a> <span class="st">"email"</span>: <span class="st">"hannah.rodriguez@example.com"</span></span>
|
| 268 |
+
<span id="cb2-61"><a href="#cb2-61" aria-hidden="true" tabindex="-1"></a> }</span>
|
| 269 |
+
<span id="cb2-62"><a href="#cb2-62" aria-hidden="true" tabindex="-1"></a>]</span>
|
| 270 |
+
<span id="cb2-63"><a href="#cb2-63" aria-hidden="true" tabindex="-1"></a></span>
|
| 271 |
+
<span id="cb2-64"><a href="#cb2-64" aria-hidden="true" tabindex="-1"></a><span class="co"># Writing the JSON data to a file</span></span>
|
| 272 |
+
<span id="cb2-65"><a href="#cb2-65" aria-hidden="true" tabindex="-1"></a>file_path <span class="op">=</span> <span class="st">'contactdb.json'</span></span>
|
| 273 |
+
<span id="cb2-66"><a href="#cb2-66" aria-hidden="true" tabindex="-1"></a><span class="co"># df = pd.read_json(file_path)</span></span>
|
| 274 |
+
<span id="cb2-67"><a href="#cb2-67" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> pd.DataFrame()</span>
|
| 275 |
+
<span id="cb2-68"><a href="#cb2-68" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> df.from_dict(people)</span>
|
| 276 |
+
<span id="cb2-69"><a href="#cb2-69" aria-hidden="true" tabindex="-1"></a>df[<span class="st">'id'</span>] <span class="op">=</span> df.index</span>
|
| 277 |
+
<span id="cb2-70"><a href="#cb2-70" aria-hidden="true" tabindex="-1"></a>df.to_json(file_path, orient<span class="op">=</span><span class="st">'records'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 278 |
+
</div>
|
| 279 |
+
<div id="cell-5" class="cell" data-execution_count="4">
|
| 280 |
+
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>db <span class="op">=</span> pd.read_json(<span class="st">'contactdb.json'</span>)</span>
|
| 281 |
+
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>db.to_dict(<span class="st">'records'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 282 |
+
<div class="cell-output cell-output-display" data-execution_count="4">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
<pre><code>[{'firstname': 'John',
|
| 284 |
'lastname': 'Doe',
|
| 285 |
'phone': '555-1234',
|
| 286 |
+
'email': 'john.doe@example.com',
|
| 287 |
+
'id': 0},
|
| 288 |
{'firstname': 'Jane',
|
| 289 |
'lastname': 'Smith',
|
| 290 |
'phone': '555-5678',
|
| 291 |
+
'email': 'jane.smith@example.com',
|
| 292 |
+
'id': 1},
|
| 293 |
{'firstname': 'Alice',
|
| 294 |
'lastname': 'Johnson',
|
| 295 |
'phone': '555-8765',
|
| 296 |
+
'email': 'alice.johnson@example.com',
|
| 297 |
+
'id': 2},
|
| 298 |
{'firstname': 'Bob',
|
| 299 |
'lastname': 'Williams',
|
| 300 |
'phone': '555-4321',
|
| 301 |
+
'email': 'bob.williams@example.com',
|
| 302 |
+
'id': 3},
|
| 303 |
{'firstname': 'Charlie',
|
| 304 |
'lastname': 'Brown',
|
| 305 |
'phone': '555-9876',
|
| 306 |
+
'email': 'charlie.brown@example.com',
|
| 307 |
+
'id': 4},
|
| 308 |
{'firstname': 'Diana',
|
| 309 |
'lastname': 'Miller',
|
| 310 |
'phone': '555-6543',
|
| 311 |
+
'email': 'diana.miller@example.com',
|
| 312 |
+
'id': 5},
|
| 313 |
{'firstname': 'Edward',
|
| 314 |
'lastname': 'Davis',
|
| 315 |
'phone': '555-3456',
|
| 316 |
+
'email': 'edward.davis@example.com',
|
| 317 |
+
'id': 6},
|
| 318 |
{'firstname': 'Fiona',
|
| 319 |
'lastname': 'Garcia',
|
| 320 |
'phone': '555-6789',
|
| 321 |
+
'email': 'fiona.garcia@example.com',
|
| 322 |
+
'id': 7},
|
| 323 |
{'firstname': 'George',
|
| 324 |
'lastname': 'Martinez',
|
| 325 |
'phone': '555-7890',
|
| 326 |
+
'email': 'george.martinez@example.com',
|
| 327 |
+
'id': 8},
|
| 328 |
{'firstname': 'Hannah',
|
| 329 |
'lastname': 'Rodriguez',
|
| 330 |
'phone': '555-8901',
|
| 331 |
+
'email': 'hannah.rodriguez@example.com',
|
| 332 |
+
'id': 9}]</code></pre>
|
| 333 |
+
</div>
|
| 334 |
+
</div>
|
| 335 |
+
<div id="cell-6" class="cell" data-execution_count="5">
|
| 336 |
+
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>q <span class="op">=</span> <span class="st">'1234'</span></span>
|
| 337 |
+
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="co"># db[db['firstname'].str.lower().str.contains(q)]</span></span>
|
| 338 |
+
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>predicate_firstname <span class="op">=</span> db[<span class="st">'firstname'</span>].<span class="bu">str</span>.lower().<span class="bu">str</span>.contains(q.lower())</span>
|
| 339 |
+
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>predicate_lastname <span class="op">=</span> db[<span class="st">'lastname'</span>].<span class="bu">str</span>.lower().<span class="bu">str</span>.contains(q.lower())</span>
|
| 340 |
+
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>predicate_phone <span class="op">=</span> db[<span class="st">'phone'</span>].<span class="bu">str</span>.lower().<span class="bu">str</span>.contains(q.lower())</span>
|
| 341 |
+
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>predicate_email<span class="op">=</span> db[<span class="st">'email'</span>].<span class="bu">str</span>.lower().<span class="bu">str</span>.contains(q.lower())</span>
|
| 342 |
+
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a>db[predicate_firstname<span class="op">|</span>predicate_lastname<span class="op">|</span>predicate_phone<span class="op">|</span>predicate_email]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 343 |
+
<div class="cell-output cell-output-display" data-execution_count="5">
|
| 344 |
<div>
|
| 345 |
<div>
|
| 346 |
|
|
|
|
| 353 |
<th data-quarto-table-cell-role="th">lastname</th>
|
| 354 |
<th data-quarto-table-cell-role="th">phone</th>
|
| 355 |
<th data-quarto-table-cell-role="th">email</th>
|
| 356 |
+
<th data-quarto-table-cell-role="th">id</th>
|
| 357 |
</tr>
|
| 358 |
</thead>
|
| 359 |
<tbody>
|
|
|
|
| 363 |
<td>Doe</td>
|
| 364 |
<td>555-1234</td>
|
| 365 |
<td>john.doe@example.com</td>
|
| 366 |
+
<td>0</td>
|
| 367 |
+
</tr>
|
| 368 |
+
</tbody>
|
| 369 |
+
</table>
|
| 370 |
+
|
| 371 |
+
</div>
|
| 372 |
+
</div>
|
| 373 |
+
</div>
|
| 374 |
+
</div>
|
| 375 |
+
<div id="cell-7" class="cell" data-execution_count="6">
|
| 376 |
+
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>db.columns</span>
|
| 377 |
+
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a></span>
|
| 378 |
+
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>a<span class="op">=</span>{<span class="st">'firstname'</span>: <span class="st">'Rahul'</span>,</span>
|
| 379 |
+
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="st">'lastname'</span>: <span class="st">'Saraf'</span>,</span>
|
| 380 |
+
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> <span class="st">'phone'</span>:<span class="st">'7347306799'</span>,</span>
|
| 381 |
+
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="st">'email'</span>: <span class="st">'rahuketu86'</span>}</span>
|
| 382 |
+
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a>a[<span class="st">'id'</span>] <span class="op">=</span> <span class="bu">len</span>(db)</span>
|
| 383 |
+
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a><span class="co"># db.loc[len(db)] = </span></span>
|
| 384 |
+
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a>a</span>
|
| 385 |
+
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a>db.loc[<span class="bu">len</span>(db), a.keys()] <span class="op">=</span> a.values()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 386 |
+
</div>
|
| 387 |
+
<div id="cell-8" class="cell" data-execution_count="7">
|
| 388 |
+
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>db</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 389 |
+
<div class="cell-output cell-output-display" data-execution_count="7">
|
| 390 |
+
<div>
|
| 391 |
+
<div>
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
<table class="dataframe table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
|
| 395 |
+
<thead>
|
| 396 |
+
<tr class="header">
|
| 397 |
+
<th data-quarto-table-cell-role="th"></th>
|
| 398 |
+
<th data-quarto-table-cell-role="th">firstname</th>
|
| 399 |
+
<th data-quarto-table-cell-role="th">lastname</th>
|
| 400 |
+
<th data-quarto-table-cell-role="th">phone</th>
|
| 401 |
+
<th data-quarto-table-cell-role="th">email</th>
|
| 402 |
+
<th data-quarto-table-cell-role="th">id</th>
|
| 403 |
+
</tr>
|
| 404 |
+
</thead>
|
| 405 |
+
<tbody>
|
| 406 |
+
<tr class="odd">
|
| 407 |
+
<td data-quarto-table-cell-role="th">0</td>
|
| 408 |
+
<td>John</td>
|
| 409 |
+
<td>Doe</td>
|
| 410 |
+
<td>555-1234</td>
|
| 411 |
+
<td>john.doe@example.com</td>
|
| 412 |
+
<td>0.0</td>
|
| 413 |
+
</tr>
|
| 414 |
+
<tr class="even">
|
| 415 |
+
<td data-quarto-table-cell-role="th">1</td>
|
| 416 |
+
<td>Jane</td>
|
| 417 |
+
<td>Smith</td>
|
| 418 |
+
<td>555-5678</td>
|
| 419 |
+
<td>jane.smith@example.com</td>
|
| 420 |
+
<td>1.0</td>
|
| 421 |
+
</tr>
|
| 422 |
+
<tr class="odd">
|
| 423 |
+
<td data-quarto-table-cell-role="th">2</td>
|
| 424 |
+
<td>Alice</td>
|
| 425 |
+
<td>Johnson</td>
|
| 426 |
+
<td>555-8765</td>
|
| 427 |
+
<td>alice.johnson@example.com</td>
|
| 428 |
+
<td>2.0</td>
|
| 429 |
+
</tr>
|
| 430 |
+
<tr class="even">
|
| 431 |
+
<td data-quarto-table-cell-role="th">3</td>
|
| 432 |
+
<td>Bob</td>
|
| 433 |
+
<td>Williams</td>
|
| 434 |
+
<td>555-4321</td>
|
| 435 |
+
<td>bob.williams@example.com</td>
|
| 436 |
+
<td>3.0</td>
|
| 437 |
+
</tr>
|
| 438 |
+
<tr class="odd">
|
| 439 |
+
<td data-quarto-table-cell-role="th">4</td>
|
| 440 |
+
<td>Charlie</td>
|
| 441 |
+
<td>Brown</td>
|
| 442 |
+
<td>555-9876</td>
|
| 443 |
+
<td>charlie.brown@example.com</td>
|
| 444 |
+
<td>4.0</td>
|
| 445 |
+
</tr>
|
| 446 |
+
<tr class="even">
|
| 447 |
+
<td data-quarto-table-cell-role="th">5</td>
|
| 448 |
+
<td>Diana</td>
|
| 449 |
+
<td>Miller</td>
|
| 450 |
+
<td>555-6543</td>
|
| 451 |
+
<td>diana.miller@example.com</td>
|
| 452 |
+
<td>5.0</td>
|
| 453 |
+
</tr>
|
| 454 |
+
<tr class="odd">
|
| 455 |
+
<td data-quarto-table-cell-role="th">6</td>
|
| 456 |
+
<td>Edward</td>
|
| 457 |
+
<td>Davis</td>
|
| 458 |
+
<td>555-3456</td>
|
| 459 |
+
<td>edward.davis@example.com</td>
|
| 460 |
+
<td>6.0</td>
|
| 461 |
+
</tr>
|
| 462 |
+
<tr class="even">
|
| 463 |
+
<td data-quarto-table-cell-role="th">7</td>
|
| 464 |
+
<td>Fiona</td>
|
| 465 |
+
<td>Garcia</td>
|
| 466 |
+
<td>555-6789</td>
|
| 467 |
+
<td>fiona.garcia@example.com</td>
|
| 468 |
+
<td>7.0</td>
|
| 469 |
+
</tr>
|
| 470 |
+
<tr class="odd">
|
| 471 |
+
<td data-quarto-table-cell-role="th">8</td>
|
| 472 |
+
<td>George</td>
|
| 473 |
+
<td>Martinez</td>
|
| 474 |
+
<td>555-7890</td>
|
| 475 |
+
<td>george.martinez@example.com</td>
|
| 476 |
+
<td>8.0</td>
|
| 477 |
+
</tr>
|
| 478 |
+
<tr class="even">
|
| 479 |
+
<td data-quarto-table-cell-role="th">9</td>
|
| 480 |
+
<td>Hannah</td>
|
| 481 |
+
<td>Rodriguez</td>
|
| 482 |
+
<td>555-8901</td>
|
| 483 |
+
<td>hannah.rodriguez@example.com</td>
|
| 484 |
+
<td>9.0</td>
|
| 485 |
+
</tr>
|
| 486 |
+
<tr class="odd">
|
| 487 |
+
<td data-quarto-table-cell-role="th">10</td>
|
| 488 |
+
<td>Rahul</td>
|
| 489 |
+
<td>Saraf</td>
|
| 490 |
+
<td>7347306799</td>
|
| 491 |
+
<td>rahuketu86</td>
|
| 492 |
+
<td>10.0</td>
|
| 493 |
</tr>
|
| 494 |
</tbody>
|
| 495 |
</table>
|
|
|
|
| 498 |
</div>
|
| 499 |
</div>
|
| 500 |
</div>
|
| 501 |
+
<div id="cell-9" class="cell" data-execution_count="8">
|
| 502 |
+
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> Contact(<span class="bu">object</span>):</span>
|
| 503 |
+
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>) <span class="op">-></span> <span class="va">None</span>:</span>
|
| 504 |
+
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.refresh()</span>
|
| 505 |
+
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a></span>
|
| 506 |
+
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> search(<span class="va">self</span>, q):</span>
|
| 507 |
+
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a> predicate_firstname <span class="op">=</span> <span class="va">self</span>.db[<span class="st">'firstname'</span>].<span class="bu">str</span>.lower().<span class="bu">str</span>.contains(q.lower())</span>
|
| 508 |
+
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a> predicate_lastname <span class="op">=</span> <span class="va">self</span>.db[<span class="st">'lastname'</span>].<span class="bu">str</span>.lower().<span class="bu">str</span>.contains(q.lower())</span>
|
| 509 |
+
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a> predicate_phone <span class="op">=</span> <span class="va">self</span>.db[<span class="st">'phone'</span>].<span class="bu">str</span>.lower().<span class="bu">str</span>.contains(q.lower())</span>
|
| 510 |
+
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a> predicate_email<span class="op">=</span> <span class="va">self</span>.db[<span class="st">'email'</span>].<span class="bu">str</span>.lower().<span class="bu">str</span>.contains(q.lower())</span>
|
| 511 |
+
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a> filter_df <span class="op">=</span> <span class="va">self</span>.db[predicate_firstname<span class="op">|</span>predicate_lastname<span class="op">|</span>predicate_phone<span class="op">|</span>predicate_email]</span>
|
| 512 |
+
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> filter_df.to_dict(<span class="st">'records'</span>)</span>
|
| 513 |
+
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a> </span>
|
| 514 |
+
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> refresh(<span class="va">self</span>):</span>
|
| 515 |
+
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.file_path <span class="op">=</span> <span class="st">'contactdb.json'</span></span>
|
| 516 |
+
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.db <span class="op">=</span> pd.read_json(<span class="va">self</span>.file_path)</span>
|
| 517 |
+
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a></span>
|
| 518 |
+
<span id="cb8-17"><a href="#cb8-17" aria-hidden="true" tabindex="-1"></a></span>
|
| 519 |
+
<span id="cb8-18"><a href="#cb8-18" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> <span class="bu">all</span>(<span class="va">self</span>):</span>
|
| 520 |
+
<span id="cb8-19"><a href="#cb8-19" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="va">self</span>.db.to_dict(<span class="st">'records'</span>)</span>
|
| 521 |
+
<span id="cb8-20"><a href="#cb8-20" aria-hidden="true" tabindex="-1"></a> </span>
|
| 522 |
+
<span id="cb8-21"><a href="#cb8-21" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> get(<span class="va">self</span>, <span class="bu">id</span>):</span>
|
| 523 |
+
<span id="cb8-22"><a href="#cb8-22" aria-hidden="true" tabindex="-1"></a> contact <span class="op">=</span> <span class="va">self</span>.db[<span class="va">self</span>.db[<span class="st">'id'</span>]<span class="op">==</span><span class="bu">id</span>]</span>
|
| 524 |
+
<span id="cb8-23"><a href="#cb8-23" aria-hidden="true" tabindex="-1"></a> <span class="co"># print(contact)</span></span>
|
| 525 |
+
<span id="cb8-24"><a href="#cb8-24" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> contact.to_dict(<span class="st">'records'</span>)[<span class="dv">0</span>]</span>
|
| 526 |
+
<span id="cb8-25"><a href="#cb8-25" aria-hidden="true" tabindex="-1"></a> </span>
|
| 527 |
+
<span id="cb8-26"><a href="#cb8-26" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> add(<span class="va">self</span>, firstname, lastname, phone, email):</span>
|
| 528 |
+
<span id="cb8-27"><a href="#cb8-27" aria-hidden="true" tabindex="-1"></a> a<span class="op">=</span>{<span class="st">'firstname'</span>: firstname,</span>
|
| 529 |
+
<span id="cb8-28"><a href="#cb8-28" aria-hidden="true" tabindex="-1"></a> <span class="st">'lastname'</span>: lastname,</span>
|
| 530 |
+
<span id="cb8-29"><a href="#cb8-29" aria-hidden="true" tabindex="-1"></a> <span class="st">'phone'</span>:phone,</span>
|
| 531 |
+
<span id="cb8-30"><a href="#cb8-30" aria-hidden="true" tabindex="-1"></a> <span class="st">'email'</span>: email}</span>
|
| 532 |
+
<span id="cb8-31"><a href="#cb8-31" aria-hidden="true" tabindex="-1"></a> a[<span class="st">'id'</span>] <span class="op">=</span> <span class="bu">len</span>(<span class="va">self</span>.db)</span>
|
| 533 |
+
<span id="cb8-32"><a href="#cb8-32" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.db.loc[<span class="bu">len</span>(<span class="va">self</span>.db), a.keys()] <span class="op">=</span> a.values()</span>
|
| 534 |
+
<span id="cb8-33"><a href="#cb8-33" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.db.to_json(file_path, orient<span class="op">=</span><span class="st">'records'</span>)</span>
|
| 535 |
+
<span id="cb8-34"><a href="#cb8-34" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.refresh()</span>
|
| 536 |
+
<span id="cb8-35"><a href="#cb8-35" aria-hidden="true" tabindex="-1"></a></span>
|
| 537 |
+
<span id="cb8-36"><a href="#cb8-36" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> delete(<span class="va">self</span>, <span class="bu">id</span>):</span>
|
| 538 |
+
<span id="cb8-37"><a href="#cb8-37" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.db.drop(index<span class="op">=</span><span class="bu">id</span>, inplace<span class="op">=</span><span class="va">True</span>, errors<span class="op">=</span><span class="st">'raise'</span>)</span>
|
| 539 |
+
<span id="cb8-38"><a href="#cb8-38" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.db.to_json(file_path, orient<span class="op">=</span><span class="st">'records'</span>)</span>
|
| 540 |
+
<span id="cb8-39"><a href="#cb8-39" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.refresh()</span>
|
| 541 |
+
<span id="cb8-40"><a href="#cb8-40" aria-hidden="true" tabindex="-1"></a></span>
|
| 542 |
+
<span id="cb8-41"><a href="#cb8-41" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> edit(<span class="va">self</span>, <span class="bu">id</span>, firstname, lastname, phone, email):</span>
|
| 543 |
+
<span id="cb8-42"><a href="#cb8-42" aria-hidden="true" tabindex="-1"></a> a<span class="op">=</span>{<span class="st">'firstname'</span>: firstname,</span>
|
| 544 |
+
<span id="cb8-43"><a href="#cb8-43" aria-hidden="true" tabindex="-1"></a> <span class="st">'lastname'</span>: lastname,</span>
|
| 545 |
+
<span id="cb8-44"><a href="#cb8-44" aria-hidden="true" tabindex="-1"></a> <span class="st">'phone'</span>:phone,</span>
|
| 546 |
+
<span id="cb8-45"><a href="#cb8-45" aria-hidden="true" tabindex="-1"></a> <span class="st">'email'</span>: email,</span>
|
| 547 |
+
<span id="cb8-46"><a href="#cb8-46" aria-hidden="true" tabindex="-1"></a> <span class="st">'id'</span>: <span class="bu">id</span>}</span>
|
| 548 |
+
<span id="cb8-47"><a href="#cb8-47" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.db.loc[<span class="bu">id</span>, a.keys()] <span class="op">=</span> a.values()</span>
|
| 549 |
+
<span id="cb8-48"><a href="#cb8-48" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.db.to_json(file_path, orient<span class="op">=</span><span class="st">'records'</span>)</span>
|
| 550 |
+
<span id="cb8-49"><a href="#cb8-49" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.refresh()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 551 |
+
</div>
|
| 552 |
+
<div id="cell-10" class="cell" data-execution_count="10">
|
| 553 |
+
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>c <span class="op">=</span> Contact()</span>
|
| 554 |
+
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>c.db.loc[<span class="dv">2</span>].to_dict().values()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 555 |
+
<div class="cell-output cell-output-display" data-execution_count="10">
|
| 556 |
+
<pre><code>dict_values(['Alice', 'Johnson', '555-8765', 'alice.johnson@example.com', 2])</code></pre>
|
| 557 |
+
</div>
|
| 558 |
+
</div>
|
| 559 |
+
<div id="cell-11" class="cell" data-execution_count="11">
|
| 560 |
+
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>new_vals <span class="op">=</span> [<span class="dv">2</span>, <span class="st">'Alice'</span>, <span class="st">'Johnson'</span>, <span class="st">'555-8767'</span>, <span class="st">'alice.johnson@example.com'</span>]</span>
|
| 561 |
+
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>c.edit(<span class="op">*</span>new_vals)</span>
|
| 562 |
+
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a>c.get(<span class="dv">2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 563 |
+
<div class="cell-output cell-output-display" data-execution_count="11">
|
| 564 |
+
<pre><code>{'firstname': 'Alice',
|
| 565 |
+
'lastname': 'Johnson',
|
| 566 |
+
'phone': '555-8767',
|
| 567 |
+
'email': 'alice.johnson@example.com',
|
| 568 |
+
'id': 2}</code></pre>
|
| 569 |
+
</div>
|
| 570 |
+
</div>
|
| 571 |
+
<div id="cell-12" class="cell" data-execution_count="13">
|
| 572 |
+
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>c <span class="op">=</span> Contact()</span>
|
| 573 |
+
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>a_dict <span class="op">=</span> c.db.loc[<span class="dv">0</span>].to_dict()</span>
|
| 574 |
+
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a>a_dict.pop(<span class="st">'id'</span>, <span class="va">None</span>)<span class="op">;</span> a_dict</span>
|
| 575 |
+
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a>c.delete(<span class="dv">0</span>)</span>
|
| 576 |
+
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a></span>
|
| 577 |
+
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a>c.add(<span class="op">*</span><span class="bu">list</span>(a_dict.values()))</span>
|
| 578 |
+
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a>c.<span class="bu">all</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 579 |
+
<div class="cell-output cell-output-display" data-execution_count="13">
|
| 580 |
+
<pre><code>[{'firstname': 'Alice',
|
| 581 |
+
'lastname': 'Johnson',
|
| 582 |
+
'phone': '555-8767',
|
| 583 |
+
'email': 'alice.johnson@example.com',
|
| 584 |
+
'id': 2},
|
| 585 |
+
{'firstname': 'Bob',
|
| 586 |
+
'lastname': 'Williams',
|
| 587 |
+
'phone': '555-4321',
|
| 588 |
+
'email': 'bob.williams@example.com',
|
| 589 |
+
'id': 3},
|
| 590 |
+
{'firstname': 'Charlie',
|
| 591 |
+
'lastname': 'Brown',
|
| 592 |
+
'phone': '555-9876',
|
| 593 |
+
'email': 'charlie.brown@example.com',
|
| 594 |
+
'id': 4},
|
| 595 |
+
{'firstname': 'Diana',
|
| 596 |
+
'lastname': 'Miller',
|
| 597 |
+
'phone': '555-6543',
|
| 598 |
+
'email': 'diana.miller@example.com',
|
| 599 |
+
'id': 5},
|
| 600 |
+
{'firstname': 'Edward',
|
| 601 |
+
'lastname': 'Davis',
|
| 602 |
+
'phone': '555-3456',
|
| 603 |
+
'email': 'edward.davis@example.com',
|
| 604 |
+
'id': 6},
|
| 605 |
+
{'firstname': 'Fiona',
|
| 606 |
+
'lastname': 'Garcia',
|
| 607 |
+
'phone': '555-6789',
|
| 608 |
+
'email': 'fiona.garcia@example.com',
|
| 609 |
+
'id': 7},
|
| 610 |
+
{'firstname': 'George',
|
| 611 |
+
'lastname': 'Martinez',
|
| 612 |
+
'phone': '555-7890',
|
| 613 |
+
'email': 'george.martinez@example.com',
|
| 614 |
+
'id': 8},
|
| 615 |
+
{'firstname': 'Hannah',
|
| 616 |
+
'lastname': 'Rodriguez',
|
| 617 |
+
'phone': '555-8901',
|
| 618 |
+
'email': 'hannah.rodriguez@example.com',
|
| 619 |
+
'id': 9},
|
| 620 |
+
{'firstname': 'John',
|
| 621 |
+
'lastname': 'Doe',
|
| 622 |
+
'phone': '555-1234',
|
| 623 |
+
'email': 'john.doe@example.com',
|
| 624 |
+
'id': 9},
|
| 625 |
+
{'firstname': 'Jane',
|
| 626 |
+
'lastname': 'Smith',
|
| 627 |
+
'phone': '555-5678',
|
| 628 |
+
'email': 'jane.smith@example.com',
|
| 629 |
+
'id': 9}]</code></pre>
|
| 630 |
+
</div>
|
| 631 |
+
</div>
|
| 632 |
+
<div id="cell-13" class="cell" data-execution_count="14">
|
| 633 |
+
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>app <span class="op">=</span> Flask(<span class="va">__name__</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 634 |
+
</div>
|
| 635 |
+
<div id="cell-14" class="cell" data-execution_count="15">
|
| 636 |
+
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="at">@app.get</span>(<span class="st">"/"</span>)</span>
|
| 637 |
+
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> index():</span>
|
| 638 |
+
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> redirect(<span class="st">"/contacts"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 639 |
+
</div>
|
| 640 |
+
<div id="cell-15" class="cell" data-execution_count="16">
|
| 641 |
+
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="at">@app.get</span>(<span class="st">"/contacts"</span>)</span>
|
| 642 |
+
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> contacts():</span>
|
| 643 |
+
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a> search <span class="op">=</span> request.args.get(<span class="st">"q"</span>)</span>
|
| 644 |
+
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a> contact_set <span class="op">=</span> <span class="va">None</span></span>
|
| 645 |
+
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> search <span class="kw">is</span> <span class="kw">not</span> <span class="va">None</span>: contact_set <span class="op">=</span> Contact().search(search)</span>
|
| 646 |
+
<span id="cb17-6"><a href="#cb17-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span>: contact_set <span class="op">=</span> Contact().<span class="bu">all</span>()</span>
|
| 647 |
+
<span id="cb17-7"><a href="#cb17-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># print(contact_set)</span></span>
|
| 648 |
+
<span id="cb17-8"><a href="#cb17-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> render_template(<span class="st">"index.html"</span>, contact_set<span class="op">=</span>contact_set)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 649 |
+
</div>
|
| 650 |
+
<div id="cell-16" class="cell" data-execution_count="17">
|
| 651 |
+
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>Contact().db[df[<span class="st">'id'</span>]<span class="op">==</span><span class="dv">1</span>].to_dict(<span class="st">'records'</span>)[<span class="dv">0</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 652 |
+
<div class="cell-output cell-output-display" data-execution_count="17">
|
| 653 |
+
<pre><code>{'firstname': 'Bob',
|
| 654 |
+
'lastname': 'Williams',
|
| 655 |
+
'phone': '555-4321',
|
| 656 |
+
'email': 'bob.williams@example.com',
|
| 657 |
+
'id': 3}</code></pre>
|
| 658 |
+
</div>
|
| 659 |
+
</div>
|
| 660 |
+
<div id="cell-17" class="cell" data-execution_count="18">
|
| 661 |
+
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="at">@app.get</span>(<span class="st">"/contacts/<int:id>"</span>)</span>
|
| 662 |
+
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> view(<span class="bu">id</span>:<span class="bu">int</span>):</span>
|
| 663 |
+
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> render_template(<span class="st">"view.html"</span>, contact<span class="op">=</span>Contact().get(<span class="bu">id</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 664 |
+
</div>
|
| 665 |
+
<div id="cell-18" class="cell" data-execution_count="19">
|
| 666 |
+
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> app.app_context():</span>
|
| 667 |
+
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(view(<span class="dv">2</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 668 |
+
<div class="cell-output cell-output-stdout">
|
| 669 |
+
<pre><code><!doctype html>
|
| 670 |
+
<html>
|
| 671 |
+
<head>
|
| 672 |
+
<title>Contact.App</title>
|
| 673 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css">
|
| 674 |
+
</head>
|
| 675 |
+
<body>
|
| 676 |
+
<main class="container">
|
| 677 |
+
<h1> CONTACTS.APP</h1>
|
| 678 |
+
<h2> A demo contact application </h2>
|
| 679 |
+
<hr>
|
| 680 |
+
|
| 681 |
+
<article>
|
| 682 |
+
<h1>Alice Johnson </h1>
|
| 683 |
+
<div role="group">
|
| 684 |
+
<div>555-8767</div>
|
| 685 |
+
<div>alice.johnson@example.com</div>
|
| 686 |
+
</div>
|
| 687 |
+
<footer>
|
| 688 |
+
<a href="/contacts" role="button">Back</a>
|
| 689 |
+
</footer>
|
| 690 |
+
</article>
|
| 691 |
+
|
| 692 |
+
</main>
|
| 693 |
+
</body>
|
| 694 |
+
</html></code></pre>
|
| 695 |
+
</div>
|
| 696 |
+
</div>
|
| 697 |
+
<div id="cell-19" class="cell" data-execution_count="20">
|
| 698 |
+
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>Contact().get(<span class="dv">2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 699 |
+
<div class="cell-output cell-output-display" data-execution_count="20">
|
| 700 |
+
<pre><code>{'firstname': 'Alice',
|
| 701 |
+
'lastname': 'Johnson',
|
| 702 |
+
'phone': '555-8767',
|
| 703 |
+
'email': 'alice.johnson@example.com',
|
| 704 |
+
'id': 2}</code></pre>
|
| 705 |
+
</div>
|
| 706 |
+
</div>
|
| 707 |
+
<div id="cell-20" class="cell" data-execution_count="21">
|
| 708 |
+
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="at">@app.put</span>(<span class="st">"/contacts/<int:id>/edit"</span>)</span>
|
| 709 |
+
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> edit(<span class="bu">id</span>):</span>
|
| 710 |
+
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="st">"Edit View"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 711 |
+
</div>
|
| 712 |
+
<div id="cell-21" class="cell" data-execution_count="22">
|
| 713 |
+
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="at">@app.post</span>(<span class="st">"/contacts/new"</span>)</span>
|
| 714 |
+
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> new():</span>
|
| 715 |
+
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="st">"New View"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 716 |
+
</div>
|
| 717 |
+
<div id="cell-22" class="cell" data-execution_count="24">
|
| 718 |
+
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> nbdev.export <span class="im">import</span> nb_export</span>
|
| 719 |
+
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a>nb_export(<span class="st">"01_main.ipynb"</span>, lib_path<span class="op">=</span><span class="st">"."</span>, name<span class="op">=</span><span class="st">'main'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
| 720 |
</div>
|
| 721 |
|
| 722 |
|
_site/search.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
"href": "01_main.html",
|
| 5 |
"title": "Main App",
|
| 6 |
"section": "",
|
| 7 |
-
"text": "The autoreload extension is already loaded. To reload it, use:\n %reload_ext autoreload\n\n\n\nimport pandas as pd\nfrom flask import Flask, redirect, request\nfrom flask import render_template\nimport json\n\n\npeople = [\n {\n \"firstname\": \"John\",\n \"lastname\": \"Doe\",\n \"phone\": \"555-1234\",\n \"email\": \"john.doe@example.com\"\n },\n {\n \"firstname\": \"Jane\",\n \"lastname\": \"Smith\",\n \"phone\": \"555-5678\",\n \"email\": \"jane.smith@example.com\"\n },\n {\n \"firstname\": \"Alice\",\n \"lastname\": \"Johnson\",\n \"phone\": \"555-8765\",\n \"email\": \"alice.johnson@example.com\"\n },\n {\n \"firstname\": \"Bob\",\n \"lastname\": \"Williams\",\n \"phone\": \"555-4321\",\n \"email\": \"bob.williams@example.com\"\n },\n {\n \"firstname\": \"Charlie\",\n \"lastname\": \"Brown\",\n \"phone\": \"555-9876\",\n \"email\": \"charlie.brown@example.com\"\n },\n {\n \"firstname\": \"Diana\",\n \"lastname\": \"Miller\",\n \"phone\": \"555-6543\",\n \"email\": \"diana.miller@example.com\"\n },\n {\n \"firstname\": \"Edward\",\n \"lastname\": \"Davis\",\n \"phone\": \"555-3456\",\n \"email\": \"edward.davis@example.com\"\n },\n {\n \"firstname\": \"Fiona\",\n \"lastname\": \"Garcia\",\n \"phone\": \"555-6789\",\n \"email\": \"fiona.garcia@example.com\"\n },\n {\n \"firstname\": \"George\",\n \"lastname\": \"Martinez\",\n \"phone\": \"555-7890\",\n \"email\": \"george.martinez@example.com\"\n },\n {\n \"firstname\": \"Hannah\",\n \"lastname\": \"Rodriguez\",\n \"phone\": \"555-8901\",\n \"email\": \"hannah.rodriguez@example.com\"\n }\n]\n\n# Writing the JSON data to a file\nfile_path = 'contactdb.json'\nwith open(file_path, 'w') as json_file:\n json.dump(people, json_file, indent=4)\n\nfile_path\n\n'contactdb.json'\n\n\n\ndb = pd.read_json('contactdb.json')\ndb.to_dict('records')\n\n[{'firstname': 'John',\n 'lastname': 'Doe',\n 'phone': '555-1234',\n 'email': 'john.doe@example.com'},\n {'firstname': 'Jane',\n 'lastname': 'Smith',\n 'phone': '555-5678',\n 'email': 'jane.smith@example.com'},\n {'firstname': 'Alice',\n 'lastname': 'Johnson',\n 'phone': '555-8765',\n 'email': 'alice.johnson@example.com'},\n {'firstname': 'Bob',\n 'lastname': 'Williams',\n 'phone': '555-4321',\n 'email': 'bob.williams@example.com'},\n {'firstname': 'Charlie',\n 'lastname': 'Brown',\n 'phone': '555-9876',\n 'email': 'charlie.brown@example.com'},\n {'firstname': 'Diana',\n 'lastname': 'Miller',\n 'phone': '555-6543',\n 'email': 'diana.miller@example.com'},\n {'firstname': 'Edward',\n 'lastname': 'Davis',\n 'phone': '555-3456',\n 'email': 'edward.davis@example.com'},\n {'firstname': 'Fiona',\n 'lastname': 'Garcia',\n 'phone': '555-6789',\n 'email': 'fiona.garcia@example.com'},\n {'firstname': 'George',\n 'lastname': 'Martinez',\n 'phone': '555-7890',\n 'email': 'george.martinez@example.com'},\n {'firstname': 'Hannah',\n 'lastname': 'Rodriguez',\n 'phone': '555-8901',\n 'email': 'hannah.rodriguez@example.com'}]\n\n\n\nq = '1234'\n# db[db['firstname'].str.lower().str.contains(q)]\npredicate_firstname = db['firstname'].str.lower().str.contains(q.lower())\npredicate_lastname = db['lastname'].str.lower().str.contains(q.lower())\npredicate_phone = db['phone'].str.lower().str.contains(q.lower())\npredicate_email= db['email'].str.lower().str.contains(q.lower())\ndb[predicate_firstname|predicate_lastname|predicate_phone|predicate_email]\n\n\n\n\n\n\n\n\n\nfirstname\nlastname\nphone\nemail\n\n\n\n\n0\nJohn\nDoe\n555-1234\njohn.doe@example.com\n\n\n\n\n\n\n\n\n\nclass Contact(object):\n def __init__(self) -> None:\n self.db = pd.read_json('contactdb.json')\n\n def search(self, q):\n predicate_firstname = self.db['firstname'].str.lower().str.contains(q.lower())\n predicate_lastname = self.db['lastname'].str.lower().str.contains(q.lower())\n predicate_phone = self.db['phone'].str.lower().str.contains(q.lower())\n predicate_email= self.db['email'].str.lower().str.contains(q.lower())\n filter_df = self.db[predicate_firstname|predicate_lastname|predicate_phone|predicate_email]\n return filter_df.to_dict('records')\n\n def all(self):\n return self.db.to_dict('records')\n\n\napp = Flask(__name__)\n\n\n@app.get(\"/\")\ndef index():\n return redirect(\"/contacts\")\n\n\n@app.get(\"/contacts\")\ndef contacts():\n search = request.args.get(\"q\")\n contact_set = None\n if search is not None: contact_set = Contact().search(search)\n else: contact_set = Contact().all()\n # print(contact_set)\n return render_template(\"index.html\", contact_set=contact_set)\n\n\nfrom nbdev.export import nb_export\nnb_export(\"01_main.ipynb\", lib_path=\".\", name='main')",
|
| 8 |
"crumbs": [
|
| 9 |
"Main App"
|
| 10 |
]
|
|
|
|
| 4 |
"href": "01_main.html",
|
| 5 |
"title": "Main App",
|
| 6 |
"section": "",
|
| 7 |
+
"text": "import pandas as pd\nfrom flask import Flask, redirect, request, render_template\nimport json\nfrom IPython.display import display_html\n\n\npeople = [\n {\n \"firstname\": \"John\",\n \"lastname\": \"Doe\",\n \"phone\": \"555-1234\",\n \"email\": \"john.doe@example.com\"\n },\n {\n \"firstname\": \"Jane\",\n \"lastname\": \"Smith\",\n \"phone\": \"555-5678\",\n \"email\": \"jane.smith@example.com\"\n },\n {\n \"firstname\": \"Alice\",\n \"lastname\": \"Johnson\",\n \"phone\": \"555-8765\",\n \"email\": \"alice.johnson@example.com\"\n },\n {\n \"firstname\": \"Bob\",\n \"lastname\": \"Williams\",\n \"phone\": \"555-4321\",\n \"email\": \"bob.williams@example.com\"\n },\n {\n \"firstname\": \"Charlie\",\n \"lastname\": \"Brown\",\n \"phone\": \"555-9876\",\n \"email\": \"charlie.brown@example.com\"\n },\n {\n \"firstname\": \"Diana\",\n \"lastname\": \"Miller\",\n \"phone\": \"555-6543\",\n \"email\": \"diana.miller@example.com\"\n },\n {\n \"firstname\": \"Edward\",\n \"lastname\": \"Davis\",\n \"phone\": \"555-3456\",\n \"email\": \"edward.davis@example.com\"\n },\n {\n \"firstname\": \"Fiona\",\n \"lastname\": \"Garcia\",\n \"phone\": \"555-6789\",\n \"email\": \"fiona.garcia@example.com\"\n },\n {\n \"firstname\": \"George\",\n \"lastname\": \"Martinez\",\n \"phone\": \"555-7890\",\n \"email\": \"george.martinez@example.com\"\n },\n {\n \"firstname\": \"Hannah\",\n \"lastname\": \"Rodriguez\",\n \"phone\": \"555-8901\",\n \"email\": \"hannah.rodriguez@example.com\"\n }\n]\n\n# Writing the JSON data to a file\nfile_path = 'contactdb.json'\n# df = pd.read_json(file_path)\ndf = pd.DataFrame()\ndf = df.from_dict(people)\ndf['id'] = df.index\ndf.to_json(file_path, orient='records')\n\n\ndb = pd.read_json('contactdb.json')\ndb.to_dict('records')\n\n[{'firstname': 'John',\n 'lastname': 'Doe',\n 'phone': '555-1234',\n 'email': 'john.doe@example.com',\n 'id': 0},\n {'firstname': 'Jane',\n 'lastname': 'Smith',\n 'phone': '555-5678',\n 'email': 'jane.smith@example.com',\n 'id': 1},\n {'firstname': 'Alice',\n 'lastname': 'Johnson',\n 'phone': '555-8765',\n 'email': 'alice.johnson@example.com',\n 'id': 2},\n {'firstname': 'Bob',\n 'lastname': 'Williams',\n 'phone': '555-4321',\n 'email': 'bob.williams@example.com',\n 'id': 3},\n {'firstname': 'Charlie',\n 'lastname': 'Brown',\n 'phone': '555-9876',\n 'email': 'charlie.brown@example.com',\n 'id': 4},\n {'firstname': 'Diana',\n 'lastname': 'Miller',\n 'phone': '555-6543',\n 'email': 'diana.miller@example.com',\n 'id': 5},\n {'firstname': 'Edward',\n 'lastname': 'Davis',\n 'phone': '555-3456',\n 'email': 'edward.davis@example.com',\n 'id': 6},\n {'firstname': 'Fiona',\n 'lastname': 'Garcia',\n 'phone': '555-6789',\n 'email': 'fiona.garcia@example.com',\n 'id': 7},\n {'firstname': 'George',\n 'lastname': 'Martinez',\n 'phone': '555-7890',\n 'email': 'george.martinez@example.com',\n 'id': 8},\n {'firstname': 'Hannah',\n 'lastname': 'Rodriguez',\n 'phone': '555-8901',\n 'email': 'hannah.rodriguez@example.com',\n 'id': 9}]\n\n\n\nq = '1234'\n# db[db['firstname'].str.lower().str.contains(q)]\npredicate_firstname = db['firstname'].str.lower().str.contains(q.lower())\npredicate_lastname = db['lastname'].str.lower().str.contains(q.lower())\npredicate_phone = db['phone'].str.lower().str.contains(q.lower())\npredicate_email= db['email'].str.lower().str.contains(q.lower())\ndb[predicate_firstname|predicate_lastname|predicate_phone|predicate_email]\n\n\n\n\n\n\n\n\n\nfirstname\nlastname\nphone\nemail\nid\n\n\n\n\n0\nJohn\nDoe\n555-1234\njohn.doe@example.com\n0\n\n\n\n\n\n\n\n\n\ndb.columns\n\na={'firstname': 'Rahul',\n 'lastname': 'Saraf',\n 'phone':'7347306799',\n 'email': 'rahuketu86'}\na['id'] = len(db)\n# db.loc[len(db)] = \na\ndb.loc[len(db), a.keys()] = a.values()\n\n\ndb\n\n\n\n\n\n\n\n\n\nfirstname\nlastname\nphone\nemail\nid\n\n\n\n\n0\nJohn\nDoe\n555-1234\njohn.doe@example.com\n0.0\n\n\n1\nJane\nSmith\n555-5678\njane.smith@example.com\n1.0\n\n\n2\nAlice\nJohnson\n555-8765\nalice.johnson@example.com\n2.0\n\n\n3\nBob\nWilliams\n555-4321\nbob.williams@example.com\n3.0\n\n\n4\nCharlie\nBrown\n555-9876\ncharlie.brown@example.com\n4.0\n\n\n5\nDiana\nMiller\n555-6543\ndiana.miller@example.com\n5.0\n\n\n6\nEdward\nDavis\n555-3456\nedward.davis@example.com\n6.0\n\n\n7\nFiona\nGarcia\n555-6789\nfiona.garcia@example.com\n7.0\n\n\n8\nGeorge\nMartinez\n555-7890\ngeorge.martinez@example.com\n8.0\n\n\n9\nHannah\nRodriguez\n555-8901\nhannah.rodriguez@example.com\n9.0\n\n\n10\nRahul\nSaraf\n7347306799\nrahuketu86\n10.0\n\n\n\n\n\n\n\n\n\nclass Contact(object):\n def __init__(self) -> None:\n self.refresh()\n\n def search(self, q):\n predicate_firstname = self.db['firstname'].str.lower().str.contains(q.lower())\n predicate_lastname = self.db['lastname'].str.lower().str.contains(q.lower())\n predicate_phone = self.db['phone'].str.lower().str.contains(q.lower())\n predicate_email= self.db['email'].str.lower().str.contains(q.lower())\n filter_df = self.db[predicate_firstname|predicate_lastname|predicate_phone|predicate_email]\n return filter_df.to_dict('records')\n \n def refresh(self):\n self.file_path = 'contactdb.json'\n self.db = pd.read_json(self.file_path)\n\n\n def all(self):\n return self.db.to_dict('records')\n \n def get(self, id):\n contact = self.db[self.db['id']==id]\n # print(contact)\n return contact.to_dict('records')[0]\n \n def add(self, firstname, lastname, phone, email):\n a={'firstname': firstname,\n 'lastname': lastname,\n 'phone':phone,\n 'email': email}\n a['id'] = len(self.db)\n self.db.loc[len(self.db), a.keys()] = a.values()\n self.db.to_json(file_path, orient='records')\n self.refresh()\n\n def delete(self, id):\n self.db.drop(index=id, inplace=True, errors='raise')\n self.db.to_json(file_path, orient='records')\n self.refresh()\n\n def edit(self, id, firstname, lastname, phone, email):\n a={'firstname': firstname,\n 'lastname': lastname,\n 'phone':phone,\n 'email': email,\n 'id': id}\n self.db.loc[id, a.keys()] = a.values()\n self.db.to_json(file_path, orient='records')\n self.refresh()\n\n\nc = Contact()\nc.db.loc[2].to_dict().values()\n\ndict_values(['Alice', 'Johnson', '555-8765', 'alice.johnson@example.com', 2])\n\n\n\nnew_vals = [2, 'Alice', 'Johnson', '555-8767', 'alice.johnson@example.com']\nc.edit(*new_vals)\nc.get(2)\n\n{'firstname': 'Alice',\n 'lastname': 'Johnson',\n 'phone': '555-8767',\n 'email': 'alice.johnson@example.com',\n 'id': 2}\n\n\n\nc = Contact()\na_dict = c.db.loc[0].to_dict()\na_dict.pop('id', None); a_dict\nc.delete(0)\n\nc.add(*list(a_dict.values()))\nc.all()\n\n[{'firstname': 'Alice',\n 'lastname': 'Johnson',\n 'phone': '555-8767',\n 'email': 'alice.johnson@example.com',\n 'id': 2},\n {'firstname': 'Bob',\n 'lastname': 'Williams',\n 'phone': '555-4321',\n 'email': 'bob.williams@example.com',\n 'id': 3},\n {'firstname': 'Charlie',\n 'lastname': 'Brown',\n 'phone': '555-9876',\n 'email': 'charlie.brown@example.com',\n 'id': 4},\n {'firstname': 'Diana',\n 'lastname': 'Miller',\n 'phone': '555-6543',\n 'email': 'diana.miller@example.com',\n 'id': 5},\n {'firstname': 'Edward',\n 'lastname': 'Davis',\n 'phone': '555-3456',\n 'email': 'edward.davis@example.com',\n 'id': 6},\n {'firstname': 'Fiona',\n 'lastname': 'Garcia',\n 'phone': '555-6789',\n 'email': 'fiona.garcia@example.com',\n 'id': 7},\n {'firstname': 'George',\n 'lastname': 'Martinez',\n 'phone': '555-7890',\n 'email': 'george.martinez@example.com',\n 'id': 8},\n {'firstname': 'Hannah',\n 'lastname': 'Rodriguez',\n 'phone': '555-8901',\n 'email': 'hannah.rodriguez@example.com',\n 'id': 9},\n {'firstname': 'John',\n 'lastname': 'Doe',\n 'phone': '555-1234',\n 'email': 'john.doe@example.com',\n 'id': 9},\n {'firstname': 'Jane',\n 'lastname': 'Smith',\n 'phone': '555-5678',\n 'email': 'jane.smith@example.com',\n 'id': 9}]\n\n\n\napp = Flask(__name__)\n\n\n@app.get(\"/\")\ndef index():\n return redirect(\"/contacts\")\n\n\n@app.get(\"/contacts\")\ndef contacts():\n search = request.args.get(\"q\")\n contact_set = None\n if search is not None: contact_set = Contact().search(search)\n else: contact_set = Contact().all()\n # print(contact_set)\n return render_template(\"index.html\", contact_set=contact_set)\n\n\nContact().db[df['id']==1].to_dict('records')[0]\n\n{'firstname': 'Bob',\n 'lastname': 'Williams',\n 'phone': '555-4321',\n 'email': 'bob.williams@example.com',\n 'id': 3}\n\n\n\n@app.get(\"/contacts/<int:id>\")\ndef view(id:int):\n return render_template(\"view.html\", contact=Contact().get(id))\n\n\nwith app.app_context():\n print(view(2))\n\n<!doctype html>\n<html>\n <head>\n <title>Contact.App</title>\n <link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css\">\n </head>\n <body>\n <main class=\"container\">\n <h1> CONTACTS.APP</h1>\n <h2> A demo contact application </h2>\n <hr>\n \n <article>\n <h1>Alice Johnson </h1>\n <div role=\"group\">\n <div>555-8767</div>\n <div>alice.johnson@example.com</div>\n </div>\n <footer>\n <a href=\"/contacts\" role=\"button\">Back</a>\n </footer>\n </article>\n\n </main>\n </body>\n</html>\n\n\n\nContact().get(2)\n\n{'firstname': 'Alice',\n 'lastname': 'Johnson',\n 'phone': '555-8767',\n 'email': 'alice.johnson@example.com',\n 'id': 2}\n\n\n\n@app.put(\"/contacts/<int:id>/edit\")\ndef edit(id):\n return \"Edit View\"\n\n\n@app.post(\"/contacts/new\")\ndef new():\n return \"New View\"\n\n\nfrom nbdev.export import nb_export\nnb_export(\"01_main.ipynb\", lib_path=\".\", name='main')",
|
| 8 |
"crumbs": [
|
| 9 |
"Main App"
|
| 10 |
]
|
contactdb.json
CHANGED
|
@@ -1,62 +1 @@
|
|
| 1 |
-
[
|
| 2 |
-
{
|
| 3 |
-
"firstname": "John",
|
| 4 |
-
"lastname": "Doe",
|
| 5 |
-
"phone": "555-1234",
|
| 6 |
-
"email": "john.doe@example.com"
|
| 7 |
-
},
|
| 8 |
-
{
|
| 9 |
-
"firstname": "Jane",
|
| 10 |
-
"lastname": "Smith",
|
| 11 |
-
"phone": "555-5678",
|
| 12 |
-
"email": "jane.smith@example.com"
|
| 13 |
-
},
|
| 14 |
-
{
|
| 15 |
-
"firstname": "Alice",
|
| 16 |
-
"lastname": "Johnson",
|
| 17 |
-
"phone": "555-8765",
|
| 18 |
-
"email": "alice.johnson@example.com"
|
| 19 |
-
},
|
| 20 |
-
{
|
| 21 |
-
"firstname": "Bob",
|
| 22 |
-
"lastname": "Williams",
|
| 23 |
-
"phone": "555-4321",
|
| 24 |
-
"email": "bob.williams@example.com"
|
| 25 |
-
},
|
| 26 |
-
{
|
| 27 |
-
"firstname": "Charlie",
|
| 28 |
-
"lastname": "Brown",
|
| 29 |
-
"phone": "555-9876",
|
| 30 |
-
"email": "charlie.brown@example.com"
|
| 31 |
-
},
|
| 32 |
-
{
|
| 33 |
-
"firstname": "Diana",
|
| 34 |
-
"lastname": "Miller",
|
| 35 |
-
"phone": "555-6543",
|
| 36 |
-
"email": "diana.miller@example.com"
|
| 37 |
-
},
|
| 38 |
-
{
|
| 39 |
-
"firstname": "Edward",
|
| 40 |
-
"lastname": "Davis",
|
| 41 |
-
"phone": "555-3456",
|
| 42 |
-
"email": "edward.davis@example.com"
|
| 43 |
-
},
|
| 44 |
-
{
|
| 45 |
-
"firstname": "Fiona",
|
| 46 |
-
"lastname": "Garcia",
|
| 47 |
-
"phone": "555-6789",
|
| 48 |
-
"email": "fiona.garcia@example.com"
|
| 49 |
-
},
|
| 50 |
-
{
|
| 51 |
-
"firstname": "George",
|
| 52 |
-
"lastname": "Martinez",
|
| 53 |
-
"phone": "555-7890",
|
| 54 |
-
"email": "george.martinez@example.com"
|
| 55 |
-
},
|
| 56 |
-
{
|
| 57 |
-
"firstname": "Hannah",
|
| 58 |
-
"lastname": "Rodriguez",
|
| 59 |
-
"phone": "555-8901",
|
| 60 |
-
"email": "hannah.rodriguez@example.com"
|
| 61 |
-
}
|
| 62 |
-
]
|
|
|
|
| 1 |
+
[{"firstname":"Alice","lastname":"Johnson","phone":"555-8767","email":"alice.johnson@example.com","id":2.0},{"firstname":"Bob","lastname":"Williams","phone":"555-4321","email":"bob.williams@example.com","id":3.0},{"firstname":"Charlie","lastname":"Brown","phone":"555-9876","email":"charlie.brown@example.com","id":4.0},{"firstname":"Diana","lastname":"Miller","phone":"555-6543","email":"diana.miller@example.com","id":5.0},{"firstname":"Edward","lastname":"Davis","phone":"555-3456","email":"edward.davis@example.com","id":6.0},{"firstname":"Fiona","lastname":"Garcia","phone":"555-6789","email":"fiona.garcia@example.com","id":7.0},{"firstname":"George","lastname":"Martinez","phone":"555-7890","email":"george.martinez@example.com","id":8.0},{"firstname":"Hannah","lastname":"Rodriguez","phone":"555-8901","email":"hannah.rodriguez@example.com","id":9.0},{"firstname":"John","lastname":"Doe","phone":"555-1234","email":"john.doe@example.com","id":9.0},{"firstname":"Jane","lastname":"Smith","phone":"555-5678","email":"jane.smith@example.com","id":9.0}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main.py
CHANGED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
# AUTOGENERATED! DO NOT EDIT! File to edit: 01_main.ipynb.
|
| 2 |
|
| 3 |
# %% auto 0
|
| 4 |
-
__all__ = ['app', 'Contact', 'index', 'contacts']
|
| 5 |
|
| 6 |
# %% 01_main.ipynb 2
|
| 7 |
import pandas as pd
|
| 8 |
-
from flask import Flask, redirect, request
|
| 9 |
-
from flask import render_template
|
| 10 |
import json
|
|
|
|
| 11 |
|
| 12 |
-
# %% 01_main.ipynb
|
| 13 |
class Contact(object):
|
| 14 |
def __init__(self) -> None:
|
| 15 |
-
self.
|
| 16 |
|
| 17 |
def search(self, q):
|
| 18 |
predicate_firstname = self.db['firstname'].str.lower().str.contains(q.lower())
|
|
@@ -21,19 +21,55 @@ class Contact(object):
|
|
| 21 |
predicate_email= self.db['email'].str.lower().str.contains(q.lower())
|
| 22 |
filter_df = self.db[predicate_firstname|predicate_lastname|predicate_phone|predicate_email]
|
| 23 |
return filter_df.to_dict('records')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def all(self):
|
| 26 |
return self.db.to_dict('records')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# %% 01_main.ipynb
|
| 29 |
app = Flask(__name__)
|
| 30 |
|
| 31 |
-
# %% 01_main.ipynb
|
| 32 |
@app.get("/")
|
| 33 |
def index():
|
| 34 |
return redirect("/contacts")
|
| 35 |
|
| 36 |
-
# %% 01_main.ipynb
|
| 37 |
@app.get("/contacts")
|
| 38 |
def contacts():
|
| 39 |
search = request.args.get("q")
|
|
@@ -42,3 +78,18 @@ def contacts():
|
|
| 42 |
else: contact_set = Contact().all()
|
| 43 |
# print(contact_set)
|
| 44 |
return render_template("index.html", contact_set=contact_set)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# AUTOGENERATED! DO NOT EDIT! File to edit: 01_main.ipynb.
|
| 2 |
|
| 3 |
# %% auto 0
|
| 4 |
+
__all__ = ['app', 'Contact', 'index', 'contacts', 'view', 'edit', 'new']
|
| 5 |
|
| 6 |
# %% 01_main.ipynb 2
|
| 7 |
import pandas as pd
|
| 8 |
+
from flask import Flask, redirect, request, render_template
|
|
|
|
| 9 |
import json
|
| 10 |
+
from IPython.display import display_html
|
| 11 |
|
| 12 |
+
# %% 01_main.ipynb 8
|
| 13 |
class Contact(object):
|
| 14 |
def __init__(self) -> None:
|
| 15 |
+
self.refresh()
|
| 16 |
|
| 17 |
def search(self, q):
|
| 18 |
predicate_firstname = self.db['firstname'].str.lower().str.contains(q.lower())
|
|
|
|
| 21 |
predicate_email= self.db['email'].str.lower().str.contains(q.lower())
|
| 22 |
filter_df = self.db[predicate_firstname|predicate_lastname|predicate_phone|predicate_email]
|
| 23 |
return filter_df.to_dict('records')
|
| 24 |
+
|
| 25 |
+
def refresh(self):
|
| 26 |
+
self.file_path = 'contactdb.json'
|
| 27 |
+
self.db = pd.read_json(self.file_path)
|
| 28 |
+
|
| 29 |
|
| 30 |
def all(self):
|
| 31 |
return self.db.to_dict('records')
|
| 32 |
+
|
| 33 |
+
def get(self, id):
|
| 34 |
+
contact = self.db[self.db['id']==id]
|
| 35 |
+
# print(contact)
|
| 36 |
+
return contact.to_dict('records')[0]
|
| 37 |
+
|
| 38 |
+
def add(self, firstname, lastname, phone, email):
|
| 39 |
+
a={'firstname': firstname,
|
| 40 |
+
'lastname': lastname,
|
| 41 |
+
'phone':phone,
|
| 42 |
+
'email': email}
|
| 43 |
+
a['id'] = len(self.db)
|
| 44 |
+
self.db.loc[len(self.db), a.keys()] = a.values()
|
| 45 |
+
self.db.to_json(file_path, orient='records')
|
| 46 |
+
self.refresh()
|
| 47 |
+
|
| 48 |
+
def delete(self, id):
|
| 49 |
+
self.db.drop(index=id, inplace=True, errors='raise')
|
| 50 |
+
self.db.to_json(file_path, orient='records')
|
| 51 |
+
self.refresh()
|
| 52 |
+
|
| 53 |
+
def edit(self, id, firstname, lastname, phone, email):
|
| 54 |
+
a={'firstname': firstname,
|
| 55 |
+
'lastname': lastname,
|
| 56 |
+
'phone':phone,
|
| 57 |
+
'email': email,
|
| 58 |
+
'id': id}
|
| 59 |
+
self.db.loc[id, a.keys()] = a.values()
|
| 60 |
+
self.db.to_json(file_path, orient='records')
|
| 61 |
+
self.refresh()
|
| 62 |
+
|
| 63 |
|
| 64 |
+
# %% 01_main.ipynb 12
|
| 65 |
app = Flask(__name__)
|
| 66 |
|
| 67 |
+
# %% 01_main.ipynb 13
|
| 68 |
@app.get("/")
|
| 69 |
def index():
|
| 70 |
return redirect("/contacts")
|
| 71 |
|
| 72 |
+
# %% 01_main.ipynb 14
|
| 73 |
@app.get("/contacts")
|
| 74 |
def contacts():
|
| 75 |
search = request.args.get("q")
|
|
|
|
| 78 |
else: contact_set = Contact().all()
|
| 79 |
# print(contact_set)
|
| 80 |
return render_template("index.html", contact_set=contact_set)
|
| 81 |
+
|
| 82 |
+
# %% 01_main.ipynb 16
|
| 83 |
+
@app.get("/contacts/<int:id>")
|
| 84 |
+
def view(id:int):
|
| 85 |
+
return render_template("view.html", contact=Contact().get(id))
|
| 86 |
+
|
| 87 |
+
# %% 01_main.ipynb 19
|
| 88 |
+
@app.put("/contacts/<int:id>/edit")
|
| 89 |
+
def edit(id):
|
| 90 |
+
return "Edit View"
|
| 91 |
+
|
| 92 |
+
# %% 01_main.ipynb 20
|
| 93 |
+
@app.post("/contacts/new")
|
| 94 |
+
def new():
|
| 95 |
+
return "New View"
|
templates/index.html
CHANGED
|
@@ -2,6 +2,12 @@
|
|
| 2 |
|
| 3 |
{% block content %}
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
<table class="striped">
|
| 6 |
<thead>
|
| 7 |
<tr>
|
|
@@ -9,6 +15,7 @@
|
|
| 9 |
<th>Last Name</th>
|
| 10 |
<th>Phone</th>
|
| 11 |
<th>Email</th>
|
|
|
|
| 12 |
</tr>
|
| 13 |
</thead>
|
| 14 |
{% for contact in contact_set %}
|
|
@@ -17,10 +24,16 @@
|
|
| 17 |
<td>{{contact['lastname']}}</td>
|
| 18 |
<td>{{contact['phone']}}</td>
|
| 19 |
<td>{{contact['email']}}</td>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
</tr>
|
| 21 |
{% endfor %}
|
| 22 |
-
|
| 23 |
-
|
| 24 |
</table>
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
{% endblock %}
|
|
|
|
| 2 |
|
| 3 |
{% block content %}
|
| 4 |
|
| 5 |
+
<form action="/contacts" method="get">
|
| 6 |
+
<fieldset role="search">
|
| 7 |
+
<input id="search" type="search" name="q" value="{{ request.args.get('q') or '' }}" />
|
| 8 |
+
<input type="submit" value="Search"/>
|
| 9 |
+
</fieldset>
|
| 10 |
+
</form>
|
| 11 |
<table class="striped">
|
| 12 |
<thead>
|
| 13 |
<tr>
|
|
|
|
| 15 |
<th>Last Name</th>
|
| 16 |
<th>Phone</th>
|
| 17 |
<th>Email</th>
|
| 18 |
+
<th>Controls</th>
|
| 19 |
</tr>
|
| 20 |
</thead>
|
| 21 |
{% for contact in contact_set %}
|
|
|
|
| 24 |
<td>{{contact['lastname']}}</td>
|
| 25 |
<td>{{contact['phone']}}</td>
|
| 26 |
<td>{{contact['email']}}</td>
|
| 27 |
+
<td>
|
| 28 |
+
<div role="group">
|
| 29 |
+
<a href="/contacts/{{contact.id}}/edit" role="button">Edit</a>
|
| 30 |
+
<a href="/contacts/{{contact.id}}" role="button">View</a>
|
| 31 |
+
</div>
|
| 32 |
+
</td>
|
| 33 |
</tr>
|
| 34 |
{% endfor %}
|
|
|
|
|
|
|
| 35 |
</table>
|
| 36 |
+
<p>
|
| 37 |
+
<a href="/contacts/new" role="button">Add contact</a>
|
| 38 |
+
</p>
|
| 39 |
{% endblock %}
|
templates/layout.html
CHANGED
|
@@ -6,6 +6,9 @@
|
|
| 6 |
</head>
|
| 7 |
<body>
|
| 8 |
<main class="container">
|
|
|
|
|
|
|
|
|
|
| 9 |
{% block content %}{% endblock %}
|
| 10 |
</main>
|
| 11 |
</body>
|
|
|
|
| 6 |
</head>
|
| 7 |
<body>
|
| 8 |
<main class="container">
|
| 9 |
+
<h1> CONTACTS.APP</h1>
|
| 10 |
+
<h2> A demo contact application </h2>
|
| 11 |
+
<hr>
|
| 12 |
{% block content %}{% endblock %}
|
| 13 |
</main>
|
| 14 |
</body>
|
templates/view.html
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{% extends "layout.html" %}
|
| 2 |
+
|
| 3 |
+
{% block content %}
|
| 4 |
+
<article>
|
| 5 |
+
<h1>{{contact['firstname']}} {{contact['lastname']}} </h1>
|
| 6 |
+
<div role="group">
|
| 7 |
+
<div>{{contact['phone']}}</div>
|
| 8 |
+
<div>{{contact['email']}}</div>
|
| 9 |
+
</div>
|
| 10 |
+
<footer>
|
| 11 |
+
<a href="/contacts" role="button">Back</a>
|
| 12 |
+
</footer>
|
| 13 |
+
</article>
|
| 14 |
+
{% endblock %}
|