Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

tkoyama010
Copy link
Member

@tkoyama010 tkoyama010 commented Jul 7, 2025

Summary

Inspired in SciPy2025 tutorial.

This PR adds automatic display support for PyVista Plotter objects in Jupyter notebooks without requiring the show() method call. The feature is implemented using the _repr_html_ method that Jupyter automatically calls for rich display of objects.

Changes

  • Added _repr_html_ method to the Plotter class
  • Automatically detects Jupyter environment and uses configured backend
  • Supports all existing backends (static, trame, html, etc.)
  • Gracefully handles exceptions and falls back to None
  • Respects jupyter_backend='none' setting to disable auto-display
  • Maintains full backward compatibility with existing show() method

Usage

# Before (still works):
plotter = pv.Plotter()
plotter.add_mesh(pv.Sphere())
plotter.show()

# After (new automatic display):
plotter = pv.Plotter()
plotter.add_mesh(pv.Sphere())
plotter  # Automatically displays in Jupyter!

Testing

  • Added comprehensive test suite in tests/plotting/jupyter/test_auto_display.py
  • Created Jupyter notebook for manual testing
  • Tests cover various scenarios including backend switching and error handling
  • All tests pass successfully

Benefits

  • More intuitive workflow for Jupyter users
  • Consistent with other plotting libraries like matplotlib
  • No breaking changes to existing code
  • Optional - can be disabled with jupyter_backend='none'

This feature allows PyVista Plotter objects to be automatically displayed
in Jupyter notebooks without calling show(), by implementing the _repr_html_
method that Jupyter uses for rich display.

Key changes:
- Added _repr_html_ method to Plotter class in pyvista/plotting/plotter.py
- The method automatically detects Jupyter environment and uses configured backend
- Supports all existing backends (static, trame, html, etc.)
- Gracefully handles exceptions and falls back to None
- Respects jupyter_backend='none' setting to disable auto-display
- Maintains backward compatibility with existing show() method

Testing:
- Added comprehensive test suite in tests/plotting/jupyter/test_auto_display.py
- Created Jupyter notebook for manual testing
- Tests cover various scenarios including backend switching and error handling
@tkoyama010 tkoyama010 marked this pull request as draft July 7, 2025 21:46
@pyvista-bot pyvista-bot added the enhancement Changes that enhance the library label Jul 7, 2025
@pyvista-bot
Copy link
Contributor

@pyvista-bot pyvista-bot temporarily deployed to pull request July 7, 2025 23:58 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Changes that enhance the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants