{"action":"created","issue":{"url":"https://api.github.com/repos/jupyter-widgets/ipywidgets/issues/2171","repository_url":"https://api.github.com/repos/jupyter-widgets/ipywidgets","labels_url":"https://api.github.com/repos/jupyter-widgets/ipywidgets/issues/2171/labels{/name}","comments_url":"https://api.github.com/repos/jupyter-widgets/ipywidgets/issues/2171/comments","events_url":"https://api.github.com/repos/jupyter-widgets/ipywidgets/issues/2171/events","html_url":"https://github.com/jupyter-widgets/ipywidgets/issues/2171","id":349321667,"node_id":"MDU6SXNzdWUzNDkzMjE2Njc=","number":2171,"title":"Remove method for a WidgetView not called when clearing outputs","user":{"login":"jtpio","id":591645,"node_id":"MDQ6VXNlcjU5MTY0NQ==","avatar_url":"https://avatars2.githubusercontent.com/u/591645?v=4","gravatar_id":"","url":"https://api.github.com/users/jtpio","html_url":"https://github.com/jtpio","followers_url":"https://api.github.com/users/jtpio/followers","following_url":"https://api.github.com/users/jtpio/following{/other_user}","gists_url":"https://api.github.com/users/jtpio/gists{/gist_id}","starred_url":"https://api.github.com/users/jtpio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jtpio/subscriptions","organizations_url":"https://api.github.com/users/jtpio/orgs","repos_url":"https://api.github.com/users/jtpio/repos","events_url":"https://api.github.com/users/jtpio/events{/privacy}","received_events_url":"https://api.github.com/users/jtpio/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-08-09T23:06:23Z","updated_at":"2018-08-10T00:14:44Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"When clearing a view using \"Clear Outputs\" from the context menu (or re-executing the notebook cell), the user defined `remove` method from a `DOMWidgetView` is never called.\r\n\r\nThis seems to happen only in JupyterLab (and also when using an `Output` widget). As far as I remember it used to work fine when creating widgets on the fly in the classic notebook (from [Building a Custom Widget](https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Custom.html)).\r\n\r\nA simple way to reproduce this is to start from the [Jupyter Widget Hello](https://github.com/vidartf/jupyter-widget-hello) and add the following to the view:\r\n\r\n```typescript\r\nexport\r\nclass ExampleView extends DOMWidgetView {\r\n\r\n  interval : number\r\n\r\n  initialize() {\r\n    console.log('initialize');\r\n  }\r\n\r\n  render() {\r\n    super.render();\r\n    console.log('render');\r\n    this.interval = window.setInterval(() => {\r\n      console.log(`Interval: ${this.model.get('value')}`);\r\n    }, 1000);\r\n    console.log(`create interval ${this.interval}`);\r\n    this.el.textContent = this.model.get('value');\r\n  }\r\n\r\n  remove() {\r\n    console.log('remove')\r\n    window.clearInterval(this.interval);\r\n    console.log(`cleared interval ${this.interval}`);\r\n    super.remove();\r\n  }\r\n}\r\n```\r\n\r\nSince the `remove` method is not called, the interval keeps running in the background and new ones are added when re-executing the cells. This can be problematic when the view allocates more memory (sprites, images and so on) and needs to do some cleanup before being removed.\r\n\r\nAfter digging a little bit in the code, the further I could get down the chain is in the `dispose` method of the `WidgtetRenderer`, which doesn't seem to hold information related to the current `viewId`.\r\n\r\nhttps://github.com/jupyter-widgets/ipywidgets/blob/537b171d5faec7883a38098dcf5c7a952499cb23/packages/jupyterlab-manager/src/renderer.ts#L77-L82\r\n\r\nThe view is however properly disposed with the `remove` method being triggered when closing the widget with `widget.close()`:\r\n\r\nhttps://github.com/jupyter-widgets/ipywidgets/blob/537b171d5faec7883a38098dcf5c7a952499cb23/packages/base/src/manager-base.ts#L180\r\n\r\nIs there a way to know which views are attached to an output area, so they could be removed whenever the output area is cleared?"},"comment":{"url":"https://api.github.com/repos/jupyter-widgets/ipywidgets/issues/comments/411936857","html_url":"https://github.com/jupyter-widgets/ipywidgets/issues/2171#issuecomment-411936857","issue_url":"https://api.github.com/repos/jupyter-widgets/ipywidgets/issues/2171","id":411936857,"node_id":"MDEyOklzc3VlQ29tbWVudDQxMTkzNjg1Nw==","user":{"login":"jasongrout","id":192614,"node_id":"MDQ6VXNlcjE5MjYxNA==","avatar_url":"https://avatars0.githubusercontent.com/u/192614?v=4","gravatar_id":"","url":"https://api.github.com/users/jasongrout","html_url":"https://github.com/jasongrout","followers_url":"https://api.github.com/users/jasongrout/followers","following_url":"https://api.github.com/users/jasongrout/following{/other_user}","gists_url":"https://api.github.com/users/jasongrout/gists{/gist_id}","starred_url":"https://api.github.com/users/jasongrout/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasongrout/subscriptions","organizations_url":"https://api.github.com/users/jasongrout/orgs","repos_url":"https://api.github.com/users/jasongrout/repos","events_url":"https://api.github.com/users/jasongrout/events{/privacy}","received_events_url":"https://api.github.com/users/jasongrout/received_events","type":"User","site_admin":false},"created_at":"2018-08-10T00:14:44Z","updated_at":"2018-08-10T00:14:44Z","author_association":"MEMBER","body":"What should be happening here is that the phosphor dispose method is called when the phosphor widget is removed from the page in JupyterLab, and that calls the view remove method: https://github.com/jupyter-widgets/ipywidgets/blob/537b171d5faec7883a38098dcf5c7a952499cb23/packages/base/src/widget.ts#L718\r\n\r\nAre you saying this only happens in JLab when you are displaying a widget inside of an output widget?"}}
{"actor":{"display_login":"jasongrout"}}