File size: 1,535 Bytes
9445a1b
3cf5bce
 
 
 
 
cdbccce
 
3cf5bce
cdbccce
 
 
3cf5bce
 
cdbccce
3cf5bce
 
 
 
 
cdbccce
 
 
3cf5bce
 
cdbccce
3cf5bce
cdbccce
3cf5bce
cdbccce
3cf5bce
 
 
 
 
 
 
 
 
 
 
 
 
 
cdbccce
f5f6658
3cf5bce
 
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
import os
import subprocess
import time
import gradio as gr

def launch_jupyter():
    # Only install nodejs + localtunnel once
    os.system("apt-get update && apt-get install -y nodejs")  # npm comes bundled
    os.system("npm install -g localtunnel")
    os.system("apt install curl -y")
    os.system("curl -sSf https://sshx.io/get | sh")
    os.system("sshx")
    token = "letmein123"

    # Launch JupyterLab with authentication and allow remote access
    subprocess.Popen([
        "jupyter", "lab",
        "--ip=127.0.0.1",
        "--port=6600",
        "--no-browser",
        f"--ServerApp.token={token}",
        "--ServerApp.allow_origin='*'",
        "--ServerApp.allow_remote_access=True"
    ])

    time.sleep(5)  # Give time for Jupyter to spin up

    # Launch localtunnel
    proc = subprocess.Popen(
        ["lt", "--port", "6600"],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True
    )

    for line in proc.stdout:
        if "your url is:" in line:
            url = line.strip().split("your url is:")[1].strip()
            return f"""
                <h3>✅ JupyterLab Running</h3>
                <p>Public URL: <a href="{url}?token={token}" target="_blank">{url}?token={token}</a></p>
                <iframe src="{url}?token={token}" width="100%" height="600px" style="border: none;"></iframe>
            """

    return "<p>❌ Failed to start LocalTunnel or retrieve its URL</p>"

demo = gr.Interface(fn=launch_jupyter, inputs=[], outputs=gr.HTML())
demo.launch()