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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM