File size: 1,921 Bytes
ea72295
3cf5bce
ea72295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# run_jupyterlab.py
import subprocess
import sys
import os
import signal # For handling termination signals

def launch_jupyterlab():
    """Launches JupyterLab programmatically on port 7860 and waits for it to finish."""
    print("Attempting to launch JupyterLab on port 7860...")

    command = [
        sys.executable,
        "-m",
        "jupyterlab",
        "--no-browser", # Remove this line if you want it to open automatically in your browser
        "--port",
        "7860",
        "--ip=0.0.0.0" # IMPORTANT: Make sure it listens on all interfaces inside the container
    ]

    # Optional: Uncomment and modify if you want to specify a working directory
    # command.extend(["--notebook-dir", "/path/to/your/notebooks"])

    try:
        print(f"Executing command: {' '.join(command)}")
        # Use subprocess.Popen to run the command
        process = subprocess.Popen(command)

        print("\nJupyterLab launched! Check your terminal for the URL.")
        print("You should see a URL like 'http://localhost:7860/lab?token=...'")
        print("Copy and paste this URL into your web browser to access JupyterLab.")
        print("Keep this terminal window open (or container running) while you are using JupyterLab.")

        # --- IMPORTANT CHANGE HERE ---
        # Wait for the JupyterLab process to terminate.
        # This keeps the Python script (and thus the container's main process) alive.
        print("\nWaiting for JupyterLab process to terminate...")
        process.wait() 
        print("JupyterLab process terminated.")

    except FileNotFoundError:
        print("Error: 'jupyterlab' command not found.")
        print("Please ensure JupyterLab is installed. You can install it with:")
        print("pip install jupyterlab")
    except Exception as e:
        print(f"An error occurred while launching JupyterLab: {e}")

if __name__ == "__main__":
    launch_jupyterlab()