Skip to content

[ENH]: (sphinxext) persist :context: across docstring or add plot setup directive #30410

@nlgranger

Description

@nlgranger

Problem

In the sphinx extension, the :context: makes it possible to reuse code accross calls.

However, this does not seem to work across different docstrings. So if one wants to document each method of a class with a plot, you have to re-instantiate the class every time.

Using the global plot_pre_code config option is not really manageable outside of a small project.

Proposed solution

option A

Make it so that the context persists across docstrings. But I'm not sure sphinx allows that.

class Whatever:
    """The Whatever class

        .. plot::
            :context:
            :nofigs:

            w = Whatever()
    """

    def a(self):
        """the a method.

        .. plot::
            :context:

            plt.figure()
            plt.scatter(*w.a())
        """
        return [(0., 1.), (0., 1.)]

    def b(self):
        """the b method.

        .. plot::
            :context:

            plt.figure()
            plt.scatter(*w.b())
        """
        return [(0., 1.), (0., 1.)]

option B

Add a .. plotsetup:: directive similar to the doctest testsetup directive. That way, one might be able to write:

class Whatever:
    """The Whatever class

        .. plotsetup::
            w = Whatever()
    """

    def a(self):
        """the a method.

        .. plot::
            plt.figure()
            plt.scatter(*w.a())
        """
        return [(0., 1.), (0., 1.)]

    def b(self):
        """the b method.

        .. plot::
            plt.figure()
            plt.scatter(*w.b())
        """
        return [(0., 1.), (0., 1.)]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions