-
Notifications
You must be signed in to change notification settings - Fork 1
/
colab.py
94 lines (73 loc) · 2.52 KB
/
colab.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import subprocess
from importlib import import_module
def run_foreground(cmd: str) -> None:
"""
Run a bash command in foreground.
Reference: http://blog.kagesenshi.org/2008/02/teeing-python-subprocesspopen-output.html
Args:
cmd: Bash command
Returns:
None
"""
p = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
while True:
line = p.stdout.readline()
print(line.strip())
if line == "" and p.poll() is not None:
break
return None
def run_background(command: str) -> None:
"""
Run a bash command in background.
Args:
command: Bash command
Returns:
None
"""
subprocess.Popen(command, shell=True)
def jupyter(subdomain: str, port: int = 9003) -> None:
"""
Start a jupyter notebook server using localtunnel.
Returns:
None
"""
command = f"jupyter-notebook --ip='*' --no-browser --allow-root --port 9003 & npx localtunnel -p {port} -s {subdomain} --allow-invalid-cert"
run_foreground(command)
def vscode(
subdomain: str = "amitness",
port: int = 9000,
config_save_path: str = "/content/drive/MyDrive/colab/.vscode",
) -> None:
"""
Start VSCode server which persists all settings and extensions.
Args:
subdomain: Subdomain for localtunnel.
port: Port for running code-server
config_save_path: Path in Google Drive to save VSCode settings
Returns:
None
"""
drive = import_module("google.colab.drive")
drive.mount("/content/drive")
subprocess.run(["curl", "-fsSL", "https://code-server.dev/install.sh", "-O"])
subprocess.run(["bash", "install.sh", "--version", "3.10.2"])
subprocess.run(["pip3", "install", "flake8", "--user"])
subprocess.run(["pip3", "install", "black", "--user"])
print(f"https://{subdomain}.loca.lt/?folder=/content/drive/MyDrive/colab")
run_foreground(
f"code-server --port {port} --auth none --disable-telemetry --force --user-data-dir {config_save_path} & npx localtunnel -p {port} -s {subdomain} --allow-invalid-cert"
)
def expose_port(port: int, path: str = "/") -> None:
"""
Expose port as an external URL.
The URL is only accessible to you and available till the notebook runs.
Args:
port: Port a service is running on
path: Path the service is running on
Returns:
None
"""
output = import_module("google.colab.output")
output.serve_kernel_port_as_window(port, path=path)