pyvista / pyvista

3D plotting and mesh analysis through a streamlined interface for the Visualization Toolkit (VTK)

Home Page:https://docs.pyvista.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Plotter does not render after Pandas.read_excel() is called in a Jupyter notebook

mdmelin opened this issue · comments

Describe the bug, what's wrong, and what you expected.

I was having difficulty getting a plot to show up inside a Jupyter notebook. The pyvista plotter was infinitely stuck on the loading message. I traced this back to using pd.read_excel() before trying to plot something. Commenting this out and restarting the kernel resolved the issue. I tried using pd.read_csv() and pd.read_excel() with an .xls file and it worked fine. It seems that the problem is restricted to the loading of .xlsx files.

The pandas version is 1.5.3

Steps to reproduce the bug.

This code must be run in a Jupyter notebook. It will not result in bugged behavior for a Python file.

import pandas as pd
import pyvista as pv
from pyvista import examples
from pathlib import Path

file = Path.home() / 'Downloads' / 'Financial Sample.xlsx' #any xlsx file
data = pd.read_excel(file) # comment this out and pyvista should plot fine
data = examples.download_saddle_surface()
data.plot(color='tan')

System Information

--------------------------------------------------------------------------------
  Date: Tue Apr 09 22:25:35 2024 Pacific Daylight Time

                  OS : Windows
              CPU(s) : 32
             Machine : AMD64
        Architecture : 64bit
                 RAM : 63.9 GiB
         Environment : Jupyter
          GPU Vendor : NVIDIA Corporation
        GPU Renderer : NVIDIA GeForce RTX 3080/PCIe/SSE2
         GPU Version : 4.5.0 NVIDIA 536.23
    MathText Support : False

  Python 3.10.12 | packaged by Anaconda, Inc. | (main, Jul  5 2023, 19:01:18)
  [MSC v.1916 64 bit (AMD64)]

             pyvista : 0.43.3
                 vtk : 9.3.0
               numpy : 1.23.5
          matplotlib : 3.8.3
              scooby : 0.9.2
               pooch : 1.8.0
              pillow : 10.0.0
             imageio : 2.33.1
           pyvistaqt : 0.11.0
               PyQt5 : 5.15.10
             IPython : 8.15.0
            colorcet : 3.0.1
             cmocean : 3.1.3
          ipywidgets : 8.1.1
               scipy : 1.10.1
                tqdm : 4.66.1
              meshio : 5.3.5
          jupyterlab : 4.0.8
               trame : 3.5.2
        trame_client : 2.15.0
        trame_server : 2.16.0
           trame_vtk : 2.8.0
       trame_vuetify : 2.4.2
jupyter_server_proxy : 4.1.0
        nest_asyncio : 1.5.6
--------------------------------------------------------------------------------

Screenshots

pd_issue

I've not contributed to PyVista before. But if it's helpful, I'm happy to take a crack at it if someone can point me in the right direction.

that's super weird. It looks like read_excel has an engine option, and openpyxl is used by default for xlsx files. Maybe check if just importing openpyxl without reading a file is enough to also break the plotter? Or check if the calamine engine also breaks it?

Looks like it may be related to the openpyxl engine like you suspected. import openpyxl is sufficient to reproduce the issue. Works fine with calamine. I upgraded to pandas 2.2.2 and the issue persists.

I also called the imports from the openpyxl __init__.py individually.

from openpyxl.compat.numbers import NUMPY
from openpyxl.xml import DEFUSEDXML, LXML
from openpyxl.workbook import Workbook
from openpyxl.reader.excel import load_workbook as open
from openpyxl.reader.excel import load_workbook
import openpyxl._constants as constants

Calling any one of these imports was sufficient sufficient to reproduce the issue.

I think any sort of from openpyxl import ... is equivalent to import openpyxl so it makes sense any one of those imports will cause the same problem. What about import lxml?

works fine with import lxml

I'm at a loss. It doesn't seem like openpyxl is really importing anything that could do this. What version of openpyxl do you have? I just installed 3.1.2 and am unable to replicate.

That's really weird.

Are you using the notebook backend @darikg? I created a new environment from scratch, and it does work with just pip install pyvista openpyxl. I get a warning saying that the notebook backend can't be used because trame is not installed, so I think that plot is not interactive. When I did pip install pyvista[jupyter] to get trame and the other backend components for Jupyter, the bug reappeared. I've attached a file with the environment needed for the bug to appear on my machine (pip install pandas pyvista[jupyter]).

bug_requirements.txt

I wondered if it was maybe related to me using a Notebook inside VSCode. I opened the notebook in Chrome and that didn't fix it either.

Yeah, in a new Windows python 3.11 environment, pip install -r bug_requirements.txt, jupyter lab in chrome,

import openpyxl
from pyvista import Sphere
Sphere().plot()

works fine 😕

What if you swap the import order? I have typically been importing openpyxl after pyvista.

I just ran your code and was still encountering this bug. So I guess the order doesn't matter for me. So strange. My Python version in this environment is 3.12.2. Otherwise I'm totally at a loss...

just checked python 3.12.2 for completeness' sake, and it's fine regardless of import order. you're on windows too? i'm willing to blame some sort of poltergeist at this point

Yeah Windows 10. Weird that I consistently have the issue in fresh environments. Maybe my anaconda installation is a bit messed up. Anyways if no one else can replicate it guess we should leave it alone for now.

Yeah sorry! I'm really curious what's causing this but don't have any ideas for further isolating it.