From c06e533759bd0cff9ccba39660a52206d3a04165 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 23 Feb 2021 15:23:00 -0500 Subject: [PATCH] Open figures using the associated application on Windows (#952) This PR improves the `launch_external_viewer` function to open images using the associated application (just like double-click the images) on Windows. The implemention is done by calling the [`os.startfile`](https://docs.python.org/3/library/os.html#os.startfile) function. `os.startfile` is only available on Windows, so need to disable the pylint error `no-member`. The code was originall written in #269 by @leouieda, re-used in #529. --- pygmt/helpers/utils.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index 92f55feb678..749ebe63e5b 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -1,6 +1,7 @@ """ Utilities and common tasks for wrapping the GMT modules. """ +import os import shutil import subprocess import sys @@ -195,8 +196,9 @@ def launch_external_viewer(fname): """ Open a file in an external viewer program. - Uses the ``xdg-open`` command on Linux, the ``open`` command on macOS, and - the default web browser on other systems. + Uses the ``xdg-open`` command on Linux, the ``open`` command on macOS, the + associated application on Windows, and the default web browser on other + systems. Parameters ---------- @@ -214,8 +216,10 @@ def launch_external_viewer(fname): subprocess.run(["xdg-open", fname], check=False, **run_args) elif os_name == "darwin": # Darwin is macOS subprocess.run(["open", fname], check=False, **run_args) + elif os_name == "win32": + os.startfile(fname) # pylint: disable=no-member else: - webbrowser.open_new_tab("file://{}".format(fname)) + webbrowser.open_new_tab(f"file://{fname}") def args_in_kwargs(args, kwargs):