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

Allow for the model_id property to still be accessed after the widget is closed #179

Merged
merged 2 commits into from
Jan 23, 2025

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Jan 23, 2025

This problem was first discovered via #174

Here is a small reprex of the problem.

from ipyleaflet import Map, Marker
from shiny import reactive
import shiny.express

from shinywidgets import render_widget

map = Map(zoom=2)

@render_widget
def map_out():
    reactive.invalidate_later(1)
    marker = Marker(location=(0, 0))
    map.add(marker)
    return map

After the 1st invalidation, you'll get:

AttributeError: 'NoneType' object has no attribute 'comm_id'

This happens because .add() reads of the .model_id property of every Widget instance (i.e., Marker()) that has ever been added to it. And, in recent versions of shinywidgets, we now .close() widgets when the reactive context they were initialized in get invalidated. When a widget gets closed, normally reading .model_id will result in error since that property reads .comm.comm_id, but .comm is None.

The workaround here is come up with a "mock" comm object that we add after close that allows for the .model_id property to still be read.

@cpsievert cpsievert changed the title Allow for the model_id attribute to still be accessed after the widget is closed Allow for the model_id property to still be accessed after the widget is closed Jan 23, 2025
@cpsievert cpsievert merged commit 8925380 into main Jan 23, 2025
5 checks passed
@cpsievert cpsievert deleted the orphaned-comm branch January 23, 2025 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant