Skip to content

Instantly share code, notes, and snippets.

@hoangdh
Last active November 26, 2024 03:46
Show Gist options
  • Select an option

  • Save hoangdh/e17a3ba8fb042b32d712ee38cdb0846f to your computer and use it in GitHub Desktop.

Select an option

Save hoangdh/e17a3ba8fb042b32d712ee38cdb0846f to your computer and use it in GitHub Desktop.

Revisions

  1. hoangdh revised this gist Nov 26, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -28,8 +28,8 @@ Description=JupyterLab Server
    [Service]
    Type=Simple
    User=jupyter
    Group=jupyter
    User=root
    Group=root
    WorkingDirectory=/data/jupyterhub/
    Environment="PATH=/data/softs/anaconda/envs/jupyterhub/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
    ExecStart=/data/softs/anaconda/envs/jupyterhub/bin/jupyterhub -f /data/jupyterhub/jupyterhub_config.py
  2. hoangdh revised this gist May 23, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,7 @@ c.JupyterHub.ip = '0.0.0.0'
    c.JupyterHub.port = 8080
    c.Authenticator.admin_users = { 'hoangdh' }
    # c.LocalAuthenticator.create_system_users=True
    c.FileContentsManager.delete_to_trash = False
    ```

    ### Systemd
  3. hoangdh revised this gist Apr 20, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -184,6 +184,7 @@ logout
    su - bi_tuna
    ./script.sh 3.10
    ```
    Tham khảo: https://help.rc.ufl.edu/doc/Managing_Python_environments_and_Jupyter_kernels

    ### Cài đặt PyArrow

  4. hoangdh revised this gist Apr 20, 2024. 1 changed file with 18 additions and 1 deletion.
    19 changes: 18 additions & 1 deletion jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -115,7 +115,7 @@ Ta thay file khởi động kernel với script ta vừa tạo bên trên
    }
    ```

    Bonus: Script tự tạo; đang cập nhật
    Bonus: Script tự tạo môi trường Conda và Kernel tương ứng

    ```
    #!/bin/bash
    @@ -168,6 +168,23 @@ cat > ~/.local/share/jupyter/kernels/${ENV_NAME}/kernel.json << EOF
    EOF
    ```

    Ví dụ: Cài đặt môi trường mới cho user `bi_tuna`

    ```
    useradd -m -d /data/users/bi_tuna/ -s /bin/bash bi_tuna
    passwd bi_tuna
    ```

    Chuyển sang user `bi_tuna`; sao chép script và chạy script để tạo môi trường mới với Python 3.10

    ```
    su - bi_tuna
    /data/bigdata/anaconda3/bin/conda init
    logout
    su - bi_tuna
    ./script.sh 3.10
    ```

    ### Cài đặt PyArrow

    > pip install pyarrow
  5. hoangdh revised this gist Apr 19, 2024. 1 changed file with 42 additions and 2 deletions.
    44 changes: 42 additions & 2 deletions jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -120,12 +120,52 @@ Bonus: Script tự tạo; đang cập nhật
    ```
    #!/bin/bash
    ENV_NAME="${USER}_p${1}"
    ENV_DISPLAYNAME="Python ${1} (Conda)"
    conda create --name ${USER}_p${1} python=${1} -y
    eval "$(conda shell.bash hook)"
    conda activate ${USER}_p${1}
    conda activate ${ENV_NAME}
    pip install --upgrade pip
    pip install ipykernel
    python -m ipykernel install --user --name="${USER}_p${1}" --display-name="Python ${1}"
    python -m ipykernel install --user --name="${ENV_NAME}" --display-name="${ENV_DISPLAYNAME}"
    cat > ~/.local/share/jupyter/kernels/${ENV_NAME}/run.sh << EOF
    #!/usr/bin/bash
    export JAVA_HOME=/opt/softs/jdk1.8.0_331
    export HADOOP_HOME=/opt/softs/hadoop
    export HADOOP_CONF_DIR=\$HADOOP_HOME/etc/hadoop
    export CLASSPATH=\$(\$HADOOP_HOME/bin/hdfs classpath --glob)
    export HADOOP_PREFIX=\$HADOOP_HOME
    export SPARK_LOCAL_HOSTNAME=10.10.10.10
    export SPARK_HOME=/opt/softs/spark
    # Change me
    export USER_ENV="/data/users/${USER}/.conda/envs/${ENV_NAME}"
    export PATH="\${USER_ENV}/bin:/data/bigdata/anaconda3/condabin/:\${PATH}"
    exec \${USER_ENV}/bin/python -m ipykernel "\$@"
    EOF
    chmod +x ~/.local/share/jupyter/kernels/${ENV_NAME}/run.sh
    cat > ~/.local/share/jupyter/kernels/${ENV_NAME}/kernel.json << EOF
    {
    "argv": [
    "~/.local/share/jupyter/kernels/${ENV_NAME}/run.sh",
    "-f",
    "{connection_file}"
    ],
    "display_name": "${ENV_DISPLAYNAME}",
    "language": "python",
    "metadata": {
    "debugger": true
    }
    }
    EOF
    ```

    ### Cài đặt PyArrow
  6. hoangdh revised this gist Apr 19, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -92,6 +92,8 @@ export PATH="${USER_ENV}/bin:/data/bigdata/anaconda3/condabin/:${PATH}"
    exec ${USER_ENV}/bin/python -m ipykernel "$@"
    ```

    > chmod +x /data/users/bi_shark/.local/share/jupyter/kernels/bi_shark/run.sh
    - Sửa file khởi động của kernel

    Ta thay file khởi động kernel với script ta vừa tạo bên trên
  7. hoangdh revised this gist Apr 19, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -96,6 +96,8 @@ exec ${USER_ENV}/bin/python -m ipykernel "$@"

    Ta thay file khởi động kernel với script ta vừa tạo bên trên

    > vi /data/users/bi_shark/.local/share/jupyter/kernels/bi_/kernel.json
    ```
    {
    "argv": [
  8. hoangdh revised this gist Apr 19, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -43,6 +43,7 @@ WantedBy=multi-user.target

    ```
    useradd -m -d /data/users/bi_shark/ -s /bin/bash bi_shark
    passwd bi_shark
    ```

    - Khởi tạo conda cho user
  9. hoangdh revised this gist Apr 19, 2024. 1 changed file with 74 additions and 1 deletion.
    75 changes: 74 additions & 1 deletion jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,80 @@ ExecStart=/data/softs/anaconda/envs/jupyterhub/bin/jupyterhub -f /data/jupyterhu
    WantedBy=multi-user.target
    ```

    ### Script tự tạo môi trường Python cho Jupyter
    ### Tạo môi trường Python cho Jupyter

    - Tạo user cho hub

    ```
    useradd -m -d /data/users/bi_shark/ -s /bin/bash bi_shark
    ```

    - Khởi tạo conda cho user

    ```
    su - bi_shark
    /data/bigdata/anaconda3/bin/conda init
    ```

    - Đăng nhập lại và tạo môi trường mới cho user

    ```
    conda create -n bi_shark python=3.9 -y
    ```

    - Cài kernel mới cho Jupyter

    ```
    conda activate bi_shark
    pip install --upgrade pip
    pip install ipykernel
    python -m ipykernel install --user --name="bi_shark" --display-name="Python 3.9 (Conda)"
    ```

    - Thêm các biến môi trường riêng cho user

    > vi /data/users/bi_shark/.local/share/jupyter/kernels/bi_shark/run.sh
    ```
    #!/usr/bin/bash
    export JAVA_HOME=/opt/softs/jdk1.8.0_331
    export HADOOP_HOME=/opt/softs/hadoop
    export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop
    export CLASSPATH=$($HADOOP_HOME/bin/hdfs classpath --glob)
    export HADOOP_PREFIX=$HADOOP_HOME
    export SPARK_LOCAL_HOSTNAME=10.10.10.10
    export SPARK_HOME=/opt/softs/spark
    # Change me
    export USER_ENV="/data/users/bi_shark/.conda/envs/bi_shark"
    export PATH="${USER_ENV}/bin:/data/bigdata/anaconda3/condabin/:${PATH}"
    exec ${USER_ENV}/bin/python -m ipykernel "$@"
    ```

    - Sửa file khởi động của kernel

    Ta thay file khởi động kernel với script ta vừa tạo bên trên

    ```
    {
    "argv": [
    "/data/users/bi_shark/.local/share/jupyter/kernels/bi_shark/run.sh",
    "-f",
    "{connection_file}"
    ],
    "display_name": "Python 3.9 (Conda)",
    "language": "python",
    "metadata": {
    "debugger": true
    }
    }
    ```

    Bonus: Script tự tạo; đang cập nhật

    ```
    #!/bin/bash
  10. hoangdh revised this gist Mar 26, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -52,6 +52,8 @@ python -m ipykernel install --user --name="${USER}_p${1}" --display-name="Python

    ### Cài đặt PyArrow

    > pip install pyarrow
    ```
    export JAVA_HOME=/opt/softs/jdk1.8.0_331
    export HADOOP_HOME=/opt/softs/hadoop
  11. hoangdh revised this gist Mar 26, 2024. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -48,4 +48,17 @@ conda activate ${USER}_p${1}
    pip install --upgrade pip
    pip install ipykernel
    python -m ipykernel install --user --name="${USER}_p${1}" --display-name="Python ${1}"
    ```
    ```

    ### Cài đặt PyArrow

    ```
    export JAVA_HOME=/opt/softs/jdk1.8.0_331
    export HADOOP_HOME=/opt/softs/hadoop
    export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop
    export SPARK_HOME=/opt/softs/spark
    export PATH=${JAVA_HOME}/bin:${PATH}
    export CLASSPATH=`/opt/softs/hadoop/bin/hdfs classpath --glob`
    ```

    Lưu ý: Chỗ CLASSPATH cần chạy lệnh và copy output vào biến.
  12. hoangdh revised this gist Mar 24, 2024. 1 changed file with 16 additions and 9 deletions.
    25 changes: 16 additions & 9 deletions jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,8 @@
    ### 1. Script tự tạo môi trường Python cho Jupyter
    ### Cài đặt Jupyter qua conda

    ```
    #!/bin/bash
    conda create --name ${USER}_p${1} python=${1} -y
    eval "$(conda shell.bash hook)"
    conda activate ${USER}_p${1}
    pip install --upgrade pip
    pip install ipykernel
    python -m ipykernel install --user --name="${USER}_p${1}" --display-name="Python ${1}"
    conda install -c conda-forge jupyterhub # installs jupyterhub and proxy
    conda install jupyterlab notebook # needed if running the notebook servers in the same environment
    ```

    ### File cấu hình
    @@ -41,4 +35,17 @@ ExecStart=/data/softs/anaconda/envs/jupyterhub/bin/jupyterhub -f /data/jupyterhu
    [Install]
    WantedBy=multi-user.target
    ```

    ### Script tự tạo môi trường Python cho Jupyter

    ```
    #!/bin/bash
    conda create --name ${USER}_p${1} python=${1} -y
    eval "$(conda shell.bash hook)"
    conda activate ${USER}_p${1}
    pip install --upgrade pip
    pip install ipykernel
    python -m ipykernel install --user --name="${USER}_p${1}" --display-name="Python ${1}"
    ```
  13. hoangdh revised this gist Mar 24, 2024. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -9,4 +9,36 @@ conda activate ${USER}_p${1}
    pip install --upgrade pip
    pip install ipykernel
    python -m ipykernel install --user --name="${USER}_p${1}" --display-name="Python ${1}"
    ```

    ### File cấu hình

    Filename: jupyterhub_config.py

    ```
    c = get_config() #noqa
    c.JupyterHub.ip = '0.0.0.0'
    c.JupyterHub.port = 8080
    c.Authenticator.admin_users = { 'hoangdh' }
    # c.LocalAuthenticator.create_system_users=True
    ```

    ### Systemd

    Filename: /lib/systemd/system/jupyterhub.service

    ```
    [Unit]
    Description=JupyterLab Server
    [Service]
    Type=Simple
    User=jupyter
    Group=jupyter
    WorkingDirectory=/data/jupyterhub/
    Environment="PATH=/data/softs/anaconda/envs/jupyterhub/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
    ExecStart=/data/softs/anaconda/envs/jupyterhub/bin/jupyterhub -f /data/jupyterhub/jupyterhub_config.py
    [Install]
    WantedBy=multi-user.target
    ```
  14. hoangdh created this gist Mar 24, 2024.
    12 changes: 12 additions & 0 deletions jupyter-notes.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    ### 1. Script tự tạo môi trường Python cho Jupyter

    ```
    #!/bin/bash
    conda create --name ${USER}_p${1} python=${1} -y
    eval "$(conda shell.bash hook)"
    conda activate ${USER}_p${1}
    pip install --upgrade pip
    pip install ipykernel
    python -m ipykernel install --user --name="${USER}_p${1}" --display-name="Python ${1}"
    ```