Hi Simon,

  Apparently gradio_client is calling your print_result callback in a different thread.  The OpenGL graphics ChimeraX uses can only be used in the main thread, and the Qt window toolkit used by ChimeraX can only be used in the main thread.  So you need to make sure everything you are doing is in the main thread if it might change the graphics or user interface.  The way to do that is instead of

def print_result(self, results):
    run(self.session, f"open {results[1]}")

use

def print_result(self, results):
    self.session.ui.thread_safe(run, self.session, f"open {results[1]}")

This thread_safe() method is described here

https://www.cgl.ucsf.edu/chimerax/docs/devel/modules/ui/gui.html#chimerax.ui.gui.UI.thread_safe

  Tom


On Oct 9, 2024, at 12:06 PM, Simon Dürr via Chimera-dev <chimera-dev@cgl.ucsf.edu> wrote:

Hi, 

I'm currently developing a chimerax extension that makes a REST Api call using the gradio_client library:
Here is a minimal working example (using gradio_client==1.4.0)

from gradio_client import Client
from chimerax.core.commands import run

def print_result(self, results):
results[1] is a path to a local file
  run(self.session, f"open {results[1]}")
result = client.submit(
input_sequence="",
input_ligand="COc1ccc(cc1)n2c3c(c(n2)C(=O)N)CCN(C3=O)c4ccc(cc4)N5CCCCC5=O",
exhaustiveness=1,
api_name="/predict",
    result_callbacks=[print_result]
)

However, when I try to open the loaded file in ChimeraX after the prediction is finished and the callback is run I get the following error:
RuntimeError: Attempted to make OpenGL context current in wrong thread (26408, context thread 21820).

I presume I need to use a Task like here to run the chimerax.core.commands.run in the same thread as the ui. 
Are there any minimal examples how to do this or other suggestions how to make this work?

Thanks,



_______________________________________________
Chimera-dev mailing list -- chimera-dev@cgl.ucsf.edu
To unsubscribe send an email to chimera-dev-leave@cgl.ucsf.edu