简体   繁体   English

在运行笔记本代码之前,如何让我的 Jupyter 笔记本服务器运行任意 Python 代码?

[英]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.

相关问题 如何在每个 jupyter notebook 内核之前运行 Python 代码 - How to run Python code before every jupyter notebook kernel 死 Kernel 在 Jupyter Notebook 中运行代码,Python 3 - Dead Kernel on running code in Jupyter Notebook, Python 3 我可以在 GPU 上运行包含 seaborn 代码的 jupyter notebook 吗? - Can I run my jupyter notebook that contains seaborn code on GPU? 在 Jupyter Notebook 中运行 Python 代码时出现问题:GoodReadsScraper - Problem running Python code in Jupyter Notebook: GoodReadsScraper Python:使用 Jupyter Notebook 运行代码(在线) - Python: Running Code using Jupyter Notebook (Online) 我的代码在 Jupyter Notebook 中工作,但不是 Python 脚本 - My code works in Jupyter Notebook but not as Python script 如何在 Python Jupyter Notebook 的按钮中编码“重新启动内核并全部运行”? - How to code "Restart Kernel and Run all" in button for Python Jupyter Notebook? "如何每天自动运行带有 Python 代码的 Jupyter Notebook?" - How to run a Jupyter notebook with Python code automatically on a daily basis? 如何在 Jupyter Notebook 中运行 GitHub 代码? - How to run GitHub code in a Jupyter Notebook? 如何将代码从 jupyter notebook 共享到安装了 python3 的服务器? - How to share code from jupyter notebook to server installed with python3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM