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:
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:
Start the server:
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.
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:
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
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.
Related reading
- Running Tensorflow in Jupyter Notebook
- RuntimeError main thread is not in main loop with Matplotlib and Flask
- RuntimeError module compiled against API version 0xc but this version of numpy is 0xb
- Sampling without replacement from a given non-uniform distribution in TensorFlow
- Run manage.py from AWS EB Linux instance
- Run py.test test in different process
- Save Dataframe to csv directly to s3 Python
- Save plot to image file instead of displaying it
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.