jupyter notebook使用小技巧

在服务器上配置部署jupyter notebook 服务

我们可以将jupyter notebook的服务部署在服务器上,之后我们就可以通过域名或者ip来使用jupyter notebook服务。

实验环境:

ubuntu16.04
python 3.6.7

下面我们通过以下步骤来安装一个在服务器端运行的jupyter notebook服务,主要步骤如下:

  1. 在服务器上创建一个文件夹,并创建好虚拟环境
    mkdir ~/my_jupyter
    cd ~/myjupyter
    python -m venv venv   # 创建虚拟环境
    source venv/bin/activate
    
  2. 在当前虚拟环境下安装jupyter
    pip install jupyter
    
  3. 生成jupyter配置文件
    jupyter notebook --generate-config
    
    运行完上述命令后,会生成 ~/.jupyter/jupyter_notebook_config.py 的配置文件。
  4. 生成jupyter notebook 的登录密钥,打开python交互式解释器,然后输入以下代码来设置密码
    from notebook.auth import passwd
    passwd()
    # 接下来输入密码即可
    
  5. 修改配置文件:在文件的末尾加上以下代码:
    c.NotebookApp.ip='*'            # 设置任何IP都可以访问
    c.NotebookApp.password = u'设置密码后产生的密文'
    c.NotebookApp.open_browser = False       # 禁止自动打开浏览器
    c.NotebookApp.port =8888   # jupyter notebook的启动端口
    c.NotebookApp.allow_remote_access = True   # 允许远程连接
    
  6. 启动jupyter notebook服务,并将其运行在后台
    sudo nohup jupyter notebook& 
    
  7. 在浏览器中输入http://服务器IP:8888即可访问到jupyter notebook服务。

添加jupyter notebook扩展

  1. 安装扩展所需要的第三方库
    pip install jupyter_contrib_nbextensions
    
  2. 将扩展安装到jupyter
    jupyter contrib nbextension install --user
    
  3. 重启jupyter notebook服务
    nohup jupyter notebook&
    

在jupyter notebook中执行shell命令

使用!前缀可以让jupyter notebook执行shell命令

  • 安装python第三方包
    !pip install requests
    
  • 执行基本shell命令
    ls
    

你可能感兴趣的:(jupyter notebook使用小技巧)