{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Obviously, stealing is not OK, also not for programmers. Nevertheless, many software is freely available, typically even opensource. Moreover, Python provides a lot out of the box. Next to software shipped with Python, there is many more on [https://pypi.python.org](pypi.python.org). PyPI stands for Python Package Index. You can install PyPI software with `pip3`, a command shipped with Python.\n", "\n", "This notebook shows you what happens if you install something, in particular where the software is stored on your hard disk. You will install `folium`, a software used to create (land) maps." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Run the cell below. Notice that *folium is not installed* and hence there is an \n", "```\n", "ImportError\n", "```." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import folium" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Import the module `sys` using the instruction `import sys`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# YOUR CODE HERE, ~1 line\n", "None\n", "# END OF YOUR CODE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`sys` is a module that has an attribute `path`. `path` contains a list of folders where python looks whenever you import something. This is called the python path, sometimes reffered to as the `PYTHONPATH` variable, because this variable can be overridden via the operating system.\n", "\n", "**Note:** There is also a module `os.path`, to work with files on your hard disk. From now on, the hard disk also means the file system. Do not confulse `os.path` (file system operations) and `sys.path` (where python looks for software).\n", "\n", "**Exercise:** Look at the content of `sys.path`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# YOUR CODE HERE, ~1 line\n", "None\n", "# END OF YOUR CODE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This list include text in between apostrophes (`'`). This is Python's way to display text elements (a.k.a. strings).\n", "\n", "**Apple computer**\n", "> You will also notice foward slashes (`/`). They separate folders in your computer, and are called path separators.\n", "\n", "**Windows computer**\n", "> You will also notice slashes (`\\`). They separate folders in your computer, and are called path separators. \n", ">\n", "> You will also notice that slahes are doubled (`\\\\`). Putting a `\\` before a character is called escaping. We will talk more about it in a future lecture.\n", "\n", "---\n", "\n", "---\n", "\n", "Among the folders you just saw, there is one with your packages. To you help you find it, execute one of the following cells:\n", "\n", "**On Windows**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for folder in sys.path:\n", " if '\\\\intropython\\\\lib\\\\site-packages' in folder:\n", " print('==> ', folder)\n", " else:\n", " print(folder)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**On Apple**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for folder in sys.path:\n", " if '/intropython/lib/python3.6/site-packages' in folder:\n", " print('==> ', folder)\n", " else:\n", " print(folder)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The folder with `==>` is the one you are looking for. Open it using Windows Explorer (on Windows) of Files (on Apple).\n", "\n", "You should see many folders with installed Python software, **MAKE SURE NOT TO DELETE OR EDIT ANYTHING**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that you cannot find `folium` in this folder. Let's install it!\n", "\n", "You should always install or uninstall software using `pip3 install ` or `pip3 uninstall `.\n", "\n", "Open Anaconda Console via the start menu, activate the Python environment you created last week via `conda activate introproc` and then install `folium` using `pip3 install folium`.\n", "\n", "**Exercise:** Run the activate and install commands in Anaconda Console, then refresh the aformentioned folder you should see folium." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's show a map of Leeuwarden with a marker where we are (approximately)! No worries about understanding the code below, make the following notes though:\n", "\n", " * with `pip3 install` and `import` we can use existing software,\n", " * folium allows us to create a map, with only 3 or 4 lines of code,\n", " * this map is stored in a variable `m`, via the first line with `m = ...`\n", " * this line creates a `folium.Map`, tells it which background-map to use (`'stamentoner'`), the centre location in latitude and longitude, and the zoom level.\n", " * then a marker is added to the map\n", " * the last expression of the cell is its output, in this case that is the map `m`. If you delete this line, the map will be created, but not show.\n", "\n", "**Exercise:** delete the last line with `m` and verify that the map will be gone. Put it back again." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import folium" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "m = folium.Map(tiles='stamentoner', location=[53.1991713, 5.7941344], zoom_start=16)\n", "\n", "folium.Marker(location=[53.1991713, 5.7941344]).add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The map `m` also has attributes, accessable via the period point (`.`). That's because `m` is a special type of variable: an `object`. Access the attribute `location` of `m`. You can start by typing `m.l` and then press the key *TAB*. Jupyter will show several options for you to choose from, choose `location`.\n", "\n", "**Exercise:** Get the location of `m`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# YOUR CODE HERE, ~1 line\n", "None\n", "# END OF YOUR CODE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The map also has an attribute `get_bounds` that is a function. Functions of objects are called methods. You also call them using brackets `()`.\n", "\n", "**Exercise:** use `get_bounds()` to get the top-left and bottom-right corners of the map." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# YOUR CODE HERE, ~1 line\n", "None\n", "# END OF YOUR CODE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You have learned about\n", "\n", " * The Python path\n", " * Installing software via pip\n", " * Importing code of others\n", " * Accessing attributes or methods with the dot `some_object.some_attribute`" ] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 2 }