### Quick Start: Provision, Execute, and Stop Colab VM Source: https://github.com/googlecolab/google-colab-cli/blob/main/README.md A quick start guide to provision a new Colab VM, execute code from stdin, and then stop the VM. The session name can be omitted if only one session is active. ```bash # 1. Provision a new session colab new # 2. Execute code from stdin echo "print('Hello from Google Colab!')" | colab exec # 3. Stop and release the VM resource colab stop ``` -------------------------------- ### Install Packages from Requirements File in Colab Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Installs packages listed in '/content/requirements.txt' on the Colab VM. It captures and displays the last few lines of the installation output. ```bash uv run colab --auth=adc install -s env-test -r /tmp/requirements.txt 2>&1 | tail -3 ``` ```output [colab] Installing packages on env-test (preferring uv)... Installation Complete (via uv)! ``` -------------------------------- ### Install Packages in Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Installs the 'torch' package into the 'trainer' Colab session using 'uv'. The output is piped to 'tail -5' to show the last 5 lines of the installation process. ```bash uv run colab --auth=adc install -s trainer torch 2>&1 | tail -5 ``` -------------------------------- ### Install Google Colab CLI Source: https://github.com/googlecolab/google-colab-cli/blob/main/README.md Install the google-colab-cli package using uv or pip. uv is the recommended package manager. ```bash # Using uv (recommended) uv tool install google-colab-cli # Using pip pip install google-colab-cli ``` -------------------------------- ### Install Packages on a Colab VM Source: https://github.com/googlecolab/google-colab-cli/blob/main/skills/colab-operator/SKILL.md Installs Python packages on the Colab VM using uv pip or pip. Supports installing from a requirements file. ```bash colab install -s pkg1 pkg2 ``` ```bash colab install -s -r requirements.txt ``` -------------------------------- ### Install Python Packages in Colab VM Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/04_automation_and_utility.md Execute pip install commands on the VM using the Python executable. Supports installing from a requirements file if provided. ```python import sys, subprocess subprocess.check_call([sys.executable, "-m", "pip", "install", "..."]) ``` -------------------------------- ### Start a Colab Session and Bridge to Browser Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Starts a Colab session named 'env-test' and prints a URL to bridge the browser to the existing session. Use `--auth=adc` for authentication. ```bash uv run colab --auth=adc url -s env-test ``` -------------------------------- ### Accelerator Training with Checkpoint Retrieval Source: https://github.com/googlecolab/google-colab-cli/blob/main/README.md Provision an A100 GPU, install requirements, run a training script, download model weights, and stop the VM. ```bash colab new -s trainer --gpu A100 colab install -s trainer torch transformers colab exec -s trainer -f train.py colab download -s trainer checkpoints/model.bin ./model.bin colab stop -s trainer ``` -------------------------------- ### Install Packages in a Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Installs the 'pillow' package into the 'data-proc' Colab session using uv. The output is truncated to show only the last 3 lines. ```bash uv run colab --auth=adc install -s data-proc pillow 2>&1 | tail -3 ``` -------------------------------- ### Install Packages in a Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Installs the 'scipy' package into the 'pivot' Colab session using 'uv' for package management. The output is truncated to show only the last three lines. ```bash uv run colab --auth=adc install -s pivot scipy 2>&1 | tail -3 ``` -------------------------------- ### Verify Local Colab CLI Installation Source: https://github.com/googlecolab/google-colab-cli/blob/main/AGENTS.md Confirm the correct `colab` executable is being used by checking `which colab` and `uv run which colab`. This is crucial to ensure the editable install is prioritized over a global one. ```bash which colab ``` ```bash uv run which colab ``` -------------------------------- ### Install Packages on Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Installs the 'jax' package onto the 'research' Colab session using 'uv'. This command is useful for setting up the environment for specific workloads. ```bash uv run colab --auth=adc install -s research jax 2>&1 | tail -20 ``` -------------------------------- ### Upload Requirements File to Colab VM Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Uploads a local 'requirements.txt' file to the Colab VM's '/content/' directory. This prepares the environment for package installation. ```bash uv run colab --auth=adc upload -s env-test /tmp/requirements.txt /content/requirements.txt ``` ```output [colab] Uploaded '/tmp/requirements.txt' to '/content/requirements.txt' ``` -------------------------------- ### Get Colab VM URL and Help Information Source: https://github.com/googlecolab/google-colab-cli/blob/main/skills/colab-operator/SKILL.md Generates a browser URL to attach the Colab UI to an existing CLI session or displays help information for commands. The help listing is alphabetical. ```bash colab url -s ``` ```bash colab url -s --open ``` ```bash colab help ``` ```bash colab help ``` ```bash colab skill ``` ```bash colab readme ``` -------------------------------- ### Get Colab CLI Version Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/04_automation_and_utility.md Show the current version of the Colab CLI. Attempts to retrieve the version using importlib.metadata, falling back to the Git commit hash if not installed. ```python import importlib.metadata importlib.metadata.version("colab") ``` ```bash git rev-parse --short HEAD ``` -------------------------------- ### Verify Package Version in Colab VM Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Executes a Python command within the Colab VM to print the installed version of the 'requests' library. This verifies that the installation was successful. ```bash echo 'import requests; print("requests:", requests.__version__)' | uv run colab --auth=adc exec -s env-test ``` ```output requests: 2.31.0 ``` -------------------------------- ### Run a One-Shot Pipeline with Colab CLI Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Chains multiple colab CLI commands using '&&' to create, install, execute, download, and stop a Colab session. This is useful for automating multi-step tasks. ```bash uv run colab --auth=adc new -s pipeline \ && uv run colab --auth=adc install -s pipeline requests 2>&1 | tail -2 \ && uv run colab --auth=adc exec -s pipeline -f /tmp/local_pipeline.py \ && uv run colab --auth=adc download -s pipeline /content/results.json /tmp/results.json \ && uv run colab --auth=adc stop -s pipeline ``` ```output [colab] Creating session 'pipeline'... [colab] Session READY. [colab] Installing packages on pipeline (preferring uv)... Installation Complete (via uv)! Wrote results.json: {'status': 'ok', 'computed_at': '2026-05-07T23:22:34.924350Z', 'value': 42} /tmp/ipykernel_38852/1782062088.py:5: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC). "computed_at": datetime.datetime.utcnow().isoformat() + "Z", [colab] Downloaded '/content/results.json' to '/tmp/results.json' [colab] Stopping session 'pipeline'... [colab] Session terminated. ``` -------------------------------- ### Colab CLI Command Index - Automation & Utilities Source: https://github.com/googlecolab/google-colab-cli/blob/main/README.md Overview of automation and utility commands for the Colab CLI. This includes authentication, drive mounting, package installation, log viewing, and version management. ```bash colab auth [-s NAME] colab drivemount [-s NAME] [PATH] colab install [-s NAME] [-r FILE | PKG...] colab log [-s NAME] [-n N] [-o FILE] colab pay colab version colab update [--install] ``` -------------------------------- ### Reinstall Colab CLI Tooling Source: https://github.com/googlecolab/google-colab-cli/blob/main/AGENTS.md Before testing shebang-based behavior after code changes, ensure the `colab` tool is up-to-date by running `uv tool install --reinstall --force --from . colab`. ```bash uv tool install --reinstall --force --from . colab ``` -------------------------------- ### Check Colab CLI Version Source: https://github.com/googlecolab/google-colab-cli/blob/main/AGENTS.md Verify the installed Colab CLI version, which includes the git short SHA, using the `colab version` command. ```bash colab version ``` -------------------------------- ### Shebang Example for GPU Execution Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/05_run_command.md An example of a Python script using a shebang line to automatically run on a Colab T4 GPU instance. This allows for direct execution of the script after making it executable. ```python #!/usr/bin/env -S colab run --gpu T4 import torch print(torch.cuda.get_device_name(0)) ``` -------------------------------- ### Colab CLI Command Index - Execution Source: https://github.com/googlecolab/google-colab-cli/blob/main/README.md Overview of execution commands for the Colab CLI. These commands allow running scripts, executing code from various sources, and starting interactive sessions. ```bash colab run [--gpu GPU] [--tpu TPU] [--keep] SCRIPT [ARGS...] colab exec [-s NAME] [-f FILE] [--output-image PATH] colab repl [-s NAME] [--output-image PATH] colab console [-s NAME] ``` -------------------------------- ### Run Python REPL in a Colab Session with SciPy Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Executes a Python script within the 'pivot' Colab session using the REPL mode, calculating and printing z-scores from a list using the installed SciPy library. ```bash echo 'from scipy.stats import zscore; print(zscore([1.2, 1.5, 1.1, 10.4, 1.3]))' | uv run colab --auth=adc repl -s pivot ``` -------------------------------- ### Workspace Notebook Execution with Drive Integration Source: https://github.com/googlecolab/google-colab-cli/blob/main/README.md Create a workspace, mount Google Drive, execute a notebook, log its output, and terminate the session. ```bash colab new -s analysis colab drivemount -s analysis colab exec -s analysis -f report.ipynb colab log -s analysis -o execution_log.md colab stop -s analysis ``` -------------------------------- ### Create a scratch file Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Executes a Python command in the Colab environment to create a 100 KB scratch file named 'scratch.log' in the /content directory. ```bash echo 'with open("/content/scratch.log", "w") as f: f.write("x" * 1024 * 100) print("created scratch.log (100 KB)")' | uv run colab --auth=adc exec -s debug ``` -------------------------------- ### Run Tests and Linting with UV Source: https://github.com/googlecolab/google-colab-cli/blob/main/AGENTS.md Use `uv run pytest tests/` to execute tests and `uv run ruff check . --fix` to resolve linting errors. ```bash uv run pytest tests/ ``` ```bash uv run ruff check . --fix ``` -------------------------------- ### Provision a New Colab VM Source: https://github.com/googlecolab/google-colab-cli/blob/main/skills/colab-operator/SKILL.md Creates a new Colab VM with optional accelerators. Always pass -s to avoid ambiguity. Unrecognized --gpu values may silently fall back. ```bash colab new -s ``` ```bash colab new -s --gpu A100 ``` ```bash colab new -s --tpu v6e1 ``` -------------------------------- ### Set up Application Default Credentials (ADC) with Colab Scopes Source: https://github.com/googlecolab/google-colab-cli/blob/main/skills/colab-operator/SKILL.md Use this command to re-mint your Application Default Credentials with the necessary scopes for Colab backends. This is the most reliable method for headless or agent use. ```bash gcloud auth application-default login \ --scopes=openid,\n https://www.googleapis.com/auth/cloud-platform,\n https://www.googleapis.com/auth/userinfo.email,\n https://www.googleapis.com/auth/colaboratory ``` -------------------------------- ### Download Model Checkpoint from Colab Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Downloads the '/content/model.bin' file from the 'trainer' Colab session to the local '/tmp/model.bin' path and then lists the downloaded file's details. ```bash uv run colab --auth=adc download -s trainer /content/model.bin /tmp/model.bin && ls -la /tmp/model.bin ``` -------------------------------- ### Get Status of a Specific Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Retrieves the status of a specific Colab session, 'gpu-eval', including its hardware, variant, and current status (e.g., IDLE). ```bash uv run colab --auth=adc status -s gpu-eval ``` -------------------------------- ### Create a Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Creates a new Colab session named 'trainer'. This is the first step before performing other operations on the session. ```bash uv run colab --auth=adc new -s trainer ``` -------------------------------- ### Create a Colab Session for Environment Testing Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Creates a new Colab session named 'env-test'. This is the initial step for setting up a reproducible environment. ```bash uv run colab --auth=adc new -s env-test ``` ```output [colab] Creating session 'env-test'... [colab] Session READY. ``` -------------------------------- ### Configure Colab VM for GCP Services Source: https://github.com/googlecolab/google-colab-cli/blob/main/skills/colab-operator/SKILL.md Authenticates the VM for GCP services or mounts Google Drive. These commands are interactive and not suitable for agent execution. ```bash colab auth -s ``` ```bash colab drivemount -s [PATH] ``` -------------------------------- ### Colab CLI Command Index - Session Management Source: https://github.com/googlecolab/google-colab-cli/blob/main/README.md Overview of session management commands for the Colab CLI. Use ` --help` for detailed options. ```bash colab new [-s NAME] [--gpu GPU] [--tpu TPU] colab sessions colab status [-s NAME] colab restart-kernel [-s NAME] colab stop [-s NAME] colab url [-s NAME] [--open] ``` -------------------------------- ### Display README File with colab README Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/04_automation_and_utility.md Prints the bundled README.md file from the colab_cli package resources. This command is useful for quickly accessing project documentation. ```bash colab README ``` -------------------------------- ### Create a Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Creates a new Colab session named 'reporter' using ADC authentication. ```bash uv run colab --auth=adc new -s reporter ``` -------------------------------- ### Run Ephemeral Accelerator Jobs Source: https://github.com/googlecolab/google-colab-cli/blob/main/README.md Execute a local script on a T4 GPU and automatically release the VM upon completion. ```bash # Run train.py on a T4 GPU and release the VM on completion colab run --gpu T4 train.py ``` -------------------------------- ### Python Prelude for Script Execution Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/05_run_command.md This prelude is prepended to user scripts to ensure they receive the same execution context as a direct Python script, correctly setting sys.argv and __name__. ```python import sys sys.argv = ['', 'arg1', 'arg2', ...] __name__ = '__main__' ``` -------------------------------- ### Query free disk space Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Uses the Colab REPL to execute a Python command that calculates and prints the free disk space in GB for the root directory. ```bash echo 'import shutil; print(shutil.disk_usage("/").free // 2**30, "GB free")' | uv run colab --auth=adc repl -s debug ``` -------------------------------- ### Colab Run Command Usage Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/05_run_command.md The basic syntax for the `colab run` command, including positional arguments and options. ```bash colab run [OPTIONS] SCRIPT [SCRIPT_ARGS]... ``` -------------------------------- ### Run Integration Test Scenario Source: https://github.com/googlecolab/google-colab-cli/blob/main/integration/README.md Executes a specific integration test scenario using `uv run` to ensure the local `colab` entry point is correctly configured on the PATH. ```bash uv run bash integration/repro_keep_alive/test.sh ``` -------------------------------- ### Inspect Colab Sessions and Logs Source: https://github.com/googlecolab/google-colab-cli/blob/main/skills/colab-operator/SKILL.md Provides commands to list sessions, check VM status, view logs, and export session data. Logs can be filtered by type and count. ```bash colab sessions ``` ```bash colab status [-s ] ``` ```bash colab log -s [-n 20] [-t TYPE] ``` ```bash colab log -s -o summary.ipynb ``` -------------------------------- ### Create a Hybrid Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Creates a new Colab session named 'hybrid' for hybrid local-cloud workflows. Uses ADC for authentication. ```bash uv run colab --auth=adc new -s hybrid ``` -------------------------------- ### List files in a directory Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Lists the contents of the /content directory in the specified Colab session. ```bash uv run colab --auth=adc ls -s debug /content ``` -------------------------------- ### Mocking stdin.isatty() for Testing Source: https://github.com/googlecolab/google-colab-cli/blob/main/AGENTS.md When testing commands that depend on `stdin.isatty()`, use the `is_stdin_tty` helper and mock it with `mocker.patch` to prevent tests from hanging in CI environments. ```python mocker.patch("colab_cli.commands.execution.is_stdin_tty", return_value=...) ``` -------------------------------- ### Create a Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Creates a new Colab session named 'data-proc' using ADC for authentication. This is the initial step for most remote operations. ```bash uv run colab --auth=adc new -s data-proc ``` -------------------------------- ### List All Colab Sessions Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Lists all currently active Colab sessions, showing their names, hardware, and variants. Useful for monitoring and managing multiple sessions. ```bash uv run colab --auth=adc sessions ``` -------------------------------- ### Create a Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Provisions a new Colab session named 'research'. This is the first step in the headline pattern for managing Colab environments. ```bash uv run colab --auth=adc new -s research ``` -------------------------------- ### Create a new Colab session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Creates a new Colab session named 'debug' using ADC authentication. This is the initial step for most operations. ```bash uv run colab --auth=adc new -s debug ``` -------------------------------- ### Display AGENTS File with colab AGENT Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/04_automation_and_utility.md Prints the bundled AGENTS.md file from the colab_cli package resources. This command provides access to agent-related information bundled with the package. ```bash colab AGENT ``` -------------------------------- ### Run Ephemeral Colab Jobs Source: https://github.com/googlecolab/google-colab-cli/blob/main/skills/colab-operator/SKILL.md Provisions a VM, runs a script, and tears down the VM in one command. Exit codes from the script propagate to the 'colab run' command. Script output is sent to stdout, while Colab chatter goes to stderr. ```bash colab run [--gpu T4] [--tpu v6e1] [--keep] [-s NAME] script.py [args...] ``` ```bash colab run job.py > out.txt ``` -------------------------------- ### Execute Shell Commands on a Colab VM Source: https://github.com/googlecolab/google-colab-cli/blob/main/skills/colab-operator/SKILL.md Runs shell commands on a Colab VM. Piped output may contain terminal control bytes and can be filtered with grep -a. 'exec' is faster if a real shell is not needed. ```bash echo "cmd" | colab console -s ``` -------------------------------- ### Mount Google Drive in Colab VM Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/04_automation_and_utility.md Execute drive.mount() to mount Google Drive and transparently proxy Colab's credential propagation flow. The CLI intercepts messages and prompts the user for OAuth consent if needed. ```python from google.colab import drive drive.mount('/content/drive') ``` -------------------------------- ### Execute a Local Script on a Colab VM Source: https://github.com/googlecolab/google-colab-cli/blob/main/skills/colab-operator/SKILL.md Runs a local script on the remote VM. The script is read locally and sent to the kernel without manual upload. Results for notebooks are saved to a new file. ```bash colab exec -s -f ``` ```bash echo "print(1)" | colab exec -s ``` ```bash cat script.py | colab exec -s ``` ```bash colab exec -s -f nb.ipynb ``` -------------------------------- ### Fetch Remote Changes Before Pushing Source: https://github.com/googlecolab/google-colab-cli/blob/main/AGENTS.md Always run `git fetch ` before pushing or merging to ensure your local repository is up-to-date with the remote. ```bash git fetch ``` -------------------------------- ### Run Colab CLI Integration Test Source: https://github.com/googlecolab/google-colab-cli/blob/main/AGENTS.md Use this command to run integration tests for the colab CLI. It ensures the 'colab' command is available in the shell environment. ```bash uv run bash integration/repro_/test.sh ``` -------------------------------- ### Colab CLI Global Options Source: https://github.com/googlecolab/google-colab-cli/blob/main/README.md Common global options applicable to most Colab CLI commands. These control authentication, configuration paths, and logging. ```bash --auth {oauth2,adc} -c, --client-oauth-config PATH --config PATH --logtostderr ``` -------------------------------- ### Execute Notebook and Save Outputs Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Executes a specified notebook file within a Colab session and saves the notebook with its outputs back to a new file. It then lists the output notebook file. ```bash uv run colab --auth=adc exec -s reporter -f /tmp/analysis.ipynb && ls /tmp/analysis_output.ipynb ``` -------------------------------- ### List disk usage in a directory Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Pipes a command to the Colab console to list disk usage for the /content directory. It uses `grep -aE` to filter the output for relevant lines, handling potential terminal control bytes. ```bash echo 'df -h /content' | uv run colab --auth=adc console -s debug 2>&1 | grep -aE 'overlay|/dev/' ``` -------------------------------- ### Create a Colab Session for Reproducible Research Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Creates a new Colab session named 'pivot' to be used for reproducible research workflows. This session will be used for subsequent operations. ```bash uv run colab --auth=adc new -s pivot ``` -------------------------------- ### Inspect Active Credentials with colab whoami Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/04_automation_and_utility.md Resolves active credentials, mints an access token, and prints its email, audience, scopes, and expiry. This command is intended for developer debugging and reflects the same credentials used by the Client. ```bash colab whoami ``` -------------------------------- ### Execute a Training Script in Colab Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Executes a Python training script located at '/tmp/train.py' within the 'trainer' Colab session. This snippet shows the output of a sample training process. ```bash uv run colab --auth=adc exec -s trainer -f /tmp/train.py ``` -------------------------------- ### Execute JAX Workload on Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Executes a Python script using JAX on the 'research' Colab session. The script processes synthetic data and prints device information and the number of processed rows. This demonstrates running custom code within a provisioned session. ```bash cat <<'EOF' | uv run colab --auth=adc exec -s research import jax, jax.numpy as jnp import numpy as np # (BigQuery substituted with synthetic data — would normally use: # df = bigquery.Client().query('SELECT * FROM bigquery-public-data.ml_datasets.iris LIMIT 100').to_dataframe()) data = np.random.RandomState(0).randn(100, 4) print('Devices:', jax.devices()) w = jax.random.normal(jax.random.PRNGKey(0), (4, 4)) out = jax.jit(lambda x, w: x @ w)(jnp.array(data), w) print(f'Processed {len(out)} rows.') EOF ``` -------------------------------- ### Authenticate Colab VM User Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/04_automation_and_utility.md Execute code on the VM to trigger user-interactive authentication using the classic Gcloud fallback. Setting USE_AUTH_EPHEM to '0' forces the kernel to print a standard gcloud verification URL. ```python import os os.environ['USE_AUTH_EPHEM'] = '0' from google.colab import auth auth.authenticate_user() ``` -------------------------------- ### Execute Notebook with Plot Redirection Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Executes a Python script within a Colab session, generating a sine wave plot and saving it as a PNG image. The output image is displayed and saved to the specified path. ```bash cat <<'EOF' | uv run colab --auth=adc exec -s reporter --output-image /tmp/sine.png import matplotlib.pyplot as plt, numpy as np x = np.linspace(0, 10, 100) plt.plot(x, np.sin(x)); plt.title('Sine'); plt.show() EOF ``` -------------------------------- ### Colab CLI Command Index - File Operations Source: https://github.com/googlecolab/google-colab-cli/blob/main/README.md Overview of file operation commands for the Colab CLI. These commands facilitate listing, uploading, downloading, deleting, and editing files on the remote VM. ```bash colab ls [-s NAME] [PATH] colab upload [-s NAME] LOCAL REMOTE colab download [-s NAME] REMOTE LOCAL colab rm [-s NAME] PATH colab edit [-s NAME] PATH ``` -------------------------------- ### Create a Long-Running Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Creates a new, long-running Colab session using the 'adc' authentication method. This is the initial step for many session management tasks. ```bash uv run colab --auth=adc new -s long-running ``` -------------------------------- ### Download Processed Data from Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Downloads the 'batch.zip' file from the 'data-proc' Colab session to the local '/tmp/' directory and lists the downloaded file. This is used to retrieve results after remote processing. ```bash uv run colab --auth=adc download -s data-proc /content/processed/batch.zip /tmp/batch.zip && ls -la /tmp/batch.zip ``` -------------------------------- ### Log and Export Session History for Reproducible Research Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Exports the session history of the 'pivot' Colab session to a notebook file for archival and replayability, then lists the created file. This captures the research workflow. ```bash uv run colab --auth=adc log -s pivot -o /tmp/pivot_discovery.ipynb && ls -la /tmp/pivot_discovery.ipynb ``` -------------------------------- ### Execute Local Script on Remote VM Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Executes a local Python script (`local_analysis.py`) on the remote Colab VM within the 'hybrid' session. This allows running local code against the Colab environment. ```bash uv run colab --auth=adc exec -s hybrid -f /tmp/local_analysis.py ``` -------------------------------- ### Create Multiple Colab Sessions Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Creates two Colab sessions concurrently: 'tpu-cluster' and 'gpu-eval'. This is useful for managing different types of workloads simultaneously. ```bash uv run colab --auth=adc new -s tpu-cluster && uv run colab --auth=adc new -s gpu-eval ``` -------------------------------- ### Log and Export History of a Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Exports the session history of a long-running Colab session to a notebook file and lists the created file. This is useful for archival and reproducibility. ```bash uv run colab --auth=adc log -s long-running -o /tmp/checkpoint.ipynb && ls -la /tmp/checkpoint.ipynb ``` -------------------------------- ### Upload a File to a Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Uploads a local CSV file to the '/content/' directory within the 'pivot' Colab session. This makes local data accessible within the Colab environment. ```bash uv run colab --auth=adc upload -s pivot /tmp/raw_data.csv /content/raw_data.csv ``` -------------------------------- ### Execute Command to Read Uploaded File in Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Reads and prints the content of the uploaded CSV file ('/content/raw_data.csv') from within the 'pivot' Colab session. This verifies the file upload and content. ```bash echo 'print(open("/content/raw_data.csv").read())' | uv run colab --auth=adc exec -s pivot ``` -------------------------------- ### Execute Python Code in a Colab Session Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/demos.md Executes a Python script within the 'data-proc' Colab session. The script generates synthetic images, applies a Gaussian blur, and zips the processed images. This demonstrates remote data processing capabilities. ```bash cat <<'EOF' | uv run colab --auth=adc exec -s data-proc # (would normally pull from GCS via 'gcloud storage cp gs://my-bucket/raw_data/*.jpg') import os, zipfile from PIL import Image, ImageFilter os.makedirs('/content/images', exist_ok=True) os.makedirs('/content/processed', exist_ok=True) # Generate 10 synthetic input images for i in range(10): Image.new('RGB', (64, 64), (i * 25, 100, 200 - i * 15)).save(f'/content/images/img_{i:02d}.jpg') # Process: blur each for src in sorted(os.listdir('/content/images')): img = Image.open(f'/content/images/{src}').filter(ImageFilter.GaussianBlur(2)) img.save(f'/content/processed/{src}') # Zip results with zipfile.ZipFile('/content/processed/batch.zip', 'w') as z: for f in sorted(os.listdir('/content/processed')): if f.endswith('.jpg'): z.write(f'/content/processed/{f}', f) print(f'Processed {len(os.listdir("/content/processed")) - 1} images, archived to batch.zip') EOF ``` -------------------------------- ### Re-authenticate with Specific Scopes using gcloud Source: https://github.com/googlecolab/google-colab-cli/blob/main/docs/04_automation_and_utility.md Use this command when your user credentials from `gcloud auth application-default login` ignore the scopes argument and raise an error on `with_scopes`. This ensures the CLI has the necessary permissions to interact with Colab services. ```bash gcloud auth application-default login \ --scopes=openid,\n https://www.googleapis.com/auth/cloud-platform,\n https://www.googleapis.com/auth/userinfo.email,\n https://www.googleapis.com/auth/colaboratory ```