@@ -471,7 +471,7 @@ def _get_embed_state(self, drop_defaults=False):
471471 return state
472472
473473 def get_view_spec (self ):
474- return {"version_major" :2 , "version_minor" :0 , "model_id" : self ._model_id }
474+ return {"version_major" : 2 , "version_minor" : 0 , "model_id" : self .model_id }
475475
476476 #-------------------------------------------------------------------------
477477 # Traits
@@ -495,6 +495,8 @@ def get_view_spec(self):
495495
496496 keys = List (help = "The traits which are synced." )
497497
498+ _model_id : None | str = None
499+
498500 @default ('keys' )
499501 def _default_keys (self ):
500502 return [name for name in self .traits (sync = True )]
@@ -537,7 +539,7 @@ def _default_comm(self):
537539
538540 def open (self ):
539541 """Open a comm to the frontend if one isn't already open."""
540- assert self ._model_id
542+ assert self .model_id
541543
542544
543545 def _create_comm (self , comm_id = None ):
@@ -564,7 +566,8 @@ def _comm_changed(self, change):
564566 _instances .pop (change ['old' ].comm_id , None )
565567 if change ['new' ]:
566568 if isinstance (_instances , dict ):
567- _instances [change ['new' ].comm_id ] = self
569+ _instances [change ["new" ].comm_id ] = self
570+ self ._model_id = change ["new" ].comm_id
568571
569572 # prevent memory leaks by using a weak reference to self.
570573 ref = weakref .ref (self )
@@ -578,14 +581,8 @@ def _handle_msg(msg):
578581
579582 change ['new' ].on_msg (_handle_msg )
580583
581-
582-
583584 @property
584585 def model_id (self ):
585- return self ._model_id
586-
587- @property
588- def _model_id (self ):
589586 """Gets the model id of this widget.
590587
591588 If a Comm doesn't exist yet, a Comm will be created automagically."""
@@ -864,10 +861,10 @@ def _repr_mimebundle_(self, **kwargs):
864861 # http://tools.ietf.org/html/rfc6838
865862 # and the currently registered mimetypes at
866863 # http://www.iana.org/assignments/media-types/media-types.xhtml.
867- data [' application/vnd.jupyter.widget-view+json' ] = {
868- ' version_major' : 2 ,
869- ' version_minor' : 0 ,
870- ' model_id' : self ._model_id
864+ data [" application/vnd.jupyter.widget-view+json" ] = {
865+ " version_major" : 2 ,
866+ " version_minor" : 0 ,
867+ " model_id" : self .model_id ,
871868 }
872869 return data
873870
0 commit comments