Run Web Application on Google Colab
Port
Command
Unused Port
import portpicker
port = portpicker.pick_unused_port()
Or
Custom Port
port = 8000
Custom Port
port = 8000 port = 8000
select_ip = "0.0.0.0:"+str(port)
Port
Command
As Local
from google.colab import output
output.serve_kernel_port_as_window(port)
Or
As Proxy
from google.colab.output import eval_js
print(eval_js( "google.colab.kernel.proxyPort(" + str(port) + ")" ))
Set port and run
python3 app.py
or
!flask run --host $select_ip --port $port
python -m http.server $port
or
python3 manage.py runserver $select_ip
Full Code: Locally with Custom Port
from google.colab import output
port = 8000
output.serve_kernel_port_as_window(port)
python -m http.server $port
Full Code: Proxy with Unused Port
import portpicker
from google.colab.output import eval_js
port = portpicker.pick_unused_port()
select_ip = "0.0.0.0:"+str(port)
print(eval_js( "google.colab.kernel.proxyPort(" + str(port) + ")" ))
python3 manage.py runserver $select_ip
All in one Class
from google.colab import output
import portpicker
from google.colab.output import eval_js
class ServerURL:
def __init__(self,run_type):
self.run_type = run_type
self.default_port = 8000
self.default_ip = "0.0.0.0"
if self.run_type == "local":
self.run_local()
elif self.run_type == "proxy":
self.run_proxy()
else:
print("Error")
def get_port(self):
return self.default_port
def run_local(self):
output.serve_kernel_port_as_window(self.default_port)
def run_proxy(self):
self.default_port = portpicker.pick_unused_port()
print(eval_js( "google.colab.kernel.proxyPort(" + str(self.default_port) + ")" ))
S = ServerURL("local")
port = S.get_port()
flask run --host 0.0.0.0 --port $port
am not able to open admin panel.
from google.colab.output import eval_js
print(eval_js("google.colab.kernel.proxyPort(8000)"))
https://us3anlz24pp-496ff2e9c6d22116-8000-colab.googleusercontent.com/admin -- is not working.. any suggestions?