notes修改宿主服务器,Jupyter Notebook远程访问服务器,并修改Jupyter Notebook主题

一、Jupyter Notebook远程访问服务器

记录一下,用采用jupyter远程连接服务器,然后在本地的notebook上面调代码:

step1: 首先输入ipython生成秘钥

在这个步骤里面,需要自己设定一下本地登录jupyter notebook的密码:

$ ipython

In [1]: from notebook.auth import passwd

In [2]: passwd()

Enter password:

Verify password:

Out[2]: 'sha1:f4eeddfa2f16:9ac17aa43b249400e313851bf95efea2a800e5fe'

In [3]: exit()

step2:生成一个jupyter notebook的config文件,注意这里命令中--个数,generate前面是两个-,config前面就是一个-。

$ jupyter notebook --generate-config

step3:修改配置文件config.py

$ vim ~/.jupyter/jupyter_notebook_config.py

在该配置文件里面加上

...省略了前面的配置内容

#------------------------------------------------------------------------------

# KernelSpecManager(LoggingConfigurable) configuration

#------------------------------------------------------------------------------

## If there is no Python kernelspec registered and the IPython kernel is

# available, ensure it is added to the spec list.

#c.KernelSpecManager.ensure_native_kernel = True

## The kernel spec class. This is configurable to allow subclassing of the

# KernelSpecManager for customized behavior.

#c.KernelSpecManager.kernel_spec_class = 'jupyter_client.kernelspec.KernelSpec'

## Whitelist of allowed kernel names.

#

# By default, all installed kernels are allowed.

#c.KernelSpecManager.whitelist = set()

#

c.NotebookApp.ip='*'

c.NotebookApp.password = u'sha1:f4eeddfa2f16:9ac17aa43b249400e313851bf95efea2a800e5fe'

c.NotebookApp.open_browser = False

c.NotebookApp.port =2333 # 设置端口

step4:启动jupyter notebook

$ jupyter notebook

启动之后在游览器上面输入http://服务器地址:端口号

http://168.43.2.14:23333

然后会提醒你输入第一步里面设置的密码,输入之后就进去了:

ac1fd4d0fb3e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

之后就可以愉快的在本地的jupyter notebook上面愉快的进行远程的操作服务器了。

二、并修改Jupyter Notebook主题

长时间看白色的jupyter notebook有点伤眼睛,所以想办法将jupyter notebook的背景颜色给改调整了一下,具体的步骤如下:

step1:在终端输入:

pip install --upgrade jupyterthemes

顾名思义,该命令就是来给jupyter notebook给安装一些主题的,安装完毕了之后

step2:我们可以看看所有的主题是什么

jt -l

上述命令就是显示出我们可以利用的主题,总共有几个主题如下:

Available Themes:

chesterish

grade3

gruvboxd

gruvboxl

monokai

oceans16

onedork

solarizedd

solarizedl

step 3: 选取主题

下面以例子来说明选取主题,假如我选取的主题是monokai

jt -t monokai chesterish

使用上述命令之后,在终端输入

jupyter notebook

这个时候,你就会发现,jupyter notebook的主题颜色发生了改变。

ac1fd4d0fb3e?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

具体还有很多的参数可以设置,可以参考GitHub上面的配置方式https://github.com/dunovank/jupyter-themes

在安装过程中,也遇到了一个小问题:

KeyError: 'allow_remote_access'

解决之道,参考参考资料第二个博客,看了解决方法之后,就明白配置出现了一点问题,只要在jupyter_notebook_config.py里面加上一行代码就能搞定了

vim ~/.jupyter/jupyter_notebook_config.py

在jupyter_notebook_config.py里面加上一下的命令,问题就解决了

c.NotebookApp.allow_remote_access = True

好了,先就写到这里了,想要换个jupyter notebook风格的小伙伴,赶紧行动起来把。

三、Jupyter Notebook中的一些常用的魔法命令

3.1 %run

目录树

└── test

└── hello.py

我们在jupyter notebook中运行hello.py文件,采用的是相对的路径的写法。

%run test/hello.py

# 结果就是hello world!了。

3.2 %timeit

这个魔法命令就是用来测试运行一行代码所要消耗的时间了

3.3 %time

这个魔法命令就是用来测试运行一个模块代码所要消耗的时间了

其他的魔法命令见%lsmagic