Skip to content

[Bug]: ipyleaflet cannot show popup sequencely and only display the last popup #194

@xiaogangzhu

Description

@xiaogangzhu

Component

UI (ui.*)

Severity

P0 - Critical (crash/unusable)

Shiny Version

1.2.1

Python Version

3.12.8

Minimal Reproducible Example

from shiny import App, reactive, render, ui, req
from shinywidgets import render_widget, output_widget
import ipyleaflet as ipyl
from ipyleaflet import (
    Map,
)
import xarray as xr
import pandas as pd
import numpy as np

import plotly.express as px
from ipywidgets import HTML
from ipyleaflet import Popup
import plotly.graph_objs as go
import asyncio
import time

app_ui = ui.page_fluid(output_widget("map"))


# Define Server
def server(input, output, session):
    click_coords = reactive.value((None, None))

    # Define interactive Plotly figure function
    def create_interactive_plot():
        # time.sleep(3)
        x = np.linspace(0, 10, 100)
        y = np.sin(x)
        fig = go.FigureWidget(data=[go.Scatter(x=x, y=y, mode="lines+markers")])
        fig.update_layout(
            title="Interactive Plotly Plot in Popup",
            width=350,
            height=250,
            margin=dict(l=10, r=10, t=30, b=10),
        )
        return fig

    @reactive.effect
    def _():
        print("start mission1")
        lat, lng = click_coords.get()
        if lat != None and lng != None:
            print(lat, lng)
            # Create a loading spinner
            loading_html = HTML()
            loading_html.value = """
            <div style="display: flex; justify-content: center; align-items: center; height: 100px;">
                <div class="spinner" style="border: 4px solid #f3f3f3; border-top: 4px solid #3498db; border-radius: 50%; width: 50px; height: 50px; animation: spin 2s linear infinite;"></div>
            </div>
            <style>
                @keyframes spin {
                    0% { transform: rotate(0deg); }
                    100% { transform: rotate(360deg); }
                }
            </style>
            """

            # Create a popup with the loading spinner
            popup = Popup(
                location=(lat, lng),
                child=loading_html,
                close_button=True,
                auto_close=True,
                close_on_escape_key=True,
            )
            map.widget.add(popup)

            # await asyncio.sleep(2)
            time.sleep(2)

            plot_widget = create_interactive_plot()
            popup.child = plot_widget

    @render_widget
    def map():
        center = (18.5, 116.4)
        zoom = 4.5

        # Create map
        m = Map(
            center=center,
            zoom=zoom,
            # interpolation="nearest",
            # basemap=basemaps.CartoDB.DarkMatter,
            scroll_wheel_zoom=True,
        )

        def handle_map_interaction(**kwargs):
            # print(kwargs)
            if kwargs.get("type") == "click":
                lat, lon = kwargs.get("coordinates")
                click_coords.set((lat, lon))

        # Observe click events
        m.on_interaction(handle_map_interaction)
        print("render map")
        return m


# Create Shiny app
app = App(app_ui, server, debug=False)

Behavior

When user click on the map I want to show a popup with plotly figure. But sometimes it may take time to generate the figure so I add a loading spin first to the popup, and when finish generating figure I replace the popup with the true figure.

The problem is the @reactive.effect function only display the popup when all the whole functions finish and does not display the loading spin. Is there any way to add a loading spin in this scenerio?

Error Messages (if any)

Environment

Ubuntu 20
Chrome up-to-date
_libgcc_mutex             0.1                        main    defaults
_openmp_mutex             5.1                       1_gnu    defaults
altair                    5.5.0                    pypi_0    pypi
anyio                     4.7.0                    pypi_0    pypi
anywidget                 0.9.13                   pypi_0    pypi
appdirs                   1.4.4                    pypi_0    pypi
argon2-cffi               23.1.0                   pypi_0    pypi
argon2-cffi-bindings      21.2.0                   pypi_0    pypi
arrow                     1.3.0                    pypi_0    pypi
asgiref                   3.8.1                    pypi_0    pypi
asttokens                 3.0.0                    pypi_0    pypi
async-lru                 2.0.4                    pypi_0    pypi
attrs                     24.3.0                   pypi_0    pypi
babel                     2.16.0                   pypi_0    pypi
beautifulsoup4            4.12.3                   pypi_0    pypi
black                     24.10.0                  pypi_0    pypi
bleach                    6.2.0                    pypi_0    pypi
blinker                   1.9.0                    pypi_0    pypi
bqplot                    0.12.43                  pypi_0    pypi
branca                    0.8.1                    pypi_0    pypi
bzip2                     1.0.8                h5eee18b_6    defaults
ca-certificates           2024.11.26           h06a4308_0    defaults
cachetools                5.5.0                    pypi_0    pypi
certifi                   2024.12.14               pypi_0    pypi
cffi                      1.17.1                   pypi_0    pypi
cftime                    1.6.4.post1              pypi_0    pypi
charset-normalizer        3.4.0                    pypi_0    pypi
click                     8.1.7                    pypi_0    pypi
colour                    0.1.5                    pypi_0    pypi
comm                      0.2.2                    pypi_0    pypi
contourpy                 1.3.1                    pypi_0    pypi
cycler                    0.12.1                   pypi_0    pypi
debugpy                   1.8.11                   pypi_0    pypi
decorator                 5.1.1                    pypi_0    pypi
defusedxml                0.7.1                    pypi_0    pypi
duckdb                    1.1.3                    pypi_0    pypi
earthengine-api           1.4.3                    pypi_0    pypi
eerepr                    0.0.4                    pypi_0    pypi
executing                 2.1.0                    pypi_0    pypi
expat                     2.6.4                h6a678d5_0    defaults
faicons                   0.2.2                    pypi_0    pypi
fastjsonschema            2.21.1                   pypi_0    pypi
filelock                  3.16.1                   pypi_0    pypi
folium                    0.19.2                   pypi_0    pypi
fonttools                 4.55.3                   pypi_0    pypi
fqdn                      1.5.1                    pypi_0    pypi
future                    1.0.0                    pypi_0    pypi
gdown                     5.2.0                    pypi_0    pypi
geemap                    0.35.1                   pypi_0    pypi
geocoder                  1.38.1                   pypi_0    pypi
geographiclib             2.0                      pypi_0    pypi
geojson                   3.1.0                    pypi_0    pypi
geojsoncontour            0.5.1                    pypi_0    pypi
geopy                     2.4.1                    pypi_0    pypi
gitdb                     4.0.11                   pypi_0    pypi
gitpython                 3.1.43                   pypi_0    pypi
google-api-core           2.24.0                   pypi_0    pypi
google-api-python-client  2.156.0                  pypi_0    pypi
google-auth               2.37.0                   pypi_0    pypi
google-auth-httplib2      0.2.0                    pypi_0    pypi
google-cloud-core         2.4.1                    pypi_0    pypi
google-cloud-storage      2.19.0                   pypi_0    pypi
google-crc32c             1.6.0                    pypi_0    pypi
google-resumable-media    2.7.2                    pypi_0    pypi
googleapis-common-protos  1.66.0                   pypi_0    pypi
h11                       0.14.0                   pypi_0    pypi
htmltools                 0.6.0                    pypi_0    pypi
httpcore                  1.0.7                    pypi_0    pypi
httplib2                  0.22.0                   pypi_0    pypi
httpx                     0.28.1                   pypi_0    pypi
humanize                  4.11.0                   pypi_0    pypi
idna                      3.10                     pypi_0    pypi
ipyevents                 2.0.2                    pypi_0    pypi
ipyfilechooser            0.6.0                    pypi_0    pypi
ipykernel                 6.29.5                   pypi_0    pypi
ipyleaflet                0.19.2                   pypi_0    pypi
ipympl                    0.9.7                    pypi_0    pypi
ipython                   8.30.0                   pypi_0    pypi
ipython-genutils          0.2.0                    pypi_0    pypi
ipytree                   0.2.2                    pypi_0    pypi
ipyvue                    1.11.2                   pypi_0    pypi
ipyvuetify                1.10.0                   pypi_0    pypi
ipywidgets                7.8.5                    pypi_0    pypi
isoduration               20.11.0                  pypi_0    pypi
jedi                      0.19.2                   pypi_0    pypi
jinja2                    3.1.4                    pypi_0    pypi
json5                     0.10.0                   pypi_0    pypi
jsonpointer               3.0.0                    pypi_0    pypi
jsonschema                4.23.0                   pypi_0    pypi
jsonschema-specifications 2024.10.1                pypi_0    pypi
jupyter-client            8.6.3                    pypi_0    pypi
jupyter-core              5.7.2                    pypi_0    pypi
jupyter-events            0.11.0                   pypi_0    pypi
jupyter-leaflet           0.19.2                   pypi_0    pypi
jupyter-lsp               2.2.5                    pypi_0    pypi
jupyter-server            2.14.2                   pypi_0    pypi
jupyter-server-terminals  0.5.3                    pypi_0    pypi
jupyterlab                4.4.2                    pypi_0    pypi
jupyterlab-pygments       0.3.0                    pypi_0    pypi
jupyterlab-server         2.27.3                   pypi_0    pypi
jupyterlab-widgets        1.1.11                   pypi_0    pypi
kiwisolver                1.4.7                    pypi_0    pypi
ld_impl_linux-64          2.40                 h12ee557_0    defaults
leafmap                   0.42.6                   pypi_0    pypi
libffi                    3.4.4                h6a678d5_1    defaults
libgcc-ng                 11.2.0               h1234567_1    defaults
libgomp                   11.2.0               h1234567_1    defaults
libstdcxx-ng              11.2.0               h1234567_1    defaults
libuuid                   1.41.5               h5eee18b_0    defaults
linkify-it-py             2.0.3                    pypi_0    pypi
magic-dash                0.3.2                    pypi_0    pypi
markdown                  3.7                      pypi_0    pypi
markdown-it-py            3.0.0                    pypi_0    pypi
markupsafe                3.0.2                    pypi_0    pypi
matplotlib                3.10.0                   pypi_0    pypi
matplotlib-inline         0.1.7                    pypi_0    pypi
mdit-py-plugins           0.4.2                    pypi_0    pypi
mdurl                     0.1.2                    pypi_0    pypi
mistune                   3.0.2                    pypi_0    pypi
mypy-extensions           1.0.0                    pypi_0    pypi
narwhals                  1.19.0                   pypi_0    pypi
nbclient                  0.10.2                   pypi_0    pypi
nbconvert                 7.16.4                   pypi_0    pypi
nbformat                  5.10.4                   pypi_0    pypi
ncurses                   6.4                  h6a678d5_0    defaults
nest-asyncio              1.6.0                    pypi_0    pypi
netcdf4                   1.7.2                    pypi_0    pypi
notebook                  7.4.2                    pypi_0    pypi
notebook-shim             0.2.4                    pypi_0    pypi
numpy                     2.2.0                    pypi_0    pypi
openssl                   3.0.15               h5eee18b_0    defaults
orjson                    3.10.13                  pypi_0    pypi
overrides                 7.7.0                    pypi_0    pypi
packaging                 24.2                     pypi_0    pypi
palmerpenguins            0.1.4                    pypi_0    pypi
pandas                    2.2.3                    pypi_0    pypi
pandocfilters             1.5.1                    pypi_0    pypi
parso                     0.8.4                    pypi_0    pypi
pathspec                  0.12.1                   pypi_0    pypi
pexpect                   4.9.0                    pypi_0    pypi
pillow                    11.0.0                   pypi_0    pypi
pip                       24.2            py312h06a4308_0    defaults
platformdirs              4.3.6                    pypi_0    pypi
plotly                    5.24.1                   pypi_0    pypi
prometheus-client         0.21.1                   pypi_0    pypi
prompt-toolkit            3.0.48                   pypi_0    pypi
proto-plus                1.25.0                   pypi_0    pypi
protobuf                  5.29.2                   pypi_0    pypi
psutil                    6.1.1                    pypi_0    pypi
psygnal                   0.11.1                   pypi_0    pypi
ptyprocess                0.7.0                    pypi_0    pypi
pure-eval                 0.2.3                    pypi_0    pypi
pyarrow                   18.1.0                   pypi_0    pypi
pyasn1                    0.6.1                    pypi_0    pypi
pyasn1-modules            0.4.1                    pypi_0    pypi
pycparser                 2.22                     pypi_0    pypi
pydeck                    0.9.1                    pypi_0    pypi
pygments                  2.18.0                   pypi_0    pypi
pymdown-extensions        10.12                    pypi_0    pypi
pyparsing                 3.2.0                    pypi_0    pypi
pyperclip                 1.9.0                    pypi_0    pypi
pyshp                     2.3.1                    pypi_0    pypi
pysocks                   1.7.1                    pypi_0    pypi
pystac                    1.11.0                   pypi_0    pypi
pystac-client             0.8.5                    pypi_0    pypi
python                    3.12.8               h5148396_0    defaults
python-box                7.3.0                    pypi_0    pypi
python-dateutil           2.9.0.post0              pypi_0    pypi
python-json-logger        3.2.1                    pypi_0    pypi
python-multipart          0.0.20                   pypi_0    pypi
pytz                      2024.2                   pypi_0    pypi
pyyaml                    6.0.2                    pypi_0    pypi
pyzmq                     26.2.0                   pypi_0    pypi
questionary               2.1.0                    pypi_0    pypi
ratelim                   0.1.6                    pypi_0    pypi
reacton                   1.9.0                    pypi_0    pypi
readline                  8.2                  h5eee18b_0    defaults
referencing               0.35.1                   pypi_0    pypi
requests                  2.32.3                   pypi_0    pypi
rfc3339-validator         0.1.4                    pypi_0    pypi
rfc3986-validator         0.1.1                    pypi_0    pypi
rich                      13.9.4                   pypi_0    pypi
rich-click                1.8.5                    pypi_0    pypi
rpds-py                   0.22.3                   pypi_0    pypi
rsa                       4.9                      pypi_0    pypi
scipy                     1.15.2                   pypi_0    pypi
scooby                    0.10.0                   pypi_0    pypi
send2trash                1.8.3                    pypi_0    pypi
setuptools                75.1.0          py312h06a4308_0    defaults
shiny                     1.2.1                    pypi_0    pypi
shinywidgets              0.3.4                    pypi_0    pypi
six                       1.17.0                   pypi_0    pypi
smmap                     5.0.1                    pypi_0    pypi
sniffio                   1.3.1                    pypi_0    pypi
solara                    1.43.0                   pypi_0    pypi
solara-server             1.43.0                   pypi_0    pypi
solara-ui                 1.43.0                   pypi_0    pypi
soupsieve                 2.6                      pypi_0    pypi
sqlite                    3.45.3               h5eee18b_0    defaults
stack-data                0.6.3                    pypi_0    pypi
starlette                 0.42.0                   pypi_0    pypi
streamlit                 1.41.1                   pypi_0    pypi
streamlit-folium          0.24.0                   pypi_0    pypi
tenacity                  9.0.0                    pypi_0    pypi
terminado                 0.18.1                   pypi_0    pypi
tinycss2                  1.4.0                    pypi_0    pypi
tk                        8.6.14               h39e8969_0    defaults
toml                      0.10.2                   pypi_0    pypi
tornado                   6.4.2                    pypi_0    pypi
tqdm                      4.67.1                   pypi_0    pypi
traitlets                 5.14.3                   pypi_0    pypi
traittypes                0.2.1                    pypi_0    pypi
types-python-dateutil     2.9.0.20241206           pypi_0    pypi
typing-extensions         4.12.2                   pypi_0    pypi
tzdata                    2024.2                   pypi_0    pypi
uc-micro-py               1.0.3                    pypi_0    pypi
unicorn                   2.1.3                    pypi_0    pypi
uri-template              1.3.0                    pypi_0    pypi
uritemplate               4.1.1                    pypi_0    pypi
urllib3                   2.2.3                    pypi_0    pypi
uvicorn                   0.34.0                   pypi_0    pypi
watchdog                  6.0.0                    pypi_0    pypi
watchfiles                1.0.3                    pypi_0    pypi
wcwidth                   0.2.13                   pypi_0    pypi
webcolors                 24.11.1                  pypi_0    pypi
webencodings              0.5.1                    pypi_0    pypi
websocket-client          1.8.0                    pypi_0    pypi
websockets                14.1                     pypi_0    pypi
wheel                     0.44.0          py312h06a4308_0    defaults
whitebox                  2.3.5                    pypi_0    pypi
whiteboxgui               2.3.0                    pypi_0    pypi
widgetsnbextension        3.6.10                   pypi_0    pypi
xarray                    2024.11.0                pypi_0    pypi
xyzservices               2024.9.0                 pypi_0    pypi
xz                        5.4.6                h5eee18b_1    defaults
zlib                      1.2.13               h5eee18b_1    defaults

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions