Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPyleaflet does not consistently work with Panel #3778

Closed
2320sharon opened this issue Aug 24, 2022 · 6 comments · Fixed by #3782
Closed

IPyleaflet does not consistently work with Panel #3778

2320sharon opened this issue Aug 24, 2022 · 6 comments · Fixed by #3782
Labels
type: bug Something isn't correct or isn't working

Comments

@2320sharon
Copy link

IPyleaflet does not consistently work with Panel

Description of expected behavior and the observed behavior

I've been attempting to create a Panel application with an ipyleaflet map in it. I have had issues getting the map to display using the command panel serve sample_app.py --show because it keeps displaying the same error message AttributeError: 'Map' object has no attribute 'widgets' even though I have installed all the relevant packages.

What's strange is that when I install the same packages in a different order is I get no errors . I have included three package_list.txt that show the two environments I created that showcase this strange behavior. In the working version of the environment I describe below I installed ipywidgets_bokeh first and then install panel and ipyleafelt whereas with the broken version of the environment I installed panel ipyleaflet first followed by ipywidgets_bokeh and it displayed the error message AttributeError: 'Map' object has no attribute 'widgets'.

Any idea why the order the packages being installed would impact whether this error message appears or not. I see each environment contains different versions of each package which may mean one package isn't compatible.
Thank you for any explanations for why this error might have occurred.

Complete, minimal, self-contained example code that reproduces the issue

Working Version of Environment

I have included the versions of all the installed packages below in test_panel_package_list.txt, which ran successfully.

conda create -n test_panel
conda activate test_panel
conda install -c bokeh ipywidgets_bokeh -y
conda install -c conda-forge panel ipyleaflet -y
conda install -c conda-forge jupyterlab -y
panel serve sample_app.py --show

Broken Version of Environment

I have included the versions of all the installed packages below in test_panel2_package_list.txt, which displayed the error message below.

conda create -n test_panel2
conda activate test_panel2
conda install -c conda-forge panel ipyleaflet -y
conda install -c bokeh ipywidgets_bokeh -y
conda install -c conda-forge jupyterlab -y
panel serve sample_app.py --show

Sample Code

from ipyleaflet import Map, Marker

import panel as pn

pn.extension("ipywidgets", sizing_mode="stretch_width")

from ipyleaflet import Map, Marker

ACCENT_BASE_COLOR = "#DAA520"


def get_marker_and_map():
    center = (52.204793, 360.121558)

    lmap = Map(center=center, zoom=15, height=500)

    marker = Marker(location=center, draggable=True)
    lmap.add_layer(marker)
    lmap.layout.height="100%"
    lmap.layout.width="100%"
    return marker, lmap

marker, lmap = get_marker_and_map()

json_widget = pn.pane.JSON({}, height=75)

def on_location_changed(event):
    new = event["new"]
    json_widget.object = {"x": new[0], "y": new[1]}
marker.observe(on_location_changed, 'location')

component = pn.Column(
    pn.panel(lmap, sizing_mode="stretch_both", min_height=500),
    json_widget
)

template = pn.template.FastListTemplate(
    site="Awesome Panel",
    title="IPyLeaflet",
    logo="https://panel.holoviz.org/_static/logo_stacked.png",
    header_background=ACCENT_BASE_COLOR,
    accent_base_color=ACCENT_BASE_COLOR,
    main=[component],
).servable()

Stack traceback and/or browser JavaScript console output

(test_panel2) C:\1_USGS_repos\2_doodleverse\4_coastseg_panel\test2\panel-master\panel-master>panel serve sample_app.py --show
2022-08-24 09:38:10,477 Starting Bokeh server version 2.4.3 (running on Tornado 6.2)
2022-08-24 09:38:10,477 User authentication hooks NOT provided (default user enabled)
2022-08-24 09:38:10,482 Bokeh app running at: http://localhost:5006/sample_app
2022-08-24 09:38:10,482 Starting Bokeh server with process id: 22872
2022-08-24 09:38:11,483 Error running application handler <bokeh.application.handlers.script.ScriptHandler object at 0x000001B0DD567670>: 'Map' object has no attribute 'widgets'
File 'ipywidget.py', line 49, in _get_ipywidget:
for w in obj.widgets.values(): Traceback (most recent call last):
  File "C:\Users\sfitzpatrick\Anaconda3\envs\test_panel2\lib\site-packages\bokeh\application\handlers\code_runner.py", line 231, in run
    exec(self._code, module.__dict__)
  File "C:\1_USGS_repos\2_doodleverse\4_coastseg_panel\test2\panel-master\panel-master\sample_app.py", line 44, in <module>
    ).servable()
  File "C:\Users\sfitzpatrick\Anaconda3\envs\test_panel2\lib\site-packages\panel\viewable.py", line 374, in servable
    self.server_doc(title=title, location=location) # type: ignore
  File "C:\Users\sfitzpatrick\Anaconda3\envs\test_panel2\lib\site-packages\panel\template\base.py", line 332, in server_doc
    return self._init_doc(doc, title=title, location=location)
  File "C:\Users\sfitzpatrick\Anaconda3\envs\test_panel2\lib\site-packages\panel\template\base.py", line 553, in _init_doc
    doc = super()._init_doc(doc, comm, title, notebook, location)
  File "C:\Users\sfitzpatrick\Anaconda3\envs\test_panel2\lib\site-packages\panel\template\base.py", line 188, in _init_doc
    model = obj.get_root(doc, comm, preprocess=False)
  File "C:\Users\sfitzpatrick\Anaconda3\envs\test_panel2\lib\site-packages\panel\viewable.py", line 558, in get_root
    root = self._get_model(doc, comm=comm)
  File "C:\Users\sfitzpatrick\Anaconda3\envs\test_panel2\lib\site-packages\panel\layout\base.py", line 146, in _get_model
    objects = self._get_objects(model, [], doc, root, comm)
  File "C:\Users\sfitzpatrick\Anaconda3\envs\test_panel2\lib\site-packages\panel\layout\base.py", line 131, in _get_objects
    child = pane._get_model(doc, root, model, comm)
  File "C:\Users\sfitzpatrick\Anaconda3\envs\test_panel2\lib\site-packages\panel\pane\ipywidget.py", line 60, in _get_model
    model = self._get_ipywidget(self.object, doc, root, comm, **kwargs)
  File "C:\Users\sfitzpatrick\Anaconda3\envs\test_panel2\lib\site-packages\panel\pane\ipywidget.py", line 49, in _get_ipywidget
    for w in obj.widgets.values():
AttributeError: 'Map' object has no attribute 'widgets'

2022-08-24 09:38:11,700 WebSocket connection opened
2022-08-24 09:38:11,702 ServerConnection created
2022-08-24 09:38:53,075 WebSocket connection closed: code=1001, reason=Non

ALL software version info

OS: Windows 10
Anaconda Version: conda 4.10.3

Package List Files for Both Environments

  1. Working Version
    test_panel_package_list.txt
  2. Broken Version
    test_panel2_package_list.txt
@hoxbro
Copy link
Member

hoxbro commented Aug 24, 2022

I see the same error as you. If I downgrade ipywidgets with conda install "ipywidgets<8", I go from a broken environment to a working one.

@2320sharon
Copy link
Author

Ah, so the issue might be with ipywidgets. Thanks for the tip about installing an older version of ipywidgets!

@hoxbro
Copy link
Member

hoxbro commented Aug 24, 2022

It seems like the new release of ipywidgets (released a week ago) moved widgets to _active_widgets, which is why the AttributeError occurs.

If that is the only change needed, the issue can be fixed by panel with an if statement.

@2320sharon
Copy link
Author

2320sharon commented Aug 24, 2022

I hope that's not too difficult of a fix. Do you mind showing what you think this if statement might look like?
Thanks for the tip about the ipywidget update to 8.0 apparently it changed quite a lot.

@hoxbro
Copy link
Member

hoxbro commented Aug 24, 2022

I would check the ipywidgets version with packaging.version and based on the version set the correct attribute here:

for w in obj.widgets.values():

If you are up for it, you can make the PR.

@philippjfr philippjfr added the type: bug Something isn't correct or isn't working label Aug 24, 2022
@philippjfr philippjfr added this to the Version 0.14.0 milestone Aug 24, 2022
@2320sharon
Copy link
Author

Thank you for fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't correct or isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants