|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Introduction to `folium.plugins`" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "Just a small description of `plugins`, what they are, how they work, and how to implement one." |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "### Template structure" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "markdown", |
| 26 | + "metadata": {}, |
| 27 | + "source": [ |
| 28 | + "To undestand how plugins work, you have to know how Folium template is made. Basically, it is made of four parts :\n", |
| 29 | + "\n", |
| 30 | + "* The **header** : where one defines imports of CSS stylesheets and Javascript external scripts. For example, the link to Leaflet library and stylesheets are there.\n", |
| 31 | + "* The **css** : where one defines specific styles.\n", |
| 32 | + "* The **html** body : where one defines the document object model. For example, the map(s)' `div`s are defined there.\n", |
| 33 | + "* The **javascript** : where the Leaflet script is written. For example, the map objects, layers, markers... are defined here.\n", |
| 34 | + "\n", |
| 35 | + "Now, a plugin is an object that inherits from `folium.plugins.Plugin`, whose goal is to write *things* into these four parts." |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "### Quick and dirty example" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "markdown", |
| 47 | + "metadata": {}, |
| 48 | + "source": [ |
| 49 | + "This is an example of plugin, that imports a Javascript library, defines a css class, creates a new div, defines a Javascript object and add it to the map.\n", |
| 50 | + "```python\n", |
| 51 | + "from folium.plugins.plugin import Plugin\n", |
| 52 | + "\n", |
| 53 | + "class MyPlugin(Plugin):\n", |
| 54 | + " def __init__(self, data):\n", |
| 55 | + " \"\"\"Creates a MyPlugin plugin to append into a map with\n", |
| 56 | + " Map.add_plugin.\n", |
| 57 | + " \"\"\"\n", |
| 58 | + " super(MyPlugin, self).__init__() # We call Plugin.__init__.\n", |
| 59 | + " # This will (in particular) define self.object_id as a random hexadecimal string (unique).\n", |
| 60 | + "\n", |
| 61 | + " self.plugin_name = 'MyPlugin' # This will help to name variables in html and js.\n", |
| 62 | + "\n", |
| 63 | + " def render_header(self, nb):\n", |
| 64 | + " \"\"\"Generates the header part of the plugin.\"\"\"\n", |
| 65 | + " return \"\"\"\n", |
| 66 | + " <link rel=\"stylesheet\" href=\"https://myplugin_stylesheet.css\">\n", |
| 67 | + " <script src=\"https://myplugin_script.js\">\n", |
| 68 | + " \"\"\" if nb==0 else \"\"\n", |
| 69 | + "\n", |
| 70 | + " def render_css(self, nb):\n", |
| 71 | + " \"\"\"Generates the css part of the plugin.\"\"\"\n", |
| 72 | + " return \"\"\"\n", |
| 73 | + " #myplugin {\n", |
| 74 | + " color: #ff00ff,\n", |
| 75 | + " width: 314px\n", |
| 76 | + " };\n", |
| 77 | + " \"\"\"\n", |
| 78 | + "\n", |
| 79 | + " def render_html(self, nb):\n", |
| 80 | + " \"\"\"Generates the html part of the plugin.\"\"\"\n", |
| 81 | + " return \"\"\"\n", |
| 82 | + " <div id=\"myplugin{id}\"></div>\n", |
| 83 | + " \"\"\".format(id = self.object_name)\n", |
| 84 | + "\n", |
| 85 | + " def render_js(self, nb):\n", |
| 86 | + " \"\"\"Generates the Javascript part of the plugin.\"\"\"\n", |
| 87 | + " return \"\"\"\n", |
| 88 | + " var MyPlugin_{id} = myplugin_script.someObject();\n", |
| 89 | + " map.addLayer(MyPlugin_{id});\n", |
| 90 | + " \"\"\".format(id = self.object_name)\n", |
| 91 | + "```\n", |
| 92 | + "Note that you may be willing to put on a map several instances of the same plugin. But you don't want the header to be written several times. This is why each method has a `nb` argument, that will be incremented at each instance's render.\n", |
| 93 | + "Hence the line\n", |
| 94 | + "\n", |
| 95 | + " if nb==0 else \"\"" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "markdown", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "### More sophisticated example" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "markdown", |
| 107 | + "metadata": {}, |
| 108 | + "source": [ |
| 109 | + "TODO : write something interesting here." |
| 110 | + ] |
| 111 | + } |
| 112 | + ], |
| 113 | + "metadata": { |
| 114 | + "kernelspec": { |
| 115 | + "display_name": "Python 2", |
| 116 | + "language": "python", |
| 117 | + "name": "python2" |
| 118 | + }, |
| 119 | + "language_info": { |
| 120 | + "codemirror_mode": { |
| 121 | + "name": "ipython", |
| 122 | + "version": 2 |
| 123 | + }, |
| 124 | + "file_extension": ".py", |
| 125 | + "mimetype": "text/x-python", |
| 126 | + "name": "python", |
| 127 | + "nbconvert_exporter": "python", |
| 128 | + "pygments_lexer": "ipython2", |
| 129 | + "version": "2.7.10" |
| 130 | + } |
| 131 | + }, |
| 132 | + "nbformat": 4, |
| 133 | + "nbformat_minor": 0 |
| 134 | +} |
0 commit comments