[关闭]
@myles 2019-01-08T20:03:46.000000Z 字数 3903 阅读 900

python 之工具收集

未分类


一、jupyter notebook 应用

1、jupyter 介绍

简而来说,Jupyter Notebook 就是以网页的形式打开,我们可以在网页页面中直接编写代码和运行代码,代码的运行结果也会直接在代码块下显示的程序。如在编程过程中需要编写说明文档,也可在同一个页面中直接编写,便于作及时的说明和解释。

2、jupyter 安装

(1)安装环境准备

(2)安装jupyter

这里必须要说下 pip3 更新,为什么要记录pip版本更新,因为踩到坑了,所有要记录下。

  1. # python3 -m pip3 install --upgrade pip
  2. # 注意:这里更新完pip3后,pip3就不可用了(坑:会报错),后面直接使用pip install jupyter即可完安装。
  1. # python3 -m pip install jupyter

3、jupyter 的使用

开启 jupyer notebook 服务

4、jupyter 命令行管理

这里要重点说下的notebook服务管理,我们可以在系统层面基于命令进行直接配置管理的,比起早先版本的配置文件管理更为人性化了,这里会重点说下3个命令:

(1)命令行帮助

  1. root@Docker_Lab_01:/home/tmp# jupyter notebook --help
  2. The Jupyter HTML Notebook.
  3. 这将启动一个基于tornadoHTML笔记本服务器,它提供一个html5/javascript笔记本客户端。
  4. Subcommands
  5. -----------
  6. Subcommands are launched as `jupyter-notebook cmd [args]`. For information on
  7. using subcommand 'cmd', do: `jupyter-notebook cmd -h`.
  8. stop
  9. Stop currently running notebook server for a given port
  10. password
  11. Set a password for the notebook server.
  12. list
  13. 列出当前运行的Notebook服务.
  14. Options
  15. -------
  16. Arguments that take values are actually convenience aliases to full
  17. Configurables, whose aliases are listed on the help line. For more information
  18. on full configurables, see '--help-all'.
  19. -y
  20. Answer yes to any questions instead of prompting.
  21. --debug
  22. set log level to logging.DEBUG (maximize logging output)
  23. --no-script
  24. DEPRECATED, IGNORED
  25. --allow-root
  26. 允许notebookroot用户下运行.
  27. --no-browser
  28. 在启动服务以后不在浏览器中打开一个窗口.
  29. --script
  30. DEPRECATED, IGNORED
  31. --generate-config
  32. generate default config file
  33. --pylab
  34. DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
  35. --no-mathjax
  36. Disable MathJax
  37. MathJax is the javascript library Jupyter uses to render math/LaTeX. It is
  38. very large, so you may want to disable it if you have a slow internet
  39. connection, or for offline use of the notebook.
  40. When disabled, equations etc. will appear as their untransformed TeX source.
  41. --certfile=<Unicode> (NotebookApp.certfile)
  42. Default: ''
  43. SSL/TLS 认证文件所在全路径.
  44. --port-retries=<Int> (NotebookApp.port_retries)
  45. Default: 50
  46. 如果指定的端口不可用,则要尝试其他端口的数量.
  47. --port=<Int> (NotebookApp.port)
  48. Default: 8888
  49. notebook服务会监听的IP端口.
  50. --transport=<CaselessStrEnum> (KernelManager.transport)
  51. Default: 'tcp'
  52. Choices: ['tcp', 'ipc']
  53. --ip=<Unicode> (NotebookApp.ip)
  54. Default: 'localhost'
  55. notebook服务会监听的IP地址.
  56. --config=<Unicode> (JupyterApp.config_file)
  57. Default: ''
  58. Full path of a config file.
  59. --client-ca=<Unicode> (NotebookApp.client_ca)
  60. Default: ''
  61. 用于ssl/tls客户端身份验证的证书颁发证书的完整路径.
  62. --keyfile=<Unicode> (NotebookApp.keyfile)
  63. Default: ''
  64. SSL/TLS 私钥文件所在全路径.
  65. --log-level=<Enum> (Application.log_level)
  66. Default: 30
  67. Choices: (0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL')
  68. Set the log level by value or name.
  69. --pylab=<Unicode> (NotebookApp.pylab)
  70. Default: 'disabled'
  71. DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
  72. --notebook-dir=<Unicode> (NotebookApp.notebook_dir)
  73. Default: ''
  74. 用于笔记本和内核的目录。
  75. --browser=<Unicode> (NotebookApp.browser)
  76. Default: ''
  77. Specify what command to use to invoke a web browser when opening the
  78. notebook. If not specified, the default browser will be determined by the
  79. `webbrowser` standard library module, which allows setting of the BROWSER
  80. environment variable to override it.
  81. To see all available configurables, use `--help-all`
  82. Examples
  83. --------
  84. jupyter notebook # start the notebook
  85. jupyter notebook --certfile=mycert.pem # use SSL/TLS certificate
  86. jupyter notebook password # enter a password to protect the serve

(2)密码设置

初次远程登录需要附带token密码进行访问,或输入密码进行登录访问,密码的设置,直接在开启服务器以下命令进行密码设置即可。

  1. # 直接使用如下的命令行,即可完成notebook远程登录密码的设置
  2. root@Docker_Lab_01:/home/tmp# jupyter notebook password
  3. Enter password:
  4. Verify password:
  5. [NotebookPasswordApp] Wrote hashed password to /root/.jupyter/jupyter_notebook_config.json
  6. # 密码文件查看,可以看到已经为 notebook添加好密码
  7. root@Docker_Lab_01:/home/iflytek/tmp# cat /root/.jupyter/jupyter_notebook_config.json
  8. {
  9. "NotebookApp": {
  10. "password": "sha1:c2b02dd5dc21:d563ad9b691d7f8c5374738d9003c035b654d40b"
  11. }

(3)开启远程访问

jupyter notebook 应用服务默认只能在本地基于浏览器访问交互式服务,如果要开启远程访问权限,需要在启动服务试开启远程接口监听,具体方法如下。

  1. # jupyter notebook --ip=0.0.0.0 --allow-root

在开启notebook 服务时,设定开启服务的监听端口为 0.0.0.0 即可开启远程浏览器访问:http://notebook_host:8888

学习参考:https://zhuanlan.zhihu.com/p/33105153

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注