File: add-example.py

package info (click to toggle)
python-pyvista 0.44.1-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 159,804 kB
  • sloc: python: 72,164; sh: 118; makefile: 68
file content (216 lines) | stat: -rw-r--r-- 6,630 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""
.. _add_example_example:

Adding a New Gallery Example
----------------------------
This example demonstrates how to add a new PyVista `Sphinx Gallery
<https://sphinx-gallery.github.io/>`_ example as well as being a template that
can be used in their creation.

Each example should have a reference tag/key in the form:

``.. _<example-name>_example:``

The ``.. _`` is necessary. Everything that follows is your reference tag, which
can potentially be used within a docstring. As convention, we keep all
references all in ``snake_case``.

This section should give a brief overview of what the example is about and/or
demonstrates.  The title should be changed to reflect the topic your example
covers.

New examples should be added as python scripts to:

``examples/<index>-<directory-name>/<some-example>.py``

.. note::
   Avoid creating new directories unless absolutely necessary.If you *must*
   create a new folder, make sure to add a ``README.txt`` containing a
   reference, a title and a single sentence description of the folder.
   Otherwise the new folder will be ignored by Sphinx.

Example file names should be hyphen separated snake case:

``some-example.py``

After this preamble is complete, the first code block begins. This is where you
typically set up your imports.

.. note::
   By default, the documentation scrapper will generate both a static image and
   an interactive widget for each plot. If you want to turn this feature off
   define at the top of your file:


   ``# sphinx_gallery_start_ignore``

   ``PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True``

   ``# sphinx_gallery_end_ignore``

   If you want to use static images only for some of your plots. Define
   ``PYVISTA_GALLERY_FORCE_STATIC`` before the ``plot``/``show`` command that
   produces the image you want to turn into static.

   .. code-block::

       ...
       pl.show()  # this will be interactive plot

       # sphinx_gallery_start_ignore
       PYVISTA_GALLERY_FORCE_STATIC = True
       # sphinx_gallery_end_ignore
       ...
       pl.show()  # this will be static plot


"""

from __future__ import annotations

import pyvista as pv
from pyvista import examples

# %%
# Section Title
# ~~~~~~~~~~~~~
# Code blocks can be broken up with text "sections" which are interpreted as
# restructured text.
#
# This will also be translated into a markdown cell in the generated jupyter
# notebook or the HTML page.
#
# Sections can contain any information you may have regarding the example
# such as step-by-step comments or notes regarding motivations etc.
#
# As in Jupyter notebooks, if a statement is unassigned at the end of a code
# block, output will be generated and printed to the screen according to its
# ``__repr__`` method.  Otherwise, you can use ``print()`` to output text.

# Create a dataset and exercise it's repr method
dataset = pv.Sphere()
dataset


# %%
# Plots and images
# ~~~~~~~~~~~~~~~~
# If you use anything that outputs an image (for example,
# :func:`pyvista.Plotter.show`) the resulting image will be rendered within the
# output HTML.
#
# .. note::
#    Unless ``sphinx_gallery_thumbnail_number = <int>`` is included at the top
#    of the example script, first figure (this one) will be used for the
#    gallery thumbnail image.
#
#    Also note that this image number uses one based indexing.

dataset.plot(text='Example Figure')


# %%
# Caveat - Plotter must be within One Cell
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# It's not possible for a single :class:`pyvista.Plotter` object across
# multiple cells because these are closed out automatically at the end of a
# cell.
#
# Here we just exercise the :class:`pyvista.Actor` ``repr`` for demonstrating
# why you might want to instantiate a plotter without showing it in the same
# cell.

pl = pv.Plotter()
actor = pl.add_mesh(dataset)
actor


# %%
# This Cell Cannot Run the Plotter
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The plotter will already be closed by ``sphinx_gallery``.

# This cannot be run here because the plotter is already closed and would raise
# an error:
# >>> pl.show()

# You can, however, close out the plotter or access other attributes.
pl.close()


# %%
# Animations
# ~~~~~~~~~~
# You can even create animations, and while there is a full example in
# :ref:`movie_example`, this cell explains how you can create an animation
# within a single cell.
#
# Here, we explode a simple sphere.

pl = pv.Plotter(off_screen=True)

# optimize for size
pl.open_gif('example_movie.gif', palettesize=16)

sphere = pv.Sphere(theta_resolution=10, phi_resolution=10)

# Add initial mesh to setup the camera
actor = pl.add_mesh(sphere)
pl.background_color = 'w'

# clear and overwrite the mesh on each frame
n_frames = 20
for i in range(n_frames):
    exploded = sphere.explode(factor=i / (n_frames * 2)).extract_surface()
    actor.mapper.dataset.copy_from(exploded)
    pl.camera.reset_clipping_range()
    pl.write_frame()  # Write this frame

# Be sure to close the plotter when finished
pl.close()


# %%
# Adding Example Files
# ~~~~~~~~~~~~~~~~~~~~
# PyVista has a variety of example files all stored at `pyvista/vtk_data
# <https://github.com/pyvista/vtk-data>`_, and you can add the file by
# following the directions there.
#
# Under the hood, PyVista uses `pooch <https://github.com/fatiando/pooch>`_,
# and you can easily access any files added with
# :func:`pyvista.examples.downloads.download_file`.

filename = examples.download_file('bunny.ply')
filename


# %%
# Adding a Wrapped Example
# ~~~~~~~~~~~~~~~~~~~~~~~~
# While it's possible to simply download a file and then read it in, it's
# better for you to write a wrapped ``download_<example-dataset>()`` within
# ``/pyvista/examples/downloads.py``. For example :func:`download_bunny()
# <pyvista.examples.downloads.download_bunny>` downloads and reads with
# :func:`pyvista.read`.
#
# If you intend on adding an example file, you should add a new function in
# ``downloads.py`` to make it easy for users to add example files.

dataset = examples.download_bunny()
dataset


# Making a Pull Request
# ~~~~~~~~~~~~~~~~~~~~~
# Once your example is complete and you've verified it builds locally, you can
# make a pull request (PR).
#
# Branches containing examples should be prefixed with `docs/` as per the branch
# naming conventions found in out `Contributing Guidelines
# <https://github.com/pyvista/pyvista/blob/main/CONTRIBUTING.rst>`_.
#
# .. note::
#    You only need to create the Python source example (``*.py``).  The jupyter
#    notebook and the example HTML will be auto-generated via `sphinx-gallery
#    <https://sphinx-gallery.github.io/>`_.