nglviewer / nglview

Jupyter widget to interactively view molecular structures and trajectories
http://nglviewer.org/nglview/latest/
Other
788 stars 132 forks source link

Save to png #1067

Closed slowkow closed 1 year ago

slowkow commented 1 year ago

I tried running the example from the documentation, but I do not see the expected files (figure_1.png and figure_2.png) being written. The script seems to finish without writing any files.

What should I change to make this script write the png files to my folder?

import time
import nglview as nv

view = nv.demo()
view
def generate_images(v=view):
    v.clear()
    v.add_cartoon(color='red')
    im0 = v.render_image()
    v.clear()
    v.add_cartoon(color='blue')
    im1 = v.render_image()
    for im in [im0, im1]:
        while not im.value:
            time.sleep(0.1)
    for n, im in zip('ab', [im0, im1]):
        with open(f'figure_{n}.png', 'wb') as fh:
            fh.write(im.value)

import threading
thread = threading.Thread(
    target=generate_images,
)
thread.daemon = True
thread.start()

I ran the script like this: python3 myscript.py

I expected to see two new files in my folder, but instead I got nothing.

Here are my version numbers:

❯ python3 -c 'import nglview; print(nglview.__version__)'
3.0.6

❯ python3 -c 'import ipywidgets; print(ipywidgets.__version__)'
8.0.6
hainm commented 1 year ago

@slowkow You have to display the view first. So you must run the code inside the notebook, not from a python script.

slowkow commented 1 year ago

Is there any way to get a png file without using jupyter notebook?

hainm commented 1 year ago

No there is not.