Flask on Google Colab

Kshitij Pawar
2 min readOct 28, 2019

--

Today we’ll learn how we can run a development server on Google Colab to test(or develop) Flask web applications.

I frequently use Google Colab thanks to its Tesla K80 support. When working on a project, if you want to create an API for your model or e-commerce site, Flask or Django are some of the options. As for back-end development, Flask comes easy to me.

Google Colab provides a VM(virtual machine) so we cannot access the localhost(all it does it route it to our local machine’s localhost) as we do on our local machine when running a local web server. What we can do is expose it to a public URL using ngrok. Here comes the Python library flask-ngrok.

Let us first install flask-ngrok in our Colab notebook:

!pip install flask-ngrok

After the library is installed let's create a simple Flask app with flask-ngrok:

from flask_ngrok import run_with_ngrok
from flask import Flask
app = Flask(__name__)
run_with_ngrok(app) #starts ngrok when the app is run
@app.route("/")
def home():
return "<h1>Running Flask on Google Colab!</h1>"

app.run()

While the cell is running:

All the requests (GET or POST) i.e., traffic is also the part of the output

A secure URL is provided for the server running on Colab. Traffic stats can be used to debug in case of any errors in your web app.

This method proves useful in ways to demonstrate your web app to your peers before deploying it into production. Especially in projects involving the requirement of high computational power (object detection, image classification).

--

--