Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC + WIP: Track current PyGMT figure to avoid calling the figure module repeatly #3397

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pygmt/_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
Private dictionary to keep tracking of current PyGMT state.

The feature is only meant for internal use by PyGMT and is experimental!
"""

_STATE = {
"current_figure": None,
}
21 changes: 15 additions & 6 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from tempfile import TemporaryDirectory
from typing import Literal

from pygmt._state import _STATE

try:
import IPython

Expand Down Expand Up @@ -123,16 +125,23 @@ def _activate_figure(self):

All plotting commands run afterward will append to this figure.

Unlike the command-line version (``gmt figure``), this method does not
trigger the generation of a figure file. An explicit call to
:meth:`pygmt.Figure.savefig` or :meth:`pygmt.Figure.psconvert` must be
made in order to get a file.
Unlike the command-line version (``gmt figure``), this method does not trigger
the generation of a figure file. An explicit call to
:meth:`pygmt.Figure.savefig` or :meth:`pygmt.Figure.psconvert` must be made in
order to get a file.
"""
# Passing format '-' tells pygmt.end to not produce any files.
fmt = "-"
# Activate the figure only if it's not already activated
if _STATE["current_figure"] == self._name:
return

with Session() as lib:
# Passing format '-' tells pygmt.end to not produce any files.
fmt = "-"
lib.call_module(module="figure", args=[self._name, fmt])

# Track the current activated figure name
_STATE["current_figure"] = self._name

def _preprocess(self, **kwargs):
"""
Call the ``figure`` module before each plotting command to ensure we're plotting
Expand Down