Run localhost server in Google Colab notebook
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Running a web server in Google Colab is possible, but localhost behaves differently than it does on your laptop. The notebook is running on a remote machine, so a server started on 127.0.0.1 is local to the Colab runtime, not directly visible in your browser unless you expose it through a proxy or tunnel.
Understand What localhost Means in Colab
In a normal local development setup, opening http://127.0.0.1:5000 in the browser reaches a server running on your own machine. In Colab, the code runs on a hosted VM, so that same address refers to the VM itself.
That means this pattern works inside the notebook process:
But opening http://127.0.0.1:8000 in your local browser will not reach that server, because your browser is not on the Colab VM.
A Practical Flask Example
For app-style experiments, Flask is a simple choice. In a notebook, run the server in a background thread so the cell does not block the entire notebook.
Install the packages first:
Then start the server:
Using 0.0.0.0 allows the server to listen on the runtime's network interface. It still is not public yet, but it is now available for a tunnel to forward.
Exposing the Server With pyngrok
The usual way to reach the app from your browser is to create a public tunnel. pyngrok gives you a simple Python interface to ngrok.
Once that prints a URL, open it in your browser and the request will be forwarded to the Flask app running inside Colab.
If your ngrok account requires an auth token, configure it first:
This is usually the most reliable way to test a notebook-hosted web app from outside the Colab runtime.
A Minimal End-to-End Example
Putting the pieces together, the full notebook flow looks like this:
That gives you a temporary public entry point to the notebook server. It is good for demos, small experiments, and validating that request handling works.
When This Approach Makes Sense
Running a server in Colab is best for short-lived experiments:
- testing a lightweight Flask endpoint,
- previewing a notebook-driven demo,
- validating a model inference endpoint quickly.
It is not a substitute for a real deployment. Colab runtimes can reset, go idle, or disconnect, which means the server and the public URL disappear with the session.
For anything persistent, deploy the app to an actual hosting environment instead of treating Colab as a web server platform.
Common Pitfalls
- Expecting
localhostin the notebook to be reachable directly from your own browser. - Running
app.run()in the foreground and blocking the notebook session. - Forgetting to expose the port through a tunnel or proxy.
- Assuming the tunnel URL will remain stable after the runtime restarts.
- Putting secrets into notebook cells when configuring public tunnels.
Summary
- A server can run inside Google Colab, but
localhostrefers to the remote runtime, not your computer. - Use a background thread so the notebook stays interactive.
- Expose the port with a tool such as
pyngrokif you need browser access. - Treat the setup as temporary and experimental, not production hosting.
- Keep security in mind, because tunneling makes the notebook service reachable from outside the runtime.
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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.