Skip to main content

Brazil Election Forecast — Get the Code

The complete code from How to Set Up a Machine Learning or Data Science Project.

What’s inside #

  • data.py — fetch and clean a Wikipedia polling table
  • model.py — the method: average last N polls, predict the winner
  • validate.py — checks the method against the 2018 and 2022 runoffs
  • main.py — orchestrates validation + the 2026 forecast, writes forecast.json
  • present.py — renders forecast.json into a deployable page
  • .github/workflows/forecast.yml — reruns the forecast on a schedule
  • .gitlab-ci.yml — the CI setup that builds and deploys the live site
  • test_*.py — the full test suite, including a live test against the deployed URL
  • COMMIT.txt — the exact commit this zip was built from
  • LICENSE — MIT, free to reuse and adapt

Running it #

Unzip the download, then from inside the folder:

uv sync
uv run main.py

This writes forecast.json and prints the forecast.

To run the tests:

uv run pytest

Requires uv and Python 3.12+.

Refer to the instructions in How to Set Up a Machine Learning or Data Science Project for how to deploy.