Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to run Flask App on Google Colab?

  • Last Updated : 01 Feb, 2021

Flask is a web framework in python language. This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a machine learning webapp.

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.

Attention reader! Don’t stop learning now. Get hold of all the important Machine Learning Concepts with the Machine Learning Foundation Course at a student-friendly price and become industry ready.



How to use Flask on Google Colab?

To create google colab file go to this link:



https://colab.research.google.com/notebooks/intro.ipynb#recent=true

1) Create new notebook in google colab

2) Install library in google colab

!pip install flask-ngrok

Flask is already install on google colab so you don’t need to install it again.

3) After that lets create a simple flask app

Python3




from flask import Flask
from flask_ngrok import run_with_ngrok
app = Flask(__name__)
run_with_ngrok(app)   
  
@app.route("/")
def home():
    return "<h1>GFG is great platform to learn</h1>"
    
app.run()

4) To run the code on google colab press shift+enter.

Output:

Click on this link to get the output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!