Skip to content

Instantly share code, notes, and snippets.

@arifsuhan
Last active July 19, 2024 16:45
Show Gist options
  • Save arifsuhan/0566da9a3e0383a48643ba5dfb10cdc0 to your computer and use it in GitHub Desktop.
Save arifsuhan/0566da9a3e0383a48643ba5dfb10cdc0 to your computer and use it in GitHub Desktop.
A general way to run Web Applications on Google Colab

Run Web Application on Google Colab

Port Selection

Port Command
Unused Port import portpicker
port = portpicker.pick_unused_port()
Or
Custom Port port = 8000
Custom Port port = 8000 port = 8000

Define IP

select_ip = "0.0.0.0:"+str(port)

Url Generate

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) + ")" ))

Start Server

Flask

Set port and run

python3 app.py
or 
!flask run --host $select_ip --port $port

Django

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
  • ServerURL 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) + ")" ))
  • driver class
S = ServerURL("local")
port = S.get_port()
flask run --host 0.0.0.0 --port $port
@arifsuhan
Copy link
Author

arifsuhan commented Oct 12, 2021

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?

  • Did you start your server? Seems working fine in my side.

Screen Recording 2021-10-12 at 10 37 41 PM

  • If server is on and still doesn't work. Then try running locally instead of proxy
from google.colab import output output.serve_kernel_port_as_window(8000)

If nothing works. Then final solution:

  1. Do Local tunneling: ngrok with colab
    or
  2. Try Google cloud shell: https://shell.cloud.google.com/ and in shell use "web-preview"

@anu-anitha
Copy link

i have no issues accessing the home page. am not able to access django admin panel.

@anu-anitha
Copy link

i have taken the project and ran in my local, am able to access admin panel. it is issue from colab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment