Skip to content

Fail to open images in external viewer programs if running PyGMT in a Python script #1061

Closed

Description

@seisman

Description of the problem

Here is a simple Python script to reproduce the issue.

import pygmt

fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame="+tFigure1")
print("Openning Figure1")
fig.show(method="external")

When I run the codes in a Python/IPython interpreter, it works well and can open the preview image. However, if I save it in a Python script and run it using python test.py, sometimes it can open the image but sometimes it fails and gives an error like (I'm using macOS):

image

The reason is simple, on macOS, we call the open program to open the image. Because we can't detect the status of the external viewer, the Python script continues to execute the code and exit. When the script exits, Python will call the destructor of the Figure class (i.e., Figure.__del__ method), which deletes the preview directory and also the preview image.

So, the preview image will show if the open program opens the image before Python deletes it, and will fail to open the image if it's already deleted.


The following script is similar to the above one, but instead, it plots and opens two images.

import pygmt

fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame="+tFigure1")
print("Openning Figure1")
fig.show(method="external")

fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame="+tFigure2")
print("Openning Figure2")
fig.show(method="external")

Again, the script works well in a Python/IPython interpreter, but usually can open Figure1 and can't open Figure2 when running a script. Figure1 can be successfully opened because plotting the second figure take some time, so the first Figure.show() call can open Figure1 before the program exits.


The only solution I can figure out is adding a short pause after calling the external viewer, i.e.,
adding time.sleep(0.5) at the end of the launch_external_viewer(fname) function.

def launch_external_viewer(fname):

It works well for me, and should also work for Linux, but I'm not sure about Windows. The 0.5 second pause time is empirical, and may not work for slow computers.

I opened the PR #1062 with the above solution, so that anyone can test it with/without the pause.

Activity

  1. added
    bug
    help wanted
    on Mar 15, 2021
  2. core-man commented on Mar 15, 2021

    @core-man
    Member

    I use CentOS 7.5 to test. Both two examples work in JupyterLab no matter if time.sleep(0.5) is added.

    However, it shows the same bug as @seisman when running a script (I tried three times for each cases).

    • The first example (only Figure 1) does not work and shows the same bug, but it works if time.sleep(0.5) is added.
    • The second example (two figures) can display Figure 1 but cannot open Figure 2, but it works if time.sleep(0.5) is added .
  3. seisman commented on Apr 6, 2021

    @seisman
    MemberAuthor

    The issue is partially fixed by #1062, but I'll leave the issue open for a longer time.

  4. weiji14 commented on Nov 3, 2021

    @weiji14
    Member

    Do we want to keep this issue open @seisman? Or can we close it since time.sleep(0.5) seems to be good enough for most cases.

  5. seisman commented on Nov 4, 2021

    @seisman
    MemberAuthor

    Or can we close it since time.sleep(0.5) seems to be good enough for most cases.

    At least one user reported to me that time.sleep(0.5) still doesn't work for him, possibly because his computer is old and slow. He has to change it to something like time.sleep(1.0) to make it work.

    Perhaps we should add a parameter like sleep=0.5 to the Figure.show() function (and launch_external_viewer) so that users can customize the waiting time.

  6. weiji14 commented on Nov 4, 2021

    @weiji14
    Member

    Perhaps we should add a parameter like sleep=0.5 to the Figure.show() function (and launch_external_viewer) so that users can customize the waiting time.

    Yep, that sounds like a good option. Will need to modify these parts:

    time.sleep(0.5)

    launch_external_viewer(pdf)

    launch_external_viewer(fname)

  7. added this to the 0.6.0 milestone on Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions