服务器部署在阿里云,想要在服务器内部进行python脚本测试比较麻烦,这时就想到了Jupyter Notebook,咱们安装后便可直接在浏览器进行文学化编程,很是方便高效。
如何使用 Jupyter notebook。Jupyter notebook 是一种 Web 应用,能让用户将说明文本、数学方程、代码和可视化内容所有组合到一个易于共享的文档中。css
Jupyter Notebook 已迅速成为处理数据的必备工具。其用途包括数据清理和探索、可视化、机器学习和大数据分析。我为个人我的博客建立了一个 notebook 示例,它展现了 notebook 的许多特色。这项工做一般在终端中完成,也即便用普通的 Python shell 或 IPython 完成。可视化在单独的窗口中进行,而文字资料以及各类函数和类脚本包含在独立的文档中。可是,notebook 能将这一切集中到一处,让用户一目了然。html
GitHub 上也直接支持 Jupyter notebook 的渲染。借助此出色的功能,你能够轻松地共享工做。http://nbviewer.jupyter.org/ 也会提供 GitHub 代码库中的 notebook ,以及存储在其余地方的 notebook。python
pip install jupyter notebook
jupyter notebook --allow-root # 若是是root用户则须要受权 # jupyter notebook
启动以后咱们能够看到结果:web
(base) [root@css Code]# jupyter notebook --allow-root [I 14:08:27.170 NotebookApp] JupyterLab extension loaded from /root/anaconda3/lib/python3.7/site-packages/jupyterlab [I 14:08:27.170 NotebookApp] JupyterLab application directory is /root/anaconda3/share/jupyter/lab [I 14:08:27.174 NotebookApp] Serving notebooks from local directory: /work/Code [I 14:08:27.174 NotebookApp] The Jupyter Notebook is running at: [I 14:08:27.174 NotebookApp] http://localhost:8888/?token=04f6dcbcca528d3f3d0ca0ff349ebee4d755a872b3c31b42 [I 14:08:27.175 NotebookApp] or http://127.0.0.1:8888/?token=04f6dcbcca528d3f3d0ca0ff349ebee4d755a872b3c42 [I 14:08:27.175 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [W 14:08:27.201 NotebookApp] No web browser found: could not locate runnable browser. [C 14:08:27.201 NotebookApp] To access the notebook, open this file in a browser: file:///root/.local/share/jupyter/runtime/nbserver-58623-open.html Or copy and paste one of these URLs: http://localhost:8888/?token=04f6dcbcca528d3f3d0ca0ff349ebee4d755a872b3c42 or http://127.0.0.1:8888/?token=04f6dcbcca528d3f3d0ca0ff349ebee4d755a872b3c42
咱们能够看到,能够经过http://127.0.0.1:8888 进行访问,若是须要公网IP访问,须要打开8888端口的防火墙:shell
firewall-cmd --zone=public --add-port=8888/tcp --permanent firewall-cmd --reload
经过上边的试了下,内网能够访问,外网还不能访问,还须要再作相关配置。编程
将生成的密钥复制下来vim
(base) [root@css scraper]# ipython Python 3.7.6 (default, Jan 8 2020, 19:59:22) Type 'copyright', 'credits' or 'license' for more information IPython 7.12.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:3c7ece6d01a3:aef0f9818ea49be2c2f2cc5f5a6228fd327ec00d'
1.生成配置文件,输入:centos
jupyter lab --generate-config
配置文件输出路径:浏览器
Writing default config to: /root/.jupyter/jupyter_notebook_config.py
2.打开 jupyter_notebook_config.py 配置文件,修改一下下面几处。服务器
vim /root/.jupyter/jupyter_notebook_config.py
# 将ip设置为*,意味容许任何IP访问 c.NotebookApp.ip = '*' # 这里的密码就是上边咱们生成的那一串 c.NotebookApp.password = 'sha1:3c7ece6d01a3:aef0f9818ea49be2c2f2cc5f5a6228fd327ec00d' # 服务器上并无浏览器能够供Jupyter打开 c.NotebookApp.open_browser = False # 监听端口设置为8888或其余本身喜欢的端口 c.NotebookApp.port = 8888 # 咱们能够修改jupyter的工做目录,也能够保持原样不变,若是修改的话,要保证这一目录已存在 #c.MappingKernelManager.root_dir = '/root/jupyter_run' # 容许远程访问 c.NotebookApp.allow_remote_access = True
三、防火墙开启
咱们再上边已经开启了防火墙,若是没开启记得必定要开启8888这个端口的防火墙,不然公网访问不了。
firewall-cmd --zone=public --add-port=8888/tcp --permanent firewall-cmd --reload
1.启动 Jupyter
jupyter lab --allow-root
2.浏览器输入
打开浏览器,输入http://公网ip地址:8888(例如,我这里输入http://98.126.219.176:8888)
3.结果显示
4.测试
新建一个文件,写一段代码打印当前时间测试:
咱们能够看到新建的文件后缀为.ipynb
,这是notebook特有的文件后缀, 之后咱们就能够经过jupty浏览器写代码了,就像写文章同样方便,这里写好测试好代码以后能够导出python文件便可,很是的方便。
相关文章:
Python 基础八-Jupyter notebook入门学习
云服务centos搭建jupyter notebook并经过外网访问
如何从外网调用内网的jupyter-notebook