Ubuntu 远程 Jupyter 配置

Ubuntu 远程 Jupyter 配置

每次上课都要重新部署环境,最近看到阿里云的大学生优惠活动,就着手了一台云服务器,于是就把环境部署在上面了。

环境:阿里云 Ubuntu 16.04 64位

新建普通用户

su root                           # 切换到管理员用户
useradd -r -m -s /bin/bash 新用户名     # 新建用户
passwd 新用户名                    # 设置新用户密码
su 新用户名                       # 切换使用新用户

云主机添加安全组

云服务器默认只开启了几个端口,我们要手动增加开放的端口,Jupyter 默认使用的是 8888 端口,具体操作看服务器提供商的文档。

安装、配置 Anaconda

为了方便下载、更新我们使用清华镜像

cd ~
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.0-Linux-x86_64.sh
sudo chmod 776 Anaconda3-5.3.0-Linux-x86_64.sh
sudo ./Anaconda3-5.3.0-Linux-x86_64.sh

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

按照提示进行安装,测试执行 conda --version

如果报错需配置环境变量,编辑 /etc/environment,添加 :/home/***/anaconda3/bin

配置 jupyter notebook 远程访问

  • 使用命令生成配置文件

    jupyter notebook --generate-config

  • 利用 IPython 生成密钥

    In [1]: from notebook.auth import passwd
    In [2]: passwd()
    Enter password:
    Verify password:
    Out[2]: 'sha1:********************************'
  • 编辑生成的配置文件 ~\.jupyter/jupyter_notebook_config.py

    # 限定可以访问的ip
    c.NotebookApp.ip= '*'
    # 粘贴上面生成的密钥
    c.NotebookApp.password = u'sha1:**************************************'
    c.NotebookApp.open_browser = False
    c.NotebookApp.port = 8888
    
    # 配置默认的工作目录
    c.NotebookApp.notebook_dir = '/home/dev'

启动和关闭 Jupyter

  • 启动

    jupyter notebook &

  • 关闭

    ps -aux         # 查看进程的 PID
    sudo kill pid

方便开发的 Jupyter 配置

pip 使用清华镜像

pip install --upgrade pip
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

小提示:安装 anaconda 时一定要勾选修改 .bashrc,自己配很麻烦,容易和系统自带的 Python2.7 冲突。

使用代码提示

  • 安装 nbextensions
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
  • 安装 nbextensions_configurator
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user
  • 重启jupyter,在弹出的主页面里,能看到增加了一个Nbextensions标签页,在这个页面里,勾选 Hinterland 即启用了代码自动补全

美化 Jupyter

pip install jupyterthemes
jt -t grade3 -fs 12 -altp -tfs 12 -nfs 12 -cellw 88% -T

参考:https://github.com/dunovank/jupyter-themes#command-line-examples

猜你喜欢

转载自www.cnblogs.com/wuxie0ne/p/10672438.html
今日推荐