conda下安装jupyter notebook

1、配置虚拟环境

首先按照《Conda常用命令》教程创建需要的虚拟环境,譬如basedeep。分别切换到这两个环境下执行以下命令。

下面名为deep的虚拟环境为例。

切换conda环境到deep

conda activate deep

deep环境安装装ipykernel

conda install ipykernel

添加虚拟环境到jupyter notebook

# python -m ipykernel install --user --name 环境名称 --display-name "在jupyter中显示的环境名称"
python -m ipykernel install --user --name deep --display-name "deep"

# 注意对于 base采用下面的命令: 
python -m ipykernel install --user --display-name "Python 3"

虚拟环境的配置信息在这个目录:/home/xp/.local/share/jupyter/kernels

2、设置密码

# 首先运行下面的命令生成配置文件
jupyter-notebook --generate-config

# 运行下面命令,并按照提示设置密码
jupyter-notebook password

3、优化notebook

默认情况下,notebook的网页时窄版的,两边存在很大的空白,以至于写代码的时候每行只能显示很少内容。我们可以调整样式让noebook以全屏方式显示。实现这个目的只需要创建文件/home/xp/.jupyter/custom/custom.css

在文件中添加以下内容。

/*This file contains any manual css for this page that needs to override the global styles.
This is only required when different pages style the same element differently. This is just
a hack to deal with our current css styles and no new styling should be added in this file.*/

.container { width:98% !important; }

#ipython-main-app {
    position: relative;
}
#jupyter-main-app {
    position: relative;
}

3、notebook配置工具

大家可以通过安装nbextension来方便为notebook增加更多高级功能,譬如自动完成等。只需在base环境下运行一下命令:

conda install -c conda-forge jupyter_contrib_nbextensions

 

最后运行jupyter notebook --port 8001 --no-browser --notebook-dir /data/xp/code

并在浏览器中运行http://127.0.0.1:8001开始jupyter notebook之旅吧。

 

发表评论

匿名网友