Uvicorn colored output does not work on Windows

Viewed 129

I'm using fastapi in Python and want to run things like normal with Uvicorn. I did a full install of fastapi with pip install "fastapi[all]" but the Uvicorn colored terminal doesn't work in Command Prompt or Powershell:

enter image description here

Should look like:

enter image description here

I understand this is because Windows doesn't use the ANSI color sequences but the Uvicorn docs say it uses colorama to provide this support (which I've run separately and works).

As a workaround, I can run Uvicorn with the --no-use-colors flag to remove the color flags and make the output readable, but I'd really like coloring if possible.

uvicorn main:app --reload --no-use-colors

2 Answers

If you run uvicorn programmatically, importing colorama and initializing with:

import colorama
colorama.init()

before importing FastAPI seems to get colors working in cmd or powershell on Windows.

As cdoolin mentioned, you can use colorama. Alternatively, you can enable ANSI support permanently or pipe the output to Out-Host:

uvicorn main:app --reload --no-use-colors | Out-Host
Related