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

Display PNG preview for widget #2682

Open
pkerpedjiev opened this issue Jan 7, 2020 · 7 comments
Open

Display PNG preview for widget #2682

pkerpedjiev opened this issue Jan 7, 2020 · 7 comments

Comments

@pkerpedjiev
Copy link

Hey! I have an IPython widget that somewhat resembles an interactive map. Whenever the user pans or zooms, the view changes. When the notebook is saved, stored and viewed on GitHub, I would like to see a preview of what was visible when the notebook was saved.

AFAICT, to accomplish this I need to set outputs["data"]["image/png"] in the notebook's saved JSON representation.

To do that I've tried the following:

  1. On every widget change, it generates a screenshot of itself and stores that as a base64 string in a traitlet in the model.
  2. This trait is @observe-ed and every time it changes I call update_display:
                self.display_data["image/png"] = change["new"]
                update_display(
                    self.display_data,
                    display_id=self.display_handle.display_id,
                    raw=True,
                )

This seems to work except that it reloads the widget as well (i.e. it disappears and reappears again). My question is, is there a way to dynamically update the cell's data without reloading the widget?

@jasongrout
Copy link
Member

I don't think so, since you are actually asking the system to rerender the displayed output with that update_display command.

@pkerpedjiev
Copy link
Author

Hmm, is there no way to go around the update_display command and just change the cell's display_data directly?

Perhaps it could be snuck in on the client?

@jasongrout
Copy link
Member

Hmm, is there no way to go around the update_display command and just change the cell's display_data directly?

I think the answer to your question is no (as in I think I understand what you are trying to do in your workaround), but having a more comprehensive self-contained example to see exactly what you are doing would be helpful.

In particular, it seems that you are updating the actual display that contains a widget, so that the mimebundle has both the widget and a png?

Perhaps it could be snuck in on the client?

That would probably be an issue for the particular client (notebook, lab, etc.), then.

@jasongrout
Copy link
Member

That said, you can see previous discussion about this topic at #2280

@jasongrout
Copy link
Member

(ah, from higlass/higlass-python#49 it looks like you knew about #2280)

@pkerpedjiev
Copy link
Author

In particular, it seems that you are updating the actual display that contains a widget, so that the mimebundle has both the widget and a png?

Yes, that's exactly right. If I do this manually by going in changing the saved .ipynb file with a base64 png, I can load it just fine.

As you saw, there were a few threads I was looking at and what I wanted to accomplish seemed so tantalizingly close that I thought it best to just ask.

Anyway, thanks for your help. I'll poke around those threads some more. #314 looks promising.

@jasongrout
Copy link
Member

Great, thanks for looking into this. A lot of people would love this. It seems like a tricky problem to solve in general, but perhaps an abstract method could be extended by each widget to provide a snapshot of itself, and that could be utilized in a general solution to save a snapshot of the top-level widget.

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

No branches or pull requests

2 participants