Friday, December 27, 2024
Google search engine
HomeLanguagesHow to run Flask App on Google Colab?

How to run Flask App on Google Colab?

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:

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

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.

ngrok_auth_token_image

ngrok_authentication_token_page

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

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()


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

Output:

Click on this link to get the output:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments