Spaces:
Running
Running
# 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() | |