文章目录
- 前言
- [1. Python环境安装](#1. Python环境安装)
- [2. Jupyter 安装](#2. Jupyter 安装)
- [3. 启动Jupyter Notebook](#3. 启动Jupyter Notebook)
- [4. 远程访问](#4. 远程访问)
-
- [4.1 安装配置cpolar内网穿透](#4.1 安装配置cpolar内网穿透)
- [4.2 创建隧道映射本地端口](#4.2 创建隧道映射本地端口)
- [5. 固定公网地址](#5. 固定公网地址)
前言
Jupyter Notebook,它是一个交互式的数据科学和计算环境,支持多种编程语言,如Python、R、Julia等。它在数据科学、机器学习、深度学习、教育和文档编写等领域都有很广泛的应用。
重要的是,Jupyter Notebook可以在Web浏览器中使用,所以我们可以在服务器端开启服务,在外通过网页远程访问运行代码进行数据分析,拥有一个可以随时随地使用的科学计算环境,而无需在本地安装配置Python环境。
1. Python环境安装
在windows安装python环境
https://www.python.org/downloads/windows/

2. Jupyter 安装
打开cmd命令行,输入:
shell
pip install jupyter

3. 启动Jupyter Notebook
Jupyter Notebook是一个开源的web应用程序,可以使用它来创建和共享包含实时代码、方程、可视化和文本的文档。同时也是IPython项目的一个派生项目,IPython项目本身曾经有一个IPython Notebook项目。后来从中拆分出去,叫做Jupyter Notebook。
启动Jupyter Notebook
shell
jupyter notebook

启动后会自动跳转到浏览器打开界面,如没有跳转,输入窗口出现的ip地址即可

4. 远程访问
这里我们使用cpolar来进行内网穿透,支持http/https/tcp协议,不限制流量,无需公网IP,也不用设置路由器,使用简单。
4.1 安装配置cpolar内网穿透
cpolar官网:https://www.cpolar.com/
访问cpolar官网,注册一个账号,然后下载并安装客户端,具体安装教程可以参考官网文档教程。
- windows系统:在官网下载安装包后,双击安装包一路默认安装即可。
- linux系统:支持一键自动安装脚本,详细请参考官网文档------入门指南

4.2 创建隧道映射本地端口
cpolar安装成功后,在浏览器上访问本地9200端口【http://localhost:9200】,使用cpolar账号登录。

点击左侧仪表盘的隧道管理------创建隧道,创建一个8888端口 http隧道,因为jupyter notebook默认是8888端口
- 隧道名称:可自定义命名,注意不要与已有的隧道名称重复
- 协议:选择http
- 本地地址:8888
- 域名类型:免费选择随机域名
- 地区:选择China vip
点击创建

隧道创建成功后,点击左侧的状态------在线隧道列表,查看所生成的公网地址,然后复制地址

打开浏览器器,我们使用上面公网地址进行连接,会出现以下问题,这个问题是jupyter notebook限制ip访问

解决方法:
先关闭cmd运行窗口,打开一个新的窗口,执行以下命令,生成一个配置文件,注意记住配置文件路径
shell
jupyter notebook --generate-config

设置访问密码
shell
jupyter notebook password

进入上面的json文件路径,打开jupyter_notebook_config.json配置文件,复制密文

然后打开上面jupyter_notebook_config.py配置文件,添加以下参数,添加后保存
shell
c.NotebookApp.ip='*' #允许访问的IP地址,设置为*代表允许任何客户端访问
c.NotebookApp.password = u'argon2:$argon2id$v=19$m=10240,t=10,p=8$QcZlyMBu9icwhDDiUl+lZw$y4EP3WI4gZjc'#刚才生成密码时上面复制的密文
c.NotebookApp.open_browser = False
c.NotebookApp.port =8888 #可自行指定一个端口, 访问时使用该端口
c.NotebookApp.allow_remote_access = True

然后cmd再次启动

打开浏览器器,我们再次使用上面公网地址进行连接,出现密码,输入上面设置的密码登录即可

5. 固定公网地址
由于以上使用cpolar所创建的隧道使用的是随机公网地址,24小时内会随机变化,不利于长期远程访问。因此我们可以为其配置二级子域名,该地址为固定地址,不会随机变化【ps:cpolar.cn已备案】
注意需要将cpolar套餐升级至基础套餐或以上,且每个套餐对应的带宽不一样。【cpolar.cn已备案】
保留一个二级子域名
登录cpolar官网,点击左侧的预留,选择保留二级子域名,设置一个二级子域名名称,点击保留,保留成功后复制保留的二级子域名名称

保留成功后复制保留的二级子域名地址

访问 http://127.0.0.1:9200/,登录cpolar web UI管理界面,点击左侧仪表盘的隧道管理------隧道列表,找到所要配置的8888隧道,点击右侧的编辑

修改隧道信息,将保留成功的二级子域名配置到隧道中
- 域名类型:选择二级子域名
- Sub Domain:填写保留成功的二级子域名
点击更新

更新完成后,打开在线隧道列表,此时可以看到公网地址已经发生变化,地址名称也变成了保留过的二级子域名名称,将其复制下来

然后使用固定http地址打开浏览器访问,登录成功,访问成功
