Skip to content

Commit 30befaf

Browse files
authored
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.
1 parent f7e2ce6 commit 30befaf

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

pygmt/helpers/utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Utilities and common tasks for wrapping the GMT modules.
33
"""
4+
import os
45
import shutil
56
import subprocess
67
import sys
@@ -195,8 +196,9 @@ def launch_external_viewer(fname):
195196
"""
196197
Open a file in an external viewer program.
197198
198-
Uses the ``xdg-open`` command on Linux, the ``open`` command on macOS, and
199-
the default web browser on other systems.
199+
Uses the ``xdg-open`` command on Linux, the ``open`` command on macOS, the
200+
associated application on Windows, and the default web browser on other
201+
systems.
200202
201203
Parameters
202204
----------
@@ -214,8 +216,10 @@ def launch_external_viewer(fname):
214216
subprocess.run(["xdg-open", fname], check=False, **run_args)
215217
elif os_name == "darwin": # Darwin is macOS
216218
subprocess.run(["open", fname], check=False, **run_args)
219+
elif os_name == "win32":
220+
os.startfile(fname) # pylint: disable=no-member
217221
else:
218-
webbrowser.open_new_tab("file://{}".format(fname))
222+
webbrowser.open_new_tab(f"file://{fname}")
219223

220224

221225
def args_in_kwargs(args, kwargs):

0 commit comments

Comments
 (0)