├── README.md ├── S05-App-1-Build-an-Interactive-Dictionary.ipynb ├── S06-Data-Analysis-with-Pandas.ipynb ├── S06-Working.ipynb ├── S07-Numpy.ipynb ├── S08-App-2-Create-Webmaps-with-Python-and-Folium.ipynb ├── S09-App-3-Build-a-Website-Blocker.ipynb ├── S10-App-4-Build-a-Website-with-Python-and-Flask.ipynb ├── S11-Graphical-User-Interfaces-with-Tkinter.ipynb ├── S12-Python-for-Interacting-with-SQLite-and-PostGreSQL-Databases.ipynb ├── S13-App-5-Build-a-Desktop-Database-Application.ipynb ├── S14-Object-Oriented-Programming.ipynb ├── S15-Python-for-Image-and-Video-Processing-with-OpenCV.ipynb ├── S16-App-6-Build-a-Webcam-Motion-Detector.ipynb ├── S17-Interactive-Data-Visualization-with-Python.ipynb ├── S18-Webscraping-with-BeautifulSoup.ipynb ├── S19-App-7-Scrape-Real-Estate-Property-Data-from-the-Web.ipynb ├── S19-App-7-Scrape-Real-Estate-Property-Data-from-the-Web_pyclass.ipynb ├── S20-App-8-Build-a-Web-Based-Financial-Graph.ipynb ├── S21-App-9-Build-a-Data-Collector-Web-App-with-PostGreSQL-and-Flask.ipynb ├── S22-App-10-Student-Project-on-Building-a-Geocoder-Web-Service.ipynb ├── Scatter_plotting.html ├── data ├── Galaxy_resized.jpg ├── Map1.html ├── Volcanoes.txt ├── balance.txt ├── books.db ├── circle_markers.png ├── data.json ├── galaxy.jpg ├── images │ ├── .DS_Store │ ├── Lighthouse.jpg │ ├── Moon sinking, sun rising.jpg │ ├── galaxy.jpg │ └── kangaroos-rain-australia_71370_990x742.jpg ├── lecture_link_demo.gif ├── lite.db ├── newlite.db ├── news.jpg ├── newsmallgray.png ├── smallgray.png ├── supermarkets-commas.txt ├── supermarkets-semi-colons.txt ├── supermarkets.csv ├── supermarkets.json ├── supermarkets.xlsx └── world.json ├── scratch.html ├── scratch1.py ├── smallgray.png ├── supermarkets-commas.txt ├── supermarkets-semi-colons.txt ├── supermarkets.csv ├── supermarkets.json └── supermarkets.xlsx /README.md: -------------------------------------------------------------------------------- 1 | # What are these? 2 | 3 | This repository contains all the source code of The Python Mega Course, a comprehensive Python course authored by Ardit Sulce. 4 | 5 | If you're not a member of the course, you can become one for just a one-time payment of $15 using the discount link below: [https://www.udemy.com/the-python-mega-course/?couponCode=GITREADSECTION](https://www.udemy.com/the-python-mega-course/?couponCode=GITREADSECTION) 6 | 7 | If you're not a member you can still use these Jupyter notebooks, but you won't be able to see the video lectures and you won't be able to ask questions to the instructor inside the course. 8 | 9 | ### How to use these notebooks? 10 | 11 | There's one notebook for each section of the coures. Simply click the notebook you want, locate the lecture, and copy the code. Or if you know how to use Jupyter notebooks, then use the green download button further above to download all the notebooks and open them in your browser. 12 | 13 | Clicking a lecture opens the corresponding video lecture on Udemy: 14 | ![Lectures are clickable](data/lecture_link_demo.gif) 15 | 16 | ### What are the prerequisites to use these? 17 | 18 | To be able to run the Python code shown in these notebooks you should have Python 3 installed in your computer. If the code uses a third party library that is mentioned in the notebooks. 19 | 20 | ### Author 21 | Ardit Sulce 22 | -------------------------------------------------------------------------------- /S05-App-1-Build-an-Interactive-Dictionary.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python Mega Course: Build 10 Real World Applications\n", 8 | "---" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "This notebook contains the source code for the video lectures of Section 5 of [The Python Mega Course: Build 10 Real World Applciations](https://www.udemy.com/the-python-mega-course/?couponCode=GITHEADSECTION)." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "# Section 5: Application 1: Build an Interactive Dictionary\n", 23 | "***" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "collapsed": true 30 | }, 31 | "source": [ 32 | "**Lecture:** [Demonstration of the Interactive Dictionary](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/7627770?start=0)\n", 33 | "---" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "This project consists of building an English dictionary where users can submit a word and the program returns the definition of the word. " 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "Download [here](http://pythonhow.com/data/python-mega-course-data/app1/) the data needed for this application" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": { 53 | "collapsed": true 54 | }, 55 | "source": [ 56 | "**Lecture:** [The Data Source](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/7627808?start=0)\n", 57 | "---" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "The data used for this project is a json dataset that contains an English dictionary. " 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": { 70 | "collapsed": true 71 | }, 72 | "source": [ 73 | "**Lecture:** [Loading the JSON Data into a Python Dictionary](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/7627840?start=0)\n", 74 | "---" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 1, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "data": { 84 | "text/plain": [ 85 | "dict" 86 | ] 87 | }, 88 | "execution_count": 1, 89 | "metadata": {}, 90 | "output_type": "execute_result" 91 | } 92 | ], 93 | "source": [ 94 | "import json\n", 95 | "data = json.load(open(\"data.json\"))\n", 96 | "type(data)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": { 102 | "collapsed": true 103 | }, 104 | "source": [ 105 | "**Lecture:** [Returning the Definition of a Word](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/7627840?start=0)\n", 106 | "---" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 3, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "Enter word: rain\n", 119 | "['Precipitation in the form of liquid water drops with diameters greater than 0.5 millimetres.', 'To fall from the clouds in drops of water.']\n" 120 | ] 121 | } 122 | ], 123 | "source": [ 124 | "import json\n", 125 | "\n", 126 | "data = json.load(open(\"data.json\"))\n", 127 | "def translate(w):\n", 128 | " return data[w]\n", 129 | "word = input(\"Enter word: \")\n", 130 | "print(translate(word))" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": { 136 | "collapsed": true 137 | }, 138 | "source": [ 139 | "**Lecture:** [Counting for Non-existing Words](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/7627870?start=0)\n", 140 | "---" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 1, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "Enter word: rainn\n", 153 | "The word doesn't exist. Please double check it.\n" 154 | ] 155 | } 156 | ], 157 | "source": [ 158 | "import json\n", 159 | "\n", 160 | "data = json.load(open(\"data.json\"))\n", 161 | "def translate(w):\n", 162 | " if w in data:\n", 163 | " return data[w]\n", 164 | " else:\n", 165 | " return \"The word doesn't exist. Please double check it.\"\n", 166 | "word = input(\"Enter word: \")\n", 167 | "print(translate(word))" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": { 173 | "collapsed": true 174 | }, 175 | "source": [ 176 | "**Lecture:** [Making the Program Letter Case Insensitive]()\n", 177 | "---" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 2, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "Enter word: RAIn\n", 190 | "['Precipitation in the form of liquid water drops with diameters greater than 0.5 millimetres.', 'To fall from the clouds in drops of water.']\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "import json\n", 196 | "\n", 197 | "data = json.load(open(\"data.json\"))\n", 198 | "def translate(w):\n", 199 | " w = w.lower()\n", 200 | " if w in data:\n", 201 | " return data[w]\n", 202 | " else:\n", 203 | " return \"The word doesn't exist. Please double check it.\"\n", 204 | "word = input(\"Enter word: \")\n", 205 | "print(translate(word))" 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": { 211 | "collapsed": true 212 | }, 213 | "source": [ 214 | "**Lecture:** [Calculating Similarity Ratio Between Two Words](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/7627914?start=0)\n", 215 | "---" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 4, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "data": { 225 | "text/plain": [ 226 | "0.8888888888888888" 227 | ] 228 | }, 229 | "execution_count": 4, 230 | "metadata": {}, 231 | "output_type": "execute_result" 232 | } 233 | ], 234 | "source": [ 235 | "#this is just testing. It's not part of the application code\n", 236 | "#returns the similarity ratio two words\n", 237 | "from difflib import SequenceMatcher\n", 238 | "SequenceMatcher(None, \"rain\", \"rainn\").ratio()" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "You can interpret the above number as rain with rainn are 88% similar." 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": { 251 | "collapsed": true 252 | }, 253 | "source": [ 254 | "**Lecture:** [Finding the Best Match of a Word out of a List of Words](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/7627930?start=0)\n", 255 | "---" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 5, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "data": { 265 | "text/plain": [ 266 | "['rain']" 267 | ] 268 | }, 269 | "execution_count": 5, 270 | "metadata": {}, 271 | "output_type": "execute_result" 272 | } 273 | ], 274 | "source": [ 275 | "#this is just testing. It's not part of the application code\n", 276 | "#given a word, returns its closest match from a list of words\n", 277 | "from difflib import get_close_matches\n", 278 | "get_close_matches(\"rainn\", [\"help\", \"pyramid\", \"rain\"])" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": {}, 284 | "source": [ 285 | "The above result means that \"rain\" is the item of the list that is closer to the word \"rainn\"" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 6, 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "data": { 295 | "text/plain": [ 296 | "['rain', 'train', 'rainy']" 297 | ] 298 | }, 299 | "execution_count": 6, 300 | "metadata": {}, 301 | "output_type": "execute_result" 302 | } 303 | ], 304 | "source": [ 305 | "#getting the closest matchesb\n", 306 | "out of all the dictionary keys\n", 307 | "get_close_matches(\"rainn\", data.keys())" 308 | ] 309 | }, 310 | { 311 | "cell_type": "markdown", 312 | "metadata": {}, 313 | "source": [ 314 | "By default you get the top three closest words of the given sequence. You can change that as following:" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 8, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "data": { 324 | "text/plain": [ 325 | "['rain', 'train', 'rainy', 'grain', 'drain']" 326 | ] 327 | }, 328 | "execution_count": 8, 329 | "metadata": {}, 330 | "output_type": "execute_result" 331 | } 332 | ], 333 | "source": [ 334 | "#getting the top five closest matches\n", 335 | "get_close_matches(\"rainn\", data.keys(), n = 5)" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 9, 341 | "metadata": {}, 342 | "outputs": [ 343 | { 344 | "data": { 345 | "text/plain": [ 346 | "'rain'" 347 | ] 348 | }, 349 | "execution_count": 9, 350 | "metadata": {}, 351 | "output_type": "execute_result" 352 | } 353 | ], 354 | "source": [ 355 | "#the first item of the result list is the closest one\n", 356 | "#you can extract that item using [0]\n", 357 | "get_close_matches(\"rainn\", data.keys(), n = 5)[0]" 358 | ] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": { 363 | "collapsed": true 364 | }, 365 | "source": [ 366 | "**Lecture:** [Making the Program Suggest a Similar Word](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/7627942?start=0)\n", 367 | "---" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 11, 373 | "metadata": {}, 374 | "outputs": [ 375 | { 376 | "name": "stdout", 377 | "output_type": "stream", 378 | "text": [ 379 | "Enter word: rainn\n", 380 | "Did you mean rain instead?\n" 381 | ] 382 | } 383 | ], 384 | "source": [ 385 | "import json\n", 386 | "from difflib import get_close_matches\n", 387 | "\n", 388 | "data = json.load(open(\"data.json\"))\n", 389 | "def translate(w):\n", 390 | " w = w.lower()\n", 391 | " if w in data:\n", 392 | " return data[w]\n", 393 | " elif len(get_close_matches(w, data.keys())) > 0:\n", 394 | " return \"Did you mean %s instead?\" % get_close_matches(w, data.keys())[0]\n", 395 | " else:\n", 396 | " return \"The word doesn't exist. Please double check it.\"\n", 397 | "word = input(\"Enter word: \")\n", 398 | "print(translate(word))" 399 | ] 400 | }, 401 | { 402 | "cell_type": "markdown", 403 | "metadata": { 404 | "collapsed": true 405 | }, 406 | "source": [ 407 | "**Lecture:** [Prompting the User to Confirm Similarity Check](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/7627950?start=0)\n", 408 | "---" 409 | ] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": 12, 414 | "metadata": {}, 415 | "outputs": [ 416 | { 417 | "name": "stdout", 418 | "output_type": "stream", 419 | "text": [ 420 | "Enter word: rainn\n", 421 | "Did you mean rain instead? Enter Y if yes, or N if no: Y\n", 422 | "['Precipitation in the form of liquid water drops with diameters greater than 0.5 millimetres.', 'To fall from the clouds in drops of water.']\n" 423 | ] 424 | } 425 | ], 426 | "source": [ 427 | "import json\n", 428 | "from difflib import get_close_matches\n", 429 | "\n", 430 | "data = json.load(open(\"data.json\"))\n", 431 | "def translate(w):\n", 432 | " w = w.lower()\n", 433 | " if w in data:\n", 434 | " return data[w]\n", 435 | " elif len(get_close_matches(w, data.keys())) > 0:\n", 436 | " yn = input(\"Did you mean %s instead? Enter Y if yes, or N if no: \" % get_close_matches(w, data.keys())[0])\n", 437 | " if yn == \"Y\":\n", 438 | " return data[get_close_matches(w, data.keys())[0]]\n", 439 | " elif yn == \"N\":\n", 440 | " return \"The word doesn't exist. Please double check it.\"\n", 441 | " else:\n", 442 | " return \"We didn't understand your entry.\"\n", 443 | " else:\n", 444 | " return \"The word doesn't exist. Please double check it.\"\n", 445 | "\n", 446 | "word = input(\"Enter word: \")\n", 447 | "print(translate(word))" 448 | ] 449 | }, 450 | { 451 | "cell_type": "markdown", 452 | "metadata": { 453 | "collapsed": true 454 | }, 455 | "source": [ 456 | "**Lecture:** [Optimizing the Final Output](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/7627982?start=0)\n", 457 | "---" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": 13, 463 | "metadata": {}, 464 | "outputs": [ 465 | { 466 | "name": "stdout", 467 | "output_type": "stream", 468 | "text": [ 469 | "Enter word: rainn\n", 470 | "Did you mean rain instead? Enter Y if yes, or N if no: Y\n", 471 | "Precipitation in the form of liquid water drops with diameters greater than 0.5 millimetres.\n", 472 | "To fall from the clouds in drops of water.\n" 473 | ] 474 | } 475 | ], 476 | "source": [ 477 | "import json\n", 478 | "from difflib import get_close_matches\n", 479 | "\n", 480 | "data = json.load(open(\"data.json\"))\n", 481 | "\n", 482 | "def translate(w):\n", 483 | " w = w.lower()\n", 484 | " if w in data:\n", 485 | " return data[w]\n", 486 | " elif len(get_close_matches(w, data.keys())) > 0:\n", 487 | " yn = input(\"Did you mean %s instead? Enter Y if yes, or N if no: \" % get_close_matches(w, data.keys())[0])\n", 488 | " if yn == \"Y\":\n", 489 | " return data[get_close_matches(w, data.keys())[0]]\n", 490 | " elif yn == \"N\":\n", 491 | " return \"The word doesn't exist. Please double check it.\"\n", 492 | " else:\n", 493 | " return \"We didn't understand your entry.\"\n", 494 | " else:\n", 495 | " return \"The word doesn't exist. Please double check it.\"\n", 496 | "\n", 497 | "word = input(\"Enter word: \")\n", 498 | "\n", 499 | "output = translate(word)\n", 500 | "\n", 501 | "if type(output) == list:\n", 502 | " for item in output:\n", 503 | " print(item)\n", 504 | "else:\n", 505 | " print(output)\n" 506 | ] 507 | }, 508 | { 509 | "cell_type": "markdown", 510 | "metadata": { 511 | "collapsed": true 512 | }, 513 | "source": [ 514 | "**Lecture:** [Exercise](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/8235648?start=0)\n", 515 | "---" 516 | ] 517 | }, 518 | { 519 | "cell_type": "markdown", 520 | "metadata": {}, 521 | "source": [ 522 | "There's currently a bug in the program. When the user inputs a proper noun (e.g. Delhi, Paris, etc.) the program will convert it to lowercase and then tries to find the lowercase version (e.g. delhi) in the dataset and it cannot find it since the dataset has Delhi, but not delhi. \n", 523 | "\n", 524 | "Please add another conditional block to the program so that the program returns the definition of names that start with a capital letter." 525 | ] 526 | }, 527 | { 528 | "cell_type": "markdown", 529 | "metadata": { 530 | "collapsed": true 531 | }, 532 | "source": [ 533 | "**Lecture:** [Solution](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/8235752?start=0)\n", 534 | "---" 535 | ] 536 | }, 537 | { 538 | "cell_type": "markdown", 539 | "metadata": {}, 540 | "source": [ 541 | "To make sure the program returns the definition of words that start with a capital letter (e.g. Delhi, Texas) line 8 and 9 were added:" 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": 14, 547 | "metadata": { 548 | "scrolled": true 549 | }, 550 | "outputs": [ 551 | { 552 | "name": "stdout", 553 | "output_type": "stream", 554 | "text": [ 555 | "Enter word: Delhi\n", 556 | "The largest metropolis by area and the second-largest metropolis by population in India.\n" 557 | ] 558 | } 559 | ], 560 | "source": [ 561 | "import json\n", 562 | "from difflib import get_close_matches\n", 563 | "data = json.load(open(\"data.json\"))\n", 564 | "def translate(w):\n", 565 | " w = w.lower()\n", 566 | " if w in data:\n", 567 | " return data[w]\n", 568 | " elif w.title() in data: #if user entered \"texas\" this will check for \"Texas\" as well.\n", 569 | " return data[w.title()]\n", 570 | " elif len(get_close_matches(w, data.keys())) > 0:\n", 571 | " yn = input(\"Did you mean %s instead? Enter Y if yes, or N if no: \" % get_close_matches(w, data.keys())[0])\n", 572 | " if yn == \"Y\":\n", 573 | " return data[get_close_matches(w, data.keys())[0]]\n", 574 | " elif yn == \"N\":\n", 575 | " return \"The word doesn't exist. Please double check it.\"\n", 576 | " else:\n", 577 | " return \"We didn't understand your entry.\"\n", 578 | " else:\n", 579 | " return \"The word doesn't exist. Please double check it.\"\n", 580 | " \n", 581 | "word = input(\"Enter word: \")\n", 582 | "output = translate(word)\n", 583 | "if type(output) == list:\n", 584 | " for item in output:\n", 585 | " print(item)\n", 586 | "else:\n", 587 | " print(output)" 588 | ] 589 | }, 590 | { 591 | "cell_type": "markdown", 592 | "metadata": {}, 593 | "source": [ 594 | "As you can see from the comments I included in the code, I have added another conditional in line 8 and 9. The w.title() will convert the first letter to uppercase and the rest to lowercase. So, if the program didn't find anything for \"texas\" in the first conditional in lines 6-7 then this conditional will try to search for \"Texas\". Even if the user entered \"TEXAS\", this conditional will convert it to \"Texas\"." 595 | ] 596 | }, 597 | { 598 | "cell_type": "markdown", 599 | "metadata": { 600 | "collapsed": true 601 | }, 602 | "source": [ 603 | "**Lecture:** [Exercise](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/8677004?start=0)\n", 604 | "---" 605 | ] 606 | }, 607 | { 608 | "cell_type": "markdown", 609 | "metadata": {}, 610 | "source": [ 611 | "A student discovered another issue with the program. The program cannot return the definition of acronyms such as USA or NATO. Therefore, please try to add another conditional that makes the program return the definition of such words. " 612 | ] 613 | }, 614 | { 615 | "cell_type": "markdown", 616 | "metadata": { 617 | "collapsed": true 618 | }, 619 | "source": [ 620 | "**Lecture:** [Solution](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/8677006?start=0)\n", 621 | "---" 622 | ] 623 | }, 624 | { 625 | "cell_type": "markdown", 626 | "metadata": {}, 627 | "source": [ 628 | "To make sure the program returns the definition of acronyms (e.g. USA, NATO) line 10 and 11 were added: " 629 | ] 630 | }, 631 | { 632 | "cell_type": "code", 633 | "execution_count": 15, 634 | "metadata": { 635 | "scrolled": true 636 | }, 637 | "outputs": [ 638 | { 639 | "name": "stdout", 640 | "output_type": "stream", 641 | "text": [ 642 | "Enter word: NATO\n", 643 | "An international organization created in 1949 by the North Atlantic Treaty for purposes of collective security.\n" 644 | ] 645 | } 646 | ], 647 | "source": [ 648 | "import json\n", 649 | "from difflib import get_close_matches\n", 650 | "data = json.load(open(\"data.json\"))\n", 651 | "def translate(w):\n", 652 | " w = w.lower()\n", 653 | " if w in data:\n", 654 | " return data[w]\n", 655 | " elif w.title() in data:\n", 656 | " return data[w.title()]\n", 657 | " elif w.upper() in data: #in case user enters words like USA or NATO\n", 658 | " return data[w.upper()]\n", 659 | " elif len(get_close_matches(w, data.keys())) > 0:\n", 660 | " yn = input(\"Did you mean %s instead? Enter Y if yes, or N if no: \" % get_close_matches(w, data.keys())[0])\n", 661 | " if yn == \"Y\":\n", 662 | " return data[get_close_matches(w, data.keys())[0]]\n", 663 | " elif yn == \"N\":\n", 664 | " return \"The word doesn't exist. Please double check it.\"\n", 665 | " else:\n", 666 | " return \"We didn't understand your entry.\"\n", 667 | " else:\n", 668 | " return \"The word doesn't exist. Please double check it.\"\n", 669 | "word = input(\"Enter word: \")\n", 670 | "output = translate(word)\n", 671 | "if type(output) == list:\n", 672 | " for item in output:\n", 673 | " print(item)\n", 674 | "else:\n", 675 | " print(output)" 676 | ] 677 | }, 678 | { 679 | "cell_type": "markdown", 680 | "metadata": { 681 | "collapsed": true 682 | }, 683 | "source": [ 684 | "**Lecture:** [Final Code of Application 1]()\n", 685 | "---" 686 | ] 687 | }, 688 | { 689 | "cell_type": "code", 690 | "execution_count": null, 691 | "metadata": { 692 | "collapsed": true 693 | }, 694 | "outputs": [], 695 | "source": [ 696 | "import json\n", 697 | "from difflib import get_close_matches\n", 698 | "data = json.load(open(\"data.json\"))\n", 699 | "def translate(w):\n", 700 | " w = w.lower()\n", 701 | " if w in data:\n", 702 | " return data[w]\n", 703 | " elif w.title() in data:\n", 704 | " return data[w.title()]\n", 705 | " elif w.upper() in data: #in case user enters words like USA or NATO\n", 706 | " return data[w.upper()]\n", 707 | " elif len(get_close_matches(w, data.keys())) > 0:\n", 708 | " yn = input(\"Did you mean %s instead? Enter Y if yes, or N if no: \" % get_close_matches(w, data.keys())[0])\n", 709 | " if yn == \"Y\":\n", 710 | " return data[get_close_matches(w, data.keys())[0]]\n", 711 | " elif yn == \"N\":\n", 712 | " return \"The word doesn't exist. Please double check it.\"\n", 713 | " else:\n", 714 | " return \"We didn't understand your entry.\"\n", 715 | " else:\n", 716 | " return \"The word doesn't exist. Please double check it.\"\n", 717 | "word = input(\"Enter word: \")\n", 718 | "output = translate(word)\n", 719 | "if type(output) == list:\n", 720 | " for item in output:\n", 721 | " print(item)\n", 722 | "else:\n", 723 | " print(output)" 724 | ] 725 | } 726 | ], 727 | "metadata": { 728 | "kernelspec": { 729 | "display_name": "Python 3", 730 | "language": "python", 731 | "name": "python3" 732 | }, 733 | "language_info": { 734 | "codemirror_mode": { 735 | "name": "ipython", 736 | "version": 3 737 | }, 738 | "file_extension": ".py", 739 | "mimetype": "text/x-python", 740 | "name": "python", 741 | "nbconvert_exporter": "python", 742 | "pygments_lexer": "ipython3", 743 | "version": "3.6.3" 744 | } 745 | }, 746 | "nbformat": 4, 747 | "nbformat_minor": 2 748 | } 749 | -------------------------------------------------------------------------------- /S07-Numpy.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python Mega Course: Build 10 Real World Applications\n", 8 | "---" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "This notebook contains the source code for the video lectures of Section 7 of [The Python Mega Course: Build 10 Real World Applciations](https://www.udemy.com/the-python-mega-course/?couponCode=GITHEADSECTION)." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "# Section 7: Numpy\n", 23 | "***" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "**Lecture 105:** [What is Numpy](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5163296?start=0)\n", 31 | "---" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "NumPy is the fundamental package for scientific computing with Python. It provides a way to store and manipulate big arrays of numeric values in a more efficient way than standard Python lists. Libraries such as pandas or opencv are based on numpy. " 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "The central object of numpy is the numpy array. Here's an example of how to create a numpy array:" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 2, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", 57 | " 17, 18, 19, 20, 21, 22, 23, 24, 25, 26])" 58 | ] 59 | }, 60 | "execution_count": 2, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "import numpy\n", 67 | "n = numpy.arange(27)\n", 68 | "n" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 3, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "data": { 78 | "text/plain": [ 79 | "array([[[ 0, 1, 2],\n", 80 | " [ 3, 4, 5],\n", 81 | " [ 6, 7, 8]],\n", 82 | "\n", 83 | " [[ 9, 10, 11],\n", 84 | " [12, 13, 14],\n", 85 | " [15, 16, 17]],\n", 86 | "\n", 87 | " [[18, 19, 20],\n", 88 | " [21, 22, 23],\n", 89 | " [24, 25, 26]]])" 90 | ] 91 | }, 92 | "execution_count": 3, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "#you can change its structure\n", 99 | "n.reshape(3,3,3)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 5, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "array([[12, 13],\n", 111 | " [14, 15],\n", 112 | " [16, 17]])" 113 | ] 114 | }, 115 | "execution_count": 5, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "#another way to create a numpy array is from Python lists\n", 122 | "m = numpy.asarray([[12,13],[14,15],[16,17]])\n", 123 | "m" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "**Lecture 106:** [Installing the OpenCV Library](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5940430?start=0)\n", 131 | "---" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "The next lecture covers opencv - an image processing library that is based on numpy. To install it:" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": null, 144 | "metadata": { 145 | "collapsed": true 146 | }, 147 | "outputs": [], 148 | "source": [ 149 | "pip install opencv-python" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "If that doesn't work on Mac, try brew install opencv" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "**Lecture 107:** [Imagex to Numpy and Vice-versa](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5941086?start=0)\n", 164 | "---" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "For this section you will need an image file. [In this link](http://pythonhow.com/data/python-mega-course-data/section_numpy/smallgray.png) you can get a small image file. Once you open the link in your browser, right click and go to *Save Page* as to save it as an image and put it in the same folder with this script." 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 11, 177 | "metadata": { 178 | "collapsed": true 179 | }, 180 | "outputs": [], 181 | "source": [ 182 | "#import opencv\n", 183 | "import cv2" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 13, 189 | "metadata": { 190 | "scrolled": true 191 | }, 192 | "outputs": [ 193 | { 194 | "data": { 195 | "text/plain": [ 196 | "array([[187, 158, 104, 121, 143],\n", 197 | " [198, 125, 255, 255, 147],\n", 198 | " [209, 134, 255, 97, 182]], dtype=uint8)" 199 | ] 200 | }, 201 | "execution_count": 13, 202 | "metadata": {}, 203 | "output_type": "execute_result" 204 | } 205 | ], 206 | "source": [ 207 | "#convert the grayscale version of the image to a numpy array\n", 208 | "im_g = cv2.imread(\"smallgray.png\", 0)\n", 209 | "im_g" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 15, 215 | "metadata": {}, 216 | "outputs": [ 217 | { 218 | "data": { 219 | "text/plain": [ 220 | "array([[187, 158, 104, 121, 143],\n", 221 | " [198, 125, 255, 255, 147],\n", 222 | " [209, 134, 255, 97, 182]], dtype=uint8)" 223 | ] 224 | }, 225 | "execution_count": 15, 226 | "metadata": {}, 227 | "output_type": "execute_result" 228 | } 229 | ], 230 | "source": [ 231 | "#convert BGR (color) version the image to a numpy array\n", 232 | "im_g = cv2.imread(\"smallgray.png\", 2)\n", 233 | "im_g" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 16, 239 | "metadata": {}, 240 | "outputs": [ 241 | { 242 | "data": { 243 | "text/plain": [ 244 | "True" 245 | ] 246 | }, 247 | "execution_count": 16, 248 | "metadata": {}, 249 | "output_type": "execute_result" 250 | } 251 | ], 252 | "source": [ 253 | "#convert a numpy array into an image\n", 254 | "cv2.imwrite(\"newsmallgray.png\", im_g)" 255 | ] 256 | }, 257 | { 258 | "cell_type": "markdown", 259 | "metadata": {}, 260 | "source": [ 261 | "There should be a newsmallgray.png file in your current directory now." 262 | ] 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "metadata": {}, 267 | "source": [ 268 | "**Lecture 108:** [Indexing, Slicing, and Iterating](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5163302?start=0)\n", 269 | "---" 270 | ] 271 | }, 272 | { 273 | "cell_type": "markdown", 274 | "metadata": {}, 275 | "source": [ 276 | "Lets suppose we have the following numpy array:" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": 17, 282 | "metadata": {}, 283 | "outputs": [ 284 | { 285 | "data": { 286 | "text/plain": [ 287 | "array([[187, 158, 104, 121, 143],\n", 288 | " [198, 125, 255, 255, 147],\n", 289 | " [209, 134, 255, 97, 182]], dtype=uint8)" 290 | ] 291 | }, 292 | "execution_count": 17, 293 | "metadata": {}, 294 | "output_type": "execute_result" 295 | } 296 | ], 297 | "source": [ 298 | "im_g = cv2.imread(\"smallgray.png\", 2)\n", 299 | "im_g" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 22, 305 | "metadata": {}, 306 | "outputs": [ 307 | { 308 | "data": { 309 | "text/plain": [ 310 | "array([[104, 121],\n", 311 | " [255, 255]], dtype=uint8)" 312 | ] 313 | }, 314 | "execution_count": 22, 315 | "metadata": {}, 316 | "output_type": "execute_result" 317 | } 318 | ], 319 | "source": [ 320 | "#intersection of rows with index 0 to 1 and columns with index 2 to 3\n", 321 | "im_g[0:2,2:4]" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 23, 327 | "metadata": {}, 328 | "outputs": [ 329 | { 330 | "data": { 331 | "text/plain": [ 332 | "array([[104, 121],\n", 333 | " [255, 255],\n", 334 | " [255, 97]], dtype=uint8)" 335 | ] 336 | }, 337 | "execution_count": 23, 338 | "metadata": {}, 339 | "output_type": "execute_result" 340 | } 341 | ], 342 | "source": [ 343 | "#intersection of all rows with columns with index 2 to 3\n", 344 | "im_g[:,2:4]" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 24, 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "data": { 354 | "text/plain": [ 355 | "182" 356 | ] 357 | }, 358 | "execution_count": 24, 359 | "metadata": {}, 360 | "output_type": "execute_result" 361 | } 362 | ], 363 | "source": [ 364 | "#cell with row index 2 and column index 4\n", 365 | "im_g[2,4]" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 25, 371 | "metadata": {}, 372 | "outputs": [ 373 | { 374 | "name": "stdout", 375 | "output_type": "stream", 376 | "text": [ 377 | "[187 158 104 121 143]\n", 378 | "[198 125 255 255 147]\n", 379 | "[209 134 255 97 182]\n" 380 | ] 381 | } 382 | ], 383 | "source": [ 384 | "#looping over the array rows\n", 385 | "for i in im_g:\n", 386 | " print(i)" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 26, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "[187 198 209]\n", 399 | "[158 125 134]\n", 400 | "[104 255 255]\n", 401 | "[121 255 97]\n", 402 | "[143 147 182]\n" 403 | ] 404 | } 405 | ], 406 | "source": [ 407 | "#looping over the array columns\n", 408 | "for i in im_g.T:\n", 409 | " print(i)" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 27, 415 | "metadata": {}, 416 | "outputs": [ 417 | { 418 | "name": "stdout", 419 | "output_type": "stream", 420 | "text": [ 421 | "187\n", 422 | "158\n", 423 | "104\n", 424 | "121\n", 425 | "143\n", 426 | "198\n", 427 | "125\n", 428 | "255\n", 429 | "255\n", 430 | "147\n", 431 | "209\n", 432 | "134\n", 433 | "255\n", 434 | "97\n", 435 | "182\n" 436 | ] 437 | } 438 | ], 439 | "source": [ 440 | "#looping over the array cells\n", 441 | "for i in im_g.flat:\n", 442 | " print(i)" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": {}, 448 | "source": [ 449 | "**Lecture 109:** [Stacking and Splitting](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5163304?start=0)\n", 450 | "---" 451 | ] 452 | }, 453 | { 454 | "cell_type": "markdown", 455 | "metadata": {}, 456 | "source": [ 457 | "Lets again suppose we have the following numpy array:" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": 29, 463 | "metadata": {}, 464 | "outputs": [ 465 | { 466 | "data": { 467 | "text/plain": [ 468 | "array([[187, 158, 104, 121, 143],\n", 469 | " [198, 125, 255, 255, 147],\n", 470 | " [209, 134, 255, 97, 182]], dtype=uint8)" 471 | ] 472 | }, 473 | "execution_count": 29, 474 | "metadata": {}, 475 | "output_type": "execute_result" 476 | } 477 | ], 478 | "source": [ 479 | "im_g = cv2.imread(\"smallgray.png\", 2)\n", 480 | "im_g" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": 31, 486 | "metadata": {}, 487 | "outputs": [ 488 | { 489 | "name": "stdout", 490 | "output_type": "stream", 491 | "text": [ 492 | "[[187 158 104 121 143 187 158 104 121 143 187 158 104 121 143]\n", 493 | " [198 125 255 255 147 198 125 255 255 147 198 125 255 255 147]\n", 494 | " [209 134 255 97 182 209 134 255 97 182 209 134 255 97 182]]\n" 495 | ] 496 | } 497 | ], 498 | "source": [ 499 | "#stacking three arrays horizontally\n", 500 | "ims = numpy.hstack((im_g, im_g, im_g))\n", 501 | "print(ims)" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": 32, 507 | "metadata": {}, 508 | "outputs": [ 509 | { 510 | "name": "stdout", 511 | "output_type": "stream", 512 | "text": [ 513 | "[[187 158 104 121 143]\n", 514 | " [198 125 255 255 147]\n", 515 | " [209 134 255 97 182]\n", 516 | " [187 158 104 121 143]\n", 517 | " [198 125 255 255 147]\n", 518 | " [209 134 255 97 182]\n", 519 | " [187 158 104 121 143]\n", 520 | " [198 125 255 255 147]\n", 521 | " [209 134 255 97 182]]\n" 522 | ] 523 | } 524 | ], 525 | "source": [ 526 | "#stacking three arrays vertically\n", 527 | "ims = numpy.vstack((im_g, im_g, im_g))\n", 528 | "print(ims)" 529 | ] 530 | }, 531 | { 532 | "cell_type": "code", 533 | "execution_count": 33, 534 | "metadata": {}, 535 | "outputs": [ 536 | { 537 | "name": "stdout", 538 | "output_type": "stream", 539 | "text": [ 540 | "[array([[187],\n", 541 | " [198],\n", 542 | " [209],\n", 543 | " [187],\n", 544 | " [198],\n", 545 | " [209],\n", 546 | " [187],\n", 547 | " [198],\n", 548 | " [209]], dtype=uint8), array([[158],\n", 549 | " [125],\n", 550 | " [134],\n", 551 | " [158],\n", 552 | " [125],\n", 553 | " [134],\n", 554 | " [158],\n", 555 | " [125],\n", 556 | " [134]], dtype=uint8), array([[104],\n", 557 | " [255],\n", 558 | " [255],\n", 559 | " [104],\n", 560 | " [255],\n", 561 | " [255],\n", 562 | " [104],\n", 563 | " [255],\n", 564 | " [255]], dtype=uint8), array([[121],\n", 565 | " [255],\n", 566 | " [ 97],\n", 567 | " [121],\n", 568 | " [255],\n", 569 | " [ 97],\n", 570 | " [121],\n", 571 | " [255],\n", 572 | " [ 97]], dtype=uint8), array([[143],\n", 573 | " [147],\n", 574 | " [182],\n", 575 | " [143],\n", 576 | " [147],\n", 577 | " [182],\n", 578 | " [143],\n", 579 | " [147],\n", 580 | " [182]], dtype=uint8)]\n" 581 | ] 582 | } 583 | ], 584 | "source": [ 585 | "#splitting horizontally into 5 numpy arrays\n", 586 | "lst = numpy.hsplit(ims, 5)\n", 587 | "print(lst)" 588 | ] 589 | }, 590 | { 591 | "cell_type": "code", 592 | "execution_count": 34, 593 | "metadata": {}, 594 | "outputs": [ 595 | { 596 | "name": "stdout", 597 | "output_type": "stream", 598 | "text": [ 599 | "[array([[187, 158, 104, 121, 143],\n", 600 | " [198, 125, 255, 255, 147],\n", 601 | " [209, 134, 255, 97, 182]], dtype=uint8), array([[187, 158, 104, 121, 143],\n", 602 | " [198, 125, 255, 255, 147],\n", 603 | " [209, 134, 255, 97, 182]], dtype=uint8), array([[187, 158, 104, 121, 143],\n", 604 | " [198, 125, 255, 255, 147],\n", 605 | " [209, 134, 255, 97, 182]], dtype=uint8)]\n" 606 | ] 607 | } 608 | ], 609 | "source": [ 610 | "#splitting vertically into 3 numpy arrays\n", 611 | "lst = numpy.vsplit(ims, 3)\n", 612 | "print(lst)" 613 | ] 614 | }, 615 | { 616 | "cell_type": "code", 617 | "execution_count": 35, 618 | "metadata": { 619 | "scrolled": true 620 | }, 621 | "outputs": [ 622 | { 623 | "data": { 624 | "text/plain": [ 625 | "array([[187, 158, 104, 121, 143],\n", 626 | " [198, 125, 255, 255, 147],\n", 627 | " [209, 134, 255, 97, 182]], dtype=uint8)" 628 | ] 629 | }, 630 | "execution_count": 35, 631 | "metadata": {}, 632 | "output_type": "execute_result" 633 | } 634 | ], 635 | "source": [ 636 | "#access the first array of the big numpy array\n", 637 | "lst[0]" 638 | ] 639 | } 640 | ], 641 | "metadata": { 642 | "kernelspec": { 643 | "display_name": "Python 3", 644 | "language": "python", 645 | "name": "python3" 646 | }, 647 | "language_info": { 648 | "codemirror_mode": { 649 | "name": "ipython", 650 | "version": 3 651 | }, 652 | "file_extension": ".py", 653 | "mimetype": "text/x-python", 654 | "name": "python", 655 | "nbconvert_exporter": "python", 656 | "pygments_lexer": "ipython3", 657 | "version": "3.6.3" 658 | } 659 | }, 660 | "nbformat": 4, 661 | "nbformat_minor": 2 662 | } 663 | -------------------------------------------------------------------------------- /S09-App-3-Build-a-Website-Blocker.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python Mega Course: Build 10 Real World Applications\n", 8 | "---" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "This notebook contains the source code for the video lectures of Section 9 of [The Python Mega Course: Build 10 Real World Applciations](https://www.udemy.com/the-python-mega-course/?couponCode=GITHEADSECTION)." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "# Section 9: Application 3: Build a Website Blocker\n", 23 | "***" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "**Lecture:** [Program Demonstration](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5163360?start=0)\n", 31 | "---" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "This lecture demonstrates the finished version of the program that you're going to build in this section." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "**Lecture:** [Application Architecture](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5163364?start=0)\n", 46 | "---" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "This lecture shows how we're going to approach the problem of writing a program that blocks access to distracting websites." 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "**Lecture:** [Setting up the Script](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5163366?start=0)\n", 61 | "---" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 33, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "1\n", 74 | "1\n", 75 | "1\n" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "import time\n", 81 | "\n", 82 | "hosts_path = \"/etc/hosts\" # If you're on Windows change to hosts_path = r\"C:\\Windows\\System32\\drivers\\etc\\hosts\"\n", 83 | "redirect = \"127.0.0.1\"\n", 84 | "website_list = [\"www.facebook.com\", \"facebook.com\", \"dub119.mail.live.com\", \"www.dub119.mail.live.com\"]\n", 85 | "\n", 86 | "i = 0\n", 87 | "while i < 3:\n", 88 | " print(1)\n", 89 | " time.sleep(5)\n", 90 | " i = i + 1" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "**Lecture:** [Setting up the Infinite Loop](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5163368?start=15)\n", 98 | "---" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": { 105 | "collapsed": true 106 | }, 107 | "outputs": [], 108 | "source": [ 109 | "import time\n", 110 | "from datetime import datetime as dt\n", 111 | "\n", 112 | "hosts_path = \"/etc/hosts\" # If you're on Windows change to hosts_path = r\"C:\\Windows\\System32\\drivers\\etc\\hosts\"\n", 113 | "redirect=\"127.0.0.1\"\n", 114 | "website_list = [\"www.facebook.com\", \"facebook.com\", \"dub119.mail.live.com\", \"www.dub119.mail.live.com\"]\n", 115 | "\n", 116 | "while True:\n", 117 | " if dt(dt.now().year,dt.now().month,dt.now().day,8) < dt.now() < dt(dt.now().year,dt.now().month,dt.now().day,16):\n", 118 | " print(\"Working hours...\")\n", 119 | " else:\n", 120 | " print(\"Fun hours...\")\n", 121 | " time.sleep(5)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "**Lecture:** [Implementing the First Part](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5163372?start=0)\n", 129 | "---" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": { 136 | "collapsed": true 137 | }, 138 | "outputs": [], 139 | "source": [ 140 | "import time\n", 141 | "from datetime import datetime as dt\n", 142 | "\n", 143 | "hosts_temp = \"hosts\"\n", 144 | "hosts_path = \"/etc/hosts\" # If you're on Windows change to hosts_path = r\"C:\\Windows\\System32\\drivers\\etc\\hosts\"\n", 145 | "redirect = \"127.0.0.1\"\n", 146 | "website_list=[\"www.facebook.com\", \"facebook.com\", \"dub119.mail.live.com\", \"www.dub119.mail.live.com\"]\n", 147 | "\n", 148 | "while True:\n", 149 | " if dt(dt.now().year,dt.now().month,dt.now().day,8) < dt.now() < dt(dt.now().year,dt.now().month,dt.now().day,16):\n", 150 | " print(\"Working hours...\")\n", 151 | " with open(hosts_temp, 'r+') as file:\n", 152 | " content = file.read()\n", 153 | " for website in website_list:\n", 154 | " if website in content:\n", 155 | " pass\n", 156 | " else:\n", 157 | " file.write(redirect + \" \" + website + \"\\n\")\n", 158 | " else:\n", 159 | " print(\"Fun hours...\")\n", 160 | " time.sleep(5)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "**Lecture:** [Implementing the Second Part](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5163376?start=0)\n", 168 | "---" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": { 175 | "collapsed": true 176 | }, 177 | "outputs": [], 178 | "source": [ 179 | "import time\n", 180 | "from datetime import datetime as dt\n", 181 | "\n", 182 | "hosts_temp = \"hosts\"\n", 183 | "hosts_path = \"/etc/hosts\" # If you're on Windows change to hosts_path = r\"C:\\Windows\\System32\\drivers\\etc\\hosts\"\n", 184 | "redirect = \"127.0.0.1\"\n", 185 | "website_list = [\"www.facebook.com\", \"facebook.com\", \"dub119.mail.live.com\", \"www.dub119.mail.live.com\"]\n", 186 | "\n", 187 | "while True:\n", 188 | " if dt(dt.now().year,dt.now().month,dt.now().day,8) < dt.now() < dt(dt.now().year,dt.now().month, dt.now().day,16):\n", 189 | " print(\"Working hours...\")\n", 190 | " with open(hosts_temp, 'r+') as file:\n", 191 | " content=file.read()\n", 192 | " for website in website_list:\n", 193 | " if website in content:\n", 194 | " pass\n", 195 | " else:\n", 196 | " file.write(redirect + \" \" + website + \"\\n\")\n", 197 | " else:\n", 198 | " with open(hosts_temp, 'r+') as file:\n", 199 | " content=file.readlines()\n", 200 | " file.seek(0)\n", 201 | " for line in content:\n", 202 | " if not any(website in line for website in website_list):\n", 203 | " file.write(line)\n", 204 | " file.truncate()\n", 205 | " print(\"Fun hours...\")\n", 206 | " time.sleep(5)" 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "**Lecture:** [The any() Function](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/11333638?start=0)\n", 214 | "---" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "Hi, in case you didn't understand the if not any(website in line for website in website_list) part in the previous video here is another example:" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 27, 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "name": "stdout", 231 | "output_type": "stream", 232 | "text": [ 233 | "False\n", 234 | "False\n", 235 | "True\n" 236 | ] 237 | } 238 | ], 239 | "source": [ 240 | "lines = [\"trees are good\", \"pool is fresh\", \"face is round\"]\n", 241 | "website_list = [\"face\", \"clock\", \"trend\"]\n", 242 | "for line in lines:\n", 243 | " print(any(website in line for website in website_list))" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "So, we start iterating over the items of website_list using a for loop. In the first iteration we would have:" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": null, 256 | "metadata": { 257 | "collapsed": true 258 | }, 259 | "outputs": [], 260 | "source": [ 261 | "any(website in \"trees are good\" for website in website_list)" 262 | ] 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "metadata": {}, 267 | "source": [ 268 | "Inside the parenthesis of *any()* there's another loop that iterates over *website_list*:" 269 | ] 270 | }, 271 | { 272 | "cell_type": "raw", 273 | "metadata": {}, 274 | "source": [ 275 | "(\"face\" in \"trees are good\")\n", 276 | "(\"clock\" in \"trees are good\")\n", 277 | "(\"trend\" in \"trees are good\")" 278 | ] 279 | }, 280 | { 281 | "cell_type": "markdown", 282 | "metadata": {}, 283 | "source": [ 284 | "If any of these is True you get the expression evalueated to True. In this case none of them is True, so you get False. If you want to return True if all of them are True, you need to use all() instead of any().\n", 285 | "\n", 286 | "So, the part any(website in line for website in website_list)will either be equal to True or False." 287 | ] 288 | }, 289 | { 290 | "cell_type": "markdown", 291 | "metadata": {}, 292 | "source": [ 293 | "**Lecture:** [Scheduling the Python Program on Windows](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5163380?start=0)\n", 294 | "---" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "This video lecture explains how to schedule a Python program for execution at a certain time during the day on a Windows computer." 302 | ] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "metadata": {}, 307 | "source": [ 308 | "**Lecture:** [Scheduling the Python Program on Mac and Linux](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5163382?start=0)\n", 309 | "---" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": {}, 315 | "source": [ 316 | "This video lecture explains how to schedule a Python program for execution at a certain time during the day on a Mac or Linux computer." 317 | ] 318 | }, 319 | { 320 | "cell_type": "markdown", 321 | "metadata": {}, 322 | "source": [ 323 | "**Lecture:** [Final Code of Application 3]()\n", 324 | "---" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": null, 330 | "metadata": { 331 | "collapsed": true 332 | }, 333 | "outputs": [], 334 | "source": [ 335 | "import time\n", 336 | "from datetime import datetime as dt\n", 337 | "\n", 338 | "hosts_temp = \"hosts\"\n", 339 | "hosts_path = \"/etc/hosts\" # If you're on Windows change to hosts_path = r\"C:\\Windows\\System32\\drivers\\etc\\hosts\"\n", 340 | "redirect = \"127.0.0.1\"\n", 341 | "website_list = [\"www.facebook.com\", \"facebook.com\", \"dub119.mail.live.com\", \"www.dub119.mail.live.com\"]\n", 342 | "\n", 343 | "while True:\n", 344 | " if dt(dt.now().year,dt.now().month,dt.now().day,8) < dt.now() < dt(dt.now().year,dt.now().month, dt.now().day,16):\n", 345 | " print(\"Working hours...\")\n", 346 | " with open(hosts_temp, 'r+') as file:\n", 347 | " content=file.read()\n", 348 | " for website in website_list:\n", 349 | " if website in content:\n", 350 | " pass\n", 351 | " else:\n", 352 | " file.write(redirect + \" \" + website + \"\\n\")\n", 353 | " else:\n", 354 | " with open(hosts_temp, 'r+') as file:\n", 355 | " content=file.readlines()\n", 356 | " file.seek(0)\n", 357 | " for line in content:\n", 358 | " if not any(website in line for website in website_list):\n", 359 | " file.write(line)\n", 360 | " file.truncate()\n", 361 | " print(\"Fun hours...\")\n", 362 | " time.sleep(5)" 363 | ] 364 | } 365 | ], 366 | "metadata": { 367 | "kernelspec": { 368 | "display_name": "Python 3", 369 | "language": "python", 370 | "name": "python3" 371 | }, 372 | "language_info": { 373 | "codemirror_mode": { 374 | "name": "ipython", 375 | "version": 3 376 | }, 377 | "file_extension": ".py", 378 | "mimetype": "text/x-python", 379 | "name": "python", 380 | "nbconvert_exporter": "python", 381 | "pygments_lexer": "ipython3", 382 | "version": "3.6.3" 383 | } 384 | }, 385 | "nbformat": 4, 386 | "nbformat_minor": 2 387 | } 388 | -------------------------------------------------------------------------------- /S11-Graphical-User-Interfaces-with-Tkinter.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python Mega Course: Build 10 Real World Applications\n", 8 | "---" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "This notebook contains the source code for the video lectures of Section 11 of [The Python Mega Course: Build 10 Real World Applciations](https://www.udemy.com/the-python-mega-course/?couponCode=GITHEADSECTION)." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "# Section 11: Graphical User Interfaces with Tkinter\n", 23 | "***" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "**Lecture:** [Introduction to Tkinter](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775338?start=0)\n", 31 | "---" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "Introduction to what Tkinter can do." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "**Lecture:** [Setting Up a GUI with Tkinter](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775342?start=0)\n", 46 | "---" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "Down below is an example of a desktop graphical interface program built with Tkinter. Try to run it on a .py file. GUIs don't work well with Jupyter." 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": { 60 | "collapsed": true 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "from tkinter import *\n", 65 | "\n", 66 | "window = Tk()\n", 67 | "\n", 68 | "b1 = Button(window, text = \"Execute\")\n", 69 | "b1.grid(row = 0, column = 0)\n", 70 | "\n", 71 | "e1 = Entry(window)\n", 72 | "e1.grid(row = 0, column = 1)\n", 73 | "\n", 74 | "t1 = Text(window, height = 1, width = 20)\n", 75 | "t1.grid(row = 0, column = 2)\n", 76 | "\n", 77 | "window.mainloop()" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "**Note**: The program doesn't have any functionalities yet so pushing the `Execute` button will not do anything." 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "**Lecture:** [Connecting GUI Widgets with Callback Functions](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775344?start=0)\n", 92 | "---" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "Let's add a function that will be executed when the user presses the `Execute` button. When `Execute` is pressed, the number entered in the text form will be multiplied by 1.6 and be displayed in the area next to it." 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 1, 105 | "metadata": { 106 | "collapsed": true 107 | }, 108 | "outputs": [], 109 | "source": [ 110 | "from tkinter import *\n", 111 | "\n", 112 | "window = Tk()\n", 113 | "\n", 114 | "def km_to_miles():\n", 115 | " miles = float(e1_value.get()) * 1.6\n", 116 | " t1.insert(END, miles)\n", 117 | "\n", 118 | "b1 = Button(window, text = \"Execute\", command = km_to_miles)\n", 119 | "b1.grid(row = 0, column = 0)\n", 120 | "\n", 121 | "e1_value = StringVar()\n", 122 | "e1 = Entry(window, textvariable = e1_value)\n", 123 | "e1.grid(row = 0, column = 1)\n", 124 | "\n", 125 | "t1 = Text(window, height = 1, width = 20)\n", 126 | "t1.grid(row = 0, column = 2)\n", 127 | "\n", 128 | "window.mainloop()" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "**Practice:** [Create a Multi-widget GUI](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5278808?start=0)\n", 136 | "---" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Please create a Python program that expects a kilogram input value and converts that value to grams, pounds, and ounces when the user pushes the convert button.\n", 144 | "\n", 145 | "The program would look similar to the one in the following picture:\n", 146 | " " 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "metadata": { 160 | "collapsed": true 161 | }, 162 | "outputs": [], 163 | "source": [ 164 | "Tips:\n", 165 | "1 kg = 1000 grams\n", 166 | "1 kg = 2.20462\n", 167 | "1 kg = 35.274" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": {}, 173 | "source": [ 174 | "## **[Solution](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5278814?start=0)**" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": null, 180 | "metadata": { 181 | "collapsed": true 182 | }, 183 | "outputs": [], 184 | "source": [ 185 | "from tkinter import *\n", 186 | " \n", 187 | "window=Tk()\n", 188 | " \n", 189 | "def from_kg():\n", 190 | " gram = float(e2_value.get()) * 1000\n", 191 | " pound = float(e2_value.get()) * 2.20462\n", 192 | " ounce = float(e2_value.get()) * 35.274\n", 193 | " t1.delete(\"1.0\", END)\n", 194 | " t1.insert(END, gram)\n", 195 | " t2.delete(\"1.0\", END)\n", 196 | " t2.insert(END, pound)\n", 197 | " t3.delete(\"1.0\", END)\n", 198 | " t3.insert(END, ounce)\n", 199 | " \n", 200 | "e1 = Label(window, text=\"Kg\")\n", 201 | "e1.grid(row=0, column=0)\n", 202 | " \n", 203 | "e2_value=StringVar()\n", 204 | "e2 = Entry(window, textvariable = e2_value)\n", 205 | "e2.grid(row=0, column=1)\n", 206 | " \n", 207 | "b1 = Button(window, text=\"Convert\", command = from_kg)\n", 208 | "b1.grid(row=0, column=2)\n", 209 | " \n", 210 | "t1 = Text(window, height=1, width=20)\n", 211 | "t1.grid(row=1, column=0)\n", 212 | " \n", 213 | "t2 = Text(window, height=1, width=20)\n", 214 | "t2.grid(row=1, column=1)\n", 215 | " \n", 216 | "t3 = Text(window,height=1, width=20)\n", 217 | "t3.grid(row=1, column=2)\n", 218 | " \n", 219 | "window.mainloop()" 220 | ] 221 | } 222 | ], 223 | "metadata": { 224 | "kernelspec": { 225 | "display_name": "Python 3", 226 | "language": "python", 227 | "name": "python3" 228 | }, 229 | "language_info": { 230 | "codemirror_mode": { 231 | "name": "ipython", 232 | "version": 3 233 | }, 234 | "file_extension": ".py", 235 | "mimetype": "text/x-python", 236 | "name": "python", 237 | "nbconvert_exporter": "python", 238 | "pygments_lexer": "ipython3", 239 | "version": "3.6.3" 240 | } 241 | }, 242 | "nbformat": 4, 243 | "nbformat_minor": 2 244 | } 245 | -------------------------------------------------------------------------------- /S12-Python-for-Interacting-with-SQLite-and-PostGreSQL-Databases.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python Mega Course: Build 10 Real World Applications\n", 8 | "---" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "This notebook contains the source code for the video lectures of Section 12 of [The Python Mega Course: Build 10 Real World Applciations](https://www.udemy.com/the-python-mega-course/?couponCode=GITHEADSECTION)." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "# Section 12: Python for Interacting with SQLite and PostGreSQL Databases\n", 23 | "***" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "**Lecture:** [Introduction to \"Python with Databases\"](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775356?start=0)\n", 31 | "---" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "An introduction on how Python is used in interaction with databases." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "**Lecture:** [Connecting and Inserting Data to SQLite via Python](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775360?start=0)\n", 46 | "---" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "The following code creates a `lite.db` database file, and a `store` table in that database. Then, it inserts some data in `store` via the `insert` function and then it prints out those data via the `view` function. If the `lite.db` exists, then no new database is created." 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 3, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "[('Water Glass', 10, 5.0), ('Water Glass', 10, 5.0), ('Water Glass', 10, 5.0), ('Water Glass', 10, 5.0)]\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "import sqlite3\n", 71 | "\n", 72 | "def create_table():\n", 73 | " conn = sqlite3.connect(\"lite.db\")\n", 74 | " cur = conn.cursor()\n", 75 | " cur.execute(\"CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price REAL)\")\n", 76 | " conn.commit()\n", 77 | " conn.close()\n", 78 | "\n", 79 | "def insert(item, quantity, price):\n", 80 | " conn = sqlite3.connect(\"lite.db\")\n", 81 | " cur = conn.cursor()\n", 82 | " cur.execute(\"INSERT INTO store VALUES (?,?,?)\",(item, quantity, price))\n", 83 | " conn.commit()\n", 84 | " conn.close()\n", 85 | "\n", 86 | "def view():\n", 87 | " conn = sqlite3.connect(\"lite.db\")\n", 88 | " cur = conn.cursor()\n", 89 | " cur.execute(\"SELECT * FROM store\")\n", 90 | " rows = cur.fetchall()\n", 91 | " conn.close()\n", 92 | " return rows\n", 93 | " \n", 94 | "create_table() \n", 95 | "insert(\"Water Glass\", 10, 5)\n", 96 | "print(view())" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "**Note**: Anytime you execute the above code, a new row will be inserted by the `insert` function into the database table." 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "**Lecture:** [Selecting, Inserting, Deleting, and Updating SQLite Records](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775362?start=0)\n", 111 | "---" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "The code below connects to a sqlite3 database, and does various operations with the database table such as updating and deleting rows." 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 14, 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "First print: [('Water Glass', 10, 5.0), ('Milk', 11, 6.0)]\n", 131 | "Second print: [('Water Glass', 10, 5.0), ('Milk', 99, 33.0)]\n", 132 | "Third print: [('Milk', 99, 33.0)]\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "import sqlite3\n", 138 | "\n", 139 | "def create_table():\n", 140 | " conn = sqlite3.connect(\"newlite.db\")\n", 141 | " cur = conn.cursor()\n", 142 | " cur.execute(\"CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price REAL)\")\n", 143 | " conn.commit()\n", 144 | " conn.close()\n", 145 | "\n", 146 | "def insert(item, quantity, price):\n", 147 | " conn = sqlite3.connect(\"newlite.db\")\n", 148 | " cur = conn.cursor()\n", 149 | " cur.execute(\"INSERT INTO store VALUES (?,?,?)\",(item, quantity, price))\n", 150 | " conn.commit()\n", 151 | " conn.close()\n", 152 | "\n", 153 | "def view():\n", 154 | " conn = sqlite3.connect(\"newlite.db\")\n", 155 | " cur = conn.cursor()\n", 156 | " cur.execute(\"SELECT * FROM store\")\n", 157 | " rows = cur.fetchall()\n", 158 | " conn.close()\n", 159 | " return rows\n", 160 | "\n", 161 | "def delete(item):\n", 162 | " conn = sqlite3.connect(\"newlite.db\")\n", 163 | " cur = conn.cursor()\n", 164 | " cur.execute(\"DELETE FROM store WHERE item=?\",(item,))\n", 165 | " conn.commit()\n", 166 | " conn.close()\n", 167 | " \n", 168 | "def update(quantity, price, item):\n", 169 | " conn = sqlite3.connect(\"newlite.db\")\n", 170 | " cur = conn.cursor()\n", 171 | " cur.execute(\"UPDATE store SET quantity=?, price=? WHERE item=?\", (quantity, price, item))\n", 172 | " conn.commit()\n", 173 | " conn.close()\n", 174 | " \n", 175 | "create_table() \n", 176 | "insert(\"Water Glass\", 10, 5)\n", 177 | "insert(\"Milk\", 11, 6)\n", 178 | "print(\"First print:\", view())\n", 179 | "update(99,33,\"Milk\")\n", 180 | "print(\"Second print:\", view())\n", 181 | "delete(\"Water Glass\")\n", 182 | "print(\"Third print:\", view())" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "**Lecture:** [Introduction to PostGreSQL Psycopg2](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775364?start=0)\n", 190 | "---" 191 | ] 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "metadata": {}, 196 | "source": [ 197 | "An introduction to using PostGreSQL in interaction with Python. The interaction is possible via the Python psycopg2 library which can be installed with `pip install psycopg2`." 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "**Lecture:** [Selecting, Inserting, Deleting, and Updating PostGreSQL Records](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775374?start=0)\n", 205 | "---" 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": {}, 211 | "source": [ 212 | "The following code does various operations with a PostGreSQL datbase." 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "**NOTE**: For the code to work you need to have PostGreSQL installed, and you should create a database there. Then, update the string inside psycopg2.connect() to reflect your credentials." 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 15, 225 | "metadata": {}, 226 | "outputs": [ 227 | { 228 | "name": "stdout", 229 | "output_type": "stream", 230 | "text": [ 231 | "First print: [('Water Glass', 10, 5.0), ('Milk', 11, 6.0)]\n", 232 | "Second print: [('Water Glass', 10, 5.0), ('Milk', 99, 33.0)]\n", 233 | "Third print: [('Milk', 99, 33.0)]\n" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "import psycopg2\n", 239 | "\n", 240 | "def create_table():\n", 241 | " conn = psycopg2.connect(\"dbname='database1' user='postgres' password='postgres123' host='localhost' port='5432'\")\n", 242 | " cur = conn.cursor()\n", 243 | " cur.execute(\"CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price REAL)\")\n", 244 | " conn.commit()\n", 245 | " conn.close()\n", 246 | "\n", 247 | "def insert(item, quantity, price):\n", 248 | " conn = psycopg2.connect(\"dbname='database1' user='postgres' password='postgres123' host='localhost' port='5432'\")\n", 249 | " cur = conn.cursor()\n", 250 | " cur.execute(\"INSERT INTO store VALUES ('%s','%s','%s')\" % (item, quantity, price))\n", 251 | " conn.commit()\n", 252 | " conn.close()\n", 253 | "\n", 254 | "def view():\n", 255 | " conn = psycopg2.connect(\"dbname='database1' user='postgres' password='postgres123' host='localhost' port='5432'\")\n", 256 | " cur = conn.cursor()\n", 257 | " cur.execute(\"SELECT * FROM store\")\n", 258 | " rows = cur.fetchall()\n", 259 | " conn.close()\n", 260 | " return rows\n", 261 | "\n", 262 | "def delete(item):\n", 263 | " conn = psycopg2.connect(\"dbname='database1' user='postgres' password='postgres123' host='localhost' port='5432'\")\n", 264 | " cur = conn.cursor()\n", 265 | " cur.execute(\"DELETE FROM store WHERE item=%s\",(item,))\n", 266 | " conn.commit()\n", 267 | " conn.close()\n", 268 | " \n", 269 | "def update(quantity, price, item):\n", 270 | " conn = psycopg2.connect(\"dbname='database1' user='postgres' password='postgres123' host='localhost' port='5432'\")\n", 271 | " cur = conn.cursor()\n", 272 | " cur.execute(\"UPDATE store SET quantity=%s, price=%s WHERE item=%s\", (quantity, price, item))\n", 273 | " conn.commit()\n", 274 | " conn.close()\n", 275 | " \n", 276 | "create_table() \n", 277 | "insert(\"Water Glass\", 10, 5)\n", 278 | "insert(\"Milk\", 11, 6)\n", 279 | "print(\"First print:\", view())\n", 280 | "update(99,33,\"Milk\")\n", 281 | "print(\"Second print:\", view())\n", 282 | "delete(\"Water Glass\")\n", 283 | "print(\"Third print:\", view())" 284 | ] 285 | } 286 | ], 287 | "metadata": { 288 | "kernelspec": { 289 | "display_name": "Python 3", 290 | "language": "python", 291 | "name": "python3" 292 | }, 293 | "language_info": { 294 | "codemirror_mode": { 295 | "name": "ipython", 296 | "version": 3 297 | }, 298 | "file_extension": ".py", 299 | "mimetype": "text/x-python", 300 | "name": "python", 301 | "nbconvert_exporter": "python", 302 | "pygments_lexer": "ipython3", 303 | "version": "3.6.3" 304 | } 305 | }, 306 | "nbformat": 4, 307 | "nbformat_minor": 2 308 | } 309 | -------------------------------------------------------------------------------- /S13-App-5-Build-a-Desktop-Database-Application.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python Mega Course: Build 10 Real World Applications\n", 8 | "---" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "This notebook contains the source code for the video lectures of Section 13 of [The Python Mega Course: Build 10 Real World Applciations](https://www.udemy.com/the-python-mega-course/?couponCode=GITHEADSECTION)." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "# Section 13: Application 5: Build a Desktop Database Application\n", 23 | "***" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "**Lecture:** [Program Demonstration](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775394?start=0)\n", 31 | "---" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "This video lecture shows the finished version of the website running on a browser." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "**Lecture:** [User Interface Design](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775396?start=0)\n", 46 | "---" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "This lecture shows a drawing of how the graphical interface is going to look like. Having a sketch helps later in the coding phase." 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "**Lecture:** [Frontend Interface](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775402?start=0)\n", 61 | "---" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "Below is the code that builds the GUI. Note that the GUI doesn't have any functionality yet." 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 2, 74 | "metadata": { 75 | "collapsed": true 76 | }, 77 | "outputs": [], 78 | "source": [ 79 | "from tkinter import *\n", 80 | "\n", 81 | "window=Tk()\n", 82 | "\n", 83 | "window.wm_title(\"BookStore\")\n", 84 | "\n", 85 | "l1=Label(window,text=\"Title\")\n", 86 | "l1.grid(row=0,column=0)\n", 87 | "\n", 88 | "l2=Label(window,text=\"Author\")\n", 89 | "l2.grid(row=0,column=2)\n", 90 | "\n", 91 | "l3=Label(window,text=\"Year\")\n", 92 | "l3.grid(row=1,column=0)\n", 93 | "\n", 94 | "l4=Label(window,text=\"ISBN\")\n", 95 | "l4.grid(row=1,column=2)\n", 96 | "\n", 97 | "title_text=StringVar()\n", 98 | "e1=Entry(window,textvariable=title_text)\n", 99 | "e1.grid(row=0,column=1)\n", 100 | "\n", 101 | "author_text=StringVar()\n", 102 | "e2=Entry(window,textvariable=author_text)\n", 103 | "e2.grid(row=0,column=3)\n", 104 | "\n", 105 | "year_text=StringVar()\n", 106 | "e3=Entry(window,textvariable=year_text)\n", 107 | "e3.grid(row=1,column=1)\n", 108 | "\n", 109 | "isbn_text=StringVar()\n", 110 | "e4=Entry(window,textvariable=isbn_text)\n", 111 | "e4.grid(row=1,column=3)\n", 112 | "\n", 113 | "list1=Listbox(window, height=6,width=35)\n", 114 | "list1.grid(row=2,column=0,rowspan=6,columnspan=2)\n", 115 | "\n", 116 | "sb1=Scrollbar(window)\n", 117 | "sb1.grid(row=2,column=2,rowspan=6)\n", 118 | "\n", 119 | "list1.configure(yscrollcommand=sb1.set)\n", 120 | "sb1.configure(command=list1.yview)\n", 121 | "\n", 122 | "b1=Button(window,text=\"View all\", width=12)\n", 123 | "b1.grid(row=2,column=3)\n", 124 | "\n", 125 | "b2=Button(window,text=\"Search entry\", width=12)\n", 126 | "b2.grid(row=3,column=3)\n", 127 | "\n", 128 | "b3=Button(window,text=\"Add entry\", width=12)\n", 129 | "b3.grid(row=4,column=3)\n", 130 | "\n", 131 | "b4=Button(window,text=\"Update selected\", width=12)\n", 132 | "b4.grid(row=5,column=3)\n", 133 | "\n", 134 | "b5=Button(window,text=\"Delete selected\", width=12)\n", 135 | "b5.grid(row=6,column=3)\n", 136 | "\n", 137 | "b6=Button(window,text=\"Close\", width=12)\n", 138 | "b6.grid(row=7,column=3)\n", 139 | "\n", 140 | "window.mainloop()" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "**Lecture:** [Backend](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775410?start=765)\n", 148 | "---" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 4, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "[(1, 'The Sun', 'John Smith', 1918, 913123132)]\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "import sqlite3\n", 166 | "\n", 167 | "def connect():\n", 168 | " conn=sqlite3.connect(\"books.db\")\n", 169 | " cur=conn.cursor()\n", 170 | " cur.execute(\"CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text, author text, year integer, isbn integer)\")\n", 171 | " conn.commit()\n", 172 | " conn.close()\n", 173 | "\n", 174 | "def insert(title, author, year, isbn):\n", 175 | " conn=sqlite3.connect(\"books.db\")\n", 176 | " cur=conn.cursor()\n", 177 | " cur.execute(\"INSERT INTO book VALUES (NULL,?,?,?,?)\",(title,author,year,isbn))\n", 178 | " conn.commit()\n", 179 | " conn.close()\n", 180 | " view()\n", 181 | "\n", 182 | "def view():\n", 183 | " conn=sqlite3.connect(\"books.db\")\n", 184 | " cur=conn.cursor()\n", 185 | " cur.execute(\"SELECT * FROM book\")\n", 186 | " rows=cur.fetchall()\n", 187 | " conn.close()\n", 188 | " return rows\n", 189 | "\n", 190 | "def search(title=\"\", author=\"\", year=\"\", isbn=\"\"):\n", 191 | " conn=sqlite3.connect(\"books.db\")\n", 192 | " cur=conn.cursor()\n", 193 | " cur.execute(\"SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?\", (title,author,year,isbn))\n", 194 | " rows=cur.fetchall()\n", 195 | " conn.close()\n", 196 | " return rows\n", 197 | "\n", 198 | "def delete(id):\n", 199 | " conn=sqlite3.connect(\"books.db\")\n", 200 | " cur=conn.cursor()\n", 201 | " cur.execute(\"DELETE FROM book WHERE id=?\",(id,))\n", 202 | " conn.commit()\n", 203 | " conn.close()\n", 204 | "\n", 205 | "def update(id, title, author, year, isbn):\n", 206 | " conn=sqlite3.connect(\"books.db\")\n", 207 | " cur=conn.cursor()\n", 208 | " cur.execute(\"UPDATE book SET title=?, author=?, year=?, isbn=? WHERE id=?\",(title,author,year,isbn,id))\n", 209 | " conn.commit()\n", 210 | " conn.close()\n", 211 | "\n", 212 | "connect()\n", 213 | "insert(\"The Sun\", \"John Smith\", 1918, 913123132)\n", 214 | "print(view())\n", 215 | "\n", 216 | "# Other things you can try:\n", 217 | "#delete(3)\n", 218 | "#update(4,\"The moon\",\"John Smooth\",1917,99999)\n", 219 | "#print(search(author=\"John Smooth\"))" 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "**Lecture:** [Connecting the Frontend to the Backend, Part 1](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775412?start=0)\n", 227 | "---" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": {}, 233 | "source": [ 234 | "This lecture covers the part of the process of connecting the frontend to the backend. The code is completed in the next lecture. Please see the complete version of the code in the next lecture." 235 | ] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": {}, 240 | "source": [ 241 | "**Lecture:** [Connecting the Frontend to the Backend, Part 2](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5170648?start=0)\n", 242 | "---" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "Here's the completed `frontend.py` code. To try it, put the `frontend.py` and `backend.py` (provided further below) in the same directory and run `frontend.py` with python `frontend.py`." 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": null, 255 | "metadata": { 256 | "collapsed": true 257 | }, 258 | "outputs": [], 259 | "source": [ 260 | "# frontend.py \n", 261 | "\n", 262 | "from tkinter import *\n", 263 | "\n", 264 | "import backend\n", 265 | "\n", 266 | "def get_selected_row(event):\n", 267 | " global selected_tuple\n", 268 | " index = list1.curselection()[0]\n", 269 | " selected_tuple = list1.get(index)\n", 270 | " e1.delete(0, END)\n", 271 | " e1.insert(END, selected_tuple[1])\n", 272 | " e2.delete(0, END)\n", 273 | " e2.insert(END, selected_tuple[2])\n", 274 | " e3.delete(0, END)\n", 275 | " e3.insert(END, selected_tuple[3])\n", 276 | " e4.delete(0, END)\n", 277 | " e4.insert(END, selected_tuple[4])\n", 278 | "\n", 279 | "def view_command():\n", 280 | " list1.delete(0, END)\n", 281 | " for row in backend.view():\n", 282 | " print(row)\n", 283 | " list1.insert(END, row)\n", 284 | "\n", 285 | "def search_command():\n", 286 | " list1.delete(0, END)\n", 287 | " for row in backend.search(title_text.get(), author_text.get(), year_text.get(), isbn_text.get()):\n", 288 | " list1.insert(END, row)\n", 289 | "\n", 290 | "def add_command():\n", 291 | " backend.insert(title_text.get(), author_text.get(), year_text.get(), isbn_text.get())\n", 292 | " list1.delete(0, END)\n", 293 | " list1.insert(END,(title_text.get(), author_text.get(), year_text.get(), isbn_text.get()))\n", 294 | "\n", 295 | "def delete_command():\n", 296 | " backend.delete(selected_tuple[0])\n", 297 | "\n", 298 | "def update_command():\n", 299 | " backend.update(selected_tuple[0], title_text.get(), author_text.get(), year_text.get(), isbn_text.get())\n", 300 | "\n", 301 | "window=Tk()\n", 302 | "\n", 303 | "window.wm_title(\"BookStore\")\n", 304 | "\n", 305 | "l1=Label(window, text = \"Title\")\n", 306 | "l1.grid(row=0, column=0)\n", 307 | "\n", 308 | "l2=Label(window, text=\"Author\")\n", 309 | "l2.grid(row=0, column=2)\n", 310 | "\n", 311 | "l3=Label(window, text=\"Year\")\n", 312 | "l3.grid(row=1, column=0)\n", 313 | "\n", 314 | "l4=Label(window, text=\"ISBN\")\n", 315 | "l4.grid(row=1, column=2)\n", 316 | "\n", 317 | "title_text = StringVar()\n", 318 | "e1=Entry(window, textvariable = title_text)\n", 319 | "e1.grid(row=0, column=1)\n", 320 | "\n", 321 | "author_text = StringVar()\n", 322 | "e2=Entry(window, textvariable=author_text)\n", 323 | "e2.grid(row=0, column=3)\n", 324 | "\n", 325 | "year_text = StringVar()\n", 326 | "e3=Entry(window, textvariable=year_text)\n", 327 | "e3.grid(row=1,column=1)\n", 328 | "\n", 329 | "isbn_text = StringVar()\n", 330 | "e4=Entry(window, textvariable=isbn_text)\n", 331 | "e4.grid(row=1, column=3)\n", 332 | "\n", 333 | "list1=Listbox(window, height=6, width=35)\n", 334 | "list1.grid(row=2, column=0, rowspan=6, columnspan=2)\n", 335 | "\n", 336 | "sb1=Scrollbar(window)\n", 337 | "sb1.grid(row=2, column=2, rowspan=6)\n", 338 | "\n", 339 | "list1.configure(yscrollcommand=sb1.set)\n", 340 | "sb1.configure(command=list1.yview)\n", 341 | "\n", 342 | "list1.bind('<>', get_selected_row)\n", 343 | "\n", 344 | "b1=Button(window,text=\"View all\", width=12, command=view_command)\n", 345 | "b1.grid(row=2, column=3)\n", 346 | "\n", 347 | "b2=Button(window,text=\"Search entry\", width=12,command=search_command)\n", 348 | "b2.grid(row=3,column=3)\n", 349 | "\n", 350 | "b3=Button(window, text=\"Add entry\", width=12, command=add_command)\n", 351 | "b3.grid(row=4, column=3)\n", 352 | "\n", 353 | "b4=Button(window, text=\"Update selected\", width=12, command=update_command)\n", 354 | "b4.grid(row=5, column=3)\n", 355 | "\n", 356 | "b5=Button(window, text=\"Delete selected\", width=12, command=delete_command)\n", 357 | "b5.grid(row=6, column=3)\n", 358 | "\n", 359 | "b6=Button(window, text=\"Close\", width=12, command=window.destroy)\n", 360 | "b6.grid(row=7, column=3)\n", 361 | "\n", 362 | "window.mainloop()" 363 | ] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "metadata": {}, 368 | "source": [ 369 | "And here's the completed `backend.py` code:" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": null, 375 | "metadata": { 376 | "collapsed": true 377 | }, 378 | "outputs": [], 379 | "source": [ 380 | "# backend.py\n", 381 | "\n", 382 | "import sqlite3\n", 383 | "\n", 384 | "def connect():\n", 385 | " conn=sqlite3.connect(\"books.db\")\n", 386 | " cur=conn.cursor()\n", 387 | " cur.execute(\"CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text, author text, year integer, isbn integer)\")\n", 388 | " conn.commit()\n", 389 | " conn.close()\n", 390 | "\n", 391 | "def insert(title, author, year, isbn):\n", 392 | " conn=sqlite3.connect(\"books.db\")\n", 393 | " cur=conn.cursor()\n", 394 | " cur.execute(\"INSERT INTO book VALUES (NULL,?,?,?,?)\",(title, author, year, isbn))\n", 395 | " conn.commit()\n", 396 | " conn.close()\n", 397 | " view()\n", 398 | "\n", 399 | "def view():\n", 400 | " conn=sqlite3.connect(\"books.db\")\n", 401 | " cur=conn.cursor()\n", 402 | " cur.execute(\"SELECT * FROM book\")\n", 403 | " rows=cur.fetchall()\n", 404 | " conn.close()\n", 405 | " return rows\n", 406 | "\n", 407 | "def search(title=\"\", author=\"\", year=\"\", isbn=\"\"):\n", 408 | " conn=sqlite3.connect(\"books.db\")\n", 409 | " cur=conn.cursor()\n", 410 | " cur.execute(\"SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?\", (title, author, year, isbn))\n", 411 | " rows=cur.fetchall()\n", 412 | " conn.close()\n", 413 | " return rows\n", 414 | "\n", 415 | "def delete(id):\n", 416 | " conn=sqlite3.connect(\"books.db\")\n", 417 | " cur=conn.cursor()\n", 418 | " cur.execute(\"DELETE FROM book WHERE id=?\",(id,))\n", 419 | " conn.commit()\n", 420 | " conn.close()\n", 421 | "\n", 422 | "def update(id, title, author, year, isbn):\n", 423 | " conn=sqlite3.connect(\"books.db\")\n", 424 | " cur=conn.cursor()\n", 425 | " cur.execute(\"UPDATE book SET title=?, author=?, year=?, isbn=? WHERE id=?\",(title, author, year, isbn, id))\n", 426 | " conn.commit()\n", 427 | " conn.close()" 428 | ] 429 | }, 430 | { 431 | "cell_type": "markdown", 432 | "metadata": {}, 433 | "source": [ 434 | "**Practice:** [Fixing the Bug](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/8174886?start=0)\n", 435 | "---" 436 | ] 437 | }, 438 | { 439 | "cell_type": "markdown", 440 | "metadata": {}, 441 | "source": [ 442 | "If you haven't noticed it already, the program has a bug. When the list box is empty and the user clicks it, an `IndexError` is generated in the terminal." 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": {}, 448 | "source": [ 449 | "Why that happens?" 450 | ] 451 | }, 452 | { 453 | "cell_type": "markdown", 454 | "metadata": {}, 455 | "source": [ 456 | "That happens because when the user clicks the list box this code is executed:" 457 | ] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "execution_count": null, 462 | "metadata": { 463 | "collapsed": true 464 | }, 465 | "outputs": [], 466 | "source": [ 467 | "list1.bind('<>',get_selected_row) " 468 | ] 469 | }, 470 | { 471 | "cell_type": "markdown", 472 | "metadata": {}, 473 | "source": [ 474 | "That line calls the `get_selected_row` function:" 475 | ] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "execution_count": null, 480 | "metadata": { 481 | "collapsed": true 482 | }, 483 | "outputs": [], 484 | "source": [ 485 | "def get_selected_row(event):\n", 486 | " global selected_tuple\n", 487 | " index=list1.curselection()[0]\n", 488 | " selected_tuple=list1.get(index)\n", 489 | " e1.delete(0,END)\n", 490 | " e1.insert(END,selected_tuple[1])\n", 491 | " e2.delete(0,END)\n", 492 | " e2.insert(END,selected_tuple[2])\n", 493 | " e3.delete(0,END)\n", 494 | " e3.insert(END,selected_tuple[3])\n", 495 | " e4.delete(0,END)\n", 496 | " e4.insert(END,selected_tuple[4])" 497 | ] 498 | }, 499 | { 500 | "cell_type": "markdown", 501 | "metadata": {}, 502 | "source": [ 503 | "Since the listbox is empty, then `list1.curselection()` will be an empty list with no items. Trying to access the first item of that list with `[0]` in line 3 will throw an error since there is no first item in the list. " 504 | ] 505 | }, 506 | { 507 | "cell_type": "markdown", 508 | "metadata": {}, 509 | "source": [ 510 | "Please try to fix that bug. The next lecture contains the solution." 511 | ] 512 | }, 513 | { 514 | "cell_type": "markdown", 515 | "metadata": {}, 516 | "source": [ 517 | "**Solution:** [Fixing the Bug](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/8174962?start=0)\n", 518 | "---" 519 | ] 520 | }, 521 | { 522 | "cell_type": "markdown", 523 | "metadata": {}, 524 | "source": [ 525 | "Changing the `def_selected_row` function to this one below fixes the issue:" 526 | ] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "execution_count": null, 531 | "metadata": { 532 | "collapsed": true 533 | }, 534 | "outputs": [], 535 | "source": [ 536 | "def get_selected_row(event):\n", 537 | " try:\n", 538 | " global selected_tuple\n", 539 | " index=list1.curselection()[0]\n", 540 | " selected_tuple=list1.get(index)\n", 541 | " e1.delete(0,END)\n", 542 | " e1.insert(END,selected_tuple[1])\n", 543 | " e2.delete(0,END)\n", 544 | " e2.insert(END,selected_tuple[2])\n", 545 | " e3.delete(0,END)\n", 546 | " e3.insert(END,selected_tuple[3])\n", 547 | " e4.delete(0,END)\n", 548 | " e4.insert(END,selected_tuple[4])\n", 549 | " except Index" 550 | ] 551 | }, 552 | { 553 | "cell_type": "markdown", 554 | "metadata": {}, 555 | "source": [ 556 | "Explanation:" 557 | ] 558 | }, 559 | { 560 | "cell_type": "markdown", 561 | "metadata": {}, 562 | "source": [ 563 | "As you can see the error was fixed by simply implementing a try and except block. When the get_selected_row function is called, Python will try to execute the indented block under try . If there is an IndexError none of the lines under try will be executed. Instead the line under except will be executed which is pass . The pass stetement means do nothing. So the function will do nothing when there's an empty listbox." 564 | ] 565 | }, 566 | { 567 | "cell_type": "markdown", 568 | "metadata": {}, 569 | "source": [ 570 | "**Lecture:** [Creating a Standalone Executable Version of the Program](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775418?start=0)\n", 571 | "---" 572 | ] 573 | }, 574 | { 575 | "cell_type": "markdown", 576 | "metadata": {}, 577 | "source": [ 578 | "This lecture covers the process of how to convert your .py files into executable files that can run on any computer." 579 | ] 580 | }, 581 | { 582 | "cell_type": "markdown", 583 | "metadata": {}, 584 | "source": [ 585 | "**Lecture:** [Final Code of Application 5]()\n", 586 | "---" 587 | ] 588 | }, 589 | { 590 | "cell_type": "code", 591 | "execution_count": null, 592 | "metadata": { 593 | "collapsed": true 594 | }, 595 | "outputs": [], 596 | "source": [ 597 | "# frontend.py \n", 598 | "\n", 599 | "from tkinter import *\n", 600 | "\n", 601 | "import backend\n", 602 | "\n", 603 | "def get_selected_row(event):\n", 604 | " try:\n", 605 | " global selected_tuple\n", 606 | " index=list1.curselection()[0]\n", 607 | " selected_tuple=list1.get(index)\n", 608 | " e1.delete(0,END)\n", 609 | " e1.insert(END,selected_tuple[1])\n", 610 | " e2.delete(0,END)\n", 611 | " e2.insert(END,selected_tuple[2])\n", 612 | " e3.delete(0,END)\n", 613 | " e3.insert(END,selected_tuple[3])\n", 614 | " e4.delete(0,END)\n", 615 | " e4.insert(END,selected_tuple[4])\n", 616 | " except Index\n", 617 | "\n", 618 | "def view_command():\n", 619 | " list1.delete(0, END)\n", 620 | " for row in backend.view():\n", 621 | " print(row)\n", 622 | " list1.insert(END, row)\n", 623 | "\n", 624 | "def search_command():\n", 625 | " list1.delete(0, END)\n", 626 | " for row in backend.search(title_text.get(), author_text.get(), year_text.get(), isbn_text.get()):\n", 627 | " list1.insert(END, row)\n", 628 | "\n", 629 | "def add_command():\n", 630 | " backend.insert(title_text.get(), author_text.get(), year_text.get(), isbn_text.get())\n", 631 | " list1.delete(0, END)\n", 632 | " list1.insert(END,(title_text.get(), author_text.get(), year_text.get(), isbn_text.get()))\n", 633 | "\n", 634 | "def delete_command():\n", 635 | " backend.delete(selected_tuple[0])\n", 636 | "\n", 637 | "def update_command():\n", 638 | " backend.update(selected_tuple[0], title_text.get(), author_text.get(), year_text.get(), isbn_text.get())\n", 639 | "\n", 640 | "window=Tk()\n", 641 | "\n", 642 | "window.wm_title(\"BookStore\")\n", 643 | "\n", 644 | "l1=Label(window, text = \"Title\")\n", 645 | "l1.grid(row=0, column=0)\n", 646 | "\n", 647 | "l2=Label(window, text=\"Author\")\n", 648 | "l2.grid(row=0, column=2)\n", 649 | "\n", 650 | "l3=Label(window, text=\"Year\")\n", 651 | "l3.grid(row=1, column=0)\n", 652 | "\n", 653 | "l4=Label(window, text=\"ISBN\")\n", 654 | "l4.grid(row=1, column=2)\n", 655 | "\n", 656 | "title_text = StringVar()\n", 657 | "e1=Entry(window, textvariable = title_text)\n", 658 | "e1.grid(row=0, column=1)\n", 659 | "\n", 660 | "author_text = StringVar()\n", 661 | "e2=Entry(window, textvariable=author_text)\n", 662 | "e2.grid(row=0, column=3)\n", 663 | "\n", 664 | "year_text = StringVar()\n", 665 | "e3=Entry(window, textvariable=year_text)\n", 666 | "e3.grid(row=1,column=1)\n", 667 | "\n", 668 | "isbn_text = StringVar()\n", 669 | "e4=Entry(window, textvariable=isbn_text)\n", 670 | "e4.grid(row=1, column=3)\n", 671 | "\n", 672 | "list1=Listbox(window, height=6, width=35)\n", 673 | "list1.grid(row=2, column=0, rowspan=6, columnspan=2)\n", 674 | "\n", 675 | "sb1=Scrollbar(window)\n", 676 | "sb1.grid(row=2, column=2, rowspan=6)\n", 677 | "\n", 678 | "list1.configure(yscrollcommand=sb1.set)\n", 679 | "sb1.configure(command=list1.yview)\n", 680 | "\n", 681 | "list1.bind('<>', get_selected_row)\n", 682 | "\n", 683 | "b1=Button(window,text=\"View all\", width=12, command=view_command)\n", 684 | "b1.grid(row=2, column=3)\n", 685 | "\n", 686 | "b2=Button(window,text=\"Search entry\", width=12,command=search_command)\n", 687 | "b2.grid(row=3,column=3)\n", 688 | "\n", 689 | "b3=Button(window, text=\"Add entry\", width=12, command=add_command)\n", 690 | "b3.grid(row=4, column=3)\n", 691 | "\n", 692 | "b4=Button(window, text=\"Update selected\", width=12, command=update_command)\n", 693 | "b4.grid(row=5, column=3)\n", 694 | "\n", 695 | "b5=Button(window, text=\"Delete selected\", width=12, command=delete_command)\n", 696 | "b5.grid(row=6, column=3)\n", 697 | "\n", 698 | "b6=Button(window, text=\"Close\", width=12, command=window.destroy)\n", 699 | "b6.grid(row=7, column=3)\n", 700 | "\n", 701 | "window.mainloop()" 702 | ] 703 | }, 704 | { 705 | "cell_type": "markdown", 706 | "metadata": {}, 707 | "source": [ 708 | "And here's the completed `backend.py` code:" 709 | ] 710 | }, 711 | { 712 | "cell_type": "code", 713 | "execution_count": null, 714 | "metadata": { 715 | "collapsed": true 716 | }, 717 | "outputs": [], 718 | "source": [ 719 | "# backend.py\n", 720 | "\n", 721 | "import sqlite3\n", 722 | "\n", 723 | "def connect():\n", 724 | " conn=sqlite3.connect(\"books.db\")\n", 725 | " cur=conn.cursor()\n", 726 | " cur.execute(\"CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text, author text, year integer, isbn integer)\")\n", 727 | " conn.commit()\n", 728 | " conn.close()\n", 729 | "\n", 730 | "def insert(title, author, year, isbn):\n", 731 | " conn=sqlite3.connect(\"books.db\")\n", 732 | " cur=conn.cursor()\n", 733 | " cur.execute(\"INSERT INTO book VALUES (NULL,?,?,?,?)\",(title, author, year, isbn))\n", 734 | " conn.commit()\n", 735 | " conn.close()\n", 736 | " view()\n", 737 | "\n", 738 | "def view():\n", 739 | " conn=sqlite3.connect(\"books.db\")\n", 740 | " cur=conn.cursor()\n", 741 | " cur.execute(\"SELECT * FROM book\")\n", 742 | " rows=cur.fetchall()\n", 743 | " conn.close()\n", 744 | " return rows\n", 745 | "\n", 746 | "def search(title=\"\", author=\"\", year=\"\", isbn=\"\"):\n", 747 | " conn=sqlite3.connect(\"books.db\")\n", 748 | " cur=conn.cursor()\n", 749 | " cur.execute(\"SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?\", (title, author, year, isbn))\n", 750 | " rows=cur.fetchall()\n", 751 | " conn.close()\n", 752 | " return rows\n", 753 | "\n", 754 | "def delete(id):\n", 755 | " conn=sqlite3.connect(\"books.db\")\n", 756 | " cur=conn.cursor()\n", 757 | " cur.execute(\"DELETE FROM book WHERE id=?\",(id,))\n", 758 | " conn.commit()\n", 759 | " conn.close()\n", 760 | "\n", 761 | "def update(id, title, author, year, isbn):\n", 762 | " conn=sqlite3.connect(\"books.db\")\n", 763 | " cur=conn.cursor()\n", 764 | " cur.execute(\"UPDATE book SET title=?, author=?, year=?, isbn=? WHERE id=?\",(title, author, year, isbn, id))\n", 765 | " conn.commit()\n", 766 | " conn.close()" 767 | ] 768 | } 769 | ], 770 | "metadata": { 771 | "kernelspec": { 772 | "display_name": "Python 3", 773 | "language": "python", 774 | "name": "python3" 775 | }, 776 | "language_info": { 777 | "codemirror_mode": { 778 | "name": "ipython", 779 | "version": 3 780 | }, 781 | "file_extension": ".py", 782 | "mimetype": "text/x-python", 783 | "name": "python", 784 | "nbconvert_exporter": "python", 785 | "pygments_lexer": "ipython3", 786 | "version": "3.6.3" 787 | } 788 | }, 789 | "nbformat": 4, 790 | "nbformat_minor": 2 791 | } 792 | -------------------------------------------------------------------------------- /S14-Object-Oriented-Programming.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python Mega Course: Build 10 Real World Applications\n", 8 | "---" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "This notebook is a summary of [The Python Mega Course: Build 10 Real World Applciations](https://www.udemy.com/the-python-mega-course), a comprehensive online Python course taught by Ardit Sulce. Each lecture name is clickable and takes you to the video lecture in the course." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "# Section 14: Object Oriented Programming\n", 23 | "***" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "**Lecture:** [Object Oriented Programming Explained](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5170344?start=45)\n", 31 | "---" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "Object oriented programming is a way to organize programs by encapsulating things in objects. Click on the lecture link to learn more." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "**Lecture:** [Turning this Application into OOP Style, Part 1](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5170346?start=15)\n", 46 | "---" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "This and the other lecture cover the process of turning the tkinter app into an object oriented design. The complete code is provided in the next lecture." 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "\n", 61 | "**Lecture:** [Turning this Application into OOP Style, Part 2](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5170348?start=0)\n", 62 | "---" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "Below is the code for `backend.py`:" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": { 76 | "collapsed": true 77 | }, 78 | "outputs": [], 79 | "source": [ 80 | "#backend.py\n", 81 | "\n", 82 | "import sqlite3\n", 83 | "\n", 84 | "class Database:\n", 85 | " def __init__(self, db):\n", 86 | " self.conn=sqlite3.connect(db)\n", 87 | " self.cur=self.conn.cursor()\n", 88 | " self.cur.execute(\"CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text, author text, year integer, isbn integer)\")\n", 89 | " self.conn.commit()\n", 90 | "\n", 91 | " def insert(self, title, author, year, isbn):\n", 92 | " self.cur.execute(\"INSERT INTO book VALUES (NULL,?,?,?,?)\",(title, author, year, isbn))\n", 93 | " self.conn.commit()\n", 94 | "\n", 95 | " def view(self):\n", 96 | " self.cur.execute(\"SELECT * FROM book\")\n", 97 | " rows=self.cur.fetchall()\n", 98 | " return rows\n", 99 | "\n", 100 | " def search(self, title=\"\", author=\"\", year=\"\", isbn=\"\"):\n", 101 | " self.cur.execute(\"SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?\", (title,author,year,isbn))\n", 102 | " rows=self.cur.fetchall()\n", 103 | " return rows\n", 104 | "\n", 105 | " def delete(self, id):\n", 106 | " self.cur.execute(\"DELETE FROM book WHERE id=?\",(id,))\n", 107 | " self.conn.commit()\n", 108 | "\n", 109 | " def update(self, id, title, author, year, isbn):\n", 110 | " self.cur.execute(\"UPDATE book SET title=?, author=?, year=?, isbn=? WHERE id=?\",(title, author, year, isbn, id))\n", 111 | " self.conn.commit()\n", 112 | "\n", 113 | " def __del__(self):\n", 114 | " self.conn.close\n" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "Here's the code for `frontend.py`:" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 2, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "ename": "ModuleNotFoundError", 131 | "evalue": "No module named 'backend'", 132 | "output_type": "error", 133 | "traceback": [ 134 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 135 | "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", 136 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtkinter\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mbackend\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mDatabase\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mdatabase\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mDatabase\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"books.db\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 137 | "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'backend'" 138 | ] 139 | } 140 | ], 141 | "source": [ 142 | "#frontend.py\n", 143 | "\n", 144 | "from tkinter import *\n", 145 | "from backend import Database\n", 146 | "\n", 147 | "database=Database(\"books.db\")\n", 148 | "\n", 149 | "class Window(object):\n", 150 | "\n", 151 | " def __init__(self, window):\n", 152 | "\n", 153 | " self.window = window\n", 154 | "\n", 155 | " self.window.wm_title(\"BookStore\")\n", 156 | "\n", 157 | " l1 = Label(window, text=\"Title\")\n", 158 | " l1.grid(row=0, column=0)\n", 159 | "\n", 160 | " l2 = Label(window, text=\"Author\")\n", 161 | " l2.grid(row=0, column=2)\n", 162 | "\n", 163 | " l3 = Label(window, text=\"Year\")\n", 164 | " l3.grid(row=1, column=0)\n", 165 | "\n", 166 | " l4 = Label(window, text=\"ISBN\")\n", 167 | " l4.grid(row=1, column=2)\n", 168 | "\n", 169 | " self.title_text = StringVar()\n", 170 | " self.e1 = Entry(window, textvariable=self.title_text)\n", 171 | " self.e1.grid(row=0, column=1)\n", 172 | "\n", 173 | " self.author_text = StringVar()\n", 174 | " self.e2 = Entry(window, textvariable=self.author_text)\n", 175 | " self.e2.grid(row=0, column=3)\n", 176 | "\n", 177 | " self.year_text=StringVar()\n", 178 | " self.e3=Entry(window,textvariable=self.year_text)\n", 179 | " self.e3.grid(row=1,column=1)\n", 180 | "\n", 181 | " self.isbn_text = StringVar()\n", 182 | " self.e4 = Entry(window, textvariable=self.isbn_text)\n", 183 | " self.e4.grid(row=1, column=3)\n", 184 | "\n", 185 | " self.list1 = Listbox(window, height=6,width=35)\n", 186 | " self.list1.grid(row=2, column=0, rowspan=6, columnspan=2)\n", 187 | "\n", 188 | " sb1 = Scrollbar(window)\n", 189 | " sb1.grid(row=2, column=2, rowspan=6)\n", 190 | "\n", 191 | " self.list1.configure(yscrollcommand=sb1.set)\n", 192 | " sb1.configure(command=self.list1.yview)\n", 193 | "\n", 194 | " self.list1.bind('<>', self.get_selected_row)\n", 195 | "\n", 196 | " b1 = Button(window, text=\"View all\", width=12, command=self.view_command)\n", 197 | " b1.grid(row=2, column=3)\n", 198 | "\n", 199 | " b2 = Button(window, text=\"Search entry\", width=12, command=self.search_command)\n", 200 | " b2.grid(row=3, column=3)\n", 201 | "\n", 202 | " b3 = Button(window, text=\"Add entry\", width=12, command=self.add_command)\n", 203 | " b3.grid(row=4, column=3)\n", 204 | "\n", 205 | " b4 = Button(window, text=\"Update selected\", width=12, command=self.update_command)\n", 206 | " b4.grid(row=5, column=3)\n", 207 | "\n", 208 | " b5 = Button(window, text=\"Delete selected\", width=12, command=self.delete_command)\n", 209 | " b5.grid(row=6,column=3)\n", 210 | "\n", 211 | " b6=Button(window, text=\"Close\", width=12, command=window.destroy)\n", 212 | " b6.grid(row=7, column=3)\n", 213 | "\n", 214 | " def get_selected_row(self, event):\n", 215 | " index = self.list1.curselection()[0]\n", 216 | " self.selected_tuple = self.list1.get(index)\n", 217 | " self.e1.delete(0, END)\n", 218 | " self.e1.insert(END, self.selected_tuple[1])\n", 219 | " self.e2.delete(0, END)\n", 220 | " self.e2.insert(END, self.selected_tuple[2])\n", 221 | " self.e3.delete(0, END)\n", 222 | " self.e3.insert(END, self.selected_tuple[3])\n", 223 | " self.e4.delete(0, END)\n", 224 | " self.e4.insert(END, self.selected_tuple[4])\n", 225 | "\n", 226 | " def view_command(self):\n", 227 | " self.list1.delete(0, END)\n", 228 | " for row in database.view():\n", 229 | " self.list1.insert(END, row)\n", 230 | "\n", 231 | " def search_command(self):\n", 232 | " self.list1.delete(0, END)\n", 233 | " for row in database.search(self.title_text.get(), self.author_text.get(), self.year_text.get(), self.isbn_text.get()):\n", 234 | " self.list1.insert(END, row)\n", 235 | "\n", 236 | " def add_command(self):\n", 237 | " database.insert(self.title_text.get(), self.author_text.get(), self.year_text.get(), self.isbn_text.get())\n", 238 | " self.list1.delete(0,END)\n", 239 | " self.list1.insert(END, (self.title_text.get(), self.author_text.get(), self.year_text.get(), self.isbn_text.get()))\n", 240 | "\n", 241 | " def delete_command(self):\n", 242 | " database.delete(self.selected_tuple[0])\n", 243 | "\n", 244 | " def update_command(self):\n", 245 | " database.update(self.selected_tuple[0], self.title_text.get(), self.author_text.get(), self.year_text.get(), self.isbn_text.get())\n", 246 | "\n", 247 | "window=Tk()\n", 248 | "Window(window)\n", 249 | "window.mainloop()" 250 | ] 251 | }, 252 | { 253 | "cell_type": "markdown", 254 | "metadata": {}, 255 | "source": [ 256 | "**Lecture:** [Creating a Bank Account Object](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5170352?start=1200)\n", 257 | "---" 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": {}, 263 | "source": [ 264 | "The code below creates an `Account` class, and after that we create an `account` object instance. For this program to work, make sure you have created a `balance.txt` file staying in the same directory with your code and type in a number inside `balance.txt`. Then run the program. The program will add 200 to the number you typed in the text file." 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 15, 270 | "metadata": { 271 | "scrolled": true 272 | }, 273 | "outputs": [ 274 | { 275 | "name": "stdout", 276 | "output_type": "stream", 277 | "text": [ 278 | "Money before depositing: 1010\n", 279 | "Money after depositing: 1210\n" 280 | ] 281 | } 282 | ], 283 | "source": [ 284 | "class Account:\n", 285 | "\n", 286 | " def __init__(self, filepath):\n", 287 | " self.filepath = filepath\n", 288 | " with open(filepath, 'r') as file:\n", 289 | " self.balance = int(file.read())\n", 290 | "\n", 291 | " def withdraw(self, amount):\n", 292 | " self.balance = self.balance - amount\n", 293 | "\n", 294 | " def deposit(self, amount):\n", 295 | " self.balance = self.balance + amount\n", 296 | "\n", 297 | " def commit(self):\n", 298 | " with open(self.filepath, 'w') as file:\n", 299 | " file.write(str(self.balance))\n", 300 | "\n", 301 | "account = Account(\"balance.txt\")\n", 302 | "print(\"Money before depositing:\", account.balance)\n", 303 | "\n", 304 | "account.deposit(200)\n", 305 | "print(\"Money after depositing:\", account.balance)\n", 306 | "account.commit()" 307 | ] 308 | }, 309 | { 310 | "cell_type": "markdown", 311 | "metadata": {}, 312 | "source": [ 313 | "**Lecture:** [Inheritance](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5170354?start=0)\n", 314 | "---" 315 | ] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "metadata": {}, 320 | "source": [ 321 | "Inheritance is the process of creating a new class out of another class. The new (child) class will be exactly like the its parent, but you can also add other methods to that child class." 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": {}, 327 | "source": [ 328 | "In the example below, we have an `Account` class and a `Checking` class which is like Account, but it also has a `transfer` method that allows to transfer money." 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 18, 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "name": "stdout", 338 | "output_type": "stream", 339 | "text": [ 340 | "907\n" 341 | ] 342 | } 343 | ], 344 | "source": [ 345 | "class Account:\n", 346 | "\n", 347 | " def __init__(self, filepath):\n", 348 | " self.filepath = filepath\n", 349 | " with open(filepath, 'r') as file:\n", 350 | " self.balance = int(file.read())\n", 351 | "\n", 352 | " def withdraw(self, amount):\n", 353 | " self.balance = self.balance - amount\n", 354 | "\n", 355 | " def deposit(self, amount):\n", 356 | " self.balance = self.balance + amount\n", 357 | "\n", 358 | " def commit(self):\n", 359 | " with open(self.filepath, 'w') as file:\n", 360 | " file.write(str(self.balance))\n", 361 | "\n", 362 | "class Checking(Account):\n", 363 | " \"\"\"This class generates checking account objects\"\"\"\n", 364 | "\n", 365 | " type=\"checking\"\n", 366 | "\n", 367 | " def __init__(self, filepath, fee):\n", 368 | " Account.__init__(self, filepath)\n", 369 | " self.fee = fee\n", 370 | "\n", 371 | " def transfer(self, amount):\n", 372 | " self.balance = self.balance - amount - self.fee\n", 373 | "\n", 374 | "checking = Checking(\"balance.txt\", 1)\n", 375 | "checking.transfer(100)\n", 376 | "print(checking.balance)\n", 377 | "checking.commit()" 378 | ] 379 | }, 380 | { 381 | "cell_type": "markdown", 382 | "metadata": {}, 383 | "source": [ 384 | "**Lecture:** [OOP Glossary](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5170356?start=0)\n", 385 | "---" 386 | ] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "metadata": {}, 391 | "source": [ 392 | "**Class**: A blueprint that allows the creation of objects." 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "**Object instance**: An object created by a class." 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "**Instance variable**: A variable that can be accessed from the object (e.g. `account.balance`)" 407 | ] 408 | }, 409 | { 410 | "cell_type": "markdown", 411 | "metadata": {}, 412 | "source": [ 413 | "**Class variable**: A variable that can be accessed from the class (e.g. `Account.balance`)" 414 | ] 415 | }, 416 | { 417 | "cell_type": "markdown", 418 | "metadata": {}, 419 | "source": [ 420 | "**Doc string**: A string that can be accessed through `__doc__` (e.g. `checking.__doc__`)" 421 | ] 422 | }, 423 | { 424 | "cell_type": "markdown", 425 | "metadata": {}, 426 | "source": [ 427 | "**Data member**: It refers to either a class variable or an instance variable" 428 | ] 429 | }, 430 | { 431 | "cell_type": "markdown", 432 | "metadata": {}, 433 | "source": [ 434 | "**Constructor**: The method that constructs the object (i.e. `__init__` method)" 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "metadata": {}, 440 | "source": [ 441 | "**Data member**: It refers to either a class variable or an instance variable" 442 | ] 443 | }, 444 | { 445 | "cell_type": "markdown", 446 | "metadata": {}, 447 | "source": [ 448 | "**Method**: A function defined inside a class." 449 | ] 450 | }, 451 | { 452 | "cell_type": "markdown", 453 | "metadata": {}, 454 | "source": [ 455 | "**Instantiation**: The process of creating an object (e.g. `account = Account(\"balance.txt\")`)" 456 | ] 457 | }, 458 | { 459 | "cell_type": "markdown", 460 | "metadata": {}, 461 | "source": [ 462 | "**Inheritance**: The process of creating an class out of another class" 463 | ] 464 | }, 465 | { 466 | "cell_type": "markdown", 467 | "metadata": {}, 468 | "source": [ 469 | "**Attributes**: Data members accessed from the object (e.g. data member `balance` accessed via `account.balance`)" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "metadata": {}, 475 | "source": [ 476 | "**Practice:** [GUI in OOP Design](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5793824?start=0)\n", 477 | "---" 478 | ] 479 | }, 480 | { 481 | "cell_type": "markdown", 482 | "metadata": {}, 483 | "source": [ 484 | "Please alter the `frontend.py` script containing the GUI code by changing its functional-oriented design into an OOP design. You can find the code of `frontend.py` and the `backend.py` files further above." 485 | ] 486 | }, 487 | { 488 | "cell_type": "markdown", 489 | "metadata": {}, 490 | "source": [ 491 | "**Solution:** [GUI in OOP Design](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/5793872?start=0)\n", 492 | "---" 493 | ] 494 | }, 495 | { 496 | "cell_type": "markdown", 497 | "metadata": {}, 498 | "source": [ 499 | "Here are the `frontend.py` and `backend.py` scripts in OOP style. To try them you should run `python frontend.py`." 500 | ] 501 | }, 502 | { 503 | "cell_type": "code", 504 | "execution_count": null, 505 | "metadata": { 506 | "collapsed": true 507 | }, 508 | "outputs": [], 509 | "source": [ 510 | "#frontend.py\n", 511 | "\n", 512 | "from tkinter import *\n", 513 | "from backend import Database\n", 514 | " \n", 515 | "database = Database(\"books.db\")\n", 516 | " \n", 517 | "class Window(object):\n", 518 | " \n", 519 | " def __init__(self, window):\n", 520 | " \n", 521 | " self.window = window\n", 522 | " \n", 523 | " self.window.wm_title(\"BookStore\")\n", 524 | " \n", 525 | " l1 = Label(window, text=\"Title\")\n", 526 | " l1.grid(row=0, column=0)\n", 527 | " \n", 528 | " l2 = Label(window, text=\"Author\")\n", 529 | " l2.grid(row=0, column=2)\n", 530 | " \n", 531 | " l3 = Label(window, text=\"Year\")\n", 532 | " l3.grid(row=1, column=0)\n", 533 | " \n", 534 | " l4 = Label(window, text=\"ISBN\")\n", 535 | " l4.grid(row=1, column=2)\n", 536 | " \n", 537 | " self.title_text = StringVar()\n", 538 | " self.e1 = Entry(window, textvariable=self.title_text)\n", 539 | " self.e1.grid(row=0, column=1)\n", 540 | " \n", 541 | " self.author_text = StringVar()\n", 542 | " self.e2 = Entry(window, textvariable=self.author_text)\n", 543 | " self.e2.grid(row=0, column=3)\n", 544 | " \n", 545 | " self.year_text = StringVar()\n", 546 | " self.e3 = Entry(window, textvariable=self.year_text)\n", 547 | " self.e3.grid(row=1, column=1)\n", 548 | " \n", 549 | " self.isbn_text = StringVar()\n", 550 | " self.e4 = Entry(window, textvariable=self.isbn_text)\n", 551 | " self.e4.grid(row=1, column=3)\n", 552 | " \n", 553 | " self.list1 = Listbox(window, height=6, width=35)\n", 554 | " self.list1.grid(row=2, column=0, rowspan=6, columnspan=2)\n", 555 | " \n", 556 | " sb1 = Scrollbar(window)\n", 557 | " sb1.grid(row=2, column=2, rowspan=6)\n", 558 | " \n", 559 | " self.list1.configure(yscrollcommand=sb1.set)\n", 560 | " sb1.configure(command=self.list1.yview)\n", 561 | " \n", 562 | " self.list1.bind('<>', self.get_selected_row)\n", 563 | " \n", 564 | " b1 = Button(window, text=\"View all\", width=12, command=self.view_command)\n", 565 | " b1.grid(row=2, column=3)\n", 566 | " \n", 567 | " b2 = Button(window, text=\"Search entry\", width=12, command=self.search_command)\n", 568 | " b2.grid(row=3, column=3)\n", 569 | " \n", 570 | " b3 = Button(window, text=\"Add entry\", width=12, command=self.add_command)\n", 571 | " b3.grid(row=4, column=3)\n", 572 | " \n", 573 | " b4 = Button(window, text=\"Update selected\", width=12, command=self.update_command)\n", 574 | " b4.grid(row=5, column=3)\n", 575 | " \n", 576 | " b5 = Button(window, text=\"Delete selected\", width=12, command=self.delete_command)\n", 577 | " b5.grid(row=6, column=3)\n", 578 | " \n", 579 | " b6 = Button(window, text=\"Close\", width=12, command=window.destroy)\n", 580 | " b6.grid(row=7, column=3)\n", 581 | " \n", 582 | " def get_selected_row(self,event):\n", 583 | " index = self.list1.curselection()[0]\n", 584 | " self.selected_tuple = self.list1.get(index)\n", 585 | " self.e1.delete(0, END)\n", 586 | " self.e1.insert(END, self.selected_tuple[1])\n", 587 | " self.e2.delete(0, END)\n", 588 | " self.e2.insert(END, self.selected_tuple[2])\n", 589 | " self.e3.delete(0, END)\n", 590 | " self.e3.insert(END, self.selected_tuple[3])\n", 591 | " self.e4.delete(0, END)\n", 592 | " self.e4.insert(END, self.selected_tuple[4])\n", 593 | " \n", 594 | " def view_command(self):\n", 595 | " self.list1.delete(0, END)\n", 596 | " for row in database.view():\n", 597 | " self.list1.insert(END, row)\n", 598 | " \n", 599 | " def search_command(self):\n", 600 | " self.list1.delete(0, END)\n", 601 | " for row in database.search(self.title_text.get(), self.author_text.get(), self.year_text.get(), self.isbn_text.get()):\n", 602 | " self.list1.insert(END, row)\n", 603 | " \n", 604 | " def add_command(self):\n", 605 | " database.insert(self.title_text.get(), self.author_text.get(), self.year_text.get(), self.isbn_text.get())\n", 606 | " self.list1.delete(0, END)\n", 607 | " self.list1.insert(END, (self.title_text.get(), self.author_text.get(), self.year_text.get(), self.isbn_text.get()))\n", 608 | " \n", 609 | " def delete_command(self):\n", 610 | " database.delete(self.selected_tuple[0])\n", 611 | " \n", 612 | " def update_command(self):\n", 613 | " database.update(self.selected_tuple[0], self.title_text.get(), self.author_text.get(), self.year_text.get(), self.isbn_text.get())\n", 614 | " \n", 615 | "window = Tk()\n", 616 | "Window(window)\n", 617 | "window.mainloop()\n" 618 | ] 619 | }, 620 | { 621 | "cell_type": "code", 622 | "execution_count": null, 623 | "metadata": { 624 | "collapsed": true 625 | }, 626 | "outputs": [], 627 | "source": [ 628 | "#backend.py\n", 629 | "\n", 630 | "import sqlite3\n", 631 | "\n", 632 | "class Database:\n", 633 | " def __init__(self, db):\n", 634 | " self.conn = sqlite3.connect(db)\n", 635 | " self.cur = self.conn.cursor()\n", 636 | " self.cur.execute(\"CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text, author text, year integer, isbn integer)\")\n", 637 | " self.conn.commit()\n", 638 | " def insert(self, title, author, year, isbn):\n", 639 | " self.cur.execute(\"INSERT INTO book VALUES (NULL,?,?,?,?)\",(title, author, year, isbn))\n", 640 | " self.conn.commit()\n", 641 | " def view(self):\n", 642 | " self.cur.execute(\"SELECT * FROM book\")\n", 643 | " rows = self.cur.fetchall()\n", 644 | " return rows\n", 645 | " def search(self, title=\"\", author=\"\", year=\"\", isbn=\"\"):\n", 646 | " self.cur.execute(\"SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?\", (title,author,year,isbn))\n", 647 | " rows = self.cur.fetchall()\n", 648 | " return rows\n", 649 | " def delete(self, id):\n", 650 | " self.cur.execute(\"DELETE FROM book WHERE id=?\",(id,))\n", 651 | " self.conn.commit()\n", 652 | " def update(self, id, title, author, year, isbn):\n", 653 | " self.cur.execute(\"UPDATE book SET title=?, author=?, year=?, isbn=? WHERE id=?\",(title, author, year, isbn, id))\n", 654 | " self.conn.commit()\n", 655 | " def __del__(self):\n", 656 | " self.conn.close()" 657 | ] 658 | } 659 | ], 660 | "metadata": { 661 | "kernelspec": { 662 | "display_name": "Python 3", 663 | "language": "python", 664 | "name": "python3" 665 | }, 666 | "language_info": { 667 | "codemirror_mode": { 668 | "name": "ipython", 669 | "version": 3 670 | }, 671 | "file_extension": ".py", 672 | "mimetype": "text/x-python", 673 | "name": "python", 674 | "nbconvert_exporter": "python", 675 | "pygments_lexer": "ipython3", 676 | "version": "3.6.3" 677 | } 678 | }, 679 | "nbformat": 4, 680 | "nbformat_minor": 2 681 | } 682 | -------------------------------------------------------------------------------- /S16-App-6-Build-a-Webcam-Motion-Detector.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python Mega Course: Build 10 Real World Applications\n", 8 | "---" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "This notebook is a summary of [The Python Mega Course: Build 10 Real World Applciations](https://www.udemy.com/the-python-mega-course), a comprehensive online Python course taught by Ardit Sulce. Each lecture name is clickable and takes you to the video lecture in the course." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "# Section 16: Application 6: Build a Webcam Motion Detector\n", 23 | "***" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "**Lecture:** [Program Demonstration](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775504?start=0)\n", 31 | "---" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "This video lecture shows the finished version of the website running on a browser." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "**Lecture:** [Detecting Objects on Camera](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775502?start=0)\n", 46 | "---" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": { 53 | "collapsed": true 54 | }, 55 | "outputs": [], 56 | "source": [ 57 | "import cv2, time, pandas\n", 58 | "\n", 59 | "first_frame = None\n", 60 | "\n", 61 | "video=cv2.VideoCapture(0)\n", 62 | "\n", 63 | "while True:\n", 64 | " check, frame = video.read()\n", 65 | " status = 0\n", 66 | " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n", 67 | " gray = cv2.GaussianBlur(gray,(21,21), 0)\n", 68 | "\n", 69 | " if first_frame is None:\n", 70 | " first_frame = gray\n", 71 | " continue\n", 72 | "\n", 73 | " delta_frame = cv2.absdiff(first_frame,gray)\n", 74 | " thresh_frame = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]\n", 75 | " thresh_frame = cv2.dilate(thresh_frame, None, iterations=2)\n", 76 | "\n", 77 | " (_,cnts,_) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n", 78 | "\n", 79 | " for contour in cnts:\n", 80 | " if cv2.contourArea(contour) < 10000:\n", 81 | " continue\n", 82 | " (x, y, w, h) = cv2.boundingRect(contour)\n", 83 | " cv2.rectangle(frame, (x, y), (x+w, y+h), (0,255,0), 3)\n", 84 | "\n", 85 | " cv2.imshow(\"Gray Frame\", gray)\n", 86 | " cv2.imshow(\"Delta Frame\", delta_frame)\n", 87 | " cv2.imshow(\"Threshold Frame\", thresh_frame)\n", 88 | " cv2.imshow(\"Color Frame\", frame)\n", 89 | "\n", 90 | " key=cv2.waitKey(1)\n", 91 | "\n", 92 | " if key == ord('q'):\n", 93 | " if status == 1:\n", 94 | " times.append(datetime.now())\n", 95 | " break\n", 96 | "\n", 97 | "print(status_list)\n", 98 | "print(times)\n", 99 | "\n", 100 | "for i in range(0,len(times), 2):\n", 101 | " df=df.append({\"Start\":times[i], \"End\":times[i+1]}, ignore_index=True)\n", 102 | "\n", 103 | "df.to_csv(\"Times.csv\")\n", 104 | "\n", 105 | "video.release()\n", 106 | "cv2.destroyAllWindows" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "**Lecture:** [Capturing Motion Time](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/4775506?start=0)\n", 114 | "---" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "The following code detects the time that an object entered and exited the frame and stores that time as a row in a CSV file." 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": { 128 | "collapsed": true 129 | }, 130 | "outputs": [], 131 | "source": [ 132 | "# motion_detector.py\n", 133 | "\n", 134 | "import cv2, time, pandas\n", 135 | "from datetime import datetime\n", 136 | "\n", 137 | "first_frame = None\n", 138 | "status_list = [None,None]\n", 139 | "times = []\n", 140 | "df = pandas.DataFrame(columns=[\"Start\",\"End\"])\n", 141 | "\n", 142 | "video=cv2.VideoCapture(0)\n", 143 | "\n", 144 | "while True:\n", 145 | " check, frame = video.read()\n", 146 | " status = 0\n", 147 | " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n", 148 | " gray = cv2.GaussianBlur(gray,(21,21), 0)\n", 149 | "\n", 150 | " if first_frame is None:\n", 151 | " first_frame = gray\n", 152 | " continue\n", 153 | "\n", 154 | " delta_frame = cv2.absdiff(first_frame,gray)\n", 155 | " thresh_frame = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]\n", 156 | " thresh_frame = cv2.dilate(thresh_frame, None, iterations=2)\n", 157 | "\n", 158 | " (_,cnts,_) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n", 159 | "\n", 160 | " for contour in cnts:\n", 161 | " if cv2.contourArea(contour) < 10000:\n", 162 | " continue\n", 163 | " status = 1\n", 164 | "\n", 165 | " (x, y, w, h) = cv2.boundingRect(contour)\n", 166 | " reci = cv2.rectangle(frame, (x, y), (x+w, y+h), (0,255,0), 3)\n", 167 | " status_list.append(status)\n", 168 | "\n", 169 | " status_list=status_list[-2:]\n", 170 | "\n", 171 | "\n", 172 | " if status_list[-1] == 1 and status_list[-2] == 0:\n", 173 | " times.append(datetime.now())\n", 174 | " if status_list[-1] == 0 and status_list[-2] == 1:\n", 175 | " times.append(datetime.now())\n", 176 | "\n", 177 | "\n", 178 | " cv2.imshow(\"Gray Frame\", gray)\n", 179 | " cv2.imshow(\"Delta Frame\", delta_frame)\n", 180 | " cv2.imshow(\"Threshold Frame\", thresh_frame)\n", 181 | " cv2.imshow(\"Color Frame\", frame)\n", 182 | "\n", 183 | " key=cv2.waitKey(1)\n", 184 | "\n", 185 | " if key == ord('q'):\n", 186 | " if status == 1:\n", 187 | " times.append(datetime.now())\n", 188 | " break\n", 189 | "\n", 190 | "print(status_list)\n", 191 | "print(times)\n", 192 | "\n", 193 | "for i in range(0,len(times), 2):\n", 194 | " df=df.append({\"Start\":times[i], \"End\":times[i+1]}, ignore_index=True)\n", 195 | "\n", 196 | "df.to_csv(\"Times.csv\")\n", 197 | "\n", 198 | "video.release()\n", 199 | "cv2.destroyAllWindows" 200 | ] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "metadata": {}, 205 | "source": [ 206 | "**Lecture:** [Final Code for Application 6]()\n", 207 | "---" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "The code below is the final code for `motion_detector.py` and for the `plotting.py` which we'll build in the next section. The `plotting.py` file is responsible for plotting the time data. Run `python plotting.py` to execute the program. " 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "**Note**: Your computer needs a camera for this to work." 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": null, 227 | "metadata": { 228 | "collapsed": true 229 | }, 230 | "outputs": [], 231 | "source": [ 232 | "# motion_detector.py\n", 233 | "\n", 234 | "import cv2, time, pandas\n", 235 | "from datetime import datetime\n", 236 | "\n", 237 | "first_frame = None\n", 238 | "status_list = [None,None]\n", 239 | "times = []\n", 240 | "df = pandas.DataFrame(columns=[\"Start\",\"End\"])\n", 241 | "\n", 242 | "video=cv2.VideoCapture(0)\n", 243 | "\n", 244 | "while True:\n", 245 | " check, frame = video.read()\n", 246 | " status = 0\n", 247 | " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n", 248 | " gray = cv2.GaussianBlur(gray,(21,21), 0)\n", 249 | "\n", 250 | " if first_frame is None:\n", 251 | " first_frame = gray\n", 252 | " continue\n", 253 | "\n", 254 | " delta_frame = cv2.absdiff(first_frame,gray)\n", 255 | " thresh_frame = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]\n", 256 | " thresh_frame = cv2.dilate(thresh_frame, None, iterations=2)\n", 257 | "\n", 258 | " (_,cnts,_) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n", 259 | "\n", 260 | " for contour in cnts:\n", 261 | " if cv2.contourArea(contour) < 10000:\n", 262 | " continue\n", 263 | " status = 1\n", 264 | "\n", 265 | " (x, y, w, h) = cv2.boundingRect(contour)\n", 266 | " reci = cv2.rectangle(frame, (x, y), (x+w, y+h), (0,255,0), 3)\n", 267 | " status_list.append(status)\n", 268 | "\n", 269 | " status_list=status_list[-2:]\n", 270 | "\n", 271 | "\n", 272 | " if status_list[-1] == 1 and status_list[-2] == 0:\n", 273 | " times.append(datetime.now())\n", 274 | " if status_list[-1] == 0 and status_list[-2] == 1:\n", 275 | " times.append(datetime.now())\n", 276 | "\n", 277 | "\n", 278 | " cv2.imshow(\"Gray Frame\", gray)\n", 279 | " cv2.imshow(\"Delta Frame\", delta_frame)\n", 280 | " cv2.imshow(\"Threshold Frame\", thresh_frame)\n", 281 | " cv2.imshow(\"Color Frame\", frame)\n", 282 | "\n", 283 | " key=cv2.waitKey(1)\n", 284 | "\n", 285 | " if key == ord('q'):\n", 286 | " if status == 1:\n", 287 | " times.append(datetime.now())\n", 288 | " break\n", 289 | "\n", 290 | "print(status_list)\n", 291 | "print(times)\n", 292 | "\n", 293 | "for i in range(0,len(times), 2):\n", 294 | " df=df.append({\"Start\":times[i], \"End\":times[i+1]}, ignore_index=True)\n", 295 | "\n", 296 | "df.to_csv(\"Times.csv\")\n", 297 | "\n", 298 | "video.release()\n", 299 | "cv2.destroyAllWindows" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": null, 305 | "metadata": { 306 | "collapsed": true 307 | }, 308 | "outputs": [], 309 | "source": [ 310 | "# plotting.py\n", 311 | "\n", 312 | "from motion_detector import df\n", 313 | "from bokeh.plotting import figure, show, output_file\n", 314 | "from bokeh.models import HoverTool, ColumnDataSource\n", 315 | "\n", 316 | "df[\"Start_string\"] = df[\"Start\"].dt.strftime(\"%Y-%m-%d %H:%M:%S\")\n", 317 | "df[\"End_string\"] = df[\"End\"].dt.strftime(\"%Y-%m-%d %H:%M:%S\")\n", 318 | "print(df)\n", 319 | "\n", 320 | "cds = ColumnDataSource(df)\n", 321 | "\n", 322 | "p = figure(x_axis_type='datetime', height=100, width=500, sizing_mode=\"scale_width\", title=\"Motion Graph\")\n", 323 | "p.yaxis.minor_tick_line_color = None\n", 324 | "p.ygrid[0].ticker.desired_num_ticks = 1\n", 325 | "\n", 326 | "hover = HoverTool(tooltips=[(\"Start\",\"@Start_string\"), (\"End\",\"@End_string\")])\n", 327 | "p.add_tools(hover)\n", 328 | "\n", 329 | "q = p.quad(left=\"Start\", right=\"End\", bottom=0, top=1, color=\"green\", source=cds)\n", 330 | "\n", 331 | "output_file(\"Graph1.html\")\n", 332 | "show(p)" 333 | ] 334 | } 335 | ], 336 | "metadata": { 337 | "kernelspec": { 338 | "display_name": "Python 3", 339 | "language": "python", 340 | "name": "python3" 341 | }, 342 | "language_info": { 343 | "codemirror_mode": { 344 | "name": "ipython", 345 | "version": 3 346 | }, 347 | "file_extension": ".py", 348 | "mimetype": "text/x-python", 349 | "name": "python", 350 | "nbconvert_exporter": "python", 351 | "pygments_lexer": "ipython3", 352 | "version": "3.6.3" 353 | } 354 | }, 355 | "nbformat": 4, 356 | "nbformat_minor": 2 357 | } 358 | -------------------------------------------------------------------------------- /S18-Webscraping-with-BeautifulSoup.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python Mega Course: Build 10 Real World Applications\n", 8 | "---" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "This notebook is a summary of [The Python Mega Course: Build 10 Real World Applciations](https://www.udemy.com/the-python-mega-course), a comprehensive online Python course taught by Ardit Sulce. Each lecture name is clickable and takes you to the video lecture in the course." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "# Section 18: Webscraping with BeautifulSoup\n", 23 | "***" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "**Lecture:** [Section Introduction](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 31 | "---" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "Webscraping is the process of extracting data from webpages. Typically webscraping deals with data that are not in a well structured format." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "**Lecture:** [The Concept Behind Webscraping](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 46 | "---" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "Webscraping technically is the process of loading the webpage source code (which could be HTML, Javascript, and CSS) into Python and then using specific Python code to parse and extract data values from the webpage source code." 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "**Lecture:** [Webscraping Example](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 61 | "---" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 3, 67 | "metadata": { 68 | "collapsed": true 69 | }, 70 | "outputs": [], 71 | "source": [ 72 | "import requests\n", 73 | "from bs4 import BeautifulSoup" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": { 80 | "collapsed": true 81 | }, 82 | "outputs": [], 83 | "source": [ 84 | "r = requests.get(\"http://pythonhow.com/example.html\")\n", 85 | "c = r.content" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": { 92 | "collapsed": true 93 | }, 94 | "outputs": [], 95 | "source": [ 96 | "print(c)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": { 103 | "collapsed": true 104 | }, 105 | "outputs": [], 106 | "source": [ 107 | "soup = BeautifulSoup(c, \"html.parser\")\n", 108 | "all = soup.find_all(\"div\", {\"class\":\"cities\"})" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": { 115 | "collapsed": true 116 | }, 117 | "outputs": [], 118 | "source": [ 119 | "all" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": { 126 | "collapsed": true 127 | }, 128 | "outputs": [], 129 | "source": [ 130 | "all[0]" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "Find all titles that have a `h2` heading tag:" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": { 144 | "collapsed": true 145 | }, 146 | "outputs": [], 147 | "source": [ 148 | "all[0].find_all(\"h2\")" 149 | ] 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "metadata": {}, 154 | "source": [ 155 | "That found the list of tags. You can now extract the text of the first tag:" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "metadata": { 162 | "collapsed": true 163 | }, 164 | "outputs": [], 165 | "source": [ 166 | "all[0].find_all(\"h2\")[0].text" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "Let's loop through all `h2` tags and extract their paragraph `p` tags:" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": { 180 | "collapsed": true 181 | }, 182 | "outputs": [], 183 | "source": [ 184 | "for item in all:\n", 185 | " print(item.find_all(\"p\")[0].text)" 186 | ] 187 | } 188 | ], 189 | "metadata": { 190 | "kernelspec": { 191 | "display_name": "Python 3", 192 | "language": "python", 193 | "name": "python3" 194 | }, 195 | "language_info": { 196 | "codemirror_mode": { 197 | "name": "ipython", 198 | "version": 3 199 | }, 200 | "file_extension": ".py", 201 | "mimetype": "text/x-python", 202 | "name": "python", 203 | "nbconvert_exporter": "python", 204 | "pygments_lexer": "ipython3", 205 | "version": "3.6.3" 206 | } 207 | }, 208 | "nbformat": 4, 209 | "nbformat_minor": 2 210 | } 211 | -------------------------------------------------------------------------------- /S19-App-7-Scrape-Real-Estate-Property-Data-from-the-Web.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python Mega Course: Build 10 Real World Applications\n", 8 | "---" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "This notebook is a summary of [The Python Mega Course: Build 10 Real World Applciations](https://www.udemy.com/the-python-mega-course), a comprehensive online Python course taught by Ardit Sulce. Each lecture name is clickable and takes you to the video lecture in the course." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "# Section 19: Application 7: Scrape Real Estate Property Data from the Web\n", 23 | "***" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "**Lecture:** [Program Demonstration](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 31 | "---" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "This video lecture shows the finished version of the website running on a browser." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "**Lecture:** [Loading the Webpage in Python](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 46 | "---" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "This code loads the webpage source code into Python ready for extracting information from it." 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": { 60 | "collapsed": true 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "import requests, re\n", 65 | "from bs4 import BeautifulSoup\n", 66 | "\n", 67 | "r = requests.get(\"http://www.pythonhow.com/real-estate/rock-springs-wy/LCWYROCKSPRINGS/\")\n", 68 | "c = r.content\n", 69 | "\n", 70 | "soup=BeautifulSoup(c, \"html.parser\")\n", 71 | "print(soup.prettify())" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "**Lecture:** [Extracting \"div\" Tags](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 79 | "---" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "We start extracting HTML tags starting from `div` tags." 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": { 93 | "collapsed": true 94 | }, 95 | "outputs": [], 96 | "source": [ 97 | "import requests, re\n", 98 | "from bs4 import BeautifulSoup\n", 99 | "\n", 100 | "r = requests.get(\"http://www.pythonhow.com/real-estate/rock-springs-wy/LCWYROCKSPRINGS/\")\n", 101 | "c = r.content\n", 102 | "\n", 103 | "soup = BeautifulSoup(c,\"html.parser\")\n", 104 | "\n", 105 | "all = soup.find_all(\"div\", {\"class\":\"propertyRow\"})\n", 106 | "all[0].find(\"h4\", {\"class\":\"propPrice\"}).text.replace(\"\\n\", \"\").replace(\" \", \"\")" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "**Lecture:** [Extracting Addresses and Property Details](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 114 | "---" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "Most of the data are stored inside `span` tags so we extract those data in this code." 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": { 128 | "collapsed": true 129 | }, 130 | "outputs": [], 131 | "source": [ 132 | "import requests, re\n", 133 | "from bs4 import BeautifulSoup\n", 134 | "\n", 135 | "r = requests.get(\"http://www.pythonhow.com/real-estate/rock-springs-wy/LCWYROCKSPRINGS/\")\n", 136 | "c = r.content\n", 137 | "\n", 138 | "soup = BeautifulSoup(c,\"html.parser\")\n", 139 | "\n", 140 | "all = soup.find_all(\"div\", {\"class\":\"propertyRow\"})\n", 141 | "all[0].find(\"h4\", {\"class\":\"propPrice\"}).text.replace(\"\\n\", \"\").replace(\" \", \"\")" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": { 148 | "collapsed": true 149 | }, 150 | "outputs": [], 151 | "source": [ 152 | "for item in all:\n", 153 | " print(item.find(\"h4\", {\"class\", \"propPrice\"}).text.replace(\"\\n\",\"\").replace(\" \", \"\"))\n", 154 | " print(item.find_all(\"span\", {\"class\",\"propAddressCollapse\"})[0].text)\n", 155 | " print(item.find_all(\"span\", {\"class\",\"propAddressCollapse\"})[1].text)\n", 156 | "\n", 157 | " try:\n", 158 | " print(item.find(\"span\", {\"class\", \"infoBed\"}).find(\"b\").text)\n", 159 | " except:\n", 160 | " print(None)\n", 161 | "\n", 162 | " try:\n", 163 | " print(item.find(\"span\", {\"class\", \"infoSqFt\"}).find(\"b\").text)\n", 164 | " except:\n", 165 | " print(None)\n", 166 | "\n", 167 | " try:\n", 168 | " print(item.find(\"span\", {\"class\", \"infoValueFullBath\"}).find(\"b\").text)\n", 169 | " except:\n", 170 | " print(None)\n", 171 | "\n", 172 | " try:\n", 173 | " print(item.find(\"span\", {\"class\", \"infoValueHalfBath\"}).find(\"b\").text)\n", 174 | " except:\n", 175 | " print(None)\n", 176 | " \n", 177 | " print(\" \")" 178 | ] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "metadata": {}, 183 | "source": [ 184 | "**Lecture:** [Extracting Elements without Unique Identifiers](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 185 | "---" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "Here we extract some more elements." 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": { 199 | "collapsed": true 200 | }, 201 | "outputs": [], 202 | "source": [ 203 | "for item in all:\n", 204 | " print(item.find(\"h4\", {\"class\", \"propPrice\"}).text.replace(\"\\n\",\"\").replace(\" \", \"\"))\n", 205 | " print(item.find_all(\"span\", {\"class\",\"propAddressCollapse\"})[0].text)\n", 206 | " print(item.find_all(\"span\", {\"class\",\"propAddressCollapse\"})[1].text)\n", 207 | "\n", 208 | " try:\n", 209 | " print(item.find(\"span\", {\"class\", \"infoBed\"}).find(\"b\").text)\n", 210 | " except:\n", 211 | " print(None)\n", 212 | "\n", 213 | " try:\n", 214 | " print(item.find(\"span\", {\"class\", \"infoSqFt\"}).find(\"b\").text)\n", 215 | " except:\n", 216 | " print(None)\n", 217 | "\n", 218 | " try:\n", 219 | " print(item.find(\"span\", {\"class\", \"infoValueFullBath\"}).find(\"b\").text)\n", 220 | " except:\n", 221 | " print(None)\n", 222 | "\n", 223 | " try:\n", 224 | " print(item.find(\"span\", {\"class\", \"infoValueHalfBath\"}).find(\"b\").text)\n", 225 | " except:\n", 226 | " print(None)\n", 227 | " \n", 228 | " for column_group in item.find_all(\"div\", {\"class\":\"columnGroup\"}):\n", 229 | " for feature_group, feature_name in zip(column_group.find_all(\"span\", {\"class\":\"featureGroup\"}), column_group.find_all(\"span\", {\"class\":\"featureName\"})):\n", 230 | " if \"Lot Size\" in feature_group.text:\n", 231 | " print(feature_name.text)\n", 232 | "\n", 233 | " print(\" \")" 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "**Lecture:** [Saving the Extracted Data in CSV Files](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 241 | "---" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": {}, 247 | "source": [ 248 | "Finally, we save the extracted data into a CSV file." 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "metadata": { 255 | "collapsed": true 256 | }, 257 | "outputs": [], 258 | "source": [ 259 | "import requests, re\n", 260 | "from bs4 import BeautifulSoup\n", 261 | "\n", 262 | "r = requests.get(\"http://www.pythonhow.com/real-estate/rock-springs-wy/LCWYROCKSPRINGS/\")\n", 263 | "c = r.content\n", 264 | "\n", 265 | "soup = BeautifulSoup(c,\"html.parser\")\n", 266 | "\n", 267 | "all = soup.find_all(\"div\",{\"class\":\"propertyRow\"})\n", 268 | "\n", 269 | "all[0].find(\"h4\", {\"class\":\"propPrice\"}).text.replace(\"\\n\", \"\").replace(\" \", \"\")" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": null, 275 | "metadata": { 276 | "collapsed": true 277 | }, 278 | "outputs": [], 279 | "source": [ 280 | "l = []\n", 281 | "for item in all:\n", 282 | " d = {}\n", 283 | " df[\"Address\"] = item.find_all(\"span\", {\"class\", \"propAddressCollapse\"})[0].text\n", 284 | " df[\"Locality\"] = item.find_all(\"span\", {\"class\", \"propAddressCollapse\"})[1].text\n", 285 | " df[\"Price\"] = item.find(\"h4\", {\"class\", \"propPrice\"}).text.replace(\"\\n\",\"\").replace(\" \", \"\")\n", 286 | " \n", 287 | " try:\n", 288 | " d[\"Beds\"] = item.find(\"span\", {\"class\", \"infoBed\"}).find(\"b\").text\n", 289 | " except:\n", 290 | " d[\"Beds\"] = None\n", 291 | "\n", 292 | " try:\n", 293 | " d[\"Area\"] = item.find(\"span\", {\"class\", \"infoSqFt\"}).find(\"b\").text\n", 294 | " except:\n", 295 | " d[\"Area\"] = None\n", 296 | "\n", 297 | " try:\n", 298 | " d[\"Full Baths\"] = item.find(\"span\", {\"class\", \"infoValueFullBath\"}).find(\"b\").text\n", 299 | " except:\n", 300 | " d[\"Full Baths\"] = None\n", 301 | "\n", 302 | " try:\n", 303 | " d[\"Half Baths\"] = item.find(\"span\", {\"class\", \"infoValueHalfBath\"}).find(\"b\").text\n", 304 | " except:\n", 305 | " d[\"Half Baths\"] = None\n", 306 | "\n", 307 | " for column_group in item.find_all(\"div\", {\"class\":\"columnGroup\"}):\n", 308 | " for feature_group, feature_name in zip(column_group.find_all(\"span\", {\"class\":\"featureGroup\"}), column_group.find_all(\"span\", {\"class\":\"featureName\"})):\n", 309 | " if \"Lot Size\" in feature_group.text:\n", 310 | " print(feature_name.text)\n", 311 | " d[\"Lot Size\"] = feature_name.text\n", 312 | " l.append(d)" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": null, 318 | "metadata": { 319 | "collapsed": true 320 | }, 321 | "outputs": [], 322 | "source": [ 323 | "import pandas\n", 324 | "df = pandas.DataFrame(l)\n", 325 | "df" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": null, 331 | "metadata": { 332 | "collapsed": true 333 | }, 334 | "outputs": [], 335 | "source": [ 336 | "df.to_csv(\"Output.csv\")" 337 | ] 338 | }, 339 | { 340 | "cell_type": "markdown", 341 | "metadata": {}, 342 | "source": [ 343 | "**Lecture:** [Crawling Through Webpages](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 344 | "---" 345 | ] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "metadata": {}, 350 | "source": [ 351 | "In case you need to extract data from multiple pages, here is how to do it." 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": null, 357 | "metadata": { 358 | "collapsed": true 359 | }, 360 | "outputs": [], 361 | "source": [ 362 | "import requests, re\n", 363 | "from bs4 import BeautifulSoup\n", 364 | "\n", 365 | "r = requests.get(\"http://www.pythonhow.com/real-estate/rock-springs-wy/LCWYROCKSPRINGS/\")\n", 366 | "c = r.content\n", 367 | "\n", 368 | "soup = BeautifulSoup(c,\"html.parser\")\n", 369 | "\n", 370 | "all = soup.find_all(\"div\",{\"class\":\"propertyRow\"})\n", 371 | "\n", 372 | "all[0].find(\"h4\", {\"class\":\"propPrice\"}).text.replace(\"\\n\", \"\").replace(\" \", \"\")\n", 373 | "\n", 374 | "page_nr = soup.find_all(\"a\",{\"class\":\"Page\"})[-1].text\n", 375 | "print(page_nr, \"number of pages were found\")\n", 376 | "\n", 377 | "l = []\n", 378 | "base_url = \"http://www.pythonhow.com/real-estate/rock-springs-wy/LCWYROCKSPRINGS/t=0&s=\"\n", 379 | "for page in range(0, int(page_nr)*10, 10):\n", 380 | " print( )\n", 381 | " r = requests.get(base_url + str(page) + \".html\")\n", 382 | " c = r.content\n", 383 | " soup = BeautifulSoup(c, \"html.parser\")\n", 384 | " all = soup.find_all(\"div\", {\"class\":\"propertyRow\"})\n", 385 | " for item in all:\n", 386 | " d = {}\n", 387 | " d[\"Address\"] = item.find_all(\"span\", {\"class\",\"propAddressCollapse\"})[0].text\n", 388 | " \n", 389 | " try:\n", 390 | " d[\"Locality\"] = item.find_all(\"span\",{\"class\",\"propAddressCollapse\"})[1].text\n", 391 | " except:\n", 392 | " d[\"Locality\"] = None\n", 393 | " d[\"Price\"] = item.find(\"h4\", {\"class\", \"propPrice\"}).text.replace(\"\\n\",\"\").replace(\" \", \"\")\n", 394 | " \n", 395 | " try:\n", 396 | " d[\"Beds\"] = item.find(\"span\", {\"class\", \"infoBed\"}).find(\"b\").text\n", 397 | " except:\n", 398 | " d[\"Beds\"] = None\n", 399 | "\n", 400 | " try:\n", 401 | " d[\"Area\"] = item.find(\"span\", {\"class\", \"infoSqFt\"}).find(\"b\").text\n", 402 | " except:\n", 403 | " d[\"Area\"] = None\n", 404 | " \n", 405 | " try:\n", 406 | " d[\"Full Baths\"] = item.find(\"span\", {\"class\", \"infoValueFullBath\"}).find(\"b\").text\n", 407 | " except:\n", 408 | " d[\"Full Baths\"] = None\n", 409 | "\n", 410 | " try:\n", 411 | " d[\"Half Baths\"] = item.find(\"span\", {\"class\", \"infoValueHalfBath\"}).find(\"b\").text\n", 412 | " except:\n", 413 | " d[\"Half Baths\"] = None\n", 414 | " \n", 415 | " for column_group in item.find_all(\"div\", {\"class\":\"columnGroup\"}):\n", 416 | " for feature_group, feature_name in zip(column_group.find_all(\"span\", {\"class\":\"featureGroup\"}), column_group.find_all(\"span\", {\"class\":\"featureName\"})):\n", 417 | " if \"Lot Size\" in feature_group.text:\n", 418 | " print(feature_name.text)\n", 419 | " d[\"Lot Size\"] = feature_name.text\n", 420 | " l.append(d)" 421 | ] 422 | }, 423 | { 424 | "cell_type": "markdown", 425 | "metadata": {}, 426 | "source": [ 427 | "**Lecture:** [Final Code of Application 7]()\n", 428 | "---" 429 | ] 430 | }, 431 | { 432 | "cell_type": "markdown", 433 | "metadata": {}, 434 | "source": [ 435 | "This is the final code. It accesses a webpage and it extracts data from that webpage and save those data in a CSV file." 436 | ] 437 | }, 438 | { 439 | "cell_type": "markdown", 440 | "metadata": {}, 441 | "source": [ 442 | "**Note**: You need internet connection for the code to work." 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": null, 448 | "metadata": { 449 | "collapsed": true 450 | }, 451 | "outputs": [], 452 | "source": [ 453 | "import requests, re\n", 454 | "from bs4 import BeautifulSoup\n", 455 | "import pandas\n", 456 | "\n", 457 | "r = requests.get(\"http://www.pythonhow.com/real-estate/rock-springs-wy/LCWYROCKSPRINGS/\")\n", 458 | "c = r.content\n", 459 | "\n", 460 | "soup = BeautifulSoup(c,\"html.parser\")\n", 461 | "\n", 462 | "all = soup.find_all(\"div\",{\"class\":\"propertyRow\"})\n", 463 | "\n", 464 | "all[0].find(\"h4\", {\"class\":\"propPrice\"}).text.replace(\"\\n\", \"\").replace(\" \", \"\")\n", 465 | "\n", 466 | "page_nr = soup.find_all(\"a\",{\"class\":\"Page\"})[-1].text\n", 467 | "print(page_nr, \"number of pages were found\")\n", 468 | "\n", 469 | "l = []\n", 470 | "base_url = \"http://www.pythonhow.com/real-estate/rock-springs-wy/LCWYROCKSPRINGS/t=0&s=\"\n", 471 | "for page in range(0, int(page_nr)*10, 10):\n", 472 | " print( )\n", 473 | " r = requests.get(base_url + str(page) + \".html\")\n", 474 | " c = r.content\n", 475 | " soup = BeautifulSoup(c, \"html.parser\")\n", 476 | " all = soup.find_all(\"div\", {\"class\":\"propertyRow\"})\n", 477 | " for item in all:\n", 478 | " d = {}\n", 479 | " d[\"Address\"] = item.find_all(\"span\", {\"class\",\"propAddressCollapse\"})[0].text\n", 480 | " \n", 481 | " try:\n", 482 | " d[\"Locality\"] = item.find_all(\"span\",{\"class\",\"propAddressCollapse\"})[1].text\n", 483 | " except:\n", 484 | " d[\"Locality\"] = None\n", 485 | " d[\"Price\"] = item.find(\"h4\", {\"class\", \"propPrice\"}).text.replace(\"\\n\",\"\").replace(\" \", \"\")\n", 486 | " \n", 487 | " try:\n", 488 | " d[\"Beds\"] = item.find(\"span\", {\"class\", \"infoBed\"}).find(\"b\").text\n", 489 | " except:\n", 490 | " d[\"Beds\"] = None\n", 491 | "\n", 492 | " try:\n", 493 | " d[\"Area\"] = item.find(\"span\", {\"class\", \"infoSqFt\"}).find(\"b\").text\n", 494 | " except:\n", 495 | " d[\"Area\"] = None\n", 496 | " \n", 497 | " try:\n", 498 | " d[\"Full Baths\"] = item.find(\"span\", {\"class\", \"infoValueFullBath\"}).find(\"b\").text\n", 499 | " except:\n", 500 | " d[\"Full Baths\"] = None\n", 501 | "\n", 502 | " try:\n", 503 | " d[\"Half Baths\"] = item.find(\"span\", {\"class\", \"infoValueHalfBath\"}).find(\"b\").text\n", 504 | " except:\n", 505 | " d[\"Half Baths\"] = None\n", 506 | " \n", 507 | " for column_group in item.find_all(\"div\", {\"class\":\"columnGroup\"}):\n", 508 | " for feature_group, feature_name in zip(column_group.find_all(\"span\", {\"class\":\"featureGroup\"}), column_group.find_all(\"span\", {\"class\":\"featureName\"})):\n", 509 | " if \"Lot Size\" in feature_group.text:\n", 510 | " print(feature_name.text)\n", 511 | " d[\"Lot Size\"] = feature_name.text\n", 512 | " l.append(d)\n", 513 | " \n", 514 | "df = pandas.DataFrame(l)\n", 515 | "df.to_csv(\"Output.csv\")" 516 | ] 517 | } 518 | ], 519 | "metadata": { 520 | "kernelspec": { 521 | "display_name": "Python 3", 522 | "language": "python", 523 | "name": "python3" 524 | }, 525 | "language_info": { 526 | "codemirror_mode": { 527 | "name": "ipython", 528 | "version": 3 529 | }, 530 | "file_extension": ".py", 531 | "mimetype": "text/x-python", 532 | "name": "python", 533 | "nbconvert_exporter": "python", 534 | "pygments_lexer": "ipython3", 535 | "version": "3.6.3" 536 | } 537 | }, 538 | "nbformat": 4, 539 | "nbformat_minor": 2 540 | } 541 | -------------------------------------------------------------------------------- /S20-App-8-Build-a-Web-Based-Financial-Graph.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python Mega Course: Build 10 Real World Applications\n", 8 | "---" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "This notebook is a summary of [The Python Mega Course: Build 10 Real World Applciations](https://www.udemy.com/the-python-mega-course), a comprehensive online Python course taught by Ardit Sulce. Each lecture name is clickable and takes you to the video lecture in the course." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "# Section 20: Application 8: Build a Web-Based Financial Graph\n", 23 | "***" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "**Lecture:** [Program Demonstration](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 31 | "---" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "This video lecture shows the finished version of the website running on a browser." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "**Lecture:** [Note](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 46 | "---" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "In the next video I install the pandas_datareader library with pip install pandas_datareader. There's currently a bug with that version of pandas_datareader, but the bug has been fixed in the development version of the library.\n", 54 | "\n", 55 | "Therefore, instead of doing pip install pandas_datareader please do the following instead:\n", 56 | "\n", 57 | "`pip install git+https://github.com/pydata/pandas-datareader.git`\n", 58 | "\n", 59 | "That will install the development version which works just fine." 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "**Lecture:** [Downloading Datasets with Python](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 67 | "---" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": { 74 | "collapsed": true 75 | }, 76 | "outputs": [], 77 | "source": [ 78 | "from pandas_datareader import data\n", 79 | "import datetime\n", 80 | "\n", 81 | "start = datetime.datetime(2016, 1, 1)\n", 82 | "end = datetime.datetime(2016, 3, 10)\n", 83 | "\n", 84 | "df = data.DataReader(name=\"GOOG\", data_source=\"google\", start=start, end=end)\n", 85 | "df" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "**Lecture:** [Stock Market Data](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 93 | "---" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "This video lecture explains what stock market data are and how to read them." 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "**Lecture:** [Stock Market Data Candlestick Charts](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 108 | "---" 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "metadata": {}, 114 | "source": [ 115 | "This video lecture explains how to read a stock market candlestick chart." 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "**Lecture:** [Candlestick Charts with Bokeh Quadrants](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 123 | "---" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "To create charts usign quadrant glyphs you can use `figure.quad`, but it's better to use `figure.rect`. We will actually use figure.rect in the next lecture." 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "**Lecture:** [Candlestick Charts with Bokeh Rectangles](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 138 | "---" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "Here we start creating the figure object, and apply quadrant glyphs to the figure." 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": { 152 | "collapsed": true 153 | }, 154 | "outputs": [], 155 | "source": [ 156 | "from pandas_datareader import data\n", 157 | "import datetime\n", 158 | "\n", 159 | "start = datetime.datetime(2016, 1, 1)\n", 160 | "end = datetime.datetime(2016, 3, 10)\n", 161 | "\n", 162 | "df = data.DataReader(name=\"GOOG\", data_source=\"google\", start=start, end=end)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": null, 168 | "metadata": { 169 | "collapsed": true 170 | }, 171 | "outputs": [], 172 | "source": [ 173 | "def inc_dec(c, o): \n", 174 | " if c > o:\n", 175 | " value = \"Increase\"\n", 176 | " elif c < o:\n", 177 | " value = \"Decrease\"\n", 178 | " else:\n", 179 | " value = \"Equal\"\n", 180 | " return value\n", 181 | "\n", 182 | "df[\"Status\"] = [inc_dec(c, o) for c, o in zip(df.Close, df.Open)]\n", 183 | "df[\"Middle\"] = (df.Open + df.Close) / 2\n", 184 | "df[\"Height\"] = abs(df.Close - df.Open)" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": null, 190 | "metadata": { 191 | "collapsed": true 192 | }, 193 | "outputs": [], 194 | "source": [ 195 | "from bokeh.plotting import figure, show, output_file\n", 196 | "\n", 197 | "p = figure(x_axis_type='datetime', width=1000, height=300)\n", 198 | "p.title.text = \"Candlestick Chart\"\n", 199 | "\n", 200 | "hours_12 = 12 * 60 * 60 * 1000\n", 201 | "\n", 202 | "p.rect(df.index[df.Status==\"Increase\"], df.Middle[df.Status==\"Increase\"],\n", 203 | " hours_12, df.Height[df.Status==\"Increase\"], fill_color=\"green\", line_color=\"black\")\n", 204 | "\n", 205 | "p.rect(df.index[df.Status==\"Decrease\"], df.Middle[df.Status==\"Decrease\"],\n", 206 | " hours_12, df.Height[df.Status==\"Decrease\"], fill_color=\"red\", line_color=\"black\")\n", 207 | "\n", 208 | "output_file(\"CS.html\")\n", 209 | "show(p)" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "**Lecture:** [Candlestick Segments](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 217 | "---" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "metadata": { 224 | "collapsed": true 225 | }, 226 | "outputs": [], 227 | "source": [ 228 | "from pandas_datareader import data\n", 229 | "import datetime\n", 230 | "\n", 231 | "start = datetime.datetime(2016, 1, 1)\n", 232 | "end = datetime.datetime(2016, 3, 10)\n", 233 | "\n", 234 | "df = data.DataReader(name=\"GOOG\", data_source=\"google\", start=start, end=end)" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": null, 240 | "metadata": { 241 | "collapsed": true 242 | }, 243 | "outputs": [], 244 | "source": [ 245 | "def inc_dec(c, o): \n", 246 | " if c > o:\n", 247 | " value = \"Increase\"\n", 248 | " elif c < o:\n", 249 | " value = \"Decrease\"\n", 250 | " else:\n", 251 | " value = \"Equal\"\n", 252 | " return value\n", 253 | "\n", 254 | "df[\"Status\"] = [inc_dec(c, o) for c, o in zip(df.Close, df.Open)]\n", 255 | "df[\"Middle\"] = (df.Open + df.Close) / 2\n", 256 | "df[\"Height\"] = abs(df.Close - df.Open)" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "metadata": { 263 | "collapsed": true 264 | }, 265 | "outputs": [], 266 | "source": [ 267 | "from bokeh.plotting import figure, show, output_file\n", 268 | "\n", 269 | "p = figure(x_axis_type='datetime', width=1000, height=300)\n", 270 | "p.title.text = \"Candlestick Chart\"\n", 271 | "p.grid.grid_line_alpha = 0.3\n", 272 | "\n", 273 | "hours_12 = 12 * 60 * 60 * 1000\n", 274 | "\n", 275 | "p.segment(df.index, df.High, df.index, df.Low, color=\"Black\")\n", 276 | "\n", 277 | "p.rect(df.index[df.Status==\"Increase\"], df.Middle[df.Status==\"Increase\"],\n", 278 | " hours_12, df.Height[df.Status==\"Increase\"], fill_color=\"green\", line_color=\"black\")\n", 279 | "\n", 280 | "p.rect(df.index[df.Status==\"Decrease\"], df.Middle[df.Status==\"Decrease\"],\n", 281 | " hours_12, df.Height[df.Status==\"Decrease\"], fill_color=\"red\", line_color=\"black\")\n", 282 | "\n", 283 | "output_file(\"CS.html\")\n", 284 | "show(p)" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": {}, 290 | "source": [ 291 | "**Lecture:** [Styling the Chart](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 292 | "---" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 3, 298 | "metadata": {}, 299 | "outputs": [], 300 | "source": [ 301 | "from pandas_datareader import data\n", 302 | "import datetime\n", 303 | "from bokeh.plotting import figure, show, output_file\n", 304 | "\n", 305 | "start = datetime.datetime(2015, 11, 1)\n", 306 | "end = datetime.datetime(2016, 3, 10)\n", 307 | "\n", 308 | "df = data.DataReader(name=\"GOOG\", data_source=\"yahoo\", start=start, end=end)\n", 309 | "\n", 310 | "def inc_dec(c, o): \n", 311 | " if c > o:\n", 312 | " value = \"Increase\"\n", 313 | " elif c < o:\n", 314 | " value = \"Decrease\"\n", 315 | " else:\n", 316 | " value = \"Equal\"\n", 317 | " return value\n", 318 | "\n", 319 | "df[\"Status\"] = [inc_dec(c, o) for c, o in zip(df.Close, df.Open)]\n", 320 | "df[\"Middle\"] = (df.Open + df.Close) / 2\n", 321 | "df[\"Height\"] = abs(df.Close - df.Open)\n", 322 | "\n", 323 | "p = figure(x_axis_type='datetime', width=1000, height=300, sizing_mode='scale_width')\n", 324 | "p.title.text = \"Candlestick Chart\"\n", 325 | "p.grid.grid_line_alpha = 0.3\n", 326 | "\n", 327 | "hours_12 = 12 * 60 * 60 * 1000\n", 328 | "\n", 329 | "p.segment(df.index, df.High, df.index, df.Low, color=\"Black\")\n", 330 | "\n", 331 | "p.rect(df.index[df.Status==\"Increase\"], df.Middle[df.Status==\"Increase\"],\n", 332 | " hours_12, df.Height[df.Status==\"Increase\"], fill_color=\"#CCFFFF\", line_color=\"black\")\n", 333 | "\n", 334 | "p.rect(df.index[df.Status==\"Decrease\"], df.Middle[df.Status==\"Decrease\"],\n", 335 | " hours_12, df.Height[df.Status==\"Decrease\"], fill_color=\"#FF3333\", line_color=\"black\")\n", 336 | "\n", 337 | "output_file(\"CS.html\")\n", 338 | "show(p)" 339 | ] 340 | }, 341 | { 342 | "cell_type": "markdown", 343 | "metadata": {}, 344 | "source": [ 345 | "**Lecture:** [The Concept Behind Embedding Bokeh Charts in a Flask Webpage](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 346 | "---" 347 | ] 348 | }, 349 | { 350 | "cell_type": "markdown", 351 | "metadata": {}, 352 | "source": [ 353 | "Since Bokeh charts are can embed Bokeh charts, you can embed them in your websites. The next lecture contains the code of a Flask web app with the Bokeh candlestick chart embedded in it." 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "metadata": {}, 359 | "source": [ 360 | "**Lecture:** [Embedding the Bokeh Chart in a Webpage](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 361 | "---" 362 | ] 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "metadata": {}, 367 | "source": [ 368 | "Please see the lecture \"Final Code for Application 8\" for the code." 369 | ] 370 | }, 371 | { 372 | "cell_type": "markdown", 373 | "metadata": {}, 374 | "source": [ 375 | "**Lecture:** [Deploying the Chart Website to a Live Server](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 376 | "---" 377 | ] 378 | }, 379 | { 380 | "cell_type": "markdown", 381 | "metadata": {}, 382 | "source": [ 383 | "This lecture covers how to deploy the app on Heroku. Below is a summary of the deployment steps:" 384 | ] 385 | }, 386 | { 387 | "cell_type": "markdown", 388 | "metadata": {}, 389 | "source": [ 390 | "1. Create an account on www.heroku.com if you don't have one already.\n", 391 | "2. Download and install Heroku CLI from www.heroku.com/\n", 392 | "3. Install gunicorn with \"pip install gunicorn\". Make sure you're using pip from your virtual environment if you have one.\n", 393 | "4. Create a requirement.txt file in the main app directory where the main Python app file is located. You can create that file by running \"pip freeze > requirements.txt\" in the command line. Make sure you're using pip from your virtual environment if you have one. The requirement.txt file should now contain a list of Python packages.\n", 394 | "5. Create a file named \"Procfile\" in the main app directory. The file should not contain any extension. Then type in this line inside: \"web: gunicorn script1:app\" where \"script1\" should be replaced with the name of your Python script and \"app\" with the name of the variable holding your Flask app.\n", 395 | "6. Create a runtime.txt file in the main app directory and type \"python-3.5.1\" inside, or even better go to the \"Python runtimes for Heroku\" webpage to see the current supported runtime and put that in the file.\n", 396 | "7. Open your computer terminal/command line to point to the directory where the Python file containing your app code is located.\n", 397 | "8. Using the terminal, log in to Heroku with \"heroku login\" \n", 398 | "9. Enter your Heroku email address\n", 399 | "10. Enter your Heroku password \n", 400 | "11. Create a new Heroku app with \"heroku create myawesomeappname\"\n", 401 | "17. Initialize a local git repository with \"git init\"\n", 402 | "18. Add your local application files to git with \"git add .\"\n", 403 | "19. Tell git your email address with \"git config --global user.email \"myemail@hotmail.com\"\". Make sure the email address is inside quotes here.\n", 404 | "20. Tell git your username (just pick whatever username) with \"config --global user.name \"whateverusername\"\". The username should be in quotes.\n", 405 | "21. Commit the changes with \"git commit -m \"first commit\"\". Make sure \"first commit\" is inside quotes.\n", 406 | "22. Before pushing the changes to Heroku, tell heroku the name of the app you want to use with heroku \"git:remote --app myawesomeappname\"\n", 407 | "23. Push the changes to Heroku with \"git push heroku master\"\n", 408 | "26. That should do it. Go ahead and open your app with \"heroku open\"." 409 | ] 410 | }, 411 | { 412 | "cell_type": "markdown", 413 | "metadata": {}, 414 | "source": [ 415 | "**Lecture:** [Final Code for Application 8]()\n", 416 | "---" 417 | ] 418 | }, 419 | { 420 | "cell_type": "markdown", 421 | "metadata": {}, 422 | "source": [ 423 | "To run this web app, make sure the directory structure is like the one you see below and then execute `python script1.py` and then visit `localhost:5000` on your browser." 424 | ] 425 | }, 426 | { 427 | "cell_type": "markdown", 428 | "metadata": {}, 429 | "source": [ 430 | "Project directory tree:" 431 | ] 432 | }, 433 | { 434 | "cell_type": "markdown", 435 | "metadata": {}, 436 | "source": [ 437 | "`templates\n", 438 | " layout.html\n", 439 | " home.html\n", 440 | " about.html\n", 441 | " plot.html\n", 442 | "static\n", 443 | " css\n", 444 | " main.css\n", 445 | "script1.py\n", 446 | "plot.py\n", 447 | "runtime.txt\n", 448 | "Procfile\n", 449 | "requirements.txt`" 450 | ] 451 | }, 452 | { 453 | "cell_type": "markdown", 454 | "metadata": {}, 455 | "source": [ 456 | "script1.py" 457 | ] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "execution_count": null, 462 | "metadata": { 463 | "collapsed": true 464 | }, 465 | "outputs": [], 466 | "source": [ 467 | "from flask import Flask, render_template\n", 468 | "\n", 469 | "app=Flask(__name__)\n", 470 | "\n", 471 | "@app.route('/plot/')\n", 472 | "def plot():\n", 473 | " from pandas_datareader import data\n", 474 | " import datetime\n", 475 | " import fix_yahoo_finance as yf\n", 476 | " yf.pdr_override()\n", 477 | " from bokeh.plotting import figure, show, output_file\n", 478 | " from bokeh.embed import components\n", 479 | " from bokeh.resources import CDN \n", 480 | "\n", 481 | " start=datetime.datetime(2015,11,1)\n", 482 | " end=datetime.datetime(2016,3,10)\n", 483 | "\n", 484 | " df=data.get_data_yahoo(tickers=\"GOOG\", start=start, end=end)\n", 485 | "\n", 486 | "\n", 487 | " def inc_dec(c, o): \n", 488 | " if c > o:\n", 489 | " value = \"Increase\"\n", 490 | " elif c < o:\n", 491 | " value = \"Decrease\"\n", 492 | " else:\n", 493 | " value = \"Equal\"\n", 494 | " return value\n", 495 | "\n", 496 | " df[\"Status\"] = [inc_dec(c, o) for c, o in zip(df.Close, df.Open)]\n", 497 | " df[\"Middle\"] = (df.Open + df.Close) / 2\n", 498 | " df[\"Height\"] = abs(df.Close - df.Open)\n", 499 | "\n", 500 | " p = figure(x_axis_type='datetime', width=1000, height=300)\n", 501 | " p.title.text = \"Candlestick Chart\"\n", 502 | " p.grid.grid_line_alpha = 0.3\n", 503 | "\n", 504 | " hours_12 = 12 * 60 * 60 * 1000\n", 505 | "\n", 506 | " p.segment(df.index, df.High, df.index, df.Low, color=\"Black\")\n", 507 | "\n", 508 | " p.rect(df.index[df.Status==\"Increase\"], df.Middle[df.Status==\"Increase\"],\n", 509 | " hours_12, df.Height[df.Status==\"Increase\"], fill_color=\"#CCFFFF\", line_color=\"black\")\n", 510 | "\n", 511 | " p.rect(df.index[df.Status==\"Decrease\"], df.Middle[df.Status==\"Decrease\"],\n", 512 | " hours_12, df.Height[df.Status==\"Decrease\"], fill_color=\"#FF3333\", line_color=\"black\")\n", 513 | "\n", 514 | " script1, div1 = components(p)\n", 515 | " cdn_js = CDN.js_files[0]\n", 516 | " cdn_css = CDN.css_files[0]\n", 517 | " return render_template(\"plot.html\",\n", 518 | " script1 = script1,\n", 519 | " div1 = div1,\n", 520 | " cdn_css = cdn_css,\n", 521 | " cdn_js = cdn_js )\n", 522 | "\n", 523 | "@app.route('/')\n", 524 | "def home():\n", 525 | " return render_template(\"home.html\")\n", 526 | "\n", 527 | "@app.route('/about/')\n", 528 | "def about():\n", 529 | " return render_template(\"about.html\")\n", 530 | "\n", 531 | "if __name__ == \"__main__\":\n", 532 | " app.run(debug=True)" 533 | ] 534 | }, 535 | { 536 | "cell_type": "markdown", 537 | "metadata": {}, 538 | "source": [ 539 | "templates/layout.html" 540 | ] 541 | }, 542 | { 543 | "cell_type": "code", 544 | "execution_count": null, 545 | "metadata": {}, 546 | "outputs": [], 547 | "source": [ 548 | "\n", 549 | "\n", 550 | " \n", 551 | " Flask App\n", 552 | " \n", 554 | " \n", 555 | "
\n", 556 | "
\n", 557 | "

Ardit's web app

\n", 558 | " \n", 565 | "
\n", 566 | "
\n", 567 | "
\n", 568 | " {%block content%}\n", 569 | " {%endblock%}\n", 570 | "
\n", 571 | " \n", 572 | "" 573 | ] 574 | }, 575 | { 576 | "cell_type": "markdown", 577 | "metadata": {}, 578 | "source": [ 579 | "templates/home.html" 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": null, 585 | "metadata": {}, 586 | "outputs": [], 587 | "source": [ 588 | "{%extends \"layout.html\"%}\n", 589 | "{%block content%}\n", 590 | "
\n", 591 | "

My homepage

\n", 592 | "

This is a test website

\n", 593 | "
\n", 594 | "{%endblock%}" 595 | ] 596 | }, 597 | { 598 | "cell_type": "markdown", 599 | "metadata": {}, 600 | "source": [ 601 | "templates/about.html" 602 | ] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "execution_count": null, 607 | "metadata": {}, 608 | "outputs": [], 609 | "source": [ 610 | "{%extends \"layout.html\"%}\n", 611 | "{%block content%}\n", 612 | "
\n", 613 | "

My about page

\n", 614 | "

This is a test website again

\n", 615 | "
\n", 616 | "{%endblock%} " 617 | ] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "metadata": {}, 622 | "source": [ 623 | "templates/plot.html" 624 | ] 625 | }, 626 | { 627 | "cell_type": "code", 628 | "execution_count": null, 629 | "metadata": {}, 630 | "outputs": [], 631 | "source": [ 632 | "{%extends \"layout.html\"%}\n", 633 | "{%block content%}\n", 634 | "\n", 635 | "\n", 636 | "\n", 637 | "
\n", 638 | "

My about page

\n", 639 | "

This is a test website again

\n", 640 | "
\n", 641 | "{{script1 | safe}}\n", 642 | "{{div1 | safe}}\n", 643 | "{%endblock%}" 644 | ] 645 | }, 646 | { 647 | "cell_type": "markdown", 648 | "metadata": {}, 649 | "source": [ 650 | "static/css/main.css" 651 | ] 652 | }, 653 | { 654 | "cell_type": "code", 655 | "execution_count": null, 656 | "metadata": {}, 657 | "outputs": [], 658 | "source": [ 659 | "body {\n", 660 | " margin: 0;\n", 661 | " padding: 0;\n", 662 | " font-family: \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n", 663 | " color: #444;\n", 664 | "}\n", 665 | "\n", 666 | "/*\n", 667 | " * Formatting the header area\n", 668 | " */\n", 669 | "\n", 670 | "header {\n", 671 | " background-color: #DFB887;\n", 672 | " height: 35px;\n", 673 | " width: 100%;\n", 674 | " opacity: .9;\n", 675 | " margin-bottom: 10px;\n", 676 | "}\n", 677 | "\n", 678 | "header h1.logo {\n", 679 | " margin: 0;\n", 680 | " font-size: 1.7em;\n", 681 | " color: #fff;\n", 682 | " text-transform: uppercase;\n", 683 | " float: left;\n", 684 | "}\n", 685 | "\n", 686 | "header h1.logo:hover {\n", 687 | " color: #fff;\n", 688 | " text-decoration: none;\n", 689 | "}\n", 690 | "\n", 691 | "/*\n", 692 | " * Center the body content\n", 693 | " */\n", 694 | "\n", 695 | ".container {\n", 696 | " width: 1200px;\n", 697 | " margin: 0 auto;\n", 698 | "}\n", 699 | "\n", 700 | "div.home {\n", 701 | " padding: 10px 0 30px 0;\n", 702 | " background-color: #E6E6FA;\n", 703 | " -webkit-border-radius: 6px;\n", 704 | " -moz-border-radius: 6px;\n", 705 | " border-radius: 6px;\n", 706 | "}\n", 707 | "\n", 708 | "div.about {\n", 709 | " padding: 10px 0 30px 0;\n", 710 | " background-color: #E6E6FA;\n", 711 | " -webkit-border-radius: 6px;\n", 712 | " -moz-border-radius: 6px;\n", 713 | " border-radius: 6px;\n", 714 | "}\n", 715 | "\n", 716 | "h2 {\n", 717 | " font-size: 3em;\n", 718 | " margin-top: 40px;\n", 719 | " text-align: center;\n", 720 | " letter-spacing: -2px;\n", 721 | "}\n", 722 | "\n", 723 | "h3 {\n", 724 | " font-size: 1.7em;\n", 725 | " font-weight: 100;\n", 726 | " margin-top: 30px;\n", 727 | " text-align: center;\n", 728 | " letter-spacing: -1px;\n", 729 | " color: #999;\n", 730 | "}\n", 731 | "\n", 732 | ".menu {\n", 733 | " float: right;\n", 734 | " margin-top: 8px;\n", 735 | "}\n", 736 | "\n", 737 | ".menu li {\n", 738 | " display: inline;\n", 739 | "}\n", 740 | "\n", 741 | ".menu li + li {\n", 742 | " margin-left: 35px;\n", 743 | "}\n", 744 | "\n", 745 | ".menu li a {\n", 746 | " color: #444;\n", 747 | " text-decoration: none;\n", 748 | "}" 749 | ] 750 | }, 751 | { 752 | "cell_type": "markdown", 753 | "metadata": {}, 754 | "source": [ 755 | "The following files are only necessary if you deploy your app on Heroku. " 756 | ] 757 | }, 758 | { 759 | "cell_type": "markdown", 760 | "metadata": {}, 761 | "source": [ 762 | "Procfile" 763 | ] 764 | }, 765 | { 766 | "cell_type": "markdown", 767 | "metadata": { 768 | "collapsed": true 769 | }, 770 | "source": [ 771 | "`web: gunicorn script1:app`" 772 | ] 773 | }, 774 | { 775 | "cell_type": "markdown", 776 | "metadata": {}, 777 | "source": [ 778 | "runtime.txt (Go to the [Heroku Python runtime webpage](https://devcenter.heroku.com/articles/python-runtimes#supported-python-runtimes) to see the latest version of Python used by Heroku and put that version in runtime.txt instead of the one below)" 779 | ] 780 | }, 781 | { 782 | "cell_type": "markdown", 783 | "metadata": {}, 784 | "source": [ 785 | "`python-3.5.1`" 786 | ] 787 | }, 788 | { 789 | "cell_type": "markdown", 790 | "metadata": {}, 791 | "source": [ 792 | "requirements.txt" 793 | ] 794 | }, 795 | { 796 | "cell_type": "markdown", 797 | "metadata": { 798 | "collapsed": true 799 | }, 800 | "source": [ 801 | "`bokeh==0.11.1\n", 802 | "Flask==0.10.1\n", 803 | "gunicorn==19.6.0\n", 804 | "itsdangerous==0.24\n", 805 | "Jinja2==2.8\n", 806 | "MarkupSafe==0.23\n", 807 | "numpy==1.11.0\n", 808 | "pandas==0.18.0\n", 809 | "pandas-datareader==0.2.1\n", 810 | "python-dateutil==2.5.2\n", 811 | "pytz==2016.3\n", 812 | "PyYAML==3.11\n", 813 | "requests==2.9.1\n", 814 | "requests-file==1.4\n", 815 | "six==1.10.0\n", 816 | "tornado==4.3\n", 817 | "Werkzeug==0.11.5`" 818 | ] 819 | }, 820 | { 821 | "cell_type": "code", 822 | "execution_count": null, 823 | "metadata": {}, 824 | "outputs": [], 825 | "source": [] 826 | } 827 | ], 828 | "metadata": { 829 | "kernelspec": { 830 | "display_name": "Python 3", 831 | "language": "python", 832 | "name": "python3" 833 | }, 834 | "language_info": { 835 | "codemirror_mode": { 836 | "name": "ipython", 837 | "version": 3 838 | }, 839 | "file_extension": ".py", 840 | "mimetype": "text/x-python", 841 | "name": "python", 842 | "nbconvert_exporter": "python", 843 | "pygments_lexer": "ipython3", 844 | "version": "3.6.3" 845 | } 846 | }, 847 | "nbformat": 4, 848 | "nbformat_minor": 2 849 | } 850 | -------------------------------------------------------------------------------- /S22-App-10-Student-Project-on-Building-a-Geocoder-Web-Service.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python Mega Course: Build 10 Real World Applications\n", 8 | "---" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "This notebook is a summary of [The Python Mega Course: Build 10 Real World Applciations](https://www.udemy.com/the-python-mega-course), a comprehensive online Python course taught by Ardit Sulce. Each lecture name is clickable and takes you to the video lecture in the course." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "# Section 22: Application 10: Student Project on Building a Geocoder Web Service\n", 23 | "***" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "**Lecture:** [Program Demonstration](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 31 | "---" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "This video lecture shows the finished version of the website running on a browser. The goal for this section is that students try to build this web app on their own and then they can compare ther solution with the one provided here." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "**Lecture:** [Solution, Part 1](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 46 | "---" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "For the final solution please see the lecture titled \"Final Code of Application 10\" down below." 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "**Lecture:** [Solution, Part 2](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 61 | "---" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "For the final solution please see the lecture titled \"Final Code of Application 10\" down below." 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "**Lecture:** [End of the Course](https://www.udemy.com/the-python-mega-course/learn/v4/t/lecture/9439078?start=0)\n", 76 | "---" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "This video lecture covers some final remarks about the course and what to do next." 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "**Lecture:** [Final Code of Application 10]()\n", 91 | "---" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "Project directory tree:" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": { 104 | "collapsed": true 105 | }, 106 | "source": [ 107 | "`templates\n", 108 | " index.html\n", 109 | " download.html\n", 110 | "static\n", 111 | " main.css\n", 112 | "app.py\n", 113 | "Procfile\n", 114 | "requirements.txt\n", 115 | "runtime.txt`" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "app.py" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "metadata": { 129 | "collapsed": true 130 | }, 131 | "outputs": [], 132 | "source": [ 133 | "from flask import Flask, render_template, request, send_file\n", 134 | "from geopy.geocoders import ArcGIS\n", 135 | "import pandas\n", 136 | "import datetime\n", 137 | "\n", 138 | "app=Flask(__name__)\n", 139 | "\n", 140 | "@app.route(\"/\")\n", 141 | "def index():\n", 142 | " return render_template(\"index.html\")\n", 143 | "\n", 144 | "@app.route('/success-table', methods=['POST'])\n", 145 | "def success_table():\n", 146 | " global filename\n", 147 | " if request.method == \"POST\":\n", 148 | " file=request.files['file']\n", 149 | " try:\n", 150 | " df = pandas.read_csv(file)\n", 151 | " gc = ArcGIS(scheme='http')\n", 152 | " df[\"coordinates\"] = df[\"Address\"].apply(gc.geocode)\n", 153 | " df['Latitude'] = df['coordinates'].apply(lambda x: x.latitude if x != None else None)\n", 154 | " df['Longitude'] = df['coordinates'].apply(lambda x: x.longitude if x != None else None)\n", 155 | " df = df.drop(\"coordinates\",1)\n", 156 | " filename = datetime.datetime.now().strftime(\"sample_files/%Y-%m-%d-%H-%M-%S-%f\"+\".csv\")\n", 157 | " df.to_csv(filename,index=None)\n", 158 | " return render_template(\"index.html\", text=df.to_html(), btn='download.html')\n", 159 | " except Exception as e:\n", 160 | " return render_template(\"index.html\", text=str(e))\n", 161 | "\n", 162 | "@app.route(\"/download-file/\")\n", 163 | "def download():\n", 164 | " return send_file(filename, attachment_filename='yourfile.csv', as_attachment=True)\n", 165 | "\n", 166 | "if __name__==\"__main__\":\n", 167 | " app.run(debug=True)" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": {}, 173 | "source": [ 174 | "templates/index.html" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": null, 180 | "metadata": { 181 | "collapsed": true 182 | }, 183 | "outputs": [], 184 | "source": [ 185 | "\n", 186 | "\n", 187 | " Super Geocoder \n", 188 | "\n", 189 | " \n", 190 | "\n", 191 | " \n", 192 | "
\n", 193 | "

Super Geocoder

\n", 194 | "

Please upload your CSV file. The values containing addresses should be in a column named address or Address

\n", 195 | "
\n", 196 | " \n", 197 | " \n", 198 | "
\n", 199 | "
\n", 200 | " {{text|safe}}\n", 201 | " {% include btn ignore missing %}\n", 202 | "
\n", 203 | "
\n", 204 | " \n", 205 | "" 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": {}, 211 | "source": [ 212 | "templates/download.html" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": null, 218 | "metadata": { 219 | "collapsed": true 220 | }, 221 | "outputs": [], 222 | "source": [ 223 | "\n", 224 | "\n", 225 | "
\n", 226 | " \n", 227 | "
\n", 228 | "" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": {}, 234 | "source": [ 235 | "static/main.css" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "metadata": { 242 | "collapsed": true 243 | }, 244 | "outputs": [], 245 | "source": [ 246 | "html, body {\n", 247 | " height: 100%;\n", 248 | " margin: 0;\n", 249 | "}\n", 250 | "\n", 251 | ".container {\n", 252 | " margin: 0 auto;\n", 253 | " width: 100%;\n", 254 | " height: 100%;\n", 255 | " background-color: #006666;\n", 256 | " color: #e6ffff;\n", 257 | " overflow:hidden;\n", 258 | " text-align: center;\n", 259 | "}\n", 260 | "\n", 261 | ".container form {\n", 262 | " margin: 20px;\n", 263 | "}\n", 264 | "\n", 265 | ".container h1 {\n", 266 | " font-family: Arial, sans-serif;\n", 267 | " font-size: 30px;\n", 268 | " color: #DDCCEE;\n", 269 | " margin-top: 80px;\n", 270 | "}\n", 271 | "\n", 272 | ".container button {\n", 273 | " width:70px;\n", 274 | " height: 30px;\n", 275 | " background-color: steelblue;\n", 276 | " margin: 3px;\n", 277 | "}\n", 278 | "\n", 279 | ".container input {\n", 280 | " width: 200px;\n", 281 | " height: 15px;\n", 282 | " font-size: 15px;\n", 283 | " margin: 2px;\n", 284 | " padding: 5px;\n", 285 | " transition: all 0.2s ease-in-out;\n", 286 | "}\n", 287 | "\n", 288 | ".output {\n", 289 | " display: inline-block;\n", 290 | "}" 291 | ] 292 | }, 293 | { 294 | "cell_type": "markdown", 295 | "metadata": {}, 296 | "source": [ 297 | "The following files are only necessary if you deploy your app on Heroku" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": {}, 303 | "source": [ 304 | "Procfile" 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "metadata": { 310 | "collapsed": true 311 | }, 312 | "source": [ 313 | "`web: gunicorn app:app`" 314 | ] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "metadata": {}, 319 | "source": [ 320 | "requirements.txt" 321 | ] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": { 326 | "collapsed": true 327 | }, 328 | "source": [ 329 | "`Flask==0.10.1\n", 330 | "gunicorn==19.6.0\n", 331 | "itsdangerous==0.24\n", 332 | "Jinja2==2.8\n", 333 | "MarkupSafe==0.23\n", 334 | "Werkzeug==0.11.10`" 335 | ] 336 | }, 337 | { 338 | "cell_type": "markdown", 339 | "metadata": {}, 340 | "source": [ 341 | "runtime.txt (Go to the [Heroku Python runtime webpage](https://devcenter.heroku.com/articles/python-runtimes#supported-python-runtimes) to see the latest version of Python used by Heroku and put that version in runtime.txt instead of the one below)" 342 | ] 343 | }, 344 | { 345 | "cell_type": "markdown", 346 | "metadata": { 347 | "collapsed": true 348 | }, 349 | "source": [ 350 | "`python-3.5.1`" 351 | ] 352 | } 353 | ], 354 | "metadata": { 355 | "kernelspec": { 356 | "display_name": "Python 3", 357 | "language": "python", 358 | "name": "python3" 359 | }, 360 | "language_info": { 361 | "codemirror_mode": { 362 | "name": "ipython", 363 | "version": 3 364 | }, 365 | "file_extension": ".py", 366 | "mimetype": "text/x-python", 367 | "name": "python", 368 | "nbconvert_exporter": "python", 369 | "pygments_lexer": "ipython3", 370 | "version": "3.6.3" 371 | } 372 | }, 373 | "nbformat": 4, 374 | "nbformat_minor": 2 375 | } 376 | -------------------------------------------------------------------------------- /Scatter_plotting.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Bokeh Plot 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 |
39 | 40 | 41 | 42 | 43 | 44 | 47 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /data/Galaxy_resized.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/Galaxy_resized.jpg -------------------------------------------------------------------------------- /data/Volcanoes.txt: -------------------------------------------------------------------------------- 1 | VOLCANX020,NUMBER,NAME,LOCATION,STATUS,ELEV,TYPE,TIMEFRAME,LAT,LON 2 | 509.000000000000000,1201-01=,Baker,US-Washington,Historical,3285.000000000000000,Stratovolcanoes,D3,48.7767982,-121.8109970 3 | 511.000000000000000,1201-02-,Glacier Peak,US-Washington,Tephrochronology,3213.000000000000000,Stratovolcano,D4,48.1118011,-121.1110001 4 | 513.000000000000000,1201-03-,Rainier,US-Washington,Dendrochronology,4392.000000000000000,Stratovolcano,D3,46.8698006,-121.7509995 5 | 515.000000000000000,1201-05-,St. Helens,US-Washington,Historical,2549.000000000000000,Stratovolcano,D1,46.1997986,-122.1809998 6 | 516.000000000000000,1201-04-,Adams,US-Washington,Tephrochronology,3742.000000000000000,Stratovolcano,D6,46.2057991,-121.4909973 7 | 517.000000000000000,1201-06-,West Crater,US-Washington,Radiocarbon,1329.000000000000000,Volcanic field,D7,45.8797989,-122.0810013 8 | 518.000000000000000,1201-07-,Indian Heaven,US-Washington,Radiocarbon,1806.000000000000000,Shield volcanoes,D7,45.9297981,-121.8209991 9 | 519.000000000000000,1202-01-,Hood,US-Oregon,Historical,3426.000000000000000,Stratovolcano,D3,45.3737984,-121.6910019 10 | 521.000000000000000,1202-02-,Jefferson,US-Oregon,Varve Count,3199.000000000000000,Stratovolcano,D6,44.6917992,-121.8010025 11 | 522.000000000000000,1202-03-,Blue Lake Crater,US-Oregon,Radiocarbon,1230.000000000000000,Maar,D7,44.4197998,-121.7710037 12 | 523.000000000000000,1202-04-,Sand Mountain Field,US-Oregon,Radiocarbon,1664.000000000000000,Cinder cones,D7,44.3797989,-121.9309998 13 | 524.000000000000000,1202-05-,Washington,US-Oregon,Radiocarbon,2376.000000000000000,Shield volcano,D6,44.3317986,-121.8310013 14 | 525.000000000000000,1202-06-,Belknap,US-Oregon,Radiocarbon,2095.000000000000000,Shield volcanoes,D6,44.2848015,-121.8410034 15 | 526.000000000000000,1202-07-,North Sister Field,US-Oregon,Radiocarbon,3074.000000000000000,Complex volcano,D6,44.1697998,-121.7710037 16 | 527.000000000000000,1202-08-,South Sister,US-Oregon,Radiocarbon,3157.000000000000000,Complex volcano,D7,44.0998001,-121.7710037 17 | 528.000000000000000,1202-09-,Bachelor,US-Oregon,Tephrochronology,2763.000000000000000,Stratovolcano,D7,43.9788017,-121.6809998 18 | 531.000000000000000,1202-11-,Newberry Volcano,US-Oregon,Radiocarbon,2434.000000000000000,Shield volcano,D6,43.7218018,-121.2210007 19 | 532.000000000000000,1202-10-,Davis Lake,US-Oregon,Radiocarbon,2163.000000000000000,Volcanic field,D7,43.5698013,-121.8209991 20 | 534.000000000000000,1202-12-,Devils Garden,US-Oregon,Holocene?,1698.000000000000000,Volcanic field,?,43.5119019,-120.8610001 21 | 535.000000000000000,1202-15-,Cinnamon Butte,US-Oregon,Holocene?,1956.000000000000000,Cinder cones,?,43.2407990,-122.1009979 22 | 536.000000000000000,1202-13-,Squaw Ridge Lava Field,US-Oregon,Holocene?,1711.000000000000000,Volcanic field,?,43.4719009,-120.7509995 23 | 537.000000000000000,1202-14-,Four Craters Lava Field,US-Oregon,Holocene?,1501.000000000000000,Volcanic field,?,43.3609009,-120.6610031 24 | 538.000000000000000,1202-16-,Crater Lake,US-Oregon,Radiocarbon,2487.000000000000000,Caldera,D7,42.9299011,-122.1210022 25 | 539.000000000000000,1205-01-,Yellowstone,US-Wyoming,Tephrochronology,2805.000000000000000,Calderas,D7,44.4299011,-110.6709976 26 | 541.000000000000000,1202-17-,Diamond Craters,US-Oregon,Holocene?,1435.000000000000000,Volcanic field,?,43.0998993,-118.7509995 27 | 542.000000000000000,1202-19-,Jordan Craters,US-Oregon,Radiocarbon,1473.000000000000000,Volcanic field,D7,43.1498985,-117.4710007 28 | 543.000000000000000,1202-18-,Saddle Butte,US-Oregon,Holocene?,1700.000000000000000,Volcanic field,?,42.9999008,-117.8010025 29 | 544.000000000000000,1204-02-,Craters of the Moon,US-Idaho,Radiocarbon,2005.000000000000000,Cinder cones,D7,43.4198990,-113.5009995 30 | 545.000000000000000,1204-04-,Hell's Half Acre,US-Idaho,Radiocarbon,1631.000000000000000,Shield volcano,D7,43.4999008,-112.4509964 31 | 546.000000000000000,1204-01-,Shoshone Lava Field,US-Idaho,Holocene,1478.000000000000000,Shield volcano,D7,43.1799011,-114.3509979 32 | 547.000000000000000,1202-20-,Jackies Butte,US-Oregon,Holocene?,1418.000000000000000,Volcanic field,?,42.6058998,-117.5810013 33 | 548.000000000000000,1203-02-,Medicine Lake,US-California,Radiocarbon,2412.000000000000000,Shield volcano,D6,41.5798988,-121.5709991 34 | 549.000000000000000,1204-03-,Wapi Lava Field,US-Idaho,Radiocarbon,1604.000000000000000,Shield volcano,D7,42.8799019,-113.2210007 35 | 550.000000000000000,1203-01-,Shasta,US-California,Historical,4317.000000000000000,Stratovolcano,D4,41.4198990,-122.2009964 36 | 551.000000000000000,1203-03-,Brushy Butte,US-California,Holocene?,1174.000000000000000,Shield volcano,?,41.1778984,-121.4410019 37 | 552.000000000000000,1203-04-,Big Cave,US-California,Holocene?,1259.000000000000000,Shield volcano,?,40.9548988,-121.3610001 38 | 553.000000000000000,1203-05-,Twin Buttes,US-California,Holocene?,1631.000000000000000,Cinder cones,?,40.7798996,-121.6009979 39 | 554.000000000000000,1203-06-,Tumble Buttes,US-California,Holocene?,2191.000000000000000,Cinder cones,?,40.6799011,-121.5510025 40 | 555.000000000000000,1203-09-,Eagle Lake Field,US-California,Holocene?,1652.000000000000000,Fissure vents,?,40.6299019,-120.8310013 41 | 556.000000000000000,1203-08-,Lassen Volc Center,US-California,Historical,3187.000000000000000,Stratovolcano,D2,40.4919014,-121.5009995 42 | 557.000000000000000,1203-10-,Clear Lake,US-California,Holocene,1439.000000000000000,Volcanic field,U,38.9698982,-122.7710037 43 | 558.000000000000000,1206-01-,Steamboat Springs,US-Nevada,Pleistocene-Fumarolic,1415.000000000000000,Lava domes,Q,39.3749008,-119.7210007 44 | 560.000000000000000,1208-01-,Dotsero,US-Colorado,Radiocarbon,2230.000000000000000,Maar,D7,39.6500015,-107.0309982 45 | 561.000000000000000,1203-11-,Mono Lake Volc Field,US-California,Tephrochronology,2121.000000000000000,Cinder cones,D4,37.9999008,-119.0309982 46 | 562.000000000000000,1207-05-,Black Rock Desert,US-Utah,Radiocarbon,1800.000000000000000,Volcanic field,D6,38.9700012,-112.5009995 47 | 563.000000000000000,1203-12-,Mono Craters,US-California,Radiocarbon,2796.000000000000000,Lava domes,D6,37.8799019,-119.0009995 48 | 564.000000000000000,1203-13-,Inyo Craters,US-California,Radiocarbon,2629.000000000000000,Lava domes,D6,37.6918983,-119.0210037 49 | 565.000000000000000,1203-14-,Long Valley,US-California,Pleistocene-Fumarolic,3390.000000000000000,Caldera,Q,37.6999016,-118.8710022 50 | 566.000000000000000,1203-15-,Red Cones,US-California,Radiocarbon,2748.000000000000000,Cinder cones,D7,37.5798988,-119.0510025 51 | 568.000000000000000,1203-16-,Ubehebe Craters,US-California,Anthropology,752.000000000000000,Maars,D7,37.0199013,-117.4509964 52 | 569.000000000000000,1207-04-,Markagunt Plateau,US-Utah,Dendrochronology,2840.000000000000000,Volcanic field,D6,37.5800018,-112.6709976 53 | 570.000000000000000,1207-01-,Santa Clara,US-Utah,Holocene?,1465.000000000000000,Volcanic field,?,37.2570000,-113.6210022 54 | 571.000000000000000,1207-03-,Bald Knoll,US-Utah,Holocene,2135.000000000000000,Cinder cones,U,37.3279991,-112.4010010 55 | 572.000000000000000,1203-17-,Golden Trout Creek,US-California,Tephrochronology,2886.000000000000000,Volcanic field,D7,36.3578987,-118.3209991 56 | 574.000000000000000,1203-18-,Coso Volc Field,US-California,Holocene?,2400.000000000000000,Lava domes,?,36.0298996,-117.8209991 57 | 576.000000000000000,1209-01-,Uinkaret Field,US-Arizona,Anthropology,1555.000000000000000,Volcanic field,D6,36.3800011,-113.1309967 58 | 579.000000000000000,1203-19-,Lavic Lake,US-California,Holocene?,1495.000000000000000,Volcanic field,?,34.7500000,-116.6210022 59 | 580.000000000000000,1210-03-,Valles Caldera,US-New Mexico,Pleistocene-Fumarolic,3430.000000000000000,Caldera,Q,35.8699989,-106.5709991 60 | 581.000000000000000,1209-02-,Sunset Crater,US-Arizona,Dendrochronology,2447.000000000000000,Cinder cone,D6,35.3699989,-111.5009995 61 | 584.000000000000000,1203-20-,Amboy,US-California,Holocene,288.000000000000000,Cinder cone,U,34.5499992,-115.7809982 62 | 586.000000000000000,1210-02-,Zuni-Bandera,US-New Mexico,Anthropology,2550.000000000000000,Volcanic field,D7,34.7999992,-108.0009995 63 | 588.000000000000000,1210-01-,Carrizozo,US-New Mexico,Holocene,1731.000000000000000,Cinder cone,U,33.7801018,-105.9309998 64 | -------------------------------------------------------------------------------- /data/balance.txt: -------------------------------------------------------------------------------- 1 | 907 -------------------------------------------------------------------------------- /data/books.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/books.db -------------------------------------------------------------------------------- /data/circle_markers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/circle_markers.png -------------------------------------------------------------------------------- /data/galaxy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/galaxy.jpg -------------------------------------------------------------------------------- /data/images/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/images/.DS_Store -------------------------------------------------------------------------------- /data/images/Lighthouse.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/images/Lighthouse.jpg -------------------------------------------------------------------------------- /data/images/Moon sinking, sun rising.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/images/Moon sinking, sun rising.jpg -------------------------------------------------------------------------------- /data/images/galaxy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/images/galaxy.jpg -------------------------------------------------------------------------------- /data/images/kangaroos-rain-australia_71370_990x742.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/images/kangaroos-rain-australia_71370_990x742.jpg -------------------------------------------------------------------------------- /data/lecture_link_demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/lecture_link_demo.gif -------------------------------------------------------------------------------- /data/lite.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/lite.db -------------------------------------------------------------------------------- /data/newlite.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/newlite.db -------------------------------------------------------------------------------- /data/news.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/news.jpg -------------------------------------------------------------------------------- /data/newsmallgray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/newsmallgray.png -------------------------------------------------------------------------------- /data/smallgray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/smallgray.png -------------------------------------------------------------------------------- /data/supermarkets-commas.txt: -------------------------------------------------------------------------------- 1 | ID,Address,City,State,Country,Name,Employees 2 | 1,3666 21st St,San Francisco,CA 94114,USA, Madeira,8 3 | 2,735 Dolores St,San Francisco,CA 94119,USA,Bready Shop,15 4 | 3,332 Hill St,San Francisco,California 94114,USA,Super River,25 5 | 4,3995 23rd St,San Francisco,CA 94114,USA,Ben's Shop,10 6 | 5,1056 Sanchez St,San Francisco,California,USA,Sanchez,12 7 | 6,551 Alvarado St,San Francisco,CA 94114,USA,Richvalley,20 8 | -------------------------------------------------------------------------------- /data/supermarkets-semi-colons.txt: -------------------------------------------------------------------------------- 1 | ID;Address;City;State;Country;Name;Employees 2 | 1;3666 21st St;San Francisco;CA 94114;USA;Madeira;8 3 | 2;735 Dolores St;San Francisco;CA 94119;USA;Bready Shop;15 4 | 3;332 Hill St;San Francisco;California 94114;USA; Super River;25 5 | 4;3995 23rd St;San Francisco;CA 94114;USA;Ben's Shop;10 6 | 5;1056 Sanchez St;San Francisco;California;USA;Sanchez;12 7 | 6;551 Alvarado St;San Francisco;CA 94114;USA;Richvalley;20 8 | -------------------------------------------------------------------------------- /data/supermarkets.csv: -------------------------------------------------------------------------------- 1 | ID,Address,City,State,Country,Name,Employees 2 | 1,3666 21st St,San Francisco,CA 94114,USA,Madeira,8 3 | 2,735 Dolores St,San Francisco,CA 94119,USA,Bready Shop,15 4 | 3,332 Hill St,San Francisco,California 94114,USA,Super River,25 5 | 4,3995 23rd St,San Francisco,CA 94114,USA,Ben's Shop,10 6 | 5,1056 Sanchez St,San Francisco,California,USA,Sanchez,12 7 | 6,551 Alvarado St,San Francisco,CA 94114,USA,Richvalley,20 8 | -------------------------------------------------------------------------------- /data/supermarkets.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "ID": 1, 4 | "Address": "3666 21st St", 5 | "City": "San Francisco", 6 | "State": "CA 94114", 7 | "Country": "USA", 8 | "Name": "Madeira", 9 | "Employees": 8 10 | }, 11 | { 12 | "ID": 2, 13 | "Address": "735 Dolores St", 14 | "City": "San Francisco", 15 | "State": "CA 94119", 16 | "Country": "USA", 17 | "Name": "Bready Shop", 18 | "Employees": 15 19 | }, 20 | { 21 | "ID": 3, 22 | "Address": "332 Hill St", 23 | "City": "San Francisco", 24 | "State": "California 94114", 25 | "Country": "USA", 26 | "Name": "Super River", 27 | "Employees": 25 28 | }, 29 | { 30 | "ID": 4, 31 | "Address": "3995 23rd St", 32 | "City": "San Francisco", 33 | "State": "CA 94114", 34 | "Country": "USA", 35 | "Name": "Ben's Shop", 36 | "Employees": 10 37 | }, 38 | { 39 | "ID": 5, 40 | "Address": "1056 Sanchez St", 41 | "City": "San Francisco", 42 | "State": "California", 43 | "Country": "USA", 44 | "Name": "Sanchez", 45 | "Employees": 12 46 | }, 47 | { 48 | "ID": 6, 49 | "Address": "551 Alvarado St", 50 | "City": "San Francisco", 51 | "State": "CA 94114", 52 | "Country": "USA", 53 | "Name": "Richvalley", 54 | "Employees": 20 55 | } 56 | ] -------------------------------------------------------------------------------- /data/supermarkets.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/data/supermarkets.xlsx -------------------------------------------------------------------------------- /scratch1.py: -------------------------------------------------------------------------------- 1 | import json 2 | from difflib import get_close_matches 3 | data = json.load(open("data.json")) 4 | def translate(w): 5 | w = w.lower() 6 | if w in data: 7 | return data[w] 8 | elif w.title() in data: 9 | return data[w.title()] 10 | elif w.upper() in data: #in case user enters words like USA or NATO 11 | return data[w.upper()] 12 | elif len(get_close_matches(w, data.keys())) > 0: 13 | yn = input("Did you mean %s instead? Enter Y if yes, or N if no: " % get_close_matches(w, data.keys())[0]) 14 | if yn == "Y": 15 | return data[get_close_matches(w, data.keys())[0]] 16 | elif yn == "N": 17 | return "The word doesn't exist. Please double check it." 18 | else: 19 | return "We didn't understand your entry." 20 | else: 21 | return "The word doesn't exist. Please double check it." 22 | word = input("Enter word: ") 23 | output = translate(word) 24 | if type(output) == list: 25 | for item in output: 26 | print(item) 27 | else: 28 | print(output) -------------------------------------------------------------------------------- /smallgray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/smallgray.png -------------------------------------------------------------------------------- /supermarkets-commas.txt: -------------------------------------------------------------------------------- 1 | ID,Address,City,State,Country,Name,Employees 2 | 1,3666 21st St,San Francisco,CA 94114,USA, Madeira,8 3 | 2,735 Dolores St,San Francisco,CA 94119,USA,Bready Shop,15 4 | 3,332 Hill St,San Francisco,California 94114,USA,Super River,25 5 | 4,3995 23rd St,San Francisco,CA 94114,USA,Ben's Shop,10 6 | 5,1056 Sanchez St,San Francisco,California,USA,Sanchez,12 7 | 6,551 Alvarado St,San Francisco,CA 94114,USA,Richvalley,20 8 | -------------------------------------------------------------------------------- /supermarkets-semi-colons.txt: -------------------------------------------------------------------------------- 1 | ID;Address;City;State;Country;Name;Employees 2 | 1;3666 21st St;San Francisco;CA 94114;USA;Madeira;8 3 | 2;735 Dolores St;San Francisco;CA 94119;USA;Bready Shop;15 4 | 3;332 Hill St;San Francisco;California 94114;USA; Super River;25 5 | 4;3995 23rd St;San Francisco;CA 94114;USA;Ben's Shop;10 6 | 5;1056 Sanchez St;San Francisco;California;USA;Sanchez;12 7 | 6;551 Alvarado St;San Francisco;CA 94114;USA;Richvalley;20 8 | -------------------------------------------------------------------------------- /supermarkets.csv: -------------------------------------------------------------------------------- 1 | ID,Address,City,State,Country,Name,Employees 2 | 1,3666 21st St,San Francisco,CA 94114,USA,Madeira,8 3 | 2,735 Dolores St,San Francisco,CA 94119,USA,Bready Shop,15 4 | 3,332 Hill St,San Francisco,California 94114,USA,Super River,25 5 | 4,3995 23rd St,San Francisco,CA 94114,USA,Ben's Shop,10 6 | 5,1056 Sanchez St,San Francisco,California,USA,Sanchez,12 7 | 6,551 Alvarado St,San Francisco,CA 94114,USA,Richvalley,20 8 | -------------------------------------------------------------------------------- /supermarkets.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "ID": 1, 4 | "Address": "3666 21st St", 5 | "City": "San Francisco", 6 | "State": "CA 94114", 7 | "Country": "USA", 8 | "Name": "Madeira", 9 | "Employees": 8 10 | }, 11 | { 12 | "ID": 2, 13 | "Address": "735 Dolores St", 14 | "City": "San Francisco", 15 | "State": "CA 94119", 16 | "Country": "USA", 17 | "Name": "Bready Shop", 18 | "Employees": 15 19 | }, 20 | { 21 | "ID": 3, 22 | "Address": "332 Hill St", 23 | "City": "San Francisco", 24 | "State": "California 94114", 25 | "Country": "USA", 26 | "Name": "Super River", 27 | "Employees": 25 28 | }, 29 | { 30 | "ID": 4, 31 | "Address": "3995 23rd St", 32 | "City": "San Francisco", 33 | "State": "CA 94114", 34 | "Country": "USA", 35 | "Name": "Ben's Shop", 36 | "Employees": 10 37 | }, 38 | { 39 | "ID": 5, 40 | "Address": "1056 Sanchez St", 41 | "City": "San Francisco", 42 | "State": "California", 43 | "Country": "USA", 44 | "Name": "Sanchez", 45 | "Employees": 12 46 | }, 47 | { 48 | "ID": 6, 49 | "Address": "551 Alvarado St", 50 | "City": "San Francisco", 51 | "State": "CA 94114", 52 | "Country": "USA", 53 | "Name": "Richvalley", 54 | "Employees": 20 55 | } 56 | ] -------------------------------------------------------------------------------- /supermarkets.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arditsulceteaching/thepythonmegacourse/836574eeb29b4fbb6192c9e8ade6f63facac6858/supermarkets.xlsx --------------------------------------------------------------------------------