Skip to content

Commit

Permalink
Inject code to set pyvista backend into notebooks during docs build
Browse files Browse the repository at this point in the history
pyvista/pyvista#4809

Signed-off-by: Johannes Mueller <[email protected]>
  • Loading branch information
johannes-mueller committed Dec 7, 2023
1 parent 99de6b6 commit 7a7305a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@
ep.preprocess(nb, {'metadata': {'path': os.path.dirname(fn)}})


# Inject code to set pyvista jupyter backend into relevant notebooks
#
import nbsphinx
original_from_notebook_node = nbsphinx.Exporter.from_notebook_node

from nbconvert.preprocessors import Preprocessor

def from_notebook_node(self, nb, resources, **kwargs):

class InjectPyvistaBackendPreprocessor(Preprocessor):
def preprocess(self, nb, resources):
for index, cell in enumerate(nb.cells):
if cell['cell_type'] == 'code' and 'import pyvista as pv' in cell['source'] :
cell['source'] += "\npv.set_jupyter_backend('html')\n"
break
return nb, resources

nb, resources = InjectPyvistaBackendPreprocessor().preprocess(nb, resources=resources)
return original_from_notebook_node(self, nb, resources, **kwargs)

nbsphinx.Exporter.from_notebook_node = from_notebook_node


# Don't try to use C-code for the pytensor stuff when building the docs.
# Otherwise the demo notebooks fail on readthedocs
os.environ['PYTHONWARNINGS'] = 'ignore'
Expand Down

0 comments on commit 7a7305a

Please sign in to comment.