|
| 1 | +Usage |
| 2 | +===== |
| 3 | + |
| 4 | +ipyleaflet is an interactive widgets library, it is based on `ipywidgets <https://github.com/jupyter-widgets/ipywidgets/>`_. |
| 5 | +This means that everything in ipyleaflet (e.g. the ``Map``, ``TileLayers``, ``Markers``...) is interactive: you can dynamically update |
| 6 | +attributes from Python or from the Notebook interface. |
| 7 | + |
| 8 | +For example, you can create a ``Marker`` layer and interact with it: |
| 9 | + |
| 10 | +.. code:: |
| 11 | +
|
| 12 | + from ipyleaflet import Map, Marker |
| 13 | +
|
| 14 | + center = (52.204793, 360.121558) |
| 15 | +
|
| 16 | + m = Map(center=center, zoom=15) |
| 17 | +
|
| 18 | + marker = Marker(location=center, draggable=True) |
| 19 | + m.add_layer(marker); |
| 20 | +
|
| 21 | + display(m) |
| 22 | +
|
| 23 | + # Now that the marker is on the Map, you can drag it with your mouse, |
| 24 | + # it will automatically update the `marker.location` attribute in Python |
| 25 | +
|
| 26 | + # You can also update the marker location from Python, that will update the |
| 27 | + # marker location on the Map: |
| 28 | + marker.location = (50, 356) |
| 29 | +
|
| 30 | +`ipywidgets <https://github.com/jupyter-widgets/ipywidgets/>`_ is powered by `traitlets <https://github.com/ipython/traitlets/>`_, |
| 31 | +this brings an observer pattern implementation which allows you to react on widget attribute changes. |
| 32 | + |
| 33 | +For example, you can define a Python callback that will be called whenever the marker location has changed: |
| 34 | + |
| 35 | +.. code:: |
| 36 | +
|
| 37 | + def on_location_changed(event): |
| 38 | + # Do some computation given the new marker location, accessible from `event['new']` |
| 39 | + pass |
| 40 | +
|
| 41 | + marker.observe(on_location_changed, 'location') |
| 42 | +
|
| 43 | +Please check out the `traitlets documentation <https://traitlets.readthedocs.io/>`_ for more details about the observer pattern implementation. |
| 44 | + |
| 45 | +.. note:: |
| 46 | + Everything in ipyleaflet **is** an interactive widget, from the ``Map`` class to ``Layer`` and ``Control`` classes. This means that what we |
| 47 | + achieved here with ``marker.location``, you can achieve it with ``map.zoom``, ``layer.url``, or ``heatmap.locations`` |
| 48 | + |
| 49 | +You can try ipyleaflet online using binder, no need to install anything on your computer: |
| 50 | + |
| 51 | +.. image:: https://mybinder.org/badge_logo.svg |
| 52 | + :target: https://mybinder.org/v2/gh/jupyter-widgets/ipyleaflet/stable?filepath=examples |
0 commit comments