jupyter:python可视化

2019-03-19 77691250 0

安装

  1. pip install jupyter
  2. jupyter notebook --generate-config

使用密码登录

如果放在服务器上运行,需要使用密码登录,首先生成密码

  1. from notebook.auth import passwd
  2. passwd()

保存在配置文件jupyter_notebook_config.py

  1. import os
  2. c = get_config()
  3. c.NotebookApp.password = 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'

生成ssl证书

因为Jupyter必须要用https进行登录,所以需要生成ssl证书。

  1. openssl req -x509 -nodes -days 3650 -newkey rsa:1024 -keyout ~/.jupyter/mykey.key -out ~/.jupyter/mycert.pem

更改Jupyter的配置文件中的相关字段。

  1. c.NotebookApp.certfile = os.path.expanduser('~/.jupyter/mycert.pem')
  2. c.NotebookApp.keyfile = os.path.expanduser('~/.jupyter/mykey.key')

其他设置

  1. c.NotebookApp.ip = '*'
  2. c.NotebookApp.port = 8888
  3. c.NotebookApp.open_browser = False

安装Jupyter插件管理器

  1. pip install jupyter_contrib_nbextensions jupyterlab

jupyterlab的目的是增强jupyter的编辑能力,使jupyter如IDE一般强大。

运行:jupyter-lab

在腾讯云主机上运行jupyter会报如下错误:

  1. OSError: [Errno 99] Cannot assign requested address

启动时指定ip为0.0.0.0即可:

  1. jupyter-lab --ip=0.0.0.0

显示matplotlib图片

  1. 第一种,在代码前面加一句:%matplotlib inline

  2. 第二种,使用ipython profile create生成配置,修改ipython_config.py配置文件

    1. c.InteractiveShellApp.matplotlib = 'inline'