|
7 | 7 | from collections import OrderedDict |
8 | 8 | from typing import TYPE_CHECKING, Optional, Sequence, Union, cast |
9 | 9 |
|
10 | | -from branca.element import Element, Html, MacroElement |
| 10 | +from branca.element import Element, Figure, Html, MacroElement |
11 | 11 |
|
12 | 12 | from folium.elements import ElementAddToElement, EventHandler |
13 | 13 | from folium.template import Template |
@@ -454,27 +454,25 @@ class Popup(MacroElement): |
454 | 454 |
|
455 | 455 | _template = Template( |
456 | 456 | """ |
457 | | - {% macro script(this, kwargs) %} |
458 | | - var {{this.get_name()}} = L.popup({{ this.options|tojavascript }}); |
459 | | -
|
460 | | - {% for name, element in this.html._children.items() %} |
461 | | - {% if this.lazy %} |
462 | | - {{ this._parent.get_name() }}.once('click', function() { |
463 | | - {{ this.get_name() }}.setContent($(`{{ element.render(**kwargs).replace('\\n',' ') }}`)[0]); |
464 | | - }); |
465 | | - {% else %} |
466 | | - var {{ name }} = $(`{{ element.render(**kwargs).replace('\\n',' ') }}`)[0]; |
467 | | - {{ this.get_name() }}.setContent({{ name }}); |
468 | | - {% endif %} |
469 | | - {% endfor %} |
470 | | -
|
471 | | - {{ this._parent.get_name() }}.bindPopup({{ this.get_name() }}) |
472 | | - {% if this.show %}.openPopup(){% endif %}; |
473 | | -
|
474 | | - {% for name, element in this.script._children.items() %} |
475 | | - {{element.render()}} |
476 | | - {% endfor %} |
477 | | - {% endmacro %} |
| 457 | + var {{this.get_name()}} = L.popup({{ this.options|tojavascript }}); |
| 458 | +
|
| 459 | + {% for name, element in this.html._children.items() %} |
| 460 | + {% if this.lazy %} |
| 461 | + {{ this._parent.get_name() }}.once('click', function() { |
| 462 | + {{ this.get_name() }}.setContent($(`{{ element.render(**kwargs).replace('\\n',' ') }}`)[0]); |
| 463 | + }); |
| 464 | + {% else %} |
| 465 | + var {{ name }} = $(`{{ element.render(**kwargs).replace('\\n',' ') }}`)[0]; |
| 466 | + {{ this.get_name() }}.setContent({{ name }}); |
| 467 | + {% endif %} |
| 468 | + {% endfor %} |
| 469 | +
|
| 470 | + {{ this._parent.get_name() }}.bindPopup({{ this.get_name() }}) |
| 471 | + {% if this.show %}.openPopup(){% endif %}; |
| 472 | +
|
| 473 | + {% for name, element in this.script._children.items() %} |
| 474 | + {{element.render()}} |
| 475 | + {% endfor %} |
478 | 476 | """ |
479 | 477 | ) # noqa |
480 | 478 |
|
@@ -515,6 +513,21 @@ def __init__( |
515 | 513 | **kwargs, |
516 | 514 | ) |
517 | 515 |
|
| 516 | + def render(self, **kwargs): |
| 517 | + """Renders the HTML representation of the element.""" |
| 518 | + for name, child in self._children.items(): |
| 519 | + child.render(**kwargs) |
| 520 | + |
| 521 | + figure = self.get_root() |
| 522 | + assert isinstance( |
| 523 | + figure, Figure |
| 524 | + ), "You cannot render this Element if it is not in a Figure." |
| 525 | + |
| 526 | + figure.script.add_child( |
| 527 | + Element(self._template.render(this=self, kwargs=kwargs)), |
| 528 | + name=self.get_name(), |
| 529 | + ) |
| 530 | + |
518 | 531 |
|
519 | 532 | class Tooltip(MacroElement): |
520 | 533 | """ |
|
0 commit comments