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

Allow users to set the waiting time when displaying a preview image using an external viewer #1618

Merged
merged 4 commits into from
Nov 22, 2021
Merged
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
14 changes: 11 additions & 3 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def savefig(
if show:
launch_external_viewer(fname)

def show(self, dpi=300, width=500, method=None):
def show(self, dpi=300, width=500, method=None, waiting=0.5):
"""
Display a preview of the figure.

Expand All @@ -284,7 +284,11 @@ def show(self, dpi=300, width=500, method=None):
This is useful when running unit tests and building the documentation
in consoles without a Graphical User Interface.

Note that the external viewer does not block the current process.
Note that the external viewer does not block the current process, thus
it's necessary to suspend the execution of the current process for a
short while after launching the external viewer, so that the preview
image won't be deleted before the external viewer tries to open it. Set
the ``waiting`` parameter to a larger number if your computer is slow.

Parameters
----------
Expand All @@ -298,6 +302,10 @@ def show(self, dpi=300, width=500, method=None):
- **external**: PDF preview in an external program [default]
- **notebook**: PNG preview [default in Jupyter notebooks]
- **none**: Disable image preview
waiting : float
Suspend the execution of the current process for a given number of
seconds after launching an external viewer.
Only works if ``method="external"``.
weiji14 marked this conversation as resolved.
Show resolved Hide resolved
"""
# Module level variable to know which figures had their show method
# called. Needed for the sphinx-gallery scraper.
Expand Down Expand Up @@ -329,7 +337,7 @@ def show(self, dpi=300, width=500, method=None):

if method == "external":
pdf = self._preview(fmt="pdf", dpi=dpi, anti_alias=False, as_bytes=False)
launch_external_viewer(pdf)
launch_external_viewer(pdf, waiting=waiting)

def shift_origin(self, xshift=None, yshift=None):
"""
Expand Down
9 changes: 5 additions & 4 deletions pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def is_nonstr_iter(value):
return isinstance(value, Iterable) and not isinstance(value, str)


def launch_external_viewer(fname):
def launch_external_viewer(fname, waiting=0):
"""
Open a file in an external viewer program.

Expand Down Expand Up @@ -251,9 +251,10 @@ def launch_external_viewer(fname):
os.startfile(fname) # pylint: disable=no-member
else:
webbrowser.open_new_tab(f"file://{fname}")
# suspend the execution for 0.5 s to avoid the images being deleted
# when a Python script exits
time.sleep(0.5)
if waiting > 0:
# suspend the execution for a few seconds to avoid the images being
# deleted when a Python script exits
time.sleep(waiting)


def args_in_kwargs(args, kwargs):
Expand Down