BruceSherwood / vpython-jupyter

This repository has been moved to https://github.com/vpython/vpython-jupyter
64 stars 33 forks source link

Scene update within functions #147

Closed sntgluca closed 3 years ago

sntgluca commented 3 years ago

Dear all,

I am walking my first steps with this library, I can already say I am impressed, and yet I find it hard to understand how to run it efficiently (for instance in a notebook). I noticed I can run code from the examples in a jupyter notebook, but only if I execute the main loop directly in a cell. As soon as I try to encapsulate the loop logic in a function (or a method, which is what I am really interested in), the scene is not updated until the end of the loop.

Is there anything I can include to run the main update loop from a function? Thanks!

jcoady commented 3 years ago

Attached is an example of running a loop in a function in a jupyter notebook cell. You will need use asyncio and make sure your loop has a rate() statement which contains the code to send messages from python to the javascript front-end. In the attached notebook file the loop code is in an asynchronous function called periodic which has async/await keywords and it updates the scene continuously from the function. Try running the example in the attached notebook to see how it can be done.

import asyncio async def periodic(): while True: await asyncio.sleep(0) rate(100) if running: currentobject.rotate(angle=1e-4*sl.value, axis=vec(0,1,0))

loop = asyncio.get_event_loop() task = loop.create_task(periodic())

Note that in a Jupyter Notebook a notebook cell needs to run to completion before another notebook cell can be executed. So normally if you have a loop that runs forever like in a vpython simulation then the cell will never finish executing and you won't be able to run other cells in the notebook. But if you put your vpython loop in an asyncio coroutine like in the above example then the loop will run asynchronously and the cell will complete execution so that you can execute other cells in the notebook while the vpython simulation is running.

John

From: "thomas-bc" @.> To: "vpython" @.> Cc: "Subscribed" @.***> Sent: Saturday, April 3, 2021 5:22:38 AM Subject: [BruceSherwood/vpython-jupyter] Scene update within functions (#147)

Dear all,

I am walking my first steps with this library, I can already say I am impressed, and yet I find it hard to understand how to run it efficiently (for instance in a notebook). I noticed I can run code from the examples in a jupyter notebook, but only if I execute the main loop directly in a cell. As soon as I try to encapsulate the loop logic in a function (or a method, which is what I am really interested in), the scene is not updated until the end of the loop.

Is there anything I can include to run the main update loop from a function? Thanks!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, [ https://github.com/BruceSherwood/vpython-jupyter/issues/147 | view it on GitHub ] , or [ https://github.com/notifications/unsubscribe-auth/AAD4LZNMYYANGYOM4PMPZQLTG4CA5ANCNFSM42KGB2BQ | unsubscribe ] .

BruceSherwood commented 3 years ago

John, I don't understand why you discuss the use of asyncio. The rate() statement is necessary and sufficient.

jcoady commented 3 years ago

Yes you are right the rate() statement is necessary and sufficient. You could use asyncio in the case where you have a vpython simulation that runs forever in a notebook cell and you would like to execute other code cells in the notebook after the vpython simulation starts running.

John

From: "thomas-bc" @.> To: "vpython" @.> Cc: "johncoady" @.>, "Comment" @.> Sent: Saturday, April 3, 2021 8:45:44 AM Subject: Re: [BruceSherwood/vpython-jupyter] Scene update within functions (#147)

John, I don't understand why you discuss the use of asyncio. The rate() statement is necessary and sufficient.

— You are receiving this because you commented. Reply to this email directly, [ https://github.com/BruceSherwood/vpython-jupyter/issues/147#issuecomment-812882800 | view it on GitHub ] , or [ https://github.com/notifications/unsubscribe-auth/AAD4LZN724DDVLX5Y245FTTTG4Z2RANCNFSM42KGB2BQ | unsubscribe ] .

sntgluca commented 3 years ago

Thanks all :) I am starting to get the grip around it. I am closing the ticket, since as far as I am concerned both suggestions are working!