I would like to develop web apps in Google colab. The only issue is that you need a browser connected to local host to view the web app, but Google colab doesn\'t have a bro
The below solution, explains
To run the script in the background, use below code, that will output a link that looks like https://wrea1crizb-496ff2e9c6d22116-8888-colab.googleusercontent.com/ through which output can be seen on a web browser.
!pip install CherryPy #webserver package
#bind the port 8888 and get a weblink to access
from google.colab.output import eval_js
print(eval_js("google.colab.kernel.proxyPort(8888)"))
#run the script/API in the background
import subprocess
subprocess.Popen(["python", "/content/test.py", "8888"])
Create test.py file and add the below code,
import cherrypy
import sys
class HelloWorld:
def index(self):
return "Hello World!"
index.exposed = True
if __name__ == '__main__':
config = {'server.socket_host': '0.0.0.0','server.socket_port' : int(sys.argv[1])}
cherrypy.config.update(config)
cherrypy.quickstart(HelloWorld())