Google Colab
localhost server
Python
Jupyter notebook
web development

Run a localhost server in a Google Colab notebook

Codemia Knowledge Hub ·

You can run a web server in Google Colab, but localhost there means the remote Colab runtime, not your laptop. A server on 127.0.0.1 is visible only inside that VM, so to open it in your browser you need a proxy or tunnel such as ngrok.

What localhost means in Colab

Locally, http://127.0.0.1:5000 reaches a server on your own machine. In Colab the code runs on a hosted VM, so that address refers to the VM. This works inside the notebook process:

python
1from http.server import HTTPServer, SimpleHTTPRequestHandler
2
3server = HTTPServer(("127.0.0.1", 8000), SimpleHTTPRequestHandler)
4print("Server running on 127.0.0.1:8000")
5server.serve_forever()

But opening http://127.0.0.1:8000 in your browser will not reach it, because your browser is not on the Colab VM.

Running Flask in a background thread

Flask is a simple choice for app-style experiments. Run it in a background thread so the cell does not block the notebook, which is what happens if you call app.run() in the foreground.

Install the packages:

python
%pip install flask pyngrok

Start the server:

python
1from flask import Flask
2from threading import Thread
3
4app = Flask(__name__)
5
6
7@app.route("/")
8def index():
9    return "Hello from Colab"
10
11
12def run_app():
13    app.run(host="0.0.0.0", port=5000)
14
15
16thread = Thread(target=run_app)
17thread.daemon = True
18thread.start()

0.0.0.0 makes the server listen on the runtime's network interface. It is not public yet, but a tunnel can now forward to it.

Exposing the port with pyngrok

To reach the app from your browser, create a public tunnel. pyngrok is a Python interface to ngrok.

python
1from pyngrok import ngrok
2
3public_url = ngrok.connect(5000)
4print(public_url)

Open the printed URL and requests are forwarded to the Flask app inside Colab. If your ngrok account requires an auth token, set it first:

python
from pyngrok import ngrok

ngrok.set_auth_token("YOUR_NGROK_TOKEN")

Avoid leaving secrets like this token in notebook cells. This is usually the most reliable way to test a notebook-hosted web app from outside the runtime.

Complete notebook example

python
1%pip install flask pyngrok
2
3from flask import Flask
4from threading import Thread
5from pyngrok import ngrok
6
7app = Flask(__name__)
8
9
10@app.route("/")
11def index():
12    return "Server is running"
13
14
15def run():
16    app.run(host="0.0.0.0", port=5000)
17
18
19Thread(target=run, daemon=True).start()
20public_url = ngrok.connect(5000)
21
22print(f"Open this URL: {public_url}")

This gives you a temporary public entry point to the notebook server, good for demos, small experiments, and checking that request handling works. Because the tunnel makes the service reachable from outside the runtime, keep security in mind.

Limits of serving from Colab

Colab servers suit short-lived experiments:

  • testing a lightweight Flask endpoint,
  • previewing a notebook-driven demo,
  • validating a model inference endpoint quickly.

They are not a deployment. Colab runtimes can reset, go idle, or disconnect, and the server and public URL disappear with the session; do not expect the tunnel URL to survive a runtime restart. For anything persistent, deploy the app to a real hosting environment.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design