From b97437cbcc090d6b209f3c1b5aaf559ea5fd4e0a Mon Sep 17 00:00:00 2001 From: pelinski Date: Fri, 31 Jan 2025 17:55:38 +0000 Subject: [PATCH] Fixes connection on jupyter notebooks (broken with self.loop) + Logger transfer defaults to False --- pybela/Logger.py | 4 ++-- pybela/Watcher.py | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pybela/Logger.py b/pybela/Logger.py index 4c13498..b2cf7cb 100644 --- a/pybela/Logger.py +++ b/pybela/Logger.py @@ -32,12 +32,12 @@ def __init__(self, ip="192.168.7.2", port=5555, data_add="gui_data", control_add # -- logging methods -- - def start_logging(self, variables=[], transfer=True, logging_dir="./"): + def start_logging(self, variables=[], transfer=False, logging_dir="./"): """ Starts logging session. The session can be ended by calling stop_logging(). Args: variables (list of str, optional): List of variables to be logged. If no variables are passed, all variables in the watcher are logged. Defaults to []. - transfer (bool, optional): If True, the logged files will be transferred automatically during the logging session. Defaults to True. + transfer (bool, optional): If True, the logged files will be transferred automatically during the logging session. Defaults to False. #FIXME currently broken Returns: list of str: List of local paths to the logged files. diff --git a/pybela/Watcher.py b/pybela/Watcher.py index 5c2518c..4f5dcb3 100644 --- a/pybela/Watcher.py +++ b/pybela/Watcher.py @@ -48,8 +48,10 @@ def __init__(self, ip="192.168.7.2", port=5555, data_add="gui_data", control_add self._pybela_ws_register = _pybela_ws_register # if running in jupyter notebook, enable nest_asyncio + is_running_on_jupyter_notebook = False try: get_ipython().__class__.__name__ + is_running_on_jupyter_notebook = True nest_asyncio.apply() print("Running in Jupyter notebook. Enabling nest_asyncio.") except NameError: @@ -58,8 +60,12 @@ def __init__(self, ip="192.168.7.2", port=5555, data_add="gui_data", control_add # background event loop # If no loop exists, create a new one if self._pybela_ws_register["event-loop"] is None: - self.loop = asyncio.new_event_loop() - asyncio.set_event_loop(self.loop) + + if is_running_on_jupyter_notebook: + self.loop = asyncio.get_event_loop() + else: + self.loop = asyncio.new_event_loop() + asyncio.set_event_loop(self.loop) self._pybela_ws_register["event-loop"] = self.loop else: # if loop exists, use the existing one self.loop = self._pybela_ws_register["event-loop"]