Pyvista cannot visualize image in jupyter lab

Hello.
I used docker to run with dolfinx and now implementing code in jupyter lab environment.
However, I found error in displaying result visualization. My code is as follows (Linerar elasiticity demo code)

import numpy as np
import ufl

from mpi4py import MPI
from petsc4py.PETSc import ScalarType

from dolfinx import mesh, fem, plot, io

import ipyvtklink
import pyvista
pyvista.start_xvfb()

For visualization part,

# Create plotter and pyvista grid
p = pyvista.Plotter(notebook=True)
topology, cell_types, geometry = plot.create_vtk_mesh(V)
grid = pyvista.UnstructuredGrid(topology, cell_types, geometry)

# Attach vector values to grid and warp grid by vector
grid["u"] = uh.x.array.reshape((geometry.shape[0], 3))
actor_0 = p.add_mesh(grid, style="wireframe", color="k")
warped = grid.warp_by_vector("u", factor=1.5)
actor_1 = p.add_mesh(warped, show_edges=True)
p.show_axes()
if not pyvista.OFF_SCREEN:
    p.show()
else:
    figure_as_array = p.screenshot("deflection.png")

However, the result is

Actually, I could get visualization, but I could not interact with the result. So, I installed jupyter backend ‘trame’ for pyvista, and the result is like this.

Could you help me to solve this issue?

Thanks for reading.

What version of dolfinx are you running with your docker image? I cannot reproduce this with

docker run -ti --network=host -v $(pwd):/root/shared -w /root/shared --rm  ghcr.io/fenics/dolfinx/lab:nightly

import numpy as np
import ufl

from mpi4py import MPI
from petsc4py.PETSc import ScalarType

from dolfinx import mesh, fem, plot, io

import pyvista
pyvista.start_xvfb()
mesh = mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
grid  = pyvista.UnstructuredGrid(*plot.create_vtk_mesh(mesh))
pl = pyvista.Plotter()
pl.add_mesh(grid)
pl.show()

with v0.6.0-r1, you just have to add

python3 -m pip install --no-cache-dir pyvista[trame] -U

to the environment and restart the kernel

Thanks for reply.

Yes, I can successfully visualize the result following yours.

For me, I run in such an environment :

sudo docker container start -i dolfin

where I initiated with

sudo docker run -ti  -p 8888:8888
-v $(pwd):/root/shared -w /root/shared --name=dolfin dolfinx/lab:v0.6.0-r1

I followed your

by doing so

(base) yidongzhao@GeoSMART-GPU1:~$ python3 -m pip install --no-cache-dir pyvista[trame] -U

however, still same error happens.
Did I miss something?

Always thanks.

This is not inside the docker container, but on your local system. You need to install it inside the container, either by opening a terminal inside jupyter-lab, or by adding a code cell in a notebook that goes like

! python3 -m pip install --no-cache-dir pyvista[trame] -U

Thanks for the comment.
I ran the code in the cell, but still, the message ‘localhost refused to connect’ occurs. :sweat_smile:

I tried uninstallation of the trame library, then the result could be visualized with ‘static’, which I could not control with interactive pannel.

Would there be any other points that occured this error?

Thanks a lot.

I cannot reproduce this locally on my system.
These are the exact commands I executed:

  1. docker run -ti --network=host -v $(pwd):/root/shared -w /root/shared --rm ghcr.io/fenics/dolfinx/lab:v0.6.0-r1


3.

You are not posting your exact error message, and it is unclear if this happens when you try to install pyvista with trame, or if it happens when you try to plot

Thanks for your help.
I didn’t fully show you my error message. Sorry for that.

The error message(image?) was like this.

# Create plotter and pyvista grid
p = pyvista.Plotter(notebook=True)
topology, cell_types, geometry = plot.create_vtk_mesh(V)
grid = pyvista.UnstructuredGrid(topology, cell_types, geometry)

# Attach vector values to grid and warp grid by vector
grid["u"] = uh.x.array.reshape((geometry.shape[0], 3))
actor_0 = p.add_mesh(grid, style="wireframe", color="k")
warped = grid.warp_by_vector("u", factor=1.5)
actor_1 = p.add_mesh(warped, show_edges=True)
p.show_axes()
if not pyvista.OFF_SCREEN:
    p.show()
else:
    figure_as_array = p.screenshot("deflection.png")

However, thanks to your help, I now can use interactive image in my jupyter lab!

I used this code at first, a little bit revised (by adding name) from your suggestion :

(base) yidongzhao@GeoSMART-GPU1:~$ sudo docker run -ti --network=host -v $(pwd):/root/shared -w /root/shared --name=dolfinx dolfinx/lab:v0.6.0-r1

(which can be reused with)

(base) yidongzhao@GeoSMART-GPU1:~$ sudo docker container start -i dolfinx

However, when I tried to plot with another pyvista plotter, or re-run the cell, for example :

# Create plotter and pyvista grid
p = pyvista.Plotter(notebook=True)
topology, cell_types, geometry = plot.create_vtk_mesh(V)
grid = pyvista.UnstructuredGrid(topology, cell_types, geometry)

# Attach vector values to grid and warp grid by vector
grid["u"] = uh.x.array.reshape((geometry.shape[0], 3))
actor_0 = p.add_mesh(grid, style="wireframe", color="k")
warped = grid.warp_by_vector("u", factor=1.5)
actor_1 = p.add_mesh(warped, show_edges=True)
p.show_axes()
if not pyvista.OFF_SCREEN:
    p.show()
else:
    figure_as_array = p.screenshot("deflection.png")

the kernel is dead, and I had to restart closing the whole terminal.
Is this some issue from another aspect?

Always thanks for help.