Skip to content

Commit

Permalink
Merge pull request #1616 from terencehonles/update-shell_plus-for-jup…
Browse files Browse the repository at this point in the history
…yterlab-3

update shell_plus for jupyterlab 3
  • Loading branch information
trbs committed Feb 7, 2021
2 parents d2dd7a9 + f8afd65 commit bccecdf
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions django_extensions/management/commands/shell_plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys
import traceback
import warnings

from django.db import connections
from django.conf import settings
Expand Down Expand Up @@ -203,7 +204,7 @@ def generate_kernel_specs(self, app, ipython_arguments):

return {'django_extensions': ks}

def run_notebookapp(self, app, options, use_kernel_specs=True):
def run_notebookapp(self, app_init, options, use_kernel_specs=True):
no_browser = options['no_browser']

if self.extra_args:
Expand Down Expand Up @@ -234,7 +235,12 @@ def run_notebookapp(self, app, options, use_kernel_specs=True):
if not use_kernel_specs:
notebook_arguments.extend(ipython_arguments)

app.initialize(notebook_arguments)
if not callable(app_init):
app = app_init
warnings.warn('Initialize should be a callable not an app instance', DeprecationWarning)
app.initialize(notebook_arguments)
else:
app = app_init(notebook_arguments)

# IPython >= 3 uses kernelspecs to specify kernel CLI args
if use_kernel_specs:
Expand Down Expand Up @@ -285,10 +291,13 @@ def get_notebook(self, options):

use_kernel_specs = release.version_info[0] >= 3

def run_notebook():
def app_init(*args, **kwargs):
app = NotebookApp.instance()
self.run_notebookapp(app, options, use_kernel_specs)
app.initialize(*args, **kwargs)
return app

def run_notebook():
self.run_notebookapp(app_init, options, use_kernel_specs)
return run_notebook

@shell_runner(flags=['--lab'], name='JupyterLab Notebook')
Expand All @@ -298,10 +307,22 @@ def get_jupyterlab(self, options):
except ImportError:
return traceback.format_exc()

def run_jupyterlab():
app = LabApp.instance()
self.run_notebookapp(app, options)
# check for JupyterLab 3.0
try:
from notebook.notebookapp import NotebookApp
except ImportError:
NotebookApp = None

if not NotebookApp or not issubclass(LabApp, NotebookApp):
app_init = LabApp.initialize_server
else:
def app_init(*args, **kwargs):
app = LabApp.instance()
app.initialize(*args, **kwargs)
return app

def run_jupyterlab():
self.run_notebookapp(app_init, options)
return run_jupyterlab

@shell_runner(flags=['--plain'], name='plain Python')
Expand Down

0 comments on commit bccecdf

Please sign in to comment.