为Jupyter Notebook预加载特定的类/功能 [英] Preloading the Jupyter Notebook with specific classes/functions
问题描述
我想预装一个笔记本中具有在其他文件中定义的特定类/功能的笔记本.更具体地说,我想使用python(类似于加载配置文件,其中包含所有相关的类/函数)进行此操作.目前,我正在使用python生成笔记本,并在服务器上自动启动它们.由于不同的配置文件"需要这些笔记本,因此它们需要不同的类和功能,而无需专门导入它们.
I'd like to preload a notebook with specific classes/functions that I've defined in another file. More specifically, I'd like to do this with python (something like loading a profile, containing all relevant classes/functions). Currently, I'm generating notebooks with python and starting them automatically on the server. Because different "profiles" need these notebooks, they need different classes and functions without specifically importing them.
在生成jupyter笔记本时,我只需将jupyter笔记本模板"复制到特定文件中,然后让用户自动打开它. 我可以在其中设置某种配置文件吗?
When generating a jupyter notebook, I simply copy a jupyter notebook "template" to a specific file and let the user open this automatically. Can I set some sort of profile in
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3",
"profile": "lab_5" // <--
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
其中"lab_5"将预加载必要的类?
where "lab_5" would preload the necessary classes?
推荐答案
启动Jupyter Notebook(或实验室)时,如果其中包含python脚本文件
When you start Jupyter notebook (or lab), if you have python script file(s) in
location_of_ipython_profile/startup/
然后它们将在每次IPython启动时运行(用作标准ipython内核).例如,在我的机器上,我有一个名为00-first.py
的文件,它的内容如下:
then they will run on every IPython startup (to serve as standard ipython kernel). For example, on my machine I have a file named 00-first.py
and it has simple content as follows:
import numpy as np
如果还有其他脚本,例如50-middle.py
和99-last.py
,它们也会连续运行.
If there are other scripts, say, 50-middle.py
and 99-last.py
, they also run consecutively.
当我使用ipython内核打开一个新的Jupyter笔记本时.我将根据这些脚本文件预先执行许多操作.对于第一个脚本00-first.py
,它提供了符号np
,可以立即使用.因此,我可以运行print(np.pi)
并获取3.141592...
作为输出.
When I open a new Jupyter notebook with ipython kernel. I will have many thing pre-executed according to those script files. For the first script, 00-first.py
, it provides the symbol np
to use right away. So, I can run print(np.pi)
and get 3.141592...
as output.
要获取location_of_ipython_profile
,可以在Jupyter笔记本上运行magic命令:
To get the location_of_ipython_profile
, one can run magic command on Jupyter notebook:
!ipython locate profile
就我而言,我得到C:\Users\swatc\.ipython\profile_default
.要列出startup
文件夹中的所有文件,我运行:
In my case, I get C:\Users\swatc\.ipython\profile_default
. To list all the files in the startup
folder, I run:
!dir C:\Users\swatc\.ipython\profile_default\startup\*.*
请记住,您的计算机上可能有多个python配置文件.以上所有内容均对默认配置文件有效.
Remember that there can be several python profiles on your computer. All the above is valid for the default profile.
希望这会有所帮助.
这篇关于为Jupyter Notebook预加载特定的类/功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!