[英]How do I have my Jupyter notebook server run arbitrary Python code before running notebook code?
I'm trying to replicate the functionality of the code editor on a platform I was previously using called Odoo.sh.我正在尝试在我以前使用的名为 Odoo.sh 的平台上复制代码编辑器的功能。 The platform would let me create a
.ipynb
notebook, but in the cells I could reference pre-set variables which required no boilerplate code inside of the notebook.该平台可以让我创建一个
.ipynb
笔记本,但在单元格中,我可以引用不需要笔记本内部样板代码的预设变量。 Extremely convenient.非常方便。
If you're familiar with Odoo, it was like having odoo-bin shell
be implicitly run before executing any of the cells inside the notebook.如果您熟悉 Odoo,这就像在执行笔记本内的任何单元格之前隐式运行
odoo-bin shell
一样。 It was wonderful to work with, but Odoo.sh is proprietary, so I'm trying to replicate the same functionality on my local machine.使用起来很棒,但 Odoo.sh 是专有的,所以我试图在我的本地机器上复制相同的功能。
A minimal example of what I'm going for here would be to have the following python code run before executing any of my .ipynb
notebook file's cells.我在这里要做的一个最小的例子是在执行我的任何
.ipynb
笔记本文件的单元格之前运行以下 python 代码。
example_value = False
def example_func():
global example_value
example_value = True
example_func()
So that inside of any notebook's cells I could simply run something like example_value
and get an output of True
.因此,在任何笔记本的单元格内,我都可以简单地运行类似
example_value
并获得True
的输出。
In the case of Odoo.sh it almost seemed like there was a special custom kernel set up that was nothing more than a regular Python 3 kernel with some initialization code.在 Odoo.sh 的情况下,它似乎有一个特殊的自定义内核设置,只不过是带有一些初始化代码的常规 Python 3 内核。 This may be exactly what was going on, but I don't know enough about how Jupyter works to know for myself.
这可能正是正在发生的事情,但我对 Jupyter 的工作原理知之甚少,无法亲自了解。 How do I replicate this functionality?
如何复制此功能?
I figured it out!我想到了! You need to create a custom kernel, but for this use case you can just reuse the default IPython kernel and just pass some variables into the user namespace.
您需要创建一个自定义内核,但对于这个用例,您可以只重用默认的 IPython 内核并将一些变量传递到用户命名空间中。
First, create a Python file for your kernel.首先,为您的内核创建一个 Python 文件。 Let's use
test_kernel.py
.让我们使用
test_kernel.py
。 Here are the contents:以下是内容:
from ipykernel.ipkernel import IPythonKernel
from ipykernel.kernelapp import IPKernelApp
if __name__ == "__main__":
example_value = False
def example_func():
global example_value
example_value = True
example_func()
IPKernelApp.launch_instance(
kernel_class=IPythonKernel,
user_ns={"example_value": example_value})
See how the arbitrary code from the question is run before launching the kernel instance.在启动内核实例之前查看问题中的任意代码是如何运行的。 Using the
user_ns
argument, we can pass arbitrary data to the user environment.使用
user_ns
参数,我们可以将任意数据传递给用户环境。
To get our kernel up and running we need to make a test
directory and then a test/kernel.json
file.为了让我们的内核启动并运行,我们需要创建一个
test
目录,然后创建一个test/kernel.json
文件。 It will have these contents:它将包含以下内容:
{
"argv": ["python", "-m", "test_kernel", "-f", "{connection_file}"],
"display_name": "Test"
}
Let's install that bad boy.让我们安装那个坏男孩。 Run
jupyter kernelspec install --user test
.运行
jupyter kernelspec install --user test
。 In that command, test
is the name of the directory we created.在该命令中,
test
是我们创建的目录的名称。 The --user
argument makes Jupyter install the kernel only for the current user. --user
参数使 Jupyter 仅为当前用户安装内核。 You don't have to use it if you don't want to.如果您不想,则不必使用它。
Now we should be good to go!现在我们应该可以出发了! Start things up with
jupyter notebook
and you will see your new kernel is available to use when using notebooks.使用
jupyter notebook
启动,你会看到你的新内核在使用 notebooks 时可用。 And check it out, we can see the variable we passed into the namespace:并查看一下,我们可以看到我们传入命名空间的变量:
Last of all, be sure to note that in order for this to work your test_kernel.py
file will need to be somewhere where Python can import it.最后,请务必注意,为了使其正常工作,您的
test_kernel.py
文件需要位于 Python 可以导入它的地方。 I'm not an expert on this, but from a bit of Googling I took this to mean that the directory containing the file should either be the current working directory or be in your PATH
.我不是这方面的专家,但是从一些谷歌搜索中我认为这意味着包含文件的目录应该是当前工作目录或在您的
PATH
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.