Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting play to true in NGL trajectory viewer by default #1041

Closed
Taylor-96 opened this issue Dec 8, 2022 · 4 comments
Closed

Setting play to true in NGL trajectory viewer by default #1041

Taylor-96 opened this issue Dec 8, 2022 · 4 comments

Comments

@Taylor-96
Copy link

Hello,

I was wondering if it is possible to have the animation of a trajectory shown in an NGL viewer set to play by default rather than requiring the user to press play first? On a similar note, would it also be possible to have looping set to true by default at initialization of the viewer? Cheers!

@hainm
Copy link
Collaborator

hainm commented Dec 11, 2022

hi @Taylor-96

Please adapt below code. It should reply to two of your questions:

import nglview as nv
from nglview import SimpletrajTrajectory

traj = SimpletrajTrajectory(nv.datafiles.XTC, nv.datafiles.PDB)
view = nv.show_simpletraj(traj)

stop = False
def loop(view):
    import time
    def do():
        while True and not stop:
            if view.frame == view.max_frame:
                view.frame = 0
            view.frame = view.frame + 1
            time.sleep(0.2)
    view._run_on_another_thread(do)
        
view.on_displayed(loop)
view

To stop, just stop = True in another notebook cell.

Cheers

@Taylor-96
Copy link
Author

Hi, thanks a lot - that works great. I was just wondering if there is a way to access the values/status of the play button and loop button so that it is possible for the user to stop the animation by pressing the play button rather than manually putting stop=True in another cell?

Thanks again!

@hainm
Copy link
Collaborator

hainm commented Dec 12, 2022

Hi, thanks a lot - that works great. I was just wondering if there is a way to access the values/status of the play button and loop button so that it is possible for the user to stop the animation by pressing the play button rather than manually putting stop=True in another cell?

Thanks again!

I don't know a solution on top of my head. You can access to the player via view._iplayer.children[0]. It's ipywidgets' Play https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20List.html#Play-(Animation)-widget

https://github.com/nglviewer/nglview/blob/master/nglview/widget.py#L300-L303

@Taylor-96
Copy link
Author

Hi, thanks a lot for the suggestion and for the help with this problem. I'll look into using this to update the status of the player then.

Best wishes,

Taylor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants