Skip to content

Commit ca084d8

Browse files
RobPasMueklmcadamsSMoraisAnsysclatapie
authored
feat: facilitating example contribution (#596)
* feat: facilitating example contribution * feat: enhancements * Update doc/source/how-to/documenting.rst Co-authored-by: Kerry McAdams <[email protected]> * docs: extend with jupytext approach * fix(docs): wrong link for pyspeos * Apply suggestions from code review Co-authored-by: Roberto Pastor Muela <[email protected]> * refactor: use links file * Update doc/source/how-to/documenting.rst Co-authored-by: Camille Latapie <[email protected]> * Update doc/source/how-to/documenting.rst * Update doc/source/how-to/documenting.rst --------- Co-authored-by: Kerry McAdams <[email protected]> Co-authored-by: Sebastien Morais <[email protected]> Co-authored-by: Sébastien Morais <[email protected]> Co-authored-by: Camille Latapie <[email protected]>
1 parent 430e55a commit ca084d8

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

doc/source/how-to/documenting.rst

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,4 +913,115 @@ files for inclusion in the documentation.
913913
| examples/02-HFSS/HFSS_Spiral.py | examples-design-and-simulation-of-spiral-inductors.py |
914914
+-------------------------------------------------+-------------------------------------------------------+
915915

916-
If you have questions about SEO, email the `PyAnsy Core team <pyansys_core_email_>`_.
916+
If you have questions about SEO, email the `PyAnsys Core team <pyansys_core_email_>`_.
917+
918+
Facilitating contributions in the form of examples
919+
--------------------------------------------------
920+
921+
A common challenge in libraries is the lack of examples. Examples are
922+
critical for users to understand how to use the library and present new
923+
functionality. They also serve as a mechanism to educate users on best practices.
924+
925+
To encourage contributions of examples, it is important to provide a clear
926+
structure for contributors to follow. The build process should also be straightforward.
927+
928+
Three best practices for examples are:
929+
930+
- **Providing a template**: Ensures that contributors follow a consistent structure and
931+
simplifies the process of getting started. The template should include all necessary
932+
information, such as the name of the example, a description, and the code itself.
933+
- **Verifying the example**: Ensures that the example works as expected and helps catch
934+
errors before submission. The verification process should involve running the example
935+
and checking the output. If the example fails, it should be easy to identify and fix the issue.
936+
- **Making the example usable in multiple contexts**: Ensures that the example can be
937+
usable by a user reading the documentation, an application engineer using the library
938+
through a notebook, or a developer including the example to the documentation.
939+
940+
Providing a template
941+
~~~~~~~~~~~~~~~~~~~~
942+
943+
Here are examples of how to promote the use of templates for examples:
944+
945+
- `PyAnsys Geometry examples template <PyAnsys_geometry_examples_template_>`_
946+
- `PyPrimeMesh examples template <PyPrimemesh_examples_template_>`_
947+
948+
These templates are also referenced in the contributing guidelines:
949+
950+
- `PyAnsys Geometry example contribution guidelines <PyAnsys_geometry_examples_contribution_>`_
951+
952+
Verifying examples
953+
~~~~~~~~~~~~~~~~~~
954+
955+
The verification process depends on the library and the tools used for generating the examples.
956+
Within the PyAnsys ecosystem, Sphinx Gallery and NBSphinx are commonly used to generate examples,
957+
with slightly different build processes for each library.
958+
959+
The following approach can be applied across libraries:
960+
961+
- Create a ``Makefile`` target that runs a specific example.
962+
963+
This target is used to execute the example and check the output. Only one example is build in this
964+
case, making it very useful for documentation builds that last very long. If the example fails,
965+
it should be straightforward to identify and resolve the issue. For instance, PyAnsys Geometry
966+
includes a ``make`` target for this purpose:
967+
968+
.. code-block:: console
969+
970+
./make.bat single-example examples/01_getting_started/01_math.mystnb
971+
972+
.. code-block:: console
973+
974+
make single-example examples/01_getting_started/01_math.mystnb
975+
976+
The pull request introducing this feature is: `PyAnsys Geometry PR #1893 <PyAnsys_geometry_pull_1893_>`_
977+
This implementation is specific to NB Sphinx, but the concept of creating a target to run a specific
978+
example and verify its output can be adapted to other libraries.
979+
980+
Making the example usable in multiple contexts
981+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
982+
983+
A core objective when designing examples is to ensure they can be used seamlessly across multiple
984+
contexts: whether it is by end users exploring them interactively, by documentation tools rendering
985+
them for the web, or by engineers verifying their correctness and updating them. Achieving this
986+
flexibility requires a careful balance between accessibility, structure, and maintainability.
987+
988+
A recommended approach to support this goal is to author and maintain examples as plain
989+
Python scripts, but using `Jupytext <Jupytext_>`_ to treat those
990+
scripts as notebooks when needed. Formatting your examples using the
991+
`Jupytext light format <Jupytext_light_format_>`_
992+
makes them fully compatible with JupyterLab, where they can be opened, edited, and
993+
executed just like traditional notebooks - all without needing a IPYNB file. Here is an example
994+
of a Jupytext light format script:
995+
996+
.. code-block:: python
997+
998+
# # Dipole antenna
999+
#
1000+
# Keywords: **HFSS**, **antenna**, **3D component**, **far field**.
1001+
#
1002+
# ## Prerequisites
1003+
#
1004+
# ### Perform imports
1005+
#
1006+
# Import the packages required to run this example.
1007+
1008+
# +
1009+
import os
1010+
import tempfile
1011+
import time
1012+
1013+
from ansys.aedt.core import Hfss
1014+
1015+
# -
1016+
1017+
# ### Define constants
1018+
# Constants help ensure consistency and avoid repetition throughout the example.
1019+
1020+
AEDT_VERSION = "2025.1"
1021+
NUM_CORES = 4
1022+
NG_MODE = False # Open AEDT UI when it is launched.
1023+
1024+
Here are examples of PyAnsys projects using this approach:
1025+
1026+
- `PyAEDT examples repository <PyAEDT-examples_>`_
1027+
- `PySpeos repository <PySpeos_>`_

doc/source/links.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@
5050
.. _grpc_hello_world: https://github.com/ansys/pyansys-helloworld
5151
.. _Links_for_pymapdl: https://github.com/ansys/pymapdl/blob/main/doc/source/links.rst
5252
.. _PyAEDT: https://github.com/ansys/pyaedt
53+
.. _PyAEDT-examples: https://github.com/ansys/pyaedt-examples
5354
.. _PyAnsys_geometry_Docker_containers: https://geometry.docs.pyansys.com/version/stable/getting_started/docker/index.html
55+
.. _PyAnsys_geometry_examples_template: https://geometry.docs.pyansys.com/version/stable/examples/99_misc/template.html
56+
.. _PyAnsys_geometry_examples_contribution: https://geometry.docs.pyansys.com/version/stable/contributing.html#adding-examples
57+
.. _PyAnsys_geometry_pull_1893 : https://github.com/ansys/pyansys-geometry/pull/1893
58+
.. _PyPrimemesh_examples_template: https://prime.docs.pyansys.com/version/stable/examples/gallery_examples/misc/example_template.html#sphx-glr-examples-gallery-examples-misc-example-template-py
5459
.. _PyAnsys: https://docs.pyansys.com/version/stable/
5560
.. _PyAnsys Bot: https://github.com/apps/pyansys-bot
5661
.. _pyansys_proto_generator: https://github.com/ansys/pyansys-protos-generator
@@ -64,6 +69,7 @@
6469
.. _PyMAPDL Legacy Reader: https://github.com/ansys/pymapdl-reader
6570
.. _PyMechanical: https://github.com/ansys/pymechanical
6671
.. _PyMeilisearch: https://pymeilisearch.docs.ansys.com/
72+
.. _PySpeos: https://github.com/ansys/pyspeos
6773
.. _PyPIM: https://github.com/ansys/pypim
6874
.. _python_installer_qt_ui_releases: https://github.com/ansys/python-installer-qt-gui/releases
6975
.. _review_bot: https://github.com/ansys/review-bot
@@ -79,6 +85,7 @@
7985
.. _Docker: https://www.docker.com/
8086
.. _Flake8: https://flake8.pycqa.org/en/latest/
8187
.. _isort: https://pycqa.github.io/isort/
88+
.. _Jupytext: https://jupytext.readthedocs.io/en/latest/
8289
.. _numpy: https://numpy.org/
8390
.. _pandas: https://pandas.pydata.org/
8491
.. _pre-commit: https://pre-commit.com/
@@ -99,6 +106,7 @@
99106
.. _Docutils: https://docutils.sourceforge.io/
100107
.. _doctest: https://docs.python.org/3/library/doctest.html
101108
.. _Google_docstrings: https://github.com/google/styleguide/blob/gh-pages/pyguide.md#38-comments-and-docstrings
109+
.. _Jupytext_light_format: https://jupytext.readthedocs.io/en/latest/formats-scripts.html#the-light-format
102110
.. _Notepadpp_downloads: https://notepad-plus-plus.org/downloads/
103111
.. _Notepadpp_searching: https://npp-user-manual.org/docs/searching/
104112
.. _numpydoc: https://numpydoc.readthedocs.io/en/latest/index.html

0 commit comments

Comments
 (0)