栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Anaconda------配置jupyter

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Anaconda------配置jupyter

jupyter的功能和作用无需多讲,根据Anaconda------环境管理中创建的python36,在这里演示一下如何在这个python36中配置jupyter并且在宿主机中访问到。

环境:
虚拟机IP:192.168.43.61
宿主机IP:192.168.43.192
操作系统:centos-release-7-2.1511.el7.centos.2.10.x86_64
虚拟机:VirtualBox 6.1.24
Anaconda版本:Anaconda3-5.2.0-Linux-x86_64

1.进入python36环境
[root@localhost home]# conda activate python36

2.生成配置文件
(python36) [root@localhost home]# jupyter notebook --generate-config --allow-root
Writing default config to: /root/.jupyter/jupyter_notebook_config.py

提示创建的配置文件是/root/.jupyter/jupyter_notebook_config.py
 

3.创建密码
 

打开ipython,生成sha1的密码

(python36) [root@localhost home]# ipython
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from notebook.auth import passwd

In [2]: passwd()
Enter password: 
Verify password: 
Out[2]: 'sha1:dcf803292d2d:fb68c58cb21a9e4a062dd4374cc0b7f4fa17d7df'

In [3]: quit()
(python36) [root@localhost home]# 

需要记住自己输入的密码,这里只会显示加密后的信息,我用的密码是123456.测试呗,无所谓
 

4.生成一个自签名认证的key
 
(python36) [root@localhost home]# openssl req -x509 -nodes -days 365 -newkey rsa:4096 -keyout jkey.key -out jcert.pem
Generating a RSA private key
................................................................++++
....................++++
writing new private key to 'jkey.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:BJ
Locality Name (eg, city) []:bj
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:
(python36) [root@localhost home]# 
5.修改自动生成的配置文件 
vi /root/.jupyter/jupyter_notebook_config.py

 配置内容如下

#就是刚才passwd()指令生成的密钥复制过来
c.NotebookApp.password = 'sha1:dcf803292d2d:fb68c58cb21a9e4a062dd4374cc0b7f4fa17d7df'
#服务运行的端口,我这里指定的是8881
c.NotebookApp.port = 8881

#修改为0.0.0.0可以任意远程IP访问
c.NotebookApp.ip = '0.0.0.0'

#默认不打开浏览器
c.NotebookApp.open_browser = False 
c.NotebookApp.certfile = '/home/conda_udf_data/jupyter/certification/jcert.pem'
c.NotebookApp.keyfile = '/home/conda_udf_data/jupyter/certification/jkey.key'

 切记:上面的参数值一定要放在单引号里面
 jcert.pem和jkey.key是两个认证文件。
因为上面第2步是在home下执行的,因此创建的认证文件也是在home下面,其实这样这样不大安全,应该是事先创建好妥当的目录然后再创建认证文件,现在认证文件已经创建好了,我只能是在/home下重新创建了conda_udf_data/jupyter/certification然后把这俩认证文件移入到这个新的目录中,因此上面的c.NotebookApp.certfile 和 c.NotebookApp.keyfile 的文件路径发生了变化。

6.启动校验

jupyter notebook --allow-root 

可以看到jupyter已经启动成功,在宿主机上访问 https://192.168.43.61:8881/,可以打开jupyter WEB UI

 可以从界面上看到这里有3个目录,再来看看home目录下的情况:
 

(python36) [root@bogon home]# pwd
/home
(python36) [root@bogon home]# ll
total 0
drwxr-xr-x. 2 root root 44 Oct 13 04:44 backup
drwxr-xr-x. 3 root root 20 Oct 14 01:45 conda_udf_data
drwxr-xr-x. 2 root root  6 Oct 14 01:45 jupyter
(python36) [root@bogon home]# 

 可以看到home目录下也是这三个目录,因此可以确定一个事实:在哪个目录下启动了jupyter,那么启动的web UI 上就可以显示哪个个目录下的内容,为了避免保留一些敏感信息,还是建议创建好一个空的目录,比如/home/conda_udf_data/jupyter/jupyer_root,然后再来启动jupyter,最好的办法是在/root/.jupyter/jupyter_notebook_config.py里面设置好工作目录:

c.NotebookApp.notebook_dir = '/home/conda_udf_data/jupyter/jupyer_root'

那么完整的设置是这样:
 

#就是刚才passwd()指令生成的密钥复制过来
c.NotebookApp.password = 'sha1:dcf803292d2d:fb68c58cb21a9e4a062dd4374cc0b7f4fa17d7df'

#服务运行的端口,我这里指定的是8881
c.NotebookApp.port = 8881

#修改为0.0.0.0可以任意远程IP访问
c.NotebookApp.ip = '0.0.0.0'

#默认不打开浏览器
c.NotebookApp.open_browser = False 
c.NotebookApp.certfile = '/home/conda_udf_data/jupyter/certification/jcert.pem'
c.NotebookApp.keyfile = '/home/conda_udf_data/jupyter/certification/jkey.key'

#设置工作路径
c.NotebookApp.notebook_dir = '/home/conda_udf_data/jupyter/jupyer_root'

我先在/home/conda_udf_data/jupyter/jupyer_root下创建一个文件,比如
ip addr >ip_addr.txt
 

[root@bogon jupyer_root]# ll
total 4
-rw-r--r--. 1 root root 783 Oct 14 02:48 ip_addr.txt
[root@bogon jupyer_root]# pwd
/home/conda_udf_data/jupyter/jupyer_root
[root@bogon jupyer_root]# 

这个文件已经存在了,然后返回的到home后启动jupyter

(python36) [root@bogon jupyter]# cd /home/
(python36) [root@bogon home]# ll
total 0
drwxr-xr-x. 2 root root 44 Oct 13 04:44 backup
drwxr-xr-x. 3 root root 20 Oct 14 01:45 conda_udf_data
drwxr-xr-x. 2 root root  6 Oct 14 01:45 jupyter
(python36) [root@bogon home]# jupyter notebook --allow-root
[I 02:55:12.297 NotebookApp] JupyterLab beta preview extension loaded from /usr/lib64/anaconda3/lib/python3.6/site-packages/jupyterlab
[I 02:55:12.297 NotebookApp] JupyterLab application directory is /usr/lib64/anaconda3/share/jupyter/lab
[I 02:55:12.301 NotebookApp] Serving notebooks from local directory: /home/conda_udf_data/jupyter/jupyer_root
[I 02:55:12.302 NotebookApp] 0 active kernels
[I 02:55:12.302 NotebookApp] The Jupyter Notebook is running at:
[I 02:55:12.302 NotebookApp] https://bogon:8881/
[I 02:55:12.302 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip /confirm/iation).

在宿主机中查看https://192.168.43.61:8881

 可以看到这次启动后看到的根目录就是指定的目录。
OK,大工搞成

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/323612.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号