How to run Flask App on Google Colab?

Google Colab, a cloud-based Python programming environment, allows users to write and run code directly in a web browser. While Google Colab is typically used for data analysis and machine learning projects, you can also run Flask applications there. This article explores the steps needed to set up and run a Flask application on Google Colab.

Setting up Google Colab

To get started, launch your web browser and navigate to https://colab.research.google.com/. Sign in using your Google account or create one if required.

Install Flask and Required Packages

Flask is a popular Python web framework. Installing Flask on Google Colab is straightforward. Use the following command to install Flask and additional packages needed for Colab deployment ?

!pip install flask pyngrok

This command downloads and installs Flask along with pyngrok, which allows external access to your Flask app running in Colab.

Create a Simple Flask App

After installing the packages, let's create a basic Flask application with ngrok integration for external access ?

from flask import Flask
from pyngrok import ngrok

# Create Flask app
app = Flask(__name__)

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

@app.route('/about')
def about():
    return "This Flask app is running on Google Colab!"

# Set up ngrok tunnel
public_url = ngrok.connect(5000)
print(f"Public URL: {public_url}")

# Run the app
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

The output will show something like ?

Public URL: https://abc123.ngrok.io
 * Running on all addresses.
 * Running on http://127.0.0.1:5000/
 * Running on http://172.28.0.2:5000/

Alternative Method: Using Colab's Built-in Port Forwarding

Google Colab also provides built-in port forwarding without requiring external tools ?

from flask import Flask
import threading

app = Flask(__name__)

@app.route('/')
def home():
    return "<h1>Flask App Running on Colab!</h1><p>Access successful.</p>"

# Run Flask in a separate thread
def run_app():
    app.run(host='0.0.0.0', port=5000, debug=False)

# Start the Flask app in background
thread = threading.Thread(target=run_app)
thread.daemon = True
thread.start()

print("Flask app is running. Use Colab's port forwarding to access it.")

After running this code, click on the port icon (?) in Colab's output to access your Flask application.

Comparison of Methods

Method External Access Setup Complexity Best For
ngrok Public URL Requires installation Sharing with others
Colab Port Forwarding Limited to your session Built-in Personal testing

Key Points to Remember

  • Always bind to 0.0.0.0 to make your Flask app accessible from outside the Colab environment

  • Use debug=False in production-like environments

  • The ngrok tunnel will remain active only while your Colab session is running

  • Colab sessions have time limits, so your Flask app will stop when the session ends

Conclusion

Running Flask applications on Google Colab is straightforward using either ngrok for public access or Colab's built-in port forwarding. This setup is ideal for quick prototyping and testing Flask applications in a cloud environment without local setup requirements.

Updated on: 2026-03-27T10:38:22+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements