使用特定的类/函数预加载 Jupyter Notebook [英] Preloading the Jupyter Notebook with specific classes/functions

查看:31
本文介绍了使用特定的类/函数预加载 Jupyter Notebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我在另一个文件中定义的特定类/函数预加载一个笔记本.更具体地说,我想用 python(类似于加载配置文件,包含所有相关的类/函数)来做到这一点.目前,我正在使用 python 生成笔记本并在服务器上自动启动它们.因为不同的配置文件"需要这些notebook,所以不需要专门导入就需要不同的类和函数.

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 notebook 时,我只需将 jupyter notebook 模板"复制到特定文件,并让用户自动打开它.我可以在

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.py99-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 notebook 上运行魔法命令:

To get the location_of_ipython_profile, one can run magic command on Jupyter notebook:

!ipython locate profile

就我而言,我得到 C:Usersswatc.ipythonprofile_default.要列出 startup 文件夹中的所有文件,我运行:

In my case, I get C:Usersswatc.ipythonprofile_default. To list all the files in the startup folder, I run:

!dir C:Usersswatc.ipythonprofile_defaultstartup*.*

请记住,您的计算机上可以有多个 python 配置文件.以上所有内容均适用于默认配置文件.

Remember that there can be several python profiles on your computer. All the above is valid for the default profile.

希望这会有所帮助.

这篇关于使用特定的类/函数预加载 Jupyter Notebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆