Skip to content

Commit

Permalink
Merge pull request #13 from jobrad-gmbh/enable_jupyter
Browse files Browse the repository at this point in the history
Enable jupyter | uses code from: caburj ([email protected])
  • Loading branch information
thibaultbetremieux authored Apr 4, 2024
2 parents d1eb54e + 2776a87 commit 31d2d9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
23 changes: 19 additions & 4 deletions odoo/cli/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, locals=None, filename="<console>"):

class Shell(Command):
"""Start odoo in an interactive shell"""
supported_shells = ['ipython', 'ptpython', 'bpython', 'python']
supported_shells = ['jupyter_kernel', 'ipython', 'ptpython', 'bpython', 'python']

def init(self, args):
config.parse_config(args)
Expand Down Expand Up @@ -86,6 +86,13 @@ def console(self, local_vars):
_logger.warning("Could not start '%s' shell." % shell)
_logger.debug("Shell error:", exc_info=True)

def jupyter_kernel(self, local_vars, jupyter_connection_file):
_logger.info('🪐🛸👽 LAUNCHING JUPYTER 👽🛸🪐')
for i in sorted(local_vars):
print('%s: %s' % (i, local_vars[i]))
from ipykernel.kernelapp import launch_new_instance
launch_new_instance(argv=['-f', jupyter_connection_file], user_ns=local_vars)

def ipython(self, local_vars):
from IPython import start_ipython
start_ipython(argv=[], user_ns=local_vars)
Expand All @@ -101,7 +108,7 @@ def bpython(self, local_vars):
def python(self, local_vars):
Console(locals=local_vars).interact()

def shell(self, dbname):
def shell(self, dbname, jupyter_connection_file):
local_vars = {
'openerp': odoo,
'odoo': odoo,
Expand All @@ -115,12 +122,20 @@ def shell(self, dbname):
env = odoo.api.Environment(cr, uid, ctx)
local_vars['env'] = env
local_vars['self'] = env.user
self.console(local_vars)
if jupyter_connection_file:
self.jupyter_kernel(local_vars, jupyter_connection_file)
else:
self.console(local_vars)
cr.rollback()
else:
self.console(local_vars)

def run(self, args):
try:
i = args.index('-f')
jupyter_connection_file = args[i+1]
except ValueError:
jupyter_connection_file = None
self.init(args)
self.shell(config['db_name'])
self.shell(config['db_name'], jupyter_connection_file)
return 0
2 changes: 2 additions & 0 deletions odoo/tools/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def __init__(self, fname=None):

group.add_option("-D", "--data-dir", dest="data_dir", my_default=_get_default_datadir(),
help="Directory where to store Odoo data")
group.add_option("-f", "--connection-file", dest="connection_file")
parser.add_option_group(group)

# HTTP
Expand Down Expand Up @@ -515,6 +516,7 @@ def die(cond, msg):
self.options['demo'] = (dict(self.options['init'])
if not self.options['without_demo'] else {})
self.options['update'] = opt.update and dict.fromkeys(opt.update.split(','), 1) or {}
self.options['connection_file'] = opt.connection_file or ''
self.options['translate_modules'] = opt.translate_modules and [m.strip() for m in opt.translate_modules.split(',')] or ['all']
self.options['translate_modules'].sort()

Expand Down

0 comments on commit 31d2d9e

Please sign in to comment.