Odoo 是一款一体化管理软件,提供一系列业务应用程序,形成一整套针对各种规模公司的企业管理应用程序。 Odoo 是一款一体化商业软件,包括 CRM、网站/电子商务、计费、会计、制造、仓库和项目管理以及库存。
社区版是开源版本,而企业版则为社区版补充了商业功能和服务。
通常我会启动一个 Python 终端并加载 Odoo 环境,如下所示:
/odoo_path/odoo-bin --addons-path=/addons_path shell --config=/config_path/.odoo.conf -d database_name
在控制台内我可以调用
self
,并且环境变量可用(self.env
)。我可以在连接的数据库上运行一些命令。如果我想做一些快速测试,这非常有用。但如果我想运行一些小脚本或测试一些代码片段,那就有点不方便了。 Jupyter Notebooks 和 Jupyter Lab 是非常好的工具。
Jupyter 是一个为编程、数学和数据科学生成基于浏览器的交互式环境的项目。它通过插件(“内核”)支持多种语言,例如 Python、Ruby、Haskell、R、Scala 和 Julia。
Jupyter Notebook是传统且最稳定的应用程序。 JupyterLab具有新的界面,更适合处理由多个文件组成的大型项目。 JupyterLab 自 2018 年 2 月起处于测试阶段。
(来源:windows.net)
我想要实现的是将 Odoo 环境加载到 Jupyter Notebook 中,并使用这些代码片段而不是简单的 Python 终端。
所以我想知道是否有人知道这是否可行或者哪个是一个好的起点。有更好的选择吗?这是一个愚蠢的想法吗?
步骤1.在odoo环境中安装jupyter pip 安装 jupyter
步骤 2。 启动 odoo shell 并从 python 提示符下打开 jupyter
>>
from notebook.notebookapp import NotebookApp
import tornado.ioloop
app = NotebookApp()
app.initialize(["--ip=0.0.0.0"])
tornado.ioloop.IOLoop.current().start()
步骤3。 通过发送 CTRL+C 到 python 控制台来获取令牌
你会看到类似的东西
http://(<hostname> or 127.0.0.1):8888/?token=9d150bd69e908df9a5e30157b530624536fe0c84d8804f17
步骤 4。 打开浏览器
http://localhost:8888/?token=9d150bd69e908df9a5e30157b530624536fe0c84d8804f17
现在您位于 Jupyter 控制台或 Web 界面中
要连接odoo,您需要设置系统路径,然后连接
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
请找到附件中的最小 odoo 构建脚本
[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
花了我整个晚上。感谢 GPT 的指导和有趣的解决方案。
我刚刚成功地在 Docker 容器中的本地环境中使用 Odoo 17(终于!!!)和 Jupyter 笔记本很好地运行了这个:
Dockerfile 中的新行在带有
EXPOSE 8069 8071 8072
的行之前:
RUN pip3 install --upgrade pip setuptools wheel jupyter ipykernel
Odoo17 Jupyter 内核:
{
"argv": [
"python",
"/wrapper.py",
"-f",
"{connection_file}"
],
"display_name": "Odoo 17",
"language": "python",
"metadata": {
"debugger": true
}
}
自定义包装器/启动器主要从
odoo.cli.shell
复制粘贴,将ipykernel安装到容器内的/usr/local/share/jupyter/kernels/odoo17/kernel.json
:
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()
Docker-compose (Swarm) 配置的一部分:
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
记得用以下内容更新 jupyter_notebook_config.py:
c.ServerApp.ip = '0.0.0.0' # this is important!
c.ServerApp.notebook_dir = '/mnt/extra-addons/' # (optional)
就像魅力一样🥹