# SPDX-License-Identifier: AGPL-3.0-only # Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0 """ Colab-specific helpers for running Unsloth Studio. Uses Colab's built-in proxy - no external tunneling needed! """ from pathlib import Path import sys # Fix for Anaconda/conda-forge Python: seed platform._sys_version_cache before # any library imports that trigger attrs -> rich -> structlog -> platform crash. # See: https://github.com/python/cpython/issues/102396 _backend_dir = str(Path(__file__).parent) if _backend_dir not in sys.path: sys.path.insert(0, _backend_dir) import _platform_compat # noqa: F401 from loggers import get_logger logger = get_logger(__name__) def get_colab_url(port: int = 8888) -> str: """ Get the actual Colab proxy URL for a port. """ try: from google.colab.output import eval_js # Use Colab's proxy mechanism url = eval_js(f"google.colab.kernel.proxyPort({port})", timeout_sec = 5) return url if url else f"http://localhost:{port}" except Exception as e: logger.info(f"Note: Could not get Colab URL ({e})") return f"http://localhost:{port}" def show_link(port: int = 8888): """Display a styled clickable link to the UI.""" from IPython.display import display, HTML # Get real Colab proxy URL url = get_colab_url(port) short_url = ( url[: url.index("-", url.index(f"{port}-") + len(str(port)) + 1) + 1] + "..." if f"{port}-" in url else url ) html = f"""
If the link doesn't work, you can scroll down to view the UI generated directly in Colab.
{short_url}