We're used to the local development dance: create a virtual environment, pip install dependencies, run flask run, and test on localhost. For sharing, we use Git, Docker, or deployment platforms.
But what if you could skip all that for quick prototypes? What if you could spin up a full, sandboxed Linux machine in a browser tab and serve a real Flask API from it?
Thanks to WebAssembly, this is now a reality. A platform called Stacknow uses this tech to give you an instant Linux environment. Let's walk through how to serve a simple Flask API from it in under 5 minutes.
Step 1: Launch Your Linux Environment
No installation needed. Just go to the Stacknow Console and launch the Python environment.
You'll be greeted with a familiar IDE layout: a file explorer, a code editor, and a bash terminal at the bottom. This is your temporary, sandboxed Linux machine.
Step 2: The One "Magic" Trick: Unix Sockets
Here’s the one key difference from your local setup. Inside this sandboxed environment, your web app can't bind to a TCP port like 0.0.0.0:5000.
Instead, it must listen on a Unix socket, which is essentially a special file that processes can use to communicate. The Stacknow platform automatically detects services listening on a specific socket path and securely exposes them to the internet for you.
We'll use Gunicorn as our WSGI server because it can easily bind to a Unix socket.
Step 3: Create a Simple Flask App
In the IDE's file explorer, create a new file named app.py and paste in the following code. It's a minimal Flask app with the Gunicorn logic included.
from flask import Flask
import os
from gunicorn.app.wsgiapp import WSGIApplication
# Part 1: Our super simple Flask App
app = Flask(__name__)
@app.route("/")
def hello():
"""Our API endpoint."""
return {"message": "Hello from a Flask API served from the browser! 🚀"}
# Part 2: The logic to run Gunicorn programmatically
class StandaloneApplication(WSGIApplication):
"""A helper class to run Gunicorn with our Flask app."""
def __init__(self, app_uri, options=None):
self.options = options or {}
self.app_uri = app_uri
super().__init__()
def load_config(self):
config = {key: value for key, value in self.options.items() if key in self.cfg.settings and value is not None}
for key, value in config.items():
self.cfg.set(key.lower(), value)
def load(self):
return self.load_wsgiapp()
# Part 3: The main execution block
if __name__ == '__main__':
# Stacknow provides a unique ID for our sandbox session.
# We use it to build the required socket path.
sandbox_uuid = os.environ.get('SANDBOX_UUID')
if sandbox_uuid:
socket_path = f"/tmp/{sandbox_uuid}/app.sock"
# Configure Gunicorn to bind to our Unix socket
options = {
'bind': f'unix:{socket_path}',
'workers': 2,
}
# Run the server!
StandaloneApplication('app:app', options).run()
else:
# This is a fallback to run locally if not in the Stacknow env
app.run(debug=True, port=5001)
pip install Flask gunicorn
run the app in background
python3 app.py &
test the api server using curl
curl --unix-socket /tmp/$SANDBOX_UUID/app.sock http://localhost/
The terminal will show Gunicorn starting up. In the IDE, a panel will appear with a URL for your service. Click it, and you'll see your API's JSON response live in a new tab.
You just served a Python web API from a Linux machine that only exists inside your browser.
Why is this cool?
Zero Setup: Instantly create a clean environment for any project.
Easy Sharing: Share the link to your running sandbox for live demos or collaboration.
Safe Prototyping: Test out new ideas or untrusted libraries in a completely isolated space.
No More "It Works On My Machine": Everyone gets the exact same environment.
This is a powerful new way to handle quick development tasks. Give it a try https://stacknow.io
Top comments (0)