Jupyterlab / app.py
zyxciss's picture
Update app.py
f5f6658 verified
raw
history blame
1.35 kB
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()