How to run Flask App on Google Colab?


Google Colab, a well-known cloud-based Python programming setting, offers users skills to write and run code straight in a web browser. Even though Google Colab is typically utilised for the Analysis of data and machine learning projects, Flask apps can also be run there. We will examine the procedures needed to set up and operate a Flask application on Google Colab in this blog article.

Setting up Google Colab

Launch your web browser and navigate to https://colab.research.google.com/ in order to get started. Sign in employing your Google account or create one if required.

Install Flask

Python's Flask web framework is well-liked. Thankfully, setting up Flask on Google Colab is simple. The following command can be used to install Flask on a Colab notebook −

!pip install flask

With this command, Flask and all of its dependencies will be downloaded and installed.

Create a Simple Flask App

After installing Flask, let's build a basic Flask application. Import Flask into a brand-new Colab notebook cell and specify a fundamental route −

from flask import Flask
from flask_ngrok import run_with_ngrok

app = Flask(__name__)
run_with_ngrok(app)  # Initialize ngrok when the app is run

@app.route('/')
def hello():
    return "Hello, Flask on Google Colab!"

if __name__ == '__main__':
    app.run()

Run the Flask App on Google Colab

We must carry out the subsequent actions in order to run the Flask application on Google Colab −

  • Bind the Flask application to a public host and port. Add the following lines of code to a brand-new Colab notebook cell 

import os

# Bind to 0.0.0.0 to make the Flask app accessible from outside the Colab environment
app.run(host='0.0.0.0', port=8080)
  • Start the cell. On the supplied host and port, the Flask application is launched.

  • Check for the message "Running on http://0.0.0.0:8080/" to appear in the final output. This shows that the Flask application is functioning properly.

Accessing the Flask App

We require a mechanism to get to the Flask application now that it is operating on Google Colab. Take the following steps −

  • Select the "Runtime" menu from the Colab notebook's top menu.

  • Choose "Change runtime type."

  • Choose "GPU" or "None" as appropriate in the "Runtime type" column.

  • Down scroll and select "Save."

  • Copy the URL, for example, "http://0.0.0.0:8090/".

  • Paste the URL you obtained in Step 6 in the new browser.

  • Done! The "Hello, Flask on Google Colab!" message ought now to appear in the browser.

Conclusion

You may use Google Colab to run a Flask app and take use of the powerful cloud-based setting for web creation and evaluation. We described how to set up and operate a Flask app on Google Colab in our blog article. It's important to recall how to set up Flask, construct a straightforward app, connect it to a public host and port, and then use it employing the supplied URL. By using Google Colab's flexibility and simplicity for your website-building tasks, you may discover and develop more intricate Flask apps with this expertise.

Updated on: 31-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements