How to load the Odoo Environment into a Jupyter Notebook?
Asked Answered
C

3

8

Odoo (ERP)

Odoo is an all-in-one management software that offers a range of business applications that form a complete suite of enterprise management applications targeting companies of all sizes. Odoo is an all-in-one business software including CRM, website/e-commerce, billing, accounting, manufacturing, warehouse - and project management, and inventory.

The Community version is the open source version while the Enterprise version supplements the Community edition with commercial features and services.

Terminal

Usually I launch a Python Terminal with the Odoo environment loaded like this:

/odoo_path/odoo-bin --addons-path=/addons_path shell --config=/config_path/.odoo.conf -d database_name

Inside the console I can call to self, and the environment variable is available (self.env). And I can run some commands over the connected database. If I want to do some fast tests this is very useful. But it is kind of inconvinient if I want to run some small scripts or test some code snippets. Jupyter Notebooks and Jupyter Lab are very good tools for that.

Jupyter

Jupyter is a project which produces browser-based interactive environments for programming, mathematics, and data science. It supports a number of languages via plugins ("kernels"), such as Python, Ruby, Haskell, R, Scala and Julia.

Jupyter Notebook is the traditional and most stable application. JupyterLab has a new interface and is more suitable for working with larger projects consisting of multiple files. JupyterLab is in beta as of February 2018.


(source: windows.net)

Requests

What I want to achieve is load the Odoo environment into a Jupyter Notebook and work with these code snippets instead of the simple Python Terminal.

So I am wondering if anyone knows if this is doable or which would be a good place to start. Is there a better alternative? Is this a silly idea?

Coaster answered 16/5, 2018 at 18:50 Comment(2)
have you actually found anything?Milanmilanese
I didn't find anything when I asked the question. I would have to look for it again, but I don't think I will find a solution. Maybe creating a jupyter extension does the trick (as I have done for other libraries), but I do not really know. Let me know if you find out how to do it @ExplorerCoaster
B
1

Step 1. install jupyter in your odoo environment pip install jupyter

Step 2. Start odoo shell and open jupyter from the python prompt

>>
from notebook.notebookapp import NotebookApp
import tornado.ioloop
app = NotebookApp()
app.initialize(["--ip=0.0.0.0"])
tornado.ioloop.IOLoop.current().start()

Step 3. Get the token by sending CTRL+C to the python console

You see something like

http://(<hostname> or 127.0.0.1):8888/?token=9d150bd69e908df9a5e30157b530624536fe0c84d8804f17

Step 4. Open your browser

http://localhost:8888/?token=9d150bd69e908df9a5e30157b530624536fe0c84d8804f17

Now you are in the Jupyter console or web interface

To connect to odoo you need to set the system path and then connect

import sys

sys.path[0:0] = [
'/vagrant/odoo/py3o.template',
'/vagrant/odoo/openupgradelib',
'/vagrant/odoo/anybox.recipe.odoo',
'/vagrant/odoo/pyusb',
'/vagrant/odoo/parts/odoo',
'/vagrant/odoo/eggs/gevent-1.1.2-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/Unidecode-1.1.1-py2.7.egg',
'/vagrant/odoo/eggs/py3o.formats-0.3-py2.7.egg',
'/vagrant/odoo/eggs/isoweek-1.3.3-py2.7.egg',
'/vagrant/odoo/eggs/PyPDF2-1.26.0-py2.7.egg',
'/vagrant/odoo/eggs/simplejson-3.17.2-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/xlrd-1.2.0-py2.7.egg',
'/vagrant/odoo/eggs/openpyxl-2.4.9-py2.7.egg',
'/vagrant/odoo/eggs/vcrpy-3.0.0-py2.7.egg',
'/vagrant/odoo/eggs/py2_ipaddress-3.4.2-py2.7.egg',
'/vagrant/odoo/eggs/Babel-2.8.0-py2.7.egg',
'/vagrant/odoo/eggs/decorator-4.4.2-py2.7.egg',
'/vagrant/odoo/eggs/docutils-0.16-py2.7.egg',
'/vagrant/odoo/eggs/feedparser-5.2.1-py2.7.egg',
'/vagrant/odoo/eggs/Jinja2-2.10.1-py2.7.egg',
'/vagrant/odoo/eggs/lxml-4.5.2-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/Mako-1.1.3-py2.7.egg',
'/vagrant/odoo/eggs/mock-2.0.0-py2.7.egg',
'/vagrant/odoo/eggs/ofxparse-0.20-py2.7.egg',
'/vagrant/odoo/eggs/passlib-1.7.4-py2.7.egg',
'/vagrant/odoo/eggs/Pillow-4.1.1-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/psutil-2.2.1-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/psycogreen-1.0.2-py2.7.egg',
'/vagrant/odoo/eggs/psycopg2-2.8.6-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/Python_Chart-1.39-py2.7.egg',
'/vagrant/odoo/eggs/pydot-1.4.1-py2.7.egg',
'/vagrant/odoo/eggs/pyparsing-2.4.7-py2.7.egg',
'/vagrant/odoo/eggs/pyPdf-1.13-py2.7.egg',
'/vagrant/odoo/eggs/pyserial-3.4-py2.7.egg',
'/vagrant/odoo/eggs/python_dateutil-2.8.1-py2.7.egg',
'/vagrant/odoo/eggs/python_ldap-3.3.1-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/python_openid-2.2.5-py2.7.egg',
'/vagrant/odoo/eggs/pytz-2020.1-py2.7.egg',
'/vagrant/odoo/eggs/PyYAML-5.3.1-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/qrcode-6.1-py2.7.egg',
'/vagrant/odoo/eggs/reportlab-2.7-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/requests-2.24.0-py2.7.egg',
'/vagrant/odoo/eggs/suds_jurko-0.6-py2.7.egg',
'/vagrant/odoo/eggs/vatnumber-1.2-py2.7.egg',
'/vagrant/odoo/eggs/vobject-0.9.6.1-py2.7.egg',
'/vagrant/odoo/eggs/Werkzeug-0.11.11-py2.7.egg',
'/vagrant/odoo/eggs/XlsxWriter-1.3.6-py2.7.egg',
'/vagrant/odoo/eggs/xlwt-1.3.0-py2.7.egg',
'/vagrant/odoo/eggs/python_stdnum-1.14-py2.7.egg',
'/vagrant/odoo/eggs/urllib3-1.25.10-py2.7.egg',
'/vagrant/odoo/eggs/idna-2.10-py2.7.egg',
'/vagrant/odoo/eggs/chardet-3.0.4-py2.7.egg',
'/vagrant/odoo/eggs/certifi-2020.6.20-py2.7.egg',
'/vagrant/odoo/eggs/six-1.15.0-py2.7.egg',
'/vagrant/odoo/eggs/pyasn1_modules-0.2.8-py2.7.egg',
'/vagrant/odoo/eggs/pyasn1-0.4.8-py2.7.egg',
'/vagrant/odoo/eggs/olefile-0.46-py2.7.egg',
'/vagrant/odoo/eggs/beautifulsoup4-4.9.3-py2.7.egg',
'/vagrant/odoo/eggs/pbr-5.5.0-py2.7.egg',
'/vagrant/odoo/eggs/funcsigs-1.0.2-py2.7.egg',
'/vagrant/odoo/eggs/MarkupSafe-1.1.1-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/greenlet-0.4.17-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/zc.recipe.egg-2.0.7-py2.7.egg',
'/vagrant/odoo/eggs/wrapt-1.12.1-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/contextlib2-0.6.0.post1-py2.7.egg',
'/vagrant/odoo/eggs/et_xmlfile-1.0.1-py2.7.egg',
'/vagrant/odoo/eggs/jdcal-1.4.1-py2.7.egg',
'/vagrant/odoo/eggs/pyjon.utils-0.7-py2.7.egg',
'/vagrant/odoo/eggs/Genshi-0.7.3-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/cssselect-1.1.0-py2.7.egg',
'/vagrant/odoo/eggs/soupsieve-1.9.5-py2.7.egg',
'/vagrant/odoo/eggs/backports.functools_lru_cache-1.6.1-py2.7.egg',
'/home/vagrant/venv/lib/python2.7/site-packages',
]

from anybox.recipe.odoo.runtime.session import Session

%%capture
session = Session('/vagrant/odoo/etc/odoo.cfg', '/vagrant/odoo')

%%capture
session.open(db='odootest')

User = session.env['res.users']

user = User.browse(1)

print(user.name)
Administrator

Connect to odoo in Jupyter

Barometry answered 11/12, 2020 at 11:31 Comment(4)
Thanks for answering this old question !! I have been triyng what you suggest but I had some problems. I have installed pip install anybox_recipe_odoo and when I run the fourth cell I get this error UsageError: Line magic function %%capture` not found`. Should I do anything else?Coaster
On the other had I don't have those egg files, I have just added the site-packages folder of my environment, is that enough? I have downloaded all my Odoo modules directly from GitHub, should I add the path to them as well? If I need those eggs, how can I generate them?Coaster
I have realised thanks to your answer that if I have jupyter installed I can use Ipython on console to run some cells. That's nice !Coaster
Hi, I forget to tell the whole story, I am using buildout to build the odoo environmentBarometry
T
1

Took me the whole evening. Thanks GPT for guidance and funny solutions.

I just managed to run this nicely with Odoo 17 (FINALLY!!!) and Jupyter notebooks 7.2 on my local environment in a Docker container:

enter image description here

New line in the Dockerfile before the line with EXPOSE 8069 8071 8072:

RUN pip3 install --upgrade pip setuptools wheel jupyter ipykernel

Odoo17 Jupyter kernel:

{
  "argv": [
    "python",
    "/wrapper.py",
    "-f",
    "{connection_file}"
  ],
  "display_name": "Odoo 17",
  "language": "python",
  "metadata": {
    "debugger": true
  }
}

The custom wrapper/launcher mainly copy+pasted from odoo.cli.shell for ipykernel mounted to /usr/local/share/jupyter/kernels/odoo17/kernel.json inside the container:

from ipykernel.kernelapp import IPKernelApp
import odoo
import sys
from odoo.tools import config
from pathlib import Path

print(config['db_name'])
config.parser.prog = f'{Path(sys.argv[0]).name} Shell'
odoo.cli.server.report_configuration()
dbname = config['db_name']

local_vars = {
    'openerp': odoo,
    'odoo': odoo,
}

def load_ipython_extension(ip):
    for var_name, var_value in local_vars.items():
        ip.push({var_name: var_value})

if dbname:
    registry = odoo.registry(dbname)
    with registry.cursor() as cr:
        uid = odoo.SUPERUSER_ID
        ctx = odoo.api.Environment(cr, uid, {})['res.users'].context_get()
        env = odoo.api.Environment(cr, uid, ctx)
        local_vars['env'] = env
        local_vars['cr'] = cr
        local_vars['self'] = env.user
        from IPython import get_ipython

        ip = get_ipython()
        if ip is None:
            app = IPKernelApp.instance()
            app.initialize()
            ip = app.shell

        load_ipython_extension(ip)

        app.start()

        cr.rollback()
else:
    exit()

Par of the Docker-compose (Swarm) config:

    configs:
      - source: odoo-config-v2
        target: /etc/odoo/odoo.conf
      - source: jupyter-kernel-v1
        target: /usr/local/share/jupyter/kernels/odoo17/kernel.json
      - source: jupyter-wrapper-v1
        target: /wrapper.py
      - source: jupyter-notebook-config-v1
        target: /var/lib/odoo/.jupyter/jupyter_notebook_config.py
    command:
      - jupyter
      - notebook

Remember to update jupyter_notebook_config.py with:

c.ServerApp.ip = '0.0.0.0' # this is important!
c.ServerApp.notebook_dir = '/mnt/extra-addons/' # (optional)
Tumbledown answered 3/7 at 21:36 Comment(0)
B
0

Please find attached a minimal odoo buildout script

[buildout]
parts = odoo
versions = versions

# Mr Developer Extension
extensions = mr.developer
auto-checkout = *
always-checkout = force

[sources]
# Sources that Mr Developer needs to check out
anybox.recipe.odoo = git https://github.com/anybox/anybox.recipe.odoo branch=master
openupgradelib = git https://github.com/OCA/openupgradelib branch=master
odoocrpc = git https://github.com/OCA/odoorpc branch=master

[odoo]
release = 14.0
apply-requirements-file = True
recipe = anybox.recipe.odoo:server
eggs =
    gevent
    odoorpc
    openupgradelib
    unidecode
    inouk.recipe.odoo_cmd

odoo_scripts =
    command-line-options=-d
    odoo_cmd=odoo-bin

version = git https://github.com/odoo/odoo.git odoo ${odoo:release}

# This directive will nuke local changes, but without it, a branch with merges
# cannot be updated if the main branch has progressed. This will break
# rebuilds. Use this in production and testing, not in development.
vcs-clear-retry = True
git-warn-sha-pins = False

addons =
    git https://github.com/oca/web parts/web ${odoo:release}
    git https://github.com/oca/server-tools parts/server-tools ${odoo:release}

merges =


# Config options
options.http_enable = True
options.http_port = 8169
options.lang = nl_NL
options.limit_time_cpu = 36000
options.limit_time_real = 72000
options.log_handler = :DEBUG,werkzeug:CRITICAL,openerp.service.server:INFO,PIL:INFO
options.log_level = debug
options.logrotate = True
options.longpolling_port = 8170
# options.proxy_mode = True
# options.without_demo = False
options.workers = 4

[versions]
xlwt = 1.3.0
Barometry answered 19/12, 2020 at 11:7 Comment(1)
Where do I put or what do I do with this script? Do I need to install anybox_recipe_odoo as well?Spoils

© 2022 - 2024 — McMap. All rights reserved.