Пример #1
0
def get_kernel_id():
    # This must be run from inside a kernel.
    # Based on: https://stackoverflow.com/a/13055551/429898
    # TODO is the id persistent across sessions with the same notebook?
    connection_file_path = kernel.get_connection_file()
    connection_file = basename(connection_file_path)
    kernel_id = connection_file.split('-', 1)[1].split('.')[0]
    return kernel_id
Пример #2
0
 def connect_info(self, arg_s):
     """Print information for connecting other clients to this kernel
     
     It will print the contents of this session's connection file, as well as
     shortcuts for local clients.
     
     In the simplest case, when called from the most recently launched kernel,
     secondary clients can be connected, simply with:
     
     $> ipython <app> --existing
     
     """
     
     from IPython.core.application import BaseIPythonApplication as BaseIPApp
     
     if BaseIPApp.initialized():
         app = BaseIPApp.instance()
         security_dir = app.profile_dir.security_dir
         profile = app.profile
     else:
         profile = 'default'
         security_dir = ''
     
     try:
         connection_file = get_connection_file()
         info = get_connection_info(unpack=False)
     except Exception as e:
         error("Could not get connection info: %r" % e)
         return
     
     # add profile flag for non-default profile
     profile_flag = "--profile %s" % profile if profile != 'default' else ""
     
     # if it's in the security dir, truncate to basename
     if security_dir == os.path.dirname(connection_file):
         connection_file = os.path.basename(connection_file)
     
     
     print (info + '\n')
     print ("Paste the above JSON into a file, and connect with:\n"
         "    $> ipython <app> --existing <file>\n"
         "or, if you are local, you can connect with just:\n"
         "    $> ipython <app> --existing {0} {1}\n"
         "or even just:\n"
         "    $> ipython <app> --existing {1}\n"
         "if this is the most recent IPython session you have started.".format(
         connection_file, profile_flag
         )
     )
 def connect_info(self, arg_s):
     """Print information for connecting other clients to this kernel
     
     It will print the contents of this session's connection file, as well as
     shortcuts for local clients.
     
     In the simplest case, when called from the most recently launched kernel,
     secondary clients can be connected, simply with:
     
     $> ipython <app> --existing
     
     """
     
     from IPython.core.application import BaseIPythonApplication as BaseIPApp
     
     if BaseIPApp.initialized():
         app = BaseIPApp.instance()
         security_dir = app.profile_dir.security_dir
         profile = app.profile
     else:
         profile = 'default'
         security_dir = ''
     
     try:
         connection_file = get_connection_file()
         info = get_connection_info(unpack=False)
     except Exception as e:
         error("Could not get connection info: %r" % e)
         return
     
     # add profile flag for non-default profile
     profile_flag = "--profile %s" % profile if profile != 'default' else ""
     
     # if it's in the security dir, truncate to basename
     if security_dir == os.path.dirname(connection_file):
         connection_file = os.path.basename(connection_file)
     
     
     print (info + '\n')
     print ("Paste the above JSON into a file, and connect with:\n"
         "    $> ipython <app> --existing <file>\n"
         "or, if you are local, you can connect with just:\n"
         "    $> ipython <app> --existing {0} {1}\n"
         "or even just:\n"
         "    $> ipython <app> --existing {1}\n"
         "if this is the most recent IPython session you have started.".format(
         connection_file, profile_flag
         )
     )
Пример #4
0
    def _handle_magics(self, magic_code, code):
        """
        Handle cell magics
        """
        _exec_status = False
        _content = None
        err_content = None
        if(magic_code == 'connect_info'):
            try:
                connection_file = get_connection_file()
                _content = get_connection_info(unpack=False)
            except Exception as e:
                error("Could not get connection info: %r" % e)
                return

        if(magic_code == 'history'):
            _args = re.search(r'^%(\S+)(?:\s*)(\d*)', code)
            self._shell.run_raw('history(' + _args.group(2) + ')')
            _content = self._shell.output[:-1]

        if(magic_code == 'help'):
            _args = re.search(r'^%(\S+)(?:\s*)(\S*)', code)
            _content = self._shell.get_info(_args.group(2))

        if(magic_code == 'image'):
            _args = re.search(r'^%(\S+)(?:\s*)(\S+)', code)
            if _args is not None:
                return self._show_image_inline(_args.group(2))

        if(magic_code == 'flush'):
            self._shell.flush()
            _content = ''

        if(_content is not None):
            execute_content = {'execution_count': self.execution_count,
                               'data': {'text/plain': _content},
                               'metadata': {}}
            self.send_response(self.iopub_socket, 'execute_result',
                               execute_content)
            _exec_status = True
        else:
            err_content = {'execution_count': self.execution_count,
                           'ename': str('CellMagicError'),
                           'evalue': str(1),
                           'traceback': ['Invalid cell magic']}
            self.send_response(self.iopub_socket, 'error', err_content)

        return _exec_status, err_content
Пример #5
0
import time
from IPython.kernel import get_connection_file
from rtconsole import start_console


if __name__ == '__main__':
    import logging
    logging.basicConfig(filename='test.log', level=0)
    app = start_console(locals())
    #print statements won't work as they are now routed to the console
    time.sleep(5)
    #we can optionally save the connection file somewhere, perhaps to be picked up by some other process
    connection_file = get_connection_file(app)
    logging.debug("the connection file is '%s'" % connection_file)

    #or we can manually connect to the latest session implicitly
    #at a command line, type `ipython console --existing`

    #see if you can inpsect tt in the command line and change its value!
    t = 0
    s = 0
    hello = lambda x: x**2
    print 'hello'
    while True:
        t += 1
        s = hello(t)
        time.sleep(1)