Python can open a webbrowser for you

2025-09-04

I have a line in my justfile that goes like this:

# Quick HTTP server on port
serve port="8000":
    python3 -m http.server {{port}}

This is nice because this lets me host the current directory on a specific port by typing just serve 12345.

But ... we can do an upgrade!

# Quick HTTP server on port
serve port="8000":
    python3 -m webbrowser -t "http://localhost:{{port}}"
    python3 -m http.server {{port}}

This way, I don't have to manually go to the browser to open the served page in a new tab. It's a bit of an unknown fact, but Python ships with a webbrowser module that can handle this for you.

It's a minor edit, but automation is great! Go use it!