始终使用某些选件/软件包装载Jupyter笔记本电脑 [英] Always have Jupyter notebook load with certain options / packages
问题描述
是否有一种方法可以确保jupyter笔记本始终以以下任意一种开头:
Is there a way to ensure that jupyter notebook always starts with either:
1)某些软件包已导入 和/或 2设置了某些选项
1) Certain packages imported and/or 2 Certain options set
我不想每次在运行的每个笔记本的顶部都键入相同的内容-例如始终使用numpy或pandas.
I don't want to have to type the same things everytime at the the top of each notebook I run - e.g. always using numpy or pandas.
此外,我始终希望每个单元都能看到多个输出.我使用以下代码使它正常工作,但是我希望将其保存为某种模板,不需要我每次手动键入.
Additionally, I always want to be able to see multiple output per cell. I use the following code to let this work just fine, but I want this saved as some sort of template, that doesn't require manual effort from me to type each time.
谢谢!
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
推荐答案
首先,找到startup
文件夹的位置.
First, you find where the startup
folder is located.
# on Jupyter notebook
import IPython
IPython.paths.get_ipython_dir()
在Windows上,响应为u'C:\\Users\\yourname\\.ipython'
,而在Linux ~/.ipython
上.
On Windows, the response is u'C:\\Users\\yourname\\.ipython'
, while on Linux ~/.ipython
.
在该位置,有配置文件文件夹.至少您的计算机上有一个profile_default
.每个配置文件文件夹中都存在一个startup
文件夹.
In that location, there are profile folders. At least, there is a profile_default
on your computer. One startup
folder exists in each profile folder.
您将python脚本文件放在该文件夹中(我的情况是:C:/Users/myname/.ipython/profile_default/startup
).
You put a python script file in that folder (my case: C:/Users/myname/.ipython/profile_default/startup
).
我将脚本文件命名为00-first.py
,并将以下代码放入其中:
I name my script file 00-first.py
, and put this code in it:
import numpy as np
import pandas as pd
当我使用默认配置文件启动Jupyter笔记本服务器时,将在打开Jupyter笔记本之前执行启动脚本.
When I start the Jupyter notebook server with the default profile, the startup script will be executed prior to opening a Jupyter notebook.
在新打开的Jupyter笔记本上,您可以使用numpy和pandas(分别作为np,pd),而无需先导入它们.
On a newly open Jupyter notebook, you can use numpy and pandas (as np, pd) without importing them first.
print(np.pi) #3.141592...
这篇关于始终使用某些选件/软件包装载Jupyter笔记本电脑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!