|
| 1 | +from typing import Optional, Union |
| 2 | + |
| 3 | +from branca.element import MacroElement |
| 4 | +from jinja2 import Template |
| 5 | + |
| 6 | +from folium.elements import JSCSSMixin |
| 7 | +from folium.utilities import JsCode, camelize, parse_options |
| 8 | + |
| 9 | + |
| 10 | +class Realtime(JSCSSMixin, MacroElement): |
| 11 | + """Put realtime data on a Leaflet map: live tracking GPS units, |
| 12 | + sensor data or just about anything. |
| 13 | +
|
| 14 | + Based on: https://github.com/perliedman/leaflet-realtime |
| 15 | +
|
| 16 | + Parameters |
| 17 | + ---------- |
| 18 | + source: str, dict, JsCode |
| 19 | + The source can be one of: |
| 20 | +
|
| 21 | + * a string with the URL to get data from |
| 22 | + * a dict that is passed to javascript's `fetch` function |
| 23 | + for fetching the data |
| 24 | + * a `folium.JsCode` object in case you need more freedom. |
| 25 | + start: bool, default True |
| 26 | + Should automatic updates be enabled when layer is added |
| 27 | + on the map and stopped when layer is removed from the map |
| 28 | + interval: int, default 60000 |
| 29 | + Automatic update interval, in milliseconds |
| 30 | + get_feature_id: JsCode, optional |
| 31 | + A JS function with a geojson `feature` as parameter |
| 32 | + default returns `feature.properties.id` |
| 33 | + Function to get an identifier to uniquely identify a feature over time |
| 34 | + update_feature: JsCode, optional |
| 35 | + A JS function with a geojson `feature` as parameter |
| 36 | + Used to update an existing feature's layer; |
| 37 | + by default, points (markers) are updated, other layers are discarded |
| 38 | + and replaced with a new, updated layer. |
| 39 | + Allows to create more complex transitions, |
| 40 | + for example, when a feature is updated |
| 41 | + remove_missing: bool, default False |
| 42 | + Should missing features between updates been automatically |
| 43 | + removed from the layer |
| 44 | +
|
| 45 | +
|
| 46 | + Other keyword arguments are passed to the GeoJson layer, so you can pass |
| 47 | + `style`, `point_to_layer` and/or `on_each_feature`. |
| 48 | +
|
| 49 | + Examples |
| 50 | + -------- |
| 51 | + >>> from folium import JsCode |
| 52 | + >>> m = folium.Map(location=[40.73, -73.94], zoom_start=12) |
| 53 | + >>> rt = Realtime( |
| 54 | + ... "https://raw.githubusercontent.com/python-visualization/folium-example-data/main/subway_stations.geojson", |
| 55 | + ... get_feature_id=JsCode("(f) => { return f.properties.objectid; }"), |
| 56 | + ... point_to_layer=JsCode( |
| 57 | + ... "(f, latlng) => { return L.circleMarker(latlng, {radius: 8, fillOpacity: 0.2})}" |
| 58 | + ... ), |
| 59 | + ... interval=10000, |
| 60 | + ... ) |
| 61 | + >>> rt.add_to(m) |
| 62 | + """ |
| 63 | + |
| 64 | + _template = Template( |
| 65 | + """ |
| 66 | + {% macro script(this, kwargs) %} |
| 67 | + var {{ this.get_name() }}_options = {{ this.options|tojson }}; |
| 68 | + {% for key, value in this.functions.items() %} |
| 69 | + {{ this.get_name() }}_options["{{key}}"] = {{ value }}; |
| 70 | + {% endfor %} |
| 71 | +
|
| 72 | + var {{ this.get_name() }} = new L.realtime( |
| 73 | + {% if this.src is string or this.src is mapping -%} |
| 74 | + {{ this.src|tojson }}, |
| 75 | + {% else -%} |
| 76 | + {{ this.src.js_code }}, |
| 77 | + {% endif -%} |
| 78 | + {{ this.get_name() }}_options |
| 79 | + ); |
| 80 | + {{ this._parent.get_name() }}.addLayer( |
| 81 | + {{ this.get_name() }}._container); |
| 82 | + {% endmacro %} |
| 83 | + """ |
| 84 | + ) |
| 85 | + |
| 86 | + default_js = [ |
| 87 | + ( |
| 88 | + "Leaflet_Realtime_js", |
| 89 | + "https://cdnjs.cloudflare.com/ajax/libs/leaflet-realtime/2.2.0/leaflet-realtime.js", |
| 90 | + ) |
| 91 | + ] |
| 92 | + |
| 93 | + def __init__( |
| 94 | + self, |
| 95 | + source: Union[str, dict, JsCode], |
| 96 | + start: bool = True, |
| 97 | + interval: int = 60000, |
| 98 | + get_feature_id: Optional[JsCode] = None, |
| 99 | + update_feature: Optional[JsCode] = None, |
| 100 | + remove_missing: bool = False, |
| 101 | + **kwargs |
| 102 | + ): |
| 103 | + super().__init__() |
| 104 | + self._name = "Realtime" |
| 105 | + self.src = source |
| 106 | + |
| 107 | + kwargs["start"] = start |
| 108 | + kwargs["interval"] = interval |
| 109 | + if get_feature_id is not None: |
| 110 | + kwargs["get_feature_id"] = get_feature_id |
| 111 | + if update_feature is not None: |
| 112 | + kwargs["update_feature"] = update_feature |
| 113 | + kwargs["remove_missing"] = remove_missing |
| 114 | + |
| 115 | + # extract JsCode objects |
| 116 | + self.functions = {} |
| 117 | + for key, value in list(kwargs.items()): |
| 118 | + if isinstance(value, JsCode): |
| 119 | + self.functions[camelize(key)] = value.js_code |
| 120 | + kwargs.pop(key) |
| 121 | + |
| 122 | + self.options = parse_options(**kwargs) |
0 commit comments