File size: 1,346 Bytes
f5f6658
95d0316
f5f6658
95d0316
 
 
f5f6658
 
 
95d0316
f5f6658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95d0316
 
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
import os
import subprocess
import time
import gradio as gr

def launch_jupyter():
    # 1. Install Node.js and LocalTunnel (runtime, not Dockerfile)
    os.system("apt-get update && apt-get install -y nodejs npm")
    os.system("npm install -g localtunnel")

    # 2. Set a secure token (you can change this or generate dynamically)
    token = "letmein123"

    # 3. Launch JupyterLab with token authentication
    subprocess.Popen([
        "jupyter", "lab",
        "--ip=127.0.0.1",
        "--port=8888",
        "--no-browser",
        f"--NotebookApp.token={token}",
        "--NotebookApp.allow_origin='*'",
        "--NotebookApp.allow_remote_access=True"
    ])

    time.sleep(5)  # Give Jupyter some time to boot

    # 4. Start LocalTunnel to expose port 8888
    proc = subprocess.Popen(
        ["lt", "--port", "8888", "--subdomain", "zyxciss-jlab"],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True
    )

    # 5. Extract and return the LocalTunnel URL
    for line in proc.stdout:
        if "your url is:" in line:
            url = line.strip().split("your url is:")[1].strip()
            return f"🔓 Access JupyterLab here:\n{url}?token={token}"

    return "❌ Failed to start LocalTunnel."

# Gradio UI
demo = gr.Interface(fn=launch_jupyter, inputs=[], outputs="text")
demo.launch()