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

Interactive PyVista inside a Jupyter Notebook #12

Closed
GuillaumeFavelier opened this issue Jun 7, 2019 · 8 comments
Closed

Interactive PyVista inside a Jupyter Notebook #12

GuillaumeFavelier opened this issue Jun 7, 2019 · 8 comments
Assignees
Labels
IPython/Jupyter Questions around using PyVista in IPython/Jupyter plotting General plotting/rendering topics

Comments

@GuillaumeFavelier
Copy link

I'm currently experimenting with PyVista inside a jupyter notebook and I noticed that the figures required a specific pipeline to be displayed automatically.

Do you know a way to see the result of the first scenario plot_scene_1?

image

@GuillaumeFavelier
Copy link
Author

GuillaumeFavelier commented Jun 7, 2019

Actually if I modify plot_scene_1() like the following, it is displayed automatically:

def plot_scene_1():
    S = pv.Sphere()
    plotter = pv.Plotter()
    plotter.add_mesh(S)
    return plotter.show()
plot_scene_1()

So I work on integrating this idea in mne-python. Please don't hesitate to tell me if you know any way to do it better.

@banesullivan
Copy link
Member

banesullivan commented Jun 8, 2019

@GuillaumeFavelier: the plotter.show() call is actually returning a panel.pane.vtk.VTK object that can be displayed in the Jupyter cell output. Calling plotter.show() and not having its result be the cell's output will result in the panel-based interactive rendering not be shown.

Screen Shot 2019-06-08 at 5 53 29 PM

I expect this behavior... however this might be considered a bug as a user might expect that any call to .show() will show the renderer. Also, this is inconsistent with a the static screenshot display in Jupyter notebooks (which returns None and displays the screenshot without it being the cells output):

from pyvista import examples
import pyvista as pv

mesh = examples.download_st_helens().warp_by_scalar()

plotter = pv.Plotter()
plotter.add_mesh(mesh)
output = plotter.show(use_panel=False)

Perhaps we need to collaborate with the panel developers to see if there is a way to display a panel object without it being a cell's output?

@banesullivan banesullivan self-assigned this Jun 11, 2019
@banesullivan banesullivan added plotting General plotting/rendering topics IPython/Jupyter Questions around using PyVista in IPython/Jupyter labels Jun 13, 2019
@GuillaumeFavelier
Copy link
Author

Many thanks for the detailed explanations @banesullivan, now I understand better how it works and I think I can manage with this solution for now. Although I had to rely on the part of code detecting if the script is executed inside a notebook.

Perhaps we need to collaborate with the panel developers to see if there is a way to display a panel object without it being a cell's output?

That would wonderful of course!

@banesullivan
Copy link
Member

Although I had to rely on the part of code detecting if the script is executed inside a notebook.

That's strange, what exactly do you mean? I thought I had the panel code to only run if the code is being executed from a Jupyter notebook.

@GuillaumeFavelier
Copy link
Author

I thought I had the panel code to only run if the code is being executed from a Jupyter notebook.

And yes, it works as expected. My bad, it was not clear. What I mean is, I had to use a workaround in my specific use case because I expect that any call to .show() will show the renderer (c.f. plot_sphere_1() scenario) even if it's not the cell's output.

@GuillaumeFavelier
Copy link
Author

Actually after some more research I found a way to force panel to show the pane thanks to the show_server() function:

image

So I used the following function:

def notebook_show(disp, notebook_url="localhost:8888", port=0):
    from panel.viewable import show_server
    show_server(disp, notebook_url, port)

Do you think there would be a way to integrate such a feature in PyVista @banesullivan ? Eventually, I can open a discussion with the Panel developers to find a better/cleaner solution just in case.

@banesullivan
Copy link
Member

Do you think there would be a way to integrate such a feature in PyVista

Probably - we should add something like this in pyvista/pyvista#300. My only hesitation is that the url and port have to be known, and they might not always be notebook_url="localhost:8888", port=0 so maybe we should ping the panel dev team.

Frankly, I think all notebook related dev should be paused until a few things move forward with itk-jupyter-widgets. Reference InsightSoftwareConsortium/itkwidgets#154 and waiting on:

Once those two issues are addressed, we will favor using itk-jupyter-widgets for notebook rendering directly instead of all the hacky solutions where a VTK render window is exported/serialized for VTKjs, X3D, or K3D.

@GuillaumeFavelier
Copy link
Author

After more investigation I found that this version of plot_scene_1() is way simpler and doesn't require notebook_show() or any unnecessary tweaks anymore:

def plot_scene_1():
    from IPython.display import display
    S = pv.Sphere()
    plotter = pv.Plotter()
    plotter.add_mesh(S, color='white')
    disp = plotter.show(use_panel=True, auto_close=False)
    display(disp)
plot_scene_1()

@pyvista pyvista locked and limited conversation to collaborators May 15, 2022
@tkoyama010 tkoyama010 converted this issue into a discussion May 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
IPython/Jupyter Questions around using PyVista in IPython/Jupyter plotting General plotting/rendering topics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants