fix: auto-detect Jupyter kernel to avoid blank trame renders#548
fix: auto-detect Jupyter kernel to avoid blank trame renders#548dipinknair wants to merge 5 commits into
Conversation
| elif jupyter_backend is None: | ||
| # Auto-detect a Jupyter kernel and fall back to a safe static backend. | ||
| # Without this, PyVista defaults to "trame" (after installing | ||
| # pyvista[jupyter]), which spawns a local server and renders blank | ||
| # in VS Code / Spyder embedded notebooks. | ||
| try: | ||
| from IPython import get_ipython | ||
|
|
||
| _ip = get_ipython() | ||
| if _ip is not None and "IPKernelApp" in _ip.config: | ||
| jupyter_backend = "static" | ||
| except Exception: | ||
| pass |
There was a problem hiding this comment.
With this change we would force static renderings on Jupyter NB. And PyVista (via trame) supports interactive ones. Let's try to figure out why it's not rendering on the user's machine.
We could have some kind of fallback though in the sense that if the render goes blank (i.e. fails) we attempt to render it statically. But not by default I'd say. Thanks for investigating @dipinknair
@AlejandroFernandezLuces can you sync with @dipinknair to investigate the issue? He'll share details on the user's failure
There was a problem hiding this comment.
Sure, I'll check on my end
|
Just for the record, this is an issue we have had before with PyVista. Since the change to Trame, visualization in Jupyter and in our docs by extension has been difficult. Using PyVista and following the guide they have for using Trame and Jupyter, I'm not able to render anything on screen, I just get the loading screen:
The alternative that we can do is to fall back to HTML backend when in a Jupyter environment. It's does not have the full Trame functionality but at least it has a basic interactive 3D scene. |

Bug:
plot()renders blank after installingpyvista[jupyter]Root cause
Installing
pyvista[jupyter]sets PyVista's global Jupyter backend to"trame".PyVistaInterface.show()passedjupyter_backend=Nonestraight through topv.Plotter.show(), so it silently inherited"trame", which tries to spin upa local trame server. VS Code notebooks cannot render the resulting widget —
producing a blank output and a JS
SyntaxError: Unexpected token ':'.Fix
In
PyVistaInterface.show()(pyvista_interface.py),before calling
pv.Plotter.show(), detect whether the code is running inside aJupyter kernel and default
jupyter_backendto"static":"static"renders an inline PNG — no server required, works in VS Code,JupyterLab, and Spyder.
Behaviour after fix
USE_HTML_BACKEND)"html"(unchanged)"static"(new default)jupyter_backend="trame""trame"(explicit override respected)