Open In App
Related Articles

How to run Flask App on Google Colab?

Improve Article
Save Article
Like Article

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.

How to use Flask on Google Colab?

To create google colab file go to this link:

1) Create new notebook in google colab

2) Install flask-ngrok library in google colab

3) Nowadays, ngrok needs an authentication token to run apps on their server. So, you need to log in to ngrok website and get the authentication token. It will look something like the below image.



After getting the authentication token, just copy and paste it into the provided space.

!pip install flask-ngrok
!ngrok authtoken 'your_auth_token'

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

4) After that let’s create a simple Flask app


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

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


Click on this link to get the output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 24 Aug, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials