def reload_kedro(path, line=None): """Line magic which reloads all Kedro default variables.""" global startup_error global context global catalog global session try: import kedro.config.default_logger from kedro.framework.hooks import get_hook_manager from kedro.framework.project import configure_project from kedro.framework.session import KedroSession from kedro.framework.session.session import _activate_session from kedro.framework.cli.jupyter import collect_line_magic except ImportError: logging.error( "Kedro appears not to be installed in your current environment " "or your current IPython session was not started in a valid Kedro project." ) raise try: path = path or project_path # clear hook manager hook_manager = get_hook_manager() name_plugin_pairs = hook_manager.list_name_plugin() for name, plugin in name_plugin_pairs: hook_manager.unregister(name=name, plugin=plugin) # remove cached user modules metadata = _get_project_metadata(path) to_remove = [ mod for mod in sys.modules if mod.startswith(metadata.package_name) ] # `del` is used instead of `reload()` because: If the new version of a module does not # define a name that was defined by the old version, the old definition remains. for module in to_remove: del sys.modules[module] configure_project(metadata.package_name) session = KedroSession.create(metadata.package_name, path) _activate_session(session, force=True) logging.debug("Loading the context from %s", str(path)) context = session.load_context() catalog = context.catalog logging.info("** Kedro project %s", str(metadata.project_name)) logging.info("Defined global variable `context` and `catalog`") for line_magic in collect_line_magic(): register_line_magic(needs_local_scope(line_magic)) logging.info("Registered line magic `%s`", line_magic.__name__) except Exception as err: startup_error = err logging.exception("Kedro's ipython session startup script failed:\n%s", str(err)) raise err
def reload_kedro(path, line=None): """Line magic which reloads all Kedro default variables.""" global startup_error global context global catalog try: import kedro.config.default_logger # noqa from kedro.framework.cli.jupyter import collect_line_magic from kedro.framework.context import load_context except ImportError: logging.error( "Kedro appears not to be installed in your current environment " "or your current IPython session was not started in a valid Kedro project." ) raise try: path = path or project_path # remove cached user modules context = load_context(path) to_remove = [ mod for mod in sys.modules if mod.startswith(context.package_name) ] # `del` is used instead of `reload()` because: If the new version of a module does not # define a name that was defined by the old version, the old definition remains. for module in to_remove: del sys.modules[module] # clear hook manager; hook implementations will be re-registered when the # context is instantiated again in `load_context()` below hook_manager = get_hook_manager() name_plugin_pairs = hook_manager.list_name_plugin() for name, plugin in name_plugin_pairs: hook_manager.unregister(name=name, plugin=plugin) logging.debug("Loading the context from %s", str(path)) # Reload context to fix `pickle` related error (it is unable to serialize reloaded objects) # Some details can be found here: # https://modwsgi.readthedocs.io/en/develop/user-guides/issues-with-pickle-module.html#packing-and-script-reloading context = load_context(path) catalog = context.catalog logging.info("** Kedro project %s", str(context.project_name)) logging.info("Defined global variable `context` and `catalog`") for line_magic in collect_line_magic(): register_line_magic(needs_local_scope(line_magic)) logging.info("Registered line magic `%s`", line_magic.__name__) except Exception as err: startup_error = err logging.exception("Kedro's ipython session startup script failed:\n%s", str(err)) raise err
def reload_kedro(path, line=None): """Line magic which reloads all Kedro default variables.""" global startup_error global context global catalog try: import kedro.config.default_logger from kedro.framework.context import load_context from kedro.framework.cli.jupyter import collect_line_magic except ImportError: logging.error( "Kedro appears not to be installed in your current environment " "or your current IPython session was not started in a valid Kedro project." ) raise try: path = path or project_path logging.debug("Loading the context from %s", str(path)) context = load_context(path) catalog = context.catalog # remove cached user modules package_name = context.__module__.split(".")[0] to_remove = [ mod for mod in sys.modules if mod.startswith(package_name) ] for module in to_remove: del sys.modules[module] logging.info("** Kedro project %s", str(context.project_name)) logging.info("Defined global variable `context` and `catalog`") for line_magic in collect_line_magic(): register_line_magic(line_magic) logging.info("Registered line magic `%s`", line_magic.__name__) except Exception as err: startup_error = err logging.exception("Kedro's ipython session startup script failed:\n%s", str(err)) raise err
def test_collect_line_magic(entry_points, entry_point): entry_point.load.return_value = "line_magic" line_magics = collect_line_magic() assert line_magics == ["line_magic"] entry_points.assert_called_once_with(group="kedro.line_magic")