Skip to content

Commit

Permalink
Open figures using the associated application on Windows (#952)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
seisman committed Feb 23, 2021
1 parent f7e2ce6 commit 30befaf
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Utilities and common tasks for wrapping the GMT modules.
"""
import os
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -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
----------
Expand All @@ -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):
Expand Down

0 comments on commit 30befaf

Please sign in to comment.