def ipython_notebook(self): # analogous to the calls in the django.core shell command from IPython.frontend.html.notebook.notebookapp import NotebookApp app = NotebookApp.instance() app.initialize() # here sys.path contains, as the first element, the directory containing # manage.py. However, in the kernels that are started for the actual # notebooks, sys.path does not contain this project directory. looks # like this has been fixed in IPython, and should be out AFTER 0.13.2: # https://github.com/ipython/ipython/commit/463ab6b388436efdb8bb2f949817e94c74f51dee # until that time, after 0.13.2 is released, we need the following # os.environ modification before and restoration after app.start() # get current PYTHONPATH pps = os.environ.get('PYTHONPATH') if pps is None: # remember that there was no PYTHONPATH to start with nopp = True pps = '' else: nopp = False # break up into components ppl = pps.split(os.pathsep) # path containing manage.py project_path = os.path.abspath(os.path.dirname(sys.argv[0])) # add it ppl.append(project_path) # and set it os.environ['PYTHONPATH'] = os.pathsep.join(ppl) # start the notebook app.start() # user has quit the notebook, we restore the saved PYTHONPATH if nopp: # there was none to start with, so we delete it del os.environ['PYTHONPATH'] else: os.environ['PYTHONPATH'] = pps
def handle(self, addrport=None, *args, **options): if args: raise CommandError('Usage is notebook %s' % self.args) if addrport: if ':' in addrport: ip_addr, port = addrport.split(':') else: ip_addr = '127.0.0.1' port = addrport app = NotebookApp.instance() if 'ip_addr' in locals() and 'port' in locals(): app.ip = ip_addr app.port = int(port) if options.get('no_mathjax', False): app.enable_mathjax = False del options['no_mathjax'] app.initialize() app.start()