def _log_default(self): from IPython.config.application import Application if Application.initialized(): return Application.instance().log else: return logging.getLogger()
def load_config_file(self, suppress_errors=True): """Load the config file. By default, errors in loading config are handled, and a warning printed on screen. For testing, the suppress_errors option is set to False, so errors will make tests fail. """ base_config = 'ipython_config.py' self.log.debug("Attempting to load config file: %s" % base_config) try: Application.load_config_file( self, base_config, path=self.config_file_paths) except IOError: # ignore errors loading parent pass if self.config_file_name == base_config: # don't load secondary config return self.log.debug( "Attempting to load config file: %s" % self.config_file_name) try: Application.load_config_file( self, self.config_file_name, path=self.config_file_paths) except IOError: # Only warn if the default config file was NOT being used. if self.config_file_specified: self.log.warn("Config file not found, skipping: %s" % self.config_file_name) except: # For testing purposes. if not suppress_errors: raise self.log.warn( "Error loading config file: %s" % self.config_file_name, exc_info=True)
def load_config_file(self, suppress_errors=True): """Load the config file. By default, errors in loading config are handled, and a warning printed on screen. For testing, the suppress_errors option is set to False, so errors will make tests fail. """ self.log.debug("Searching path %s for config files", self.config_file_paths) base_config = "ipython_config.py" self.log.debug("Attempting to load config file: %s" % base_config) try: Application.load_config_file(self, base_config, path=self.config_file_paths) except ConfigFileNotFound: # ignore errors loading parent self.log.debug("Config file %s not found", base_config) pass for config_file_name in self.config_files: if not config_file_name or config_file_name == base_config: continue self.log.debug("Attempting to load config file: %s" % self.config_file_name) try: Application.load_config_file(self, config_file_name, path=self.config_file_paths) except ConfigFileNotFound: # Only warn if the default config file was NOT being used. if config_file_name in self.config_file_specified: msg = self.log.warn else: msg = self.log.debug msg("Config file not found, skipping: %s", config_file_name) except: # For testing purposes. if not suppress_errors: raise self.log.warn("Error loading config file: %s" % self.config_file_name, exc_info=True)
def pylab(self, line=''): """Load numpy and matplotlib to work interactively. This function lets you activate pylab (matplotlib, numpy and interactive support) at any point during an IPython session. It will import at the top level numpy as np, pyplot as plt, matplotlib, pylab and mlab, as well as all names from numpy and pylab. See the %matplotlib magic for more details. """ args = magic_arguments.parse_argstring(self.pylab, line) if args.no_import_all is None: # get default from Application if Application.initialized(): app = Application.instance() try: import_all = app.pylab_import_all except AttributeError: import_all = True else: # nothing specified, no app - default True import_all = True else: # invert no-import flag import_all = not args.no_import_all gui, backend, clobbered = self.shell.enable_pylab(args.gui, import_all=import_all) self._show_matplotlib_backend(args.gui, backend) if clobbered: warn("pylab import has clobbered these variables: %s" % clobbered + "\n`%pylab --no-import-all` prevents importing * from pylab and numpy" )
def pylab(self, parameter_s=''): """Load numpy and matplotlib to work interactively. %pylab [GUINAME] This function lets you activate pylab (matplotlib, numpy and interactive support) at any point during an IPython session. It will import at the top level numpy as np, pyplot as plt, matplotlib, pylab and mlab, as well as all names from numpy and pylab. If you are using the inline matplotlib backend for embedded figures, you can adjust its behavior via the %config magic:: # enable SVG figures, necessary for SVG+XHTML export in the qtconsole In [1]: %config InlineBackend.figure_format = 'svg' # change the behavior of closing all figures at the end of each # execution (cell), or allowing reuse of active figures across # cells: In [2]: %config InlineBackend.close_figures = False Parameters ---------- guiname : optional One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk', 'osx' or 'tk'). If given, the corresponding Matplotlib backend is used, otherwise matplotlib's default (which you can override in your matplotlib config file) is used. Examples -------- In this case, where the MPL default is TkAgg:: In [2]: %pylab Welcome to pylab, a matplotlib-based Python environment. Backend in use: TkAgg For more information, type 'help(pylab)'. But you can explicitly request a different backend:: In [3]: %pylab qt Welcome to pylab, a matplotlib-based Python environment. Backend in use: Qt4Agg For more information, type 'help(pylab)'. """ if Application.initialized(): app = Application.instance() try: import_all_status = app.pylab_import_all except AttributeError: import_all_status = True else: import_all_status = True self.shell.enable_pylab(parameter_s, import_all=import_all_status, welcome_message=True)
def pylab(self, parameter_s=''): """Load numpy and matplotlib to work interactively. %pylab [GUINAME] This function lets you activate pylab (matplotlib, numpy and interactive support) at any point during an IPython session. It will import at the top level numpy as np, pyplot as plt, matplotlib, pylab and mlab, as well as all names from numpy and pylab. If you are using the inline matplotlib backend for embedded figures, you can adjust its behavior via the %config magic:: # enable SVG figures, necessary for SVG+XHTML export in the qtconsole In [1]: %config InlineBackend.figure_format = 'svg' # change the behavior of closing all figures at the end of each # execution (cell), or allowing reuse of active figures across # cells: In [2]: %config InlineBackend.close_figures = False Parameters ---------- guiname : optional One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk', 'osx' or 'tk'). If given, the corresponding Matplotlib backend is used, otherwise matplotlib's default (which you can override in your matplotlib config file) is used. Examples -------- In this case, where the MPL default is TkAgg:: In [2]: %pylab Welcome to pylab, a matplotlib-based Python environment. Backend in use: TkAgg For more information, type 'help(pylab)'. But you can explicitly request a different backend:: In [3]: %pylab qt Welcome to pylab, a matplotlib-based Python environment. Backend in use: Qt4Agg For more information, type 'help(pylab)'. """ if Application.initialized(): app = Application.instance() try: import_all_status = app.pylab_import_all except AttributeError: import_all_status = True else: import_all_status = True self.shell.enable_pylab(parameter_s, import_all=import_all_status)
def load_config_file(self, profile_name): """load a config file from the config file dir profile_name : {string} name of the profile file to load without file extension. """ try: Application.load_config_file(self, profile_name + ".nbcv", path=[os.path.join(os.getcwdu(), "profile")]) return True except ConfigFileNotFound: self.log.warn("Config file for profile '%s' not found, giving up ", profile_name) return False
def pylab(self, line=''): """Load numpy and matplotlib to work interactively. This function lets you activate pylab (matplotlib, numpy and interactive support) at any point during an IPython session. %pylab makes the following imports:: import numpy import matplotlib from matplotlib import pylab, mlab, pyplot np = numpy plt = pyplot from IPython.display import display from IPython.core.pylabtools import figsize, getfigs from pylab import * from numpy import * If you pass `--no-import-all`, the last two `*` imports will be excluded. See the %matplotlib magic for more details about activating matplotlib without affecting the interactive namespace. """ args = magic_arguments.parse_argstring(self.pylab, line) if args.no_import_all is None: # get default from Application if Application.initialized(): app = Application.instance() try: import_all = app.pylab_import_all except AttributeError: import_all = True else: # nothing specified, no app - default True import_all = True else: # invert no-import flag import_all = not args.no_import_all gui, backend, clobbered = self.shell.enable_pylab( args.gui, import_all=import_all) self._show_matplotlib_backend(args.gui, backend) print("Populating the interactive namespace from numpy and matplotlib") if clobbered: warn( "pylab import has clobbered these variables: %s" % clobbered + "\n`%pylab --no-import-all` prevents importing * from pylab and numpy" )
def pylab(self, line=''): """Load numpy and matplotlib to work interactively. This function lets you activate pylab (matplotlib, numpy and interactive support) at any point during an IPython session. %pylab makes the following imports:: import numpy import matplotlib from matplotlib import pylab, mlab, pyplot np = numpy plt = pyplot from IPython.display import display from IPython.core.pylabtools import figsize, getfigs from pylab import * from numpy import * If you pass `--no-import-all`, the last two `*` imports will be excluded. See the %matplotlib magic for more details about activating matplotlib without affecting the interactive namespace. """ args = magic_arguments.parse_argstring(self.pylab, line) if args.no_import_all is None: # get default from Application if Application.initialized(): app = Application.instance() try: import_all = app.pylab_import_all except AttributeError: import_all = True else: # nothing specified, no app - default True import_all = True else: # invert no-import flag import_all = not args.no_import_all gui, backend, clobbered = self.shell.enable_pylab( args.gui, import_all=import_all) self._show_matplotlib_backend(args.gui, backend) print ( "Populating the interactive namespace from numpy and matplotlib") if clobbered: warn("pylab import has clobbered these variables: %s" % clobbered + "\n`%matplotlib` prevents importing * from pylab and numpy" )
def enable_gui(gui, kernel=None): """Enable integration with a given GUI""" if gui not in loop_map: raise ValueError("GUI %r not supported" % gui) if kernel is None: if Application.initialized(): kernel = getattr(Application.instance(), 'kernel', None) if kernel is None: raise RuntimeError("You didn't specify a kernel," " and no IPython Application with a kernel appears to be running." ) loop = loop_map[gui] if loop and kernel.eventloop is not None and kernel.eventloop is not loop: raise RuntimeError("Cannot activate multiple GUI eventloops") kernel.eventloop = loop
def load_config_file(self, profile_name): """load a config file from the config file dir profile_name : {string} name of the profile file to load without file extension. """ try: Application.load_config_file( self, profile_name + '.nbcv', path=[os.path.join(os.getcwdu(), 'profile')]) return True except ConfigFileNotFound: self.log.warn("Config file for profile '%s' not found, giving up ", profile_name) return False
def enable_gui(gui, kernel=None): """Enable integration with a given GUI""" if gui not in loop_map: e = "Invalid GUI request %r, valid ones are:%s" % (gui, loop_map.keys()) raise ValueError(e) if kernel is None: if Application.initialized(): kernel = getattr(Application.instance(), 'kernel', None) if kernel is None: raise RuntimeError("You didn't specify a kernel," " and no IPython Application with a kernel appears to be running." ) loop = loop_map[gui] if loop and kernel.eventloop is not None and kernel.eventloop is not loop: raise RuntimeError("Cannot activate multiple GUI eventloops") kernel.eventloop = loop
def enable_gui(gui, kernel=None): """Enable integration with a given GUI""" if gui not in loop_map: raise ValueError("GUI %r not supported" % gui) if kernel is None: if Application.initialized(): kernel = getattr(Application.instance(), 'kernel', None) if kernel is None: raise RuntimeError( "You didn't specify a kernel," " and no IPython Application with a kernel appears to be running." ) loop = loop_map[gui] if kernel.eventloop is not None and kernel.eventloop is not loop: raise RuntimeError("Cannot activate multiple GUI eventloops") kernel.eventloop = loop
def terminate_children(sig, frame): log = Application.instance().log log.critical("Got signal %i, terminating children..." % sig) for child in children: child.terminate() sys.exit(sig != SIGINT)
def terminate_children(sig, frame): log = Application.instance().log log.critical("Got signal %i, terminating children..."%sig) for child in children: child.terminate() sys.exit(sig != SIGINT)
def enable_gui(gui, kernel=None): """Enable integration with a given GUI""" if gui not in loop_map: e = "Invalid GUI request %r, valid ones are:%s" % (gui, list(loop_map.keys())) raise ValueError(e) if kernel is None: if Application.initialized(): kernel = getattr(Application.instance(), 'kernel', None) if kernel is None: raise RuntimeError("You didn't specify a kernel," " and no IPython Application with a kernel appears to be running." ) loop = loop_map[gui] if loop and kernel.eventloop is not None and kernel.eventloop is not loop: raise RuntimeError("Cannot activate multiple GUI eventloops") kernel.eventloop = loop
def launch_scheduler(in_addr, out_addr, mon_addr, not_addr, reg_addr, config=None, logname='root', log_url=None, loglevel=logging.DEBUG, identity=b'task', in_thread=False): ZMQStream = zmqstream.ZMQStream if config: # unwrap dict back into Config config = Config(config) if in_thread: # use instance() to get the same Context/Loop as our parent ctx = zmq.Context.instance() loop = ioloop.IOLoop.instance() else: # in a process, don't use instance() # for safety with multiprocessing ctx = zmq.Context() loop = ioloop.IOLoop() ins = ZMQStream(ctx.socket(zmq.ROUTER), loop) util.set_hwm(ins, 0) ins.setsockopt(zmq.IDENTITY, identity + b'_in') ins.bind(in_addr) outs = ZMQStream(ctx.socket(zmq.ROUTER), loop) util.set_hwm(outs, 0) outs.setsockopt(zmq.IDENTITY, identity + b'_out') outs.bind(out_addr) mons = zmqstream.ZMQStream(ctx.socket(zmq.PUB), loop) util.set_hwm(mons, 0) mons.connect(mon_addr) nots = zmqstream.ZMQStream(ctx.socket(zmq.SUB), loop) nots.setsockopt(zmq.SUBSCRIBE, b'') nots.connect(not_addr) querys = ZMQStream(ctx.socket(zmq.DEALER), loop) querys.connect(reg_addr) # setup logging. if in_thread: log = Application.instance().log else: if log_url: log = connect_logger( logname, ctx, log_url, root="scheduler", loglevel=loglevel) else: log = local_logger(logname, loglevel) scheduler = TaskScheduler(client_stream=ins, engine_stream=outs, mon_stream=mons, notifier_stream=nots, query_stream=querys, loop=loop, log=log, config=config) scheduler.start() if not in_thread: try: loop.start() except KeyboardInterrupt: scheduler.log.critical("Interrupted, exiting...")
def launch_scheduler( in_addr, out_addr, mon_addr, not_addr, config=None, logname="root", log_url=None, loglevel=logging.DEBUG, identity=b"task", in_thread=False, ): ZMQStream = zmqstream.ZMQStream if config: # unwrap dict back into Config config = Config(config) if in_thread: # use instance() to get the same Context/Loop as our parent ctx = zmq.Context.instance() loop = ioloop.IOLoop.instance() else: # in a process, don't use instance() # for safety with multiprocessing ctx = zmq.Context() loop = ioloop.IOLoop() ins = ZMQStream(ctx.socket(zmq.ROUTER), loop) ins.setsockopt(zmq.IDENTITY, identity) ins.bind(in_addr) outs = ZMQStream(ctx.socket(zmq.ROUTER), loop) outs.setsockopt(zmq.IDENTITY, identity) outs.bind(out_addr) mons = zmqstream.ZMQStream(ctx.socket(zmq.PUB), loop) mons.connect(mon_addr) nots = zmqstream.ZMQStream(ctx.socket(zmq.SUB), loop) nots.setsockopt(zmq.SUBSCRIBE, b"") nots.connect(not_addr) # setup logging. if in_thread: log = Application.instance().log else: if log_url: log = connect_logger(logname, ctx, log_url, root="scheduler", loglevel=loglevel) else: log = local_logger(logname, loglevel) scheduler = TaskScheduler( client_stream=ins, engine_stream=outs, mon_stream=mons, notifier_stream=nots, loop=loop, log=log, config=config ) scheduler.start() if not in_thread: try: loop.start() except KeyboardInterrupt: print("interrupted, exiting...", file=sys.__stderr__)
def load_config_file(self, suppress_errors=True): """Load the config file. By default, errors in loading config are handled, and a warning printed on screen. For testing, the suppress_errors option is set to False, so errors will make tests fail. """ self.log.debug("Searching path %s for config files", self.config_file_paths) base_config = 'ipython_config.py' self.log.debug("Attempting to load config file: %s" % base_config) try: Application.load_config_file( self, base_config, path=self.config_file_paths ) except ConfigFileNotFound: # ignore errors loading parent self.log.debug("Config file %s not found", base_config) pass for config_file_name in self.config_files: if not config_file_name or config_file_name == base_config: continue self.log.debug("Attempting to load config file: %s" % self.config_file_name) try: Application.load_config_file( self, config_file_name, path=self.config_file_paths ) except ConfigFileNotFound: # Only warn if the default config file was NOT being used. if config_file_name in self.config_file_specified: msg = self.log.warn else: msg = self.log.debug msg("Config file not found, skipping: %s", config_file_name) except: # For testing purposes. if not suppress_errors: raise self.log.warn("Error loading config file: %s" % self.config_file_name, exc_info=True)
def launch_scheduler(in_addr, out_addr, mon_addr, not_addr, reg_addr, config=None, logname='root', log_url=None, loglevel=logging.DEBUG, identity=b'task', in_thread=False): ZMQStream = zmqstream.ZMQStream if config: # unwrap dict back into Config config = Config(config) if in_thread: # use instance() to get the same Context/Loop as our parent ctx = zmq.Context.instance() loop = ioloop.IOLoop.instance() else: # in a process, don't use instance() # for safety with multiprocessing ctx = zmq.Context() loop = ioloop.IOLoop() ins = ZMQStream(ctx.socket(zmq.ROUTER),loop) util.set_hwm(ins, 0) ins.setsockopt(zmq.IDENTITY, identity + b'_in') ins.bind(in_addr) outs = ZMQStream(ctx.socket(zmq.ROUTER),loop) util.set_hwm(outs, 0) outs.setsockopt(zmq.IDENTITY, identity + b'_out') outs.bind(out_addr) mons = zmqstream.ZMQStream(ctx.socket(zmq.PUB),loop) util.set_hwm(mons, 0) mons.connect(mon_addr) nots = zmqstream.ZMQStream(ctx.socket(zmq.SUB),loop) nots.setsockopt(zmq.SUBSCRIBE, b'') nots.connect(not_addr) querys = ZMQStream(ctx.socket(zmq.DEALER),loop) querys.connect(reg_addr) # setup logging. if in_thread: log = Application.instance().log else: if log_url: log = connect_logger(logname, ctx, log_url, root="scheduler", loglevel=loglevel) else: log = local_logger(logname, loglevel) scheduler = TaskScheduler(client_stream=ins, engine_stream=outs, mon_stream=mons, notifier_stream=nots, query_stream=querys, loop=loop, log=log, config=config) scheduler.start() if not in_thread: try: loop.start() except KeyboardInterrupt: scheduler.log.critical("Interrupted, exiting...")
def _is_interactive(): """Determine whether the user has requested interactive mode.""" # The Python interpreter sets sys.flags correctly, so use them! if sys.flags.interactive: return True # IPython does not set sys.flags when -i is specified, so first # check it if it is already imported. if '__IPYTHON__' not in dir(builtins): return False # Then we check the application singleton and determine based on # a variable it sets. try: from IPython.config.application import Application as App return App.initialized() and App.instance().interact except (ImportError, AttributeError): return False
def load_config_file(self, suppress_errors=True): """Load the config file. By default, errors in loading config are handled, and a warning printed on screen. For testing, the suppress_errors option is set to False, so errors will make tests fail. """ base_config = 'ipython_config.py' self.log.debug("Attempting to load config file: %s" % base_config) try: Application.load_config_file(self, base_config, path=self.config_file_paths) except IOError: # ignore errors loading parent pass if self.config_file_name == base_config: # don't load secondary config return self.log.debug("Attempting to load config file: %s" % self.config_file_name) try: Application.load_config_file(self, self.config_file_name, path=self.config_file_paths) except IOError: # Only warn if the default config file was NOT being used. if self.config_file_specified: self.log.warn("Config file not found, skipping: %s" % self.config_file_name) except: # For testing purposes. if not suppress_errors: raise self.log.warn("Error loading config file: %s" % self.config_file_name, exc_info=True)
def load_default_config(ipython_dir=None): """Load the default config file from the default ipython_dir. This is useful for embedded shells. """ if ipython_dir is None: ipython_dir = get_ipython_dir() profile_dir = os.path.join(ipython_dir, 'profile_default') config = Config() for cf in Application._load_config_files("ipython_config", path=profile_dir): config.update(cf) return config
import socket import DrQueue from DrQueue import Computer as DrQueueComputer from IPython.config.application import Application if "DRQUEUE_SLAVE" in os.environ: SLAVE_IP = os.environ["DRQUEUE_SLAVE"] else: SLAVE_IP = socket.gethostbyname(socket.getfqdn()) IPENGINE_STARTUP_LOGPATH = os.path.join(os.environ["DRQUEUE_ROOT"], "logs", "ipengine_" + SLAVE_IP + ".log") IPENGINE_STARTUP_LOGFILE = open(IPENGINE_STARTUP_LOGPATH, "ab") # connect to current IPython engine instance app = Application.instance() # get engine_id engine_id = app.engine.id # get computer information engine = DrQueueComputer() # set creation time engine["created_at"] = int(time.time()) # set engine_id engine["engine_id"] = engine_id # store entry in database DrQueueComputer.store_db(engine) IPENGINE_STARTUP_LOGFILE.write("\nComputer info: \n") IPENGINE_STARTUP_LOGFILE.write(str(engine) + "\n\n")
import socket import DrQueue from DrQueue import Computer as DrQueueComputer from IPython.config.application import Application if "DRQUEUE_SLAVE" in os.environ: SLAVE_IP = os.environ["DRQUEUE_SLAVE"] else: SLAVE_IP = socket.gethostbyname(socket.getfqdn()) IPENGINE_STARTUP_LOGPATH = os.path.join(os.environ["DRQUEUE_ROOT"], "logs", "ipengine_" + SLAVE_IP + ".log") IPENGINE_STARTUP_LOGFILE = open(IPENGINE_STARTUP_LOGPATH, "ab") # connect to current IPython engine instance app = Application.instance() # get engine_id engine_id = app.engine.id # get computer information engine = DrQueueComputer() # set creation time engine['created_at'] = int(time.time()) # set engine_id engine['engine_id'] = engine_id # store entry in database DrQueueComputer.store_db(engine) IPENGINE_STARTUP_LOGFILE.write("\nComputer info: \n") IPENGINE_STARTUP_LOGFILE.write(str(engine) + "\n\n")
def get_app(): # return TerminalIPythonApp.instance() return Application.instance()
def _log_default(self): from IPython.config.application import Application return Application.instance().log
def get_app(): #return TerminalIPythonApp.instance() return Application.instance()