zyxciss commited on
Commit
ea72295
·
verified ·
1 Parent(s): cdbccce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -47
app.py CHANGED
@@ -1,48 +1,49 @@
1
- import os
2
  import subprocess
3
- import time
4
- import gradio as gr
5
-
6
- def launch_jupyter():
7
- # Only install nodejs + localtunnel once
8
- os.system("apt-get update && apt-get install -y nodejs") # npm comes bundled
9
- os.system("npm install -g localtunnel")
10
- os.system("apt install curl -y")
11
- os.system("curl -sSf https://sshx.io/get | sh")
12
- os.system("sshx")
13
- token = "letmein123"
14
-
15
- # Launch JupyterLab with authentication and allow remote access
16
- subprocess.Popen([
17
- "jupyter", "lab",
18
- "--ip=127.0.0.1",
19
- "--port=6600",
20
- "--no-browser",
21
- f"--ServerApp.token={token}",
22
- "--ServerApp.allow_origin='*'",
23
- "--ServerApp.allow_remote_access=True"
24
- ])
25
-
26
- time.sleep(5) # Give time for Jupyter to spin up
27
-
28
- # Launch localtunnel
29
- proc = subprocess.Popen(
30
- ["lt", "--port", "6600"],
31
- stdout=subprocess.PIPE,
32
- stderr=subprocess.STDOUT,
33
- text=True
34
- )
35
-
36
- for line in proc.stdout:
37
- if "your url is:" in line:
38
- url = line.strip().split("your url is:")[1].strip()
39
- return f"""
40
- <h3>✅ JupyterLab Running</h3>
41
- <p>Public URL: <a href="{url}?token={token}" target="_blank">{url}?token={token}</a></p>
42
- <iframe src="{url}?token={token}" width="100%" height="600px" style="border: none;"></iframe>
43
- """
44
-
45
- return "<p>❌ Failed to start LocalTunnel or retrieve its URL</p>"
46
-
47
- demo = gr.Interface(fn=launch_jupyter, inputs=[], outputs=gr.HTML())
48
- demo.launch()
 
 
1
+ # run_jupyterlab.py
2
  import subprocess
3
+ import sys
4
+ import os
5
+ import signal # For handling termination signals
6
+
7
+ def launch_jupyterlab():
8
+ """Launches JupyterLab programmatically on port 7860 and waits for it to finish."""
9
+ print("Attempting to launch JupyterLab on port 7860...")
10
+
11
+ command = [
12
+ sys.executable,
13
+ "-m",
14
+ "jupyterlab",
15
+ "--no-browser", # Remove this line if you want it to open automatically in your browser
16
+ "--port",
17
+ "7860",
18
+ "--ip=0.0.0.0" # IMPORTANT: Make sure it listens on all interfaces inside the container
19
+ ]
20
+
21
+ # Optional: Uncomment and modify if you want to specify a working directory
22
+ # command.extend(["--notebook-dir", "/path/to/your/notebooks"])
23
+
24
+ try:
25
+ print(f"Executing command: {' '.join(command)}")
26
+ # Use subprocess.Popen to run the command
27
+ process = subprocess.Popen(command)
28
+
29
+ print("\nJupyterLab launched! Check your terminal for the URL.")
30
+ print("You should see a URL like 'http://localhost:7860/lab?token=...'")
31
+ print("Copy and paste this URL into your web browser to access JupyterLab.")
32
+ print("Keep this terminal window open (or container running) while you are using JupyterLab.")
33
+
34
+ # --- IMPORTANT CHANGE HERE ---
35
+ # Wait for the JupyterLab process to terminate.
36
+ # This keeps the Python script (and thus the container's main process) alive.
37
+ print("\nWaiting for JupyterLab process to terminate...")
38
+ process.wait()
39
+ print("JupyterLab process terminated.")
40
+
41
+ except FileNotFoundError:
42
+ print("Error: 'jupyterlab' command not found.")
43
+ print("Please ensure JupyterLab is installed. You can install it with:")
44
+ print("pip install jupyterlab")
45
+ except Exception as e:
46
+ print(f"An error occurred while launching JupyterLab: {e}")
47
+
48
+ if __name__ == "__main__":
49
+ launch_jupyterlab()