|
| 1 | +from e2b import Template, wait_for_port |
| 2 | + |
| 3 | + |
| 4 | +def make_template( |
| 5 | + kernels: list[str] = ["python", "r", "javascript", "deno", "bash", "java"], |
| 6 | +): |
| 7 | + # Start with base template |
| 8 | + template = ( |
| 9 | + Template() |
| 10 | + .from_image("python:3.12") |
| 11 | + .set_envs( |
| 12 | + { |
| 13 | + "PIP_DEFAULT_TIMEOUT": "100", |
| 14 | + "PIP_DISABLE_PIP_VERSION_CHECK": "1", |
| 15 | + "PIP_NO_CACHE_DIR": "1", |
| 16 | + "JUPYTER_CONFIG_PATH": ".jupyter", |
| 17 | + "IPYTHON_CONFIG_PATH": ".ipython", |
| 18 | + "SERVER_PATH": ".server", |
| 19 | + "R_VERSION": "4.4.2", |
| 20 | + "R_HOME": "/opt/R/4.4.2", |
| 21 | + "JAVA_HOME": "/opt/java/openjdk", |
| 22 | + "DENO_INSTALL": "$HOME/.deno", |
| 23 | + } |
| 24 | + ) |
| 25 | + .apt_install( |
| 26 | + [ |
| 27 | + "build-essential", |
| 28 | + "curl", |
| 29 | + "git", |
| 30 | + "util-linux", |
| 31 | + "jq", |
| 32 | + "sudo", |
| 33 | + "fonts-noto-cjk", |
| 34 | + ] |
| 35 | + ) |
| 36 | + .run_cmd("curl -fsSL https://deb.nodesource.com/setup_20.x | bash -") |
| 37 | + .apt_install("nodejs") |
| 38 | + .copy("requirements.txt", "requirements.txt") |
| 39 | + .pip_install("--no-cache-dir -r requirements.txt") |
| 40 | + ) |
| 41 | + |
| 42 | + if "python" in kernels: |
| 43 | + template = template.run_cmd("ipython kernel install --name 'python3' --user") |
| 44 | + |
| 45 | + # Install R Kernel if requested |
| 46 | + if "r" in kernels: |
| 47 | + template = ( |
| 48 | + template.run_cmd( |
| 49 | + "curl -O https://cdn.rstudio.com/r/debian-12/pkgs/r-4.4.2_1_amd64.deb" |
| 50 | + ) |
| 51 | + .apt_install("./r-4.4.2_1_amd64.deb") |
| 52 | + .make_symlink("/opt/R/4.4.2/bin/R", "/usr/bin/R") |
| 53 | + .run_cmd( |
| 54 | + [ |
| 55 | + "R -e \"install.packages('IRkernel', repos='https://cloud.r-project.org')\"", |
| 56 | + "R -e \"IRkernel::installspec(user = FALSE, name = 'r', displayname = 'R')\"", |
| 57 | + ] |
| 58 | + ) |
| 59 | + ) |
| 60 | + |
| 61 | + # Install JavaScript Kernel if requested |
| 62 | + if "javascript" in kernels: |
| 63 | + template = template.npm_install( |
| 64 | + "--unsafe-perm git+https://github.com/e2b-dev/ijavascript.git", |
| 65 | + g=True, |
| 66 | + ).run_cmd("ijsinstall --install=global") |
| 67 | + |
| 68 | + # Install Deno Kernel if requested |
| 69 | + if "deno" in kernels: |
| 70 | + template = template.run_cmd( |
| 71 | + [ |
| 72 | + "curl -fsSL https://deno.land/x/install/install.sh | sh", |
| 73 | + "PATH=$HOME/.deno/bin:$PATH", |
| 74 | + "deno jupyter --unstable --install", |
| 75 | + ] |
| 76 | + ) |
| 77 | + template = template.copy( |
| 78 | + "deno.json", ".local/share/jupyter/kernels/deno/kernel.json" |
| 79 | + ) |
| 80 | + |
| 81 | + # Install Bash Kernel if requested |
| 82 | + if "bash" in kernels: |
| 83 | + template = template.pip_install("bash_kernel").run_cmd( |
| 84 | + "python -m bash_kernel.install" |
| 85 | + ) |
| 86 | + |
| 87 | + # Install Java and Java Kernel if requested |
| 88 | + if "java" in kernels: |
| 89 | + template = template.apt_install("default-jdk") |
| 90 | + template = template.run_cmd( |
| 91 | + [ |
| 92 | + "wget https://github.com/SpencerPark/IJava/releases/download/v1.3.0/ijava-1.3.0.zip", |
| 93 | + "unzip ijava-1.3.0.zip", |
| 94 | + "python install.py --sys-prefix", |
| 95 | + ] |
| 96 | + ) |
| 97 | + |
| 98 | + # Common setup steps (always run) |
| 99 | + template = ( |
| 100 | + template |
| 101 | + # Create server virtual environment |
| 102 | + .copy("server", ".server") |
| 103 | + .run_cmd( |
| 104 | + [ |
| 105 | + "python -m venv .server/.venv", |
| 106 | + ". .server/.venv/bin/activate", |
| 107 | + ] |
| 108 | + ) |
| 109 | + # Copy and install server requirements |
| 110 | + .run_cmd( |
| 111 | + ".server/.venv/bin/pip install --no-cache-dir -r .server/requirements.txt" |
| 112 | + ) |
| 113 | + # Copy configuration files |
| 114 | + .copy("matplotlibrc", ".config/matplotlib/.matplotlibrc") |
| 115 | + .copy("start-up.sh", ".jupyter/start-up.sh", mode=0o755, user="root") |
| 116 | + .run_cmd("chmod +x .jupyter/start-up.sh") |
| 117 | + .copy("jupyter_server_config.py", ".jupyter/") |
| 118 | + .make_dir(".ipython/profile_default/startup") |
| 119 | + .copy("ipython_kernel_config.py", ".ipython/profile_default/") |
| 120 | + .copy("startup_scripts", ".ipython/profile_default/startup") |
| 121 | + .set_start_cmd(".jupyter/start-up.sh", wait_for_port(49999)) |
| 122 | + ) |
| 123 | + |
| 124 | + return template |
0 commit comments